Multivariate normal density in Python?

后端 未结 10 1118
星月不相逢
星月不相逢 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:26

    You can easily compute using numpy. I have implemented as below for the purpose of machine learning course and would like to share, hope it helps to someone.

    import numpy as np
    X = np.array([[13.04681517, 14.74115241],[13.40852019, 13.7632696 ],[14.19591481, 15.85318113],[14.91470077, 16.17425987]])
    
    def est_gaus_par(X):
        mu = np.mean(X,axis=0)
        sig = np.std(X,axis=0)
        return mu,sig
    
    mu,sigma = est_gaus_par(X)
    
    def est_mult_gaus(X,mu,sigma):
        m = len(mu)
        sigma2 = np.diag(sigma)
        X = X-mu.T
        p = 1/((2*np.pi)**(m/2)*np.linalg.det(sigma2)**(0.5))*np.exp(-0.5*np.sum(X.dot(np.linalg.pinv(sigma2))*X,axis=1))
    
        return p
    
    p = est_mult_gaus(X, mu, sigma)
    

提交回复
热议问题