phi.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00039 #include <cmath>
00040 #include "fastlib/fastlib.h"
00041
00042
00055 long double phi(Vector& x , Vector& mean , Matrix& cov) {
00056
00057 long double det, f;
00058 double exponent;
00059 index_t dim;
00060 Matrix inv;
00061 Vector diff, tmp;
00062
00063 dim = x.length();
00064 la::InverseInit(cov, &inv);
00065 det = la::Determinant(cov);
00066
00067 if( det < 0){
00068 det = -det;
00069 }
00070 la::SubInit(mean,x,&diff);
00071 la::MulInit(inv, diff, &tmp);
00072 exponent = la::Dot(diff, tmp);
00073 long double tmp1, tmp2, tmp3;
00074 tmp1 = 1;
00075 tmp2 = dim;
00076 tmp2 = tmp2/2;
00077 tmp2 = pow((2*(math::PI)),tmp2);
00078 tmp1 = tmp1/tmp2;
00079 tmp3 = 1;
00080 tmp2 = sqrt(det);
00081 tmp3 = tmp3/tmp2;
00082 tmp2 = -exponent;
00083 tmp2 = tmp2 / 2;
00084 f = (tmp1*tmp3*exp(tmp2));
00085
00086 return f;
00087 }
00088
00100 long double phi(double x, double mean, double var) {
00101
00102 long double f;
00103
00104 f = exp(-1.0*((x-mean)*(x-mean)/(2*var)))/sqrt(2*math::PI*var);
00105 return f;
00106 }
00107
00121 long double phi(Vector& x, Vector& mean, Matrix& cov, ArrayList<Matrix>& d_cov, Vector *g_mean, Vector *g_cov){
00122
00123 long double det, f;
00124 double exponent;
00125 index_t dim;
00126 Matrix inv;
00127 Vector diff, tmp;
00128
00129 dim = x.length();
00130 la::InverseInit(cov, &inv);
00131 det = la::Determinant(cov);
00132
00133 if( det < 0){
00134 det = -det;
00135 }
00136 la::SubInit(mean,x,&diff);
00137 la::MulInit(inv, diff, &tmp);
00138 exponent = la::Dot(diff, tmp);
00139 long double tmp1, tmp2, tmp3;
00140 tmp1 = 1;
00141 tmp2 = dim;
00142 tmp2 = tmp2/2;
00143 tmp2 = pow((2*(math::PI)),tmp2);
00144 tmp1 = tmp1/tmp2;
00145 tmp3 = 1;
00146 tmp2 = sqrt(det);
00147 tmp3 = tmp3/tmp2;
00148 tmp2 = -exponent;
00149 tmp2 = tmp2 / 2;
00150 f = (tmp1*tmp3*exp(tmp2));
00151
00152
00153 la::ScaleInit(f,tmp,g_mean);
00154
00155
00156 double *g_cov_tmp;
00157 g_cov_tmp = (double*)malloc(d_cov.size()*sizeof(double));
00158 for(index_t i = 0; i < d_cov.size(); i++){
00159 Vector tmp_d;
00160 Matrix inv_d;
00161 long double tmp_d_cov_d_r;
00162
00163 la::MulInit(d_cov[i],tmp,&tmp_d);
00164 tmp_d_cov_d_r = la::Dot(tmp_d,tmp);
00165 la::MulInit(inv,d_cov[i],&inv_d);
00166 for(index_t j = 0; j < dim; j++)
00167 tmp_d_cov_d_r += inv_d.get(j,j);
00168 g_cov_tmp[i] = f*tmp_d_cov_d_r/2;
00169 }
00170 g_cov->Copy(g_cov_tmp,d_cov.size());
00171
00172 return f;
00173 }