phi.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00039 #include <cmath>
00040 #include "fastlib/fastlib.h"
00041 //#include "fastlib/fastlib_int.h"
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   // Calculating the g_mean values  which would be a (1 X dim) vector
00153   la::ScaleInit(f,tmp,g_mean);
00154         
00155   // Calculating the g_cov values which would be a (1 X (dim*(dim+1)/2)) vector
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 }
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3