root / branches / pthreads737 / docs / manuals / autodif / like.cpp @ 765
History  View  Annotate  Download (653 Bytes)
1 
\begin{lstlisting}


2 
// file: like.cpp

3  
4 
#include <fvar.hpp> 
5 
#include "mixture.h" 
6  
7 
dvariable log_likelihood(dvector y,dvar_vector p,dvar_vector& mu, 
8 
dvar_vector& sd) 
9 
{ 
10 
dvariable like_fun=0.;

11 
int nobs=y.size();

12 
int ngroups=p.size();

13 
dvar_vector v=elem_prod(sd,sd); // Calculate the variances

14 
dvariable sum; 
15 
dvariable diff; 
16 
for (int i=1;i<=nobs;i++) 
17 
{ 
18 
sum=0.;

19 
for (int j=1;j<=ngroups;j++) 
20 
{ 
21 
diff=y(i)mu(j); 
22 
sum+=p(j)/sd(j)*exp(diff*diff/(2.*v(j)));

23 
} 
24 
like_fun+=log(sum+1.e20); // Likelihood contribution 
25 
// for ith observation

26 
} 
27 
return(like_fun);

28 
} 
29 
\end{lstlisting}
