Multivariate normal density in Python?

后端 未结 10 1149
星月不相逢
星月不相逢 2021-01-30 19:42

Is there any python package that allows the efficient computation of the PDF (probability density function) of a multivariate normal distribution?

It doesn\'t seem to be

10条回答
  •  时光取名叫无心
    2021-01-30 20:30

    The following code helped me to solve,when given a vector what is the likelihood that vector is in a multivariate normal distribution.

    import numpy as np
    from scipy.stats import multivariate_normal
    

    data with all vectors

    d= np.array([[1,2,1],[2,1,3],[4,5,4],[2,2,1]])
    

    mean of the data in vector form, which will have same length as input vector(here its 3)

    mean = sum(d,axis=0)/len(d)
    
    OR
    mean=np.average(d , axis=0)
    mean.shape
    

    finding covarianve of vectors which will have shape of [input vector shape X input vector shape] here it is 3x3

    cov = 0
    for e in d:
      cov += np.dot((e-mean).reshape(len(e), 1), (e-mean).reshape(1, len(e)))
    cov /= len(d)
    cov.shape
    

    preparing a multivariate Gaussian distribution from mean and co variance

    dist = multivariate_normal(mean,cov)
    

    finding probability distribution function.

    print(dist.pdf([1,2,3]))
    
    3.050863384798471e-05
    

    The above value gives the likelihood.

提交回复
热议问题