Multivariate normal density in Python?

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

    Here I elaborate a bit more on how exactly to use the multivariate_normal() from the scipy package:

    # Import packages
    import numpy as np
    from scipy.stats import multivariate_normal
    
    # Prepare your data
    x = np.linspace(-10, 10, 500)
    y = np.linspace(-10, 10, 500)
    X, Y = np.meshgrid(x,y)
    
    # Get the multivariate normal distribution
    mu_x = np.mean(x)
    sigma_x = np.std(x)
    mu_y = np.mean(y)
    sigma_y = np.std(y)
    rv = multivariate_normal([mu_x, mu_y], [[sigma_x, 0], [0, sigma_y]])
    
    # Get the probability density
    pos = np.empty(X.shape + (2,))
    pos[:, :, 0] = X
    pos[:, :, 1] = Y
    pd = rv.pdf(pos)
    

提交回复
热议问题