# Density of multivariate t distribution in Python for large number of observations

346
December 15, 2016, at 12:44 PM

I am trying to evaluate the density of multivariate t distribution of a 13-d vector. Using the dmvt function from the mvtnorm package in R, the result I get is

``````[1] 1.009831e-13
``````

When i tried to write the function by myself in Python (thanks to the suggestions in this post: multivariate student t-distribution with python), I realized that the gamma function was taking very high values (given the fact that I have n=7512 observations), making my function going out of range.

I tried to modify the algorithm, using the math.lgamma() and np.linalg.slogdet() functions to transform it to the log scale, but the result I got was

`````` 8.97669876e-15
``````

This is the function that I used in python is the following:

``````def dmvt(x,mu,Sigma,df,d):
'''
Multivariate t-student density:
output:
the density of the given element
input:
x = parameter (d dimensional numpy array or scalar)
mu = mean (d dimensional numpy array or scalar)
Sigma = scale matrix (dxd numpy array)
df = degrees of freedom
d: dimension
'''
Num = math.lgamma( 1. *(d+df)/2 ) - math.lgamma( 1.*df/2 )
(sign, logdet) = np.linalg.slogdet(Sigma)
Denom =1/2*logdet + d/2*( np.log(pi)+np.log(df) ) + 1.*( (d+df)/2 )*np.log(1 + (1./df)*np.dot(np.dot((x - mu),np.linalg.inv(Sigma)), (x - mu)))
d = 1. * (Num - Denom)
return np.exp(d)
``````

Any ideas why this functions does not produce the same results as the R equivalent?

Using as `x = (0,0)` produces similar results (up to a point, die to rounding) but with `x = (1,1)`1 I get a significant difference!

POPULAR ONLINE

### Pandas Dataframe grouping and standard deviation

Given a pandas dataframe in the following format:

509

### Python - Printing out all data reveived from a socket

I found come old TCP reverse shell made in python online and im editing it to fix it up, when it prints the output of the command it doesnt print it all if the command is too bigThis is the code for sending the commands and then printing the output

295

### Effectively turning strings into unicode for python 2.7

I'm following a turtorial on LDA and encountering a problem since the turtorial is made in python 3 and I'm working in 27 (the turtorial claims to work in both)

420

### how can i create email template using Xml Odoo

how can i create email template using Xml Odoo

417