GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python_reference/useful_scripts/multivariate_gaussian_pdf.py at master · l3ldon/python_reference Expand file tree/ Copy pathmultivariate_gaussian_pdf.pyMore file actions 40 lines (31 loc) · 1.41 KB / Copy pathmultivariate_gaussian_pdf.pyFile metadata and controls40 lines (31 loc) · 1.41 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 # Sebastian Raschka 04/2014 import numpy as np def pdf_multivariate_gauss(x, mu, cov): ''' Caculate the multivariate normal density (pdf) Keyword arguments: x = numpy array of a "d x 1" sample vector mu = numpy array of a "d x 1" mean vector cov = "numpy array of a d x d" covariance matrix ''' assert(mu.shape[0] > mu.shape[1]), 'mu must be a row vector' assert(x.shape[0] > x.shape[1]), 'x must be a row vector' assert(cov.shape[0] == cov.shape[1]), 'covariance matrix must be square' assert(mu.shape[0] == cov.shape[0]), 'cov_mat and mu_vec must have the same dimensions' assert(mu.shape[0] == x.shape[0]), 'mu and x must have the same dimensions' part1 = 1 / ( ((2* np.pi)**(len(mu)/2)) * (np.linalg.det(cov)**(1/2)) ) part2 = (-1/2) * ((x-mu).T.dot(np.linalg.inv(cov))).dot((x-mu)) return float(part1 * np.exp(part2)) def test_gauss_pdf(): from matplotlib.mlab import bivariate_normal x = np.array([[0],[0]]) mu = np.array([[0],[0]]) cov = np.eye(2) mlab_gauss = bivariate_normal(x,x) mlab_gauss = float(mlab_gauss[0]) # because mlab returns an np.array impl_gauss = pdf_multivariate_gauss(x, mu, cov) print('mlab_gauss:', mlab_gauss) print('impl_gauss:', impl_gauss) assert(mlab_gauss == impl_gauss), 'Implementations of the mult. Gaussian return different pdfs' if __name__ == '__main__': test_gauss_pdf() |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |