简体   繁体   中英

CDF of a multivariate Normal in Tensorflow

I want to evaluate the cdf of a multivariate normal distribution using tensorflow. What I have tried so far:

import tensorflow as tf
ds = tf.contrib.distributions

# Initialize a single 3-variate Gaussian.
mu = [0., 0., 0.]
cov = [[ 0.36,  0.12,  0.06],
       [ 0.12,  0.29, -0.13],
       [ 0.06, -0.13,  0.26]]
mvn = ds.MultivariateNormalFullCovariance(
    loc=mu,
    covariance_matrix=cov)
value = tf.constant([0., 0., 0.])


with tf.Session() as sess:
    print mvn.cdf(value).eval()

This yields the error:

NotImplementedError: cdf is not implemented when overriding event_shape

I don't understand why I am overriding the event_shape since event_shape and the shape of value are the same. What am I doing wrong?

Youre not doing anything wrong. The CDF is not implemented for the Multivariate Normal. (I agree the error message is confusing. The error message is being thrown by TransformedDistribution which is responsible for implementing the cdf .)

If you can tolerate a Monte Carlo approximation, I suggest doing something like:

def approx_multivariate_cdf(dist, bound, num_samples=int(100e3), seed=None):
  s = dist.sample(num_samples, seed=seed)
  in_box = tf.cast(tf.reduce_all(s <= bound, axis=-1), dist.dtype)
  return tf.reduce_mean(in_box, axis=0)

(With some thought, I'm sure someone can do better than this.)

There might also be a more clever solution described here: https://arxiv.org/abs/1603.04166

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM