You are on page 1of 4

Multi Variate Gaussian Distribution Part 1

February 21, 2014

Multi Variate Gaussian Distribution

Multi Variate Gaussian Distribution

0.1 Introduction
In this article we will look at the multivariate gaussian distribution.

The mutivariate gaussian distribution is parameterized by mean vector  and covariance matrix .

fx (x1 ; : : : ; xk ) = p

(2 )k



2 (x  )

1(x ) ;

where  is a Nx1 mean vector is NxN matrix covariance matrix j j is the determinant of


The mutivariate gaussian is also used as probability density function of the vector X A naive way of implementation is to directly perform the computation as in the equation to get the result. A Gaussian is defined Since ill be using a lot of opencv conversion routines are defined for Opencv cv::Mat data structure to Eigen::MatrixXf data structure. when loading the covarince matrix ,the determinant,inverse etc are also computed which would be later required for probability calculation

void setMean(Mat &v) { Map<Eigen::Matrix<float,Dynamic,Dynamic,Eigen::RowMajor> > mappedMat((float *),1,v.size()) ; _mu=mappedMat;


Multi Variate Gaussian Distribution

} void setSigma(cv::Mat &v) { Map<Eigen::Matrix<float,Dynamic,Dynamic,Eigen::RowMajor> > mappedMat((float *),v.rows,v.cols) ; _Sigma=mappedMat; _dim=v.rows; _det=_Sigma.determinant(); _scale=1.f/(pow(2*PI*_dim*_det,0.5)); _invsigma=_Sigma.inverse(); } MatrixXf setData(cv::Mat &v) { Map<Eigen::Matrix<float,Dynamic,Dynamic,Eigen::RowMajor> > mappedMat((float *),1,v.cols) ; MatrixXf ref=mappedMat; return ref; }

The probability computation is simply writing out the formula

void validate(float &res) { if(res<0) res=0; if(res>1) res=1; if(isnan(res)) res=0; } float Prob(Mat &x) { MatrixXf tmp=setData(x); float res=0; MatrixXf tmp1=(tmp-_mu); MatrixXf tmp2=tmp1*_invsigma*tmp1.transpose(); res=scale*tmp2(0,0); validate(res); return res; }


Multi Variate Gaussian Distribution


The covariance matrix is postive semidefinate and symmteric The Cholesky decomposition of a symmetric, positive definite matrix decom- poses A into a product of a lower triangular matrix L and its transpose.

Thus the product

= LLT 1 = (LLT )1 = LT L1 yT 1y yT 1y yT LT L1y 1 T 1 L y L y

thus we can only compute (L1

y) and take its tranpose

Thus we can perform the Cholskey factorization of the covariance matrix to find

Then we take the inverse of

float Prob(Mat &x) { MatrixXf tmp=setData(x); float res=0; MatrixXf tmp1=(tmp-_mu).transpose(); tmp1=_LI*tmp1; MatrixXf tmp2=tmp1.transpose()*tmp1; res=tmp2(0,0); validate(res); return res;

0.1.1 Code
The code can be found at git repository pi19404/OpenVision in files ImgML/gaussian.hpp and ImgML.gaussian.cpp files.