Search code examples
pythontensorflowtensorflow-probability

Tensorflow Implement Multivariate Student T diagonal distribution


I'm implementing a diagonal multivariate student t distribution (so logP(x1,x2,x3,..xD) = logP(x1) + logP(x2)+ ....+ logP(xD) ) such that it can be used as base distribution for bijectors in TensorFlow

import tensorflow_probability as tfp
tfd = tfp.distributions

D = 2 # number of dimension
df = 5. # degree of freedom

# construct D univariate student t distribution

base_dist = tfd.StudentT(loc=tf.constant([0.] * D,dtype=DTYPE),
                         scale = tf.constant([1.] * D,dtype=DTYPE),
                         df = tf.constant([df],dtype=DTYPE))

Q = tfd.TransformedDistribution(distribution=base_dist,bijector=Chain)
# where Chain is a tfb.Chain() object that a sequence of bisector numbers

I change tfd.StudentT.log_prob() such that it sum over last axes. It takes shape [batch_size,dim] as input and return pdf with shape[batch_size,]

However, when I call Q.log_prob(x); I got error ValueError: event_ndims (0) must be larger than min_event_ndims (1)

I'm not sure how to fix this error; can some one helps me ?


Solution

  • TensorFlow Probability provides a way to create vector-valued distributions from scalar distributions via the tfd.Independent meta-distribution. That would automatically perform the summation in log_prob that you want.

    If you really wanted to implement things yourself, the issue you're having sounds like you didn't override the event_shape and event_shape_tensor methods (along with the batch_shape and batch_shape_tensor).

    Lastly, typically when people talk about the multivariate student t distribution, they mean the elliptical distribution described here, which is not the same thing as taking a product of 1D Student-t's distributions and then linearly transforming them. Recently, TFP added an implementation of the elliptical variant of this distribution here. It takes an affine transformation as input that you can use to setup the location/correlation structure of the distribution.