ADMB Documentation  11.1x.2708
 All Classes Files Functions Variables Typedefs Friends Defines
qfc_est.cpp
Go to the documentation of this file.
00001 
00034   #include "qfclib.h"
00035 
00036 
00037 
00038 
00046   dvar_vector logitProp(const dvar_vector& a)
00047   {
00048     int dim; 
00049     dim=a.size()+1;
00050     dvar_vector p(1,dim);
00051     dvar_vector expa=mfexp(a);
00052     p(1,dim-1)=expa/(1.+sum(expa));
00053     //p(dim)=1.-sum(p(1,dim-1));  //original version was buggy for the last term
00054     p(dim)=1./(1.+sum(expa));
00055     return p;
00056   }
00057   df1b2vector logitProp(const df1b2vector& a)
00058   {
00059     int dim; 
00060     dim=size_count(a)+1;
00061     df1b2vector p(1,dim);
00062     df1b2vector expa=mfexp(a);
00063     p(1,dim-1)=expa/(1.+sum(expa));
00064     //p(dim)=1.-sum(p(1,dim-1));  //original version was buggy for the last term
00065     p(dim)=1./(1.+sum(expa));
00066     return p;
00067   }
00068 
00069 
00070 
00071 
00077   dvar_vector invLogitProp(const dvar_vector& p)
00078   {
00079     int dim; 
00080     dim=p.size()-1;
00081     dvar_vector a(1,dim); 
00082     dvar_vector lp(1,dim+1);
00083   
00084     lp=log(p+EPS);//take log for each p
00085     a=lp(1,dim)-lp(dim+1);//each subtract the last one
00086     return a;
00087   }
00088   df1b2vector invLogitProp(const df1b2vector& p)
00089   {
00090     int dim; 
00091     dim=size_count(p)-1;
00092     df1b2vector a(1,dim); 
00093     df1b2vector lp(1,dim+1);
00094   
00095     lp=log(p+EPS);//take log for each p
00096     a=lp(1,dim)-lp(dim+1);//each subtract the last one
00097     return a;
00098   }
00099 
00100 
00101 
00102 
00103 
00110   dvar_vector normalize_p(dvar_vector& p, dvariable fpen)
00111   {
00112     dvariable psum=sum(p);
00113     p/=psum; // Now the p will sum to 1
00114     fpen+=1000.*square(log(psum)); //penalty
00115     return p;
00116   }
00117   df1b2vector normalize_p(df1b2vector& p, df1b2variable fpen)
00118   {
00119     df1b2variable psum=sum(p);
00120     p/=psum; // Now the p will sum to 1
00121     fpen+=1000.*square(log(psum)); //penalty
00122     return p;
00123   }
00124 
00125 
00126 
00127 
00137   dvar_vector posfun(dvar_vector& x,const dvector& eps, dvariable& pen) //x and eps both are vector
00138   { 
00139     for(int i=x.indexmin();i<=x.indexmax();i++)
00140     {
00141       x(i)=posfun(x(i),eps(i),pen);
00142     }
00143     return x;
00144   }
00145   df1b2vector posfun(df1b2vector& x,const dvector& eps, df1b2variable& pen) //x and eps both are vector
00146   { 
00147     for(int i=x.indexmin();i<=x.indexmax();i++)
00148     {
00149       x(i)=posfun(x(i),eps(i),pen);
00150     }
00151     return x;
00152   }
00153 
00154 
00155 
00156 
00167   dvar_matrix posfun(dvar_matrix& x,const dmatrix & eps, dvariable& pen)
00168   {
00169     for(int i=x.rowmin();i<=x.rowmax();i++)
00170     {
00171       for(int j=x.colmin();j<=x.colmax();j++)
00172       {
00173         x(i,j)=posfun(x(i,j),eps(i,j),pen);
00174       }
00175     }
00176     return x;
00177   }
00178   df1b2matrix posfun(df1b2matrix& x,const dmatrix & eps, df1b2variable& pen)
00179   {
00180     for(int i=x.rowmin();i<=x.rowmax();i++)
00181     {
00182       for(int j=x.colmin();j<=x.colmax();j++)
00183       {
00184         x(i,j)=posfun(x(i,j),eps(i,j),pen);
00185       }
00186     }
00187     return x;
00188   }
00189 
00190 
00191 
00192 
00193   
00194  
00205   dvariable mf_upper_bound2(const dvariable & x,const double fmax, dvariable & fpen)
00206   {
00207     if (value(x)<=fmax) return x;
00208     else
00209     {
00210       fpen+=.01*square(x-fmax);
00211       return x-x/(2.-fmax/x)+fmax;
00212     }
00213   }
00214 
00215   df1b2variable mf_upper_bound2(const df1b2variable & x,const double fmax, df1b2variable & fpen)
00216   {
00217     if (value(x)<=fmax) return x;
00218     else
00219     {
00220       fpen+=.01*square(x-fmax);
00221       return x-x/(2.-fmax/x)+fmax;
00222     }
00223   }
00224 
00225 
00226 
00227 
00228 
00238   dvar_vector boundp(const dvar_vector & x, const double fmin, const double fmax, const dvariable & fpen )
00239   {
00240     dvar_vector t(x.indexmin(),x.indexmax());
00241     for(int i=x.indexmin();i<=x.indexmax();i++)
00242       t(i)=boundp(x(i),fmin,fmax,fpen);
00243     return (t);
00244   }
00245   df1b2vector boundp(const df1b2vector & x, const double fmin, const double fmax, const df1b2variable & fpen )
00246   {
00247     df1b2vector t(x.indexmin(),x.indexmax());
00248     for(int i=x.indexmin();i<=x.indexmax();i++)
00249       t(i)=boundp(x(i),fmin,fmax,fpen);
00250     return (t);
00251   }
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00266   df1b2vector log_comb(const df1b2vector & n,const df1b2vector & x)
00267   {
00268     df1b2vector tmp(n.indexmin(),n.indexmax());
00269     for(int i=n.indexmin();i<=n.indexmax();i++)
00270       tmp(i)=log_comb(n(i),x(i));
00271     return tmp;
00272   }
00273   df1b2vector log_comb(const df1b2vector & n,const dvector & x)
00274   {
00275     df1b2vector tmp(n.indexmin(),n.indexmax());
00276     for(int i=n.indexmin();i<=n.indexmax();i++)
00277       tmp(i)=log_comb(n(i),x(i));
00278     return tmp;
00279   }
00280   df1b2vector log_comb(const dvector & n,const df1b2vector & x)
00281   {
00282     df1b2vector tmp(n.indexmin(),n.indexmax());
00283     for(int i=n.indexmin();i<=n.indexmax();i++)
00284       tmp(i)=log_comb(n(i),x(i));
00285     return tmp;
00286   }  
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00295   //
00296   //                                    Negative Binomial 
00297   //
00299 
00310   double  nllNegativeBinomial(const double obs, const double m, const double s )
00311   {
00312     double nll=0; 
00313     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00314     return nll;
00315   }
00316   dvariable  nllNegativeBinomial(const dvariable & obs, const double m, const double s )
00317   {
00318     RETURN_ARRAYS_INCREMENT();
00319     dvariable nll=0; 
00320     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00321     RETURN_ARRAYS_DECREMENT();
00322     return nll;
00323   } 
00324   dvariable  nllNegativeBinomial(const double obs, const dvariable & m, const dvariable & s)
00325   {
00326     RETURN_ARRAYS_INCREMENT();
00327     dvariable nll=0; 
00328     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00329     RETURN_ARRAYS_DECREMENT();
00330     return nll;
00331   } 
00332   dvariable  nllNegativeBinomial(const dvariable & obs, const dvariable & m, const double s)
00333   {
00334     RETURN_ARRAYS_INCREMENT();
00335     dvariable nll=0; 
00336     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00337     RETURN_ARRAYS_DECREMENT();
00338     return nll;
00339   }
00340   dvariable  nllNegativeBinomial(const dvariable & obs, const double m, const dvariable & s)
00341   {
00342     RETURN_ARRAYS_INCREMENT();
00343     dvariable nll=0; 
00344     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00345     RETURN_ARRAYS_DECREMENT();
00346     return nll;
00347   }
00348   dvariable  nllNegativeBinomial(const dvariable & obs, const dvariable & m, const dvariable & s)
00349   {
00350     RETURN_ARRAYS_INCREMENT();
00351     dvariable nll=0; 
00352     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00353     RETURN_ARRAYS_DECREMENT();
00354     return nll;
00355   }
00356 
00367   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const double m, const double s)
00368   {
00369     df1b2variable nll=.0; 
00370     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00371     return nll;
00372   }
00373   df1b2variable  nllNegativeBinomial(const double obs, const df1b2variable & m, const df1b2variable & s)
00374   {
00375     df1b2variable nll=.0; 
00376     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00377     return nll;
00378   }
00379   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const df1b2variable & m, const double s)
00380   {
00381     df1b2variable nll=.0; 
00382     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00383     return nll;
00384   }
00385   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const double m, const df1b2variable & s)
00386   {
00387     df1b2variable nll=.0; 
00388     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00389     return nll;
00390   }
00391   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const df1b2variable & m, const df1b2variable & s)
00392   {
00393     df1b2variable nll=.0; 
00394     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00395     return nll;
00396   }
00397 
00398 
00399 
00400 
00401 
00412   dvariable  nllNegativeBinomial(const dvector & obs, const dvariable & m, const dvariable & s)
00413   {
00414     RETURN_ARRAYS_INCREMENT();
00415     dvariable nll=0; 
00416     double n=double(obs.size());
00417     nll= -sum(gammln(obs+s))+ n*gammln(s)- n*s*log(s/(m+s)+EPS)- sum(obs)*log(m/(m+s)+EPS)+ sum(gammln(obs+1.));
00418     RETURN_ARRAYS_DECREMENT();
00419     return nll;
00420   }
00421   dvariable  nllNegativeBinomial(const dvar_vector & obs, const double m, const double s)
00422   {
00423     RETURN_ARRAYS_INCREMENT();
00424     dvariable nll=0; 
00425     double n=double(obs.size());
00426     nll= -sum(gammln(obs+s))+ n*gammln(s)- n*s*log(s/(m+s)+EPS)- sum(obs)*log(m/(m+s)+EPS)+ sum(gammln(obs+1.));
00427     RETURN_ARRAYS_DECREMENT();
00428     return nll;
00429   }
00430   dvariable  nllNegativeBinomial(const dvar_vector & obs, const dvariable & m, const dvariable & s)
00431   {
00432     RETURN_ARRAYS_INCREMENT();
00433     dvariable nll=0; 
00434     double n=double(obs.size());
00435     nll= -sum(gammln(obs+s))+ n*gammln(s)- n*s*log(s/(m+s)+EPS)- sum(obs)*log(m/(m+s)+EPS)+ sum(gammln(obs+1.));
00436     RETURN_ARRAYS_DECREMENT();
00437     return nll;
00438   }
00439 
00450   df1b2variable  nllNegativeBinomial(const df1b2vector & obs, const double m, const double s)
00451   {
00452     df1b2variable nll=.0; 
00453     double n=double(size_count(obs));
00454     nll= -sum(gammln(obs+s))+ n*gammln(s)- n*s*log(s/(m+s)+EPS)- sum(obs)*log(m/(m+s)+EPS)+ sum(gammln(obs+1.));
00455     return nll;
00456   }
00457   df1b2variable  nllNegativeBinomial(const dvector & obs, const df1b2variable & m, const df1b2variable & s)
00458   {
00459     df1b2variable nll=.0; 
00460     double n=double(size_count(obs));
00461     nll= -sum(gammln(obs+s))+ n*gammln(s)- n*s*log(s/(m+s)+EPS)- sum(obs)*log(m/(m+s)+EPS)+ sum(gammln(obs+1.));
00462     return nll;
00463   }
00464   df1b2variable  nllNegativeBinomial(const df1b2vector & obs, const df1b2variable & m, const df1b2variable & s)
00465   {
00466     df1b2variable nll=.0; 
00467     double n=double(size_count(obs));
00468     nll= -sum(gammln(obs+s))+ n*gammln(s)- n*s*log(s/(m+s)+EPS)- sum(obs)*log(m/(m+s)+EPS)+ sum(gammln(obs+1.));
00469     return nll;
00470   }
00471 
00472 
00473 
00474 
00476   //
00477   //                                    Negative Binomial with tau
00478   //
00480 
00490   double nllNegativeBinomial2(const double obs, const double m, const double tau)
00491   {
00492     return -log_density_negbinomial (obs,m,tau); //use admb built in function
00493   } 
00494   dvariable nllNegativeBinomial2(const double obs, const dvariable & m, const dvariable & tau)
00495   {
00496     return -1.*log_negbinomial_density(obs,m,tau); //use admb built in function
00497   }   
00498   dvariable nllNegativeBinomial2(const dvariable & obs, const double m, const double tau)
00499   {
00500     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00501   }   
00502   dvariable nllNegativeBinomial2(const dvariable & obs, const dvariable & m, const double tau)
00503   {
00504     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00505   } 
00506   dvariable nllNegativeBinomial2(const dvariable & obs, const double m, const dvariable & tau)
00507   {
00508     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00509   } 
00510   dvariable nllNegativeBinomial2(const dvariable & obs, const dvariable & m, const dvariable & tau)
00511   {
00512     return nllNegativeBinomial(obs, m,m/(tau-1.+EPS));
00513   } 
00514 
00524   df1b2variable nllNegativeBinomial2(const df1b2variable & obs, const double m, const double tau)
00525   {
00526     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00527   }
00528   df1b2variable nllNegativeBinomial2(const double obs, const df1b2variable & m, const df1b2variable & tau)
00529   {
00530     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00531   }
00532   df1b2variable nllNegativeBinomial2(const df1b2variable & obs, const df1b2variable & m, const df1b2variable & tau)
00533   {
00534     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00535   }
00536 
00537 
00538 
00539 
00540 
00541 
00543   //
00544   //                                    Multinomial
00545   //
00547 
00555   double  nllMultiNomial(const dvector & obsN, const dvector & p)
00556   {
00557     double nll=0;  
00558     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00559     return nll;
00560   } 
00561   dvariable  nllMultiNomial(const dvector & obsN, const dvar_vector & p)
00562   {
00563     RETURN_ARRAYS_INCREMENT(); 
00564     dvariable nll=0;  
00565     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00566     RETURN_ARRAYS_DECREMENT();
00567     return nll;
00568   }
00569   dvariable  nllMultiNomial(const dvar_vector & obsN, const dvector & p)
00570   {
00571     RETURN_ARRAYS_INCREMENT(); 
00572     dvariable nll=0;  
00573     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00574     RETURN_ARRAYS_DECREMENT();
00575     return nll;
00576   }   
00577   dvariable  nllMultiNomial(const dvar_vector & obsN, const dvar_vector & p)
00578   {
00579     RETURN_ARRAYS_INCREMENT(); 
00580     dvariable nll=0;  
00581     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00582     RETURN_ARRAYS_DECREMENT();
00583     return nll;
00584   }    
00585 
00593   df1b2variable  nllMultiNomial(const df1b2vector & obsN, const dvector & p)
00594   {
00595     df1b2variable nll=.0;  
00596     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00597     return nll;
00598   }
00599   df1b2variable  nllMultiNomial(const dvector & obsN, const df1b2vector & p)
00600   {
00601     df1b2variable nll=.0;  
00602     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00603     return nll;
00604   }  
00605   df1b2variable  nllMultiNomial(const df1b2vector & obsN, const df1b2vector & p)
00606   {
00607     df1b2variable nll=.0;  
00608     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00609     return nll;
00610   }
00611 
00612 
00613 
00614 
00616   //
00617   //                                    Dirichlet
00618   //
00620 
00628   double nllDirichlet(const dvector & p, const dvector & shape)
00629   {
00630     double nll=0;  
00631     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00632     return nll;
00633   }
00634   dvariable nllDirichlet(const dvar_vector & p, const dvector & shape)
00635   {
00636     RETURN_ARRAYS_INCREMENT();
00637     dvariable nll=0;  
00638     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00639     RETURN_ARRAYS_DECREMENT();
00640     return nll;
00641   } 
00642   dvariable nllDirichlet(const dvector & p, const dvar_vector & shape)
00643   {
00644     RETURN_ARRAYS_INCREMENT();
00645     dvariable nll=0;  
00646     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00647     RETURN_ARRAYS_DECREMENT();
00648     return nll;
00649   }
00650   dvariable nllDirichlet(const dvar_vector & p, const dvar_vector & shape)
00651   {
00652     RETURN_ARRAYS_INCREMENT();
00653     dvariable nll=0;  
00654     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00655     RETURN_ARRAYS_DECREMENT();
00656     return nll;
00657   }
00658 
00666   df1b2variable nllDirichlet(const df1b2vector & p, const dvector & shape)
00667   {
00668     df1b2variable nll=.0;  
00669     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00670     return nll;
00671   }  
00672   df1b2variable nllDirichlet(const dvector & p, const df1b2vector & shape)
00673   {
00674     df1b2variable nll=.0;  
00675     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00676     return nll;
00677   }
00678   df1b2variable nllDirichlet(const df1b2vector & p, const df1b2vector & shape)
00679   {
00680     df1b2variable nll=.0;  
00681     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00682     return nll;
00683   }  
00684 
00685  
00686 
00687 
00689   //
00690   //                                    Gamma
00691   //
00693 
00703   double nllGamma(const double & x, const double a, const double b)
00704   {
00705     return -1.*log_gamma_density(x,a,b);  //call admb built in function
00706   }
00707   dvariable nllGamma(const dvariable & x, const double a, const double b)
00708   {
00709     RETURN_ARRAYS_INCREMENT();
00710     dvariable nll=0;  
00711     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00712     RETURN_ARRAYS_DECREMENT();
00713     return nll;
00714   } 
00715   dvariable nllGamma(const double x, const dvariable & a, const dvariable & b)
00716   {
00717     RETURN_ARRAYS_INCREMENT();
00718     dvariable nll=0;  
00719     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00720     RETURN_ARRAYS_DECREMENT();
00721     return nll;
00722   } 
00723   dvariable nllGamma(const dvariable & x, const dvariable & a, const double b)
00724   {
00725     RETURN_ARRAYS_INCREMENT();
00726     dvariable nll=0;  
00727     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00728     RETURN_ARRAYS_DECREMENT();
00729     return nll;
00730   }
00731   dvariable nllGamma(const dvariable & x, const double a, const dvariable & b)
00732   {
00733     RETURN_ARRAYS_INCREMENT();
00734     dvariable nll=0;  
00735     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00736     RETURN_ARRAYS_DECREMENT();
00737     return nll;
00738   }
00739   dvariable nllGamma(const dvariable & x, const dvariable & a, const dvariable & b)
00740   {
00741     RETURN_ARRAYS_INCREMENT();
00742     dvariable nll=0;  
00743     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00744     RETURN_ARRAYS_DECREMENT();
00745     return nll;
00746   }
00747 
00757   df1b2variable nllGamma(const df1b2variable & x, const double a, const double b)
00758   {
00759     df1b2variable nll=.0;  
00760     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00761     return nll;
00762   }
00763   df1b2variable nllGamma(const double x, const df1b2variable & a, const df1b2variable & b)
00764   {
00765     df1b2variable nll=.0;  
00766     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00767     return nll;
00768   }
00769   df1b2variable nllGamma(const df1b2variable & x, const df1b2variable & a, const double b)
00770   {
00771     df1b2variable nll=.0;  
00772     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00773     return nll;
00774   }
00775   df1b2variable nllGamma(const df1b2variable & x, const double a, const df1b2variable & b)
00776   {
00777     df1b2variable nll=.0;  
00778     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00779     return nll;
00780   }
00781   df1b2variable nllGamma(const df1b2variable & x, const df1b2variable & a, const df1b2variable & b)
00782   {
00783     df1b2variable nll=.0;  
00784     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00785     return nll;
00786   }
00787 
00788 
00789 
00798   double nllGamma(const dvector & x, const double a, const double b)
00799   {
00800     double nll=0;  
00801     double n=double(x.size());
00802     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00803     return nll;
00804   }
00805   dvariable nllGamma(const dvar_vector & x, const double a, const double b)
00806   {
00807     RETURN_ARRAYS_INCREMENT();
00808     dvariable nll=0;  
00809     double n=double(x.size());
00810     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00811     RETURN_ARRAYS_DECREMENT();
00812     return nll;
00813   } 
00814   dvariable nllGamma(const dvector & x, const dvariable & a, const dvariable & b)
00815   {
00816     RETURN_ARRAYS_INCREMENT();
00817     dvariable nll=0;  
00818     double n=double(x.size());
00819     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00820     RETURN_ARRAYS_DECREMENT();
00821     return nll;
00822   }
00823   dvariable nllGamma(const dvar_vector & x, const dvariable & a, const dvariable & b)
00824   {
00825     RETURN_ARRAYS_INCREMENT();
00826     dvariable nll=0;  
00827     double n=double(x.size());
00828     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00829     RETURN_ARRAYS_DECREMENT();
00830     return nll;
00831   }
00832 
00841   df1b2variable nllGamma(const df1b2vector & x, const double a, const double b)
00842   {
00843     df1b2variable nll=.0;  
00844     double n=double(size_count(x));
00845     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00846     return nll;
00847   }  
00848   df1b2variable nllGamma(const dvector & x, const df1b2variable & a, const df1b2variable & b)
00849   {
00850     df1b2variable nll=.0;  
00851     double n=double(size_count(x));
00852     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00853     return nll;
00854   }    
00855   df1b2variable nllGamma(const df1b2vector & x, const df1b2variable & a, const df1b2variable & b)
00856   {
00857     df1b2variable nll=.0;  
00858     double n=double(size_count(x));
00859     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00860     return nll;
00861   }  
00862 
00863 
00864 
00865 
00867   //
00868   //                                    Beta
00869   //
00871    
00880   double nllBeta(const double x, const double a, const double b)
00881   {
00882     double nll=0;  
00883     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00884     return nll;
00885   }
00886   dvariable nllBeta(const dvariable & x, const double a, const double b)
00887   {
00888     RETURN_ARRAYS_INCREMENT();
00889     dvariable nll=0;  
00890     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00891     RETURN_ARRAYS_DECREMENT();
00892     return nll;
00893   }   
00894   dvariable nllBeta(const double x, const dvariable & a, const dvariable & b)
00895   {
00896     RETURN_ARRAYS_INCREMENT();
00897     dvariable nll=0;  
00898     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00899     RETURN_ARRAYS_DECREMENT();
00900     return nll;
00901   } 
00902   dvariable nllBeta(const dvariable & x, const dvariable & a, const double b)
00903   {
00904     RETURN_ARRAYS_INCREMENT();
00905     dvariable nll=0;  
00906     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00907     RETURN_ARRAYS_DECREMENT();
00908     return nll;
00909   }  
00910   dvariable nllBeta(const dvariable & x, const double a, const dvariable & b)
00911   {
00912     RETURN_ARRAYS_INCREMENT();
00913     dvariable nll=0;  
00914     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00915     RETURN_ARRAYS_DECREMENT();
00916     return nll;
00917   }  
00918   dvariable nllBeta(const dvariable & x, const dvariable & a, const dvariable & b)
00919   {
00920     RETURN_ARRAYS_INCREMENT();
00921     dvariable nll=0;  
00922     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00923     RETURN_ARRAYS_DECREMENT();
00924     return nll;
00925   }  
00926 
00935   df1b2variable nllBeta(const df1b2variable & x, const double a, const double b)
00936   {
00937     df1b2variable nll=.0;  
00938     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00939     return nll;
00940   }   
00941   df1b2variable nllBeta(const df1b2variable & x, const df1b2variable & a, const double b)
00942   {
00943     df1b2variable nll=.0;  
00944     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00945     return nll;
00946   } 
00947   df1b2variable nllBeta(const df1b2variable & x, const double a, const df1b2variable & b)
00948   {
00949     df1b2variable nll=.0;  
00950     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00951     return nll;
00952   } 
00953   df1b2variable nllBeta(const double x, const df1b2variable & a, const df1b2variable & b)
00954   {
00955     df1b2variable nll=.0;  
00956     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00957     return nll;
00958   } 
00959   df1b2variable nllBeta(const df1b2variable & x, const df1b2variable & a, const df1b2variable & b)
00960   {
00961     df1b2variable nll=.0;  
00962     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00963     return nll;
00964   } 
00965 
00966 
00967 
00976   dvariable nllBeta(const dvar_vector & x, const double a, const double b)
00977   {
00978     RETURN_ARRAYS_INCREMENT();
00979     dvariable nll=0;  
00980     double n=double(x.size());
00981     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
00982     RETURN_ARRAYS_DECREMENT();
00983     return nll;
00984   }
00985   dvariable nllBeta(const dvector & x, const dvariable & a, const dvariable & b)
00986   {
00987     RETURN_ARRAYS_INCREMENT();
00988     dvariable nll=0;  
00989     double n=double(x.size());
00990     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
00991     RETURN_ARRAYS_DECREMENT();
00992     return nll;
00993   }
00994   dvariable nllBeta(const dvar_vector & x, const dvariable & a, const dvariable & b)
00995   {
00996     RETURN_ARRAYS_INCREMENT();
00997     dvariable nll=0;  
00998     double n=double(x.size());
00999     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01000     RETURN_ARRAYS_DECREMENT();
01001     return nll;
01002   }
01003   
01012   df1b2variable nllBeta(const df1b2vector & x, const double a, const double b)
01013   {
01014     df1b2variable nll=.0;  
01015     double n=double(size_count(x));
01016     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01017     return nll;
01018   }  
01019   df1b2variable nllBeta(const dvector & x, const df1b2variable & a, const df1b2variable & b)
01020   {
01021     df1b2variable nll=.0;  
01022     double n=double(size_count(x));
01023     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01024     return nll;
01025   }   
01026   df1b2variable nllBeta(const df1b2vector & x, const df1b2variable & a, const df1b2variable & b)
01027   {
01028     df1b2variable nll=.0;  
01029     double n=double(size_count(x));
01030     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01031     return nll;
01032   } 
01033 
01034 
01035 
01036 
01038   //
01039   //                                    Normal
01040   //
01042 
01051   double nllNormal(const double x, const double mu, const double sigma)
01052   {
01053     double nll=0;  
01054     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01055     return nll;
01056   }
01057   dvariable nllNormal(const dvariable & x, const double mu, const double sigma)
01058   {
01059     RETURN_ARRAYS_INCREMENT();
01060     dvariable nll=0;  
01061     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01062     RETURN_ARRAYS_DECREMENT();
01063     return nll;
01064   }
01065   dvariable nllNormal(const double x, const dvariable & mu, const dvariable & sigma)
01066   {
01067     RETURN_ARRAYS_INCREMENT();
01068     dvariable nll=0;  
01069     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01070     RETURN_ARRAYS_DECREMENT();
01071     return nll;
01072   } 
01073   dvariable nllNormal(const dvariable & x, const dvariable & mu, const double sigma)
01074   {
01075     RETURN_ARRAYS_INCREMENT();
01076     dvariable nll=0;  
01077     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01078     RETURN_ARRAYS_DECREMENT();
01079     return nll;
01080   } 
01081   dvariable nllNormal(const dvariable & x, const double mu, const dvariable & sigma)
01082   {
01083     RETURN_ARRAYS_INCREMENT();
01084     dvariable nll=0;  
01085     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01086     RETURN_ARRAYS_DECREMENT();
01087     return nll;
01088   }
01089   dvariable nllNormal(const dvariable & x, const dvariable & mu, const dvariable & sigma)
01090   {
01091     RETURN_ARRAYS_INCREMENT();
01092     dvariable nll=0;  
01093     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01094     RETURN_ARRAYS_DECREMENT();
01095     return nll;
01096   }
01097 
01106   df1b2variable nllNormal(const df1b2variable & x, const double mu, const double sigma)
01107   {
01108     df1b2variable nll=.0;  
01109     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01110     return nll;
01111   }
01112   df1b2variable nllNormal(const df1b2variable & x, const df1b2variable & mu, const double sigma)
01113   {
01114     df1b2variable nll=.0;  
01115     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01116     return nll;
01117   }
01118   df1b2variable nllNormal(const df1b2variable & x, const double mu, const df1b2variable & sigma)
01119   {
01120     df1b2variable nll=.0;  
01121     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01122     return nll;
01123   }
01124   df1b2variable nllNormal(const double x, const df1b2variable & mu, const df1b2variable & sigma)
01125   {
01126     df1b2variable nll=.0;  
01127     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01128     return nll;
01129   }
01130   df1b2variable nllNormal(const df1b2variable & x, const df1b2variable & mu, const df1b2variable & sigma)
01131   {
01132     df1b2variable nll=.0;  
01133     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01134     return nll;
01135   }
01136 
01137 
01138 
01147   double nllNormal(const dvector & x, const double mu, const double sigma)
01148   {
01149     double nll=0;  
01150     double n=double(x.size());
01151     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01152     return nll;
01153   }
01154   dvariable nllNormal(const dvar_vector& x, const double mu, const double sigma)
01155   {
01156     RETURN_ARRAYS_INCREMENT();
01157     dvariable nll=0;  
01158     double n=double(x.size());
01159     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01160     RETURN_ARRAYS_DECREMENT();
01161     return nll;
01162   }
01163   dvariable nllNormal(const dvector & x, const dvariable & mu, const dvariable & sigma)
01164   {
01165     RETURN_ARRAYS_INCREMENT();
01166     dvariable nll=0;  
01167     double n=double(x.size());
01168     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01169     RETURN_ARRAYS_DECREMENT();
01170     return nll;
01171   }
01172   dvariable nllNormal(const dvar_vector & x, const dvariable & mu, const dvariable & sigma)
01173   {
01174     RETURN_ARRAYS_INCREMENT();
01175     dvariable nll=0;  
01176     double n=double(x.size());
01177     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01178     RETURN_ARRAYS_DECREMENT();
01179     return nll;
01180   }
01181  
01190   df1b2variable nllNormal(const df1b2vector & x, const double mu, const double sigma)
01191   {
01192     df1b2variable nll=.0;  
01193     double n=double(size_count(x));
01194     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01195     return nll;
01196   }
01197   df1b2variable nllNormal(const dvector & x, const df1b2variable & mu, const df1b2variable & sigma)
01198   {
01199     df1b2variable nll=.0;  
01200     double n=double(size_count(x));
01201     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01202     return nll;
01203   }
01204   df1b2variable nllNormal(const df1b2vector & x, const df1b2variable & mu, const df1b2variable & sigma)
01205   {
01206     df1b2variable nll=.0;  
01207     double n=double(size_count(x));
01208     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01209     return nll;
01210   }
01211  
01212 
01213 
01222   dvariable nllNormal(const dvar_vector & x, const dvector & mu, const double sigma)
01223   {
01224     RETURN_ARRAYS_INCREMENT();
01225     dvariable nll=0;  
01226     double n=double(x.size());
01227     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01228     RETURN_ARRAYS_DECREMENT();
01229     return nll;
01230   }   
01231   dvariable nllNormal(const dvector & x, const dvar_vector & mu, const dvariable & sigma)
01232   {
01233     RETURN_ARRAYS_INCREMENT();
01234     dvariable nll=0;  
01235     double n=double(x.size());
01236     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01237     RETURN_ARRAYS_DECREMENT();
01238     return nll;
01239   }
01240   dvariable nllNormal(const dvar_vector & x, const dvar_vector & mu, const dvariable & sigma)
01241   {
01242     RETURN_ARRAYS_INCREMENT();
01243     dvariable nll=0;  
01244     double n=double(x.size());
01245     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01246     RETURN_ARRAYS_DECREMENT();
01247     return nll;
01248   }
01249 
01258   df1b2variable nllNormal(const df1b2vector & x, const dvector & mu, const double sigma)
01259   {
01260     df1b2variable nll=.0;  
01261     double n=double(size_count(x));
01262     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01263     return nll;
01264   }   
01265   df1b2variable nllNormal(const dvector & x, const df1b2vector & mu, const df1b2variable & sigma)
01266   {
01267     df1b2variable nll=.0;  
01268     double n=double(size_count(x));
01269     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01270     return nll;
01271   } 
01272   df1b2variable nllNormal(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & sigma)
01273   {
01274     df1b2variable nll=.0;  
01275     double n=double(size_count(x));
01276     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01277     return nll;
01278   } 
01279 
01280 
01281 
01282 
01283 
01284 
01286   //
01287   //                                    Normal with tau
01288   //
01290 
01299   dvariable nllNormal2(const dvariable & x, const double mu, const double tau)
01300   {
01301     RETURN_ARRAYS_INCREMENT();
01302     dvariable nll=0;  
01303     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01304     RETURN_ARRAYS_DECREMENT();
01305     return nll;
01306   }
01307   dvariable nllNormal2(const double x, const dvariable & mu, const dvariable & tau)
01308   {
01309     RETURN_ARRAYS_INCREMENT();
01310     dvariable nll=0;  
01311     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01312     RETURN_ARRAYS_DECREMENT();
01313     return nll;
01314   }
01315   dvariable nllNormal2(const dvariable & x, const dvariable & mu, const dvariable & tau)
01316   {
01317     RETURN_ARRAYS_INCREMENT();
01318     dvariable nll=0;  
01319     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01320     RETURN_ARRAYS_DECREMENT();
01321     return nll;
01322   }
01323 
01332   df1b2variable nllNormal2(const df1b2variable & x, const double mu, const double tau)
01333   {
01334     df1b2variable nll=.0;  
01335     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01336     return nll;
01337   }
01338   df1b2variable nllNormal2(const double x, const df1b2variable & mu, const df1b2variable & tau)
01339   {
01340     df1b2variable nll=.0;  
01341     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01342     return nll;
01343   }
01344   df1b2variable nllNormal2(const df1b2variable & x, const df1b2variable & mu, const df1b2variable & tau)
01345   {
01346     df1b2variable nll=.0;  
01347     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01348     return nll;
01349   }
01350 
01351 
01352 
01353 
01362   dvariable nllNormal2(const dvar_vector & x, const double mu, const double tau)
01363   {
01364     RETURN_ARRAYS_INCREMENT();
01365     dvariable nll=0;  
01366     double n=double(x.size());
01367     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);//full likelihood, can drop last term
01368     RETURN_ARRAYS_DECREMENT();
01369     return nll;
01370   }
01371   dvariable nllNormal2(const dvector & x, const dvariable & mu, const dvariable & tau)
01372   {
01373     RETURN_ARRAYS_INCREMENT();
01374     dvariable nll=0;  
01375     double n=double(x.size());
01376     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);//full likelihood, can drop last term
01377     RETURN_ARRAYS_DECREMENT();
01378     return nll;
01379   }
01380   dvariable nllNormal2(const dvar_vector & x, const dvariable & mu, const dvariable & tau)
01381   {
01382     RETURN_ARRAYS_INCREMENT();
01383     dvariable nll=0;  
01384     double n=double(x.size());
01385     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);//full likelihood, can drop last term
01386     RETURN_ARRAYS_DECREMENT();
01387     return nll;
01388   }
01389 
01398   df1b2variable nllNormal2(const df1b2vector & x, const double  mu, const double tau)
01399   {
01400     df1b2variable nll=.0;  
01401     double n=double(size_count(x));
01402     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);//full likelihood, can drop last term
01403     return nll;
01404   }
01405   df1b2variable nllNormal2(const dvector & x, const df1b2variable &  mu, const df1b2variable & tau)
01406   {
01407     df1b2variable nll=.0;  
01408     double n=double(size_count(x));
01409     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);//full likelihood, can drop last term
01410     return nll;
01411   }
01412   df1b2variable nllNormal2(const df1b2vector & x, const df1b2variable &  mu, const df1b2variable & tau)
01413   {
01414     df1b2variable nll=.0;  
01415     double n=double(size_count(x));
01416     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);//full likelihood, can drop last term
01417     return nll;
01418   }
01419 
01420 
01421 
01430   dvariable nllNormal2(const dvar_vector & x, const dvector & mu, const double tau)
01431   {
01432     RETURN_ARRAYS_INCREMENT();
01433     dvariable nll=0;  
01434     double n=double(x.size());
01435     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01436     RETURN_ARRAYS_DECREMENT();
01437     return nll;
01438   } 
01439   dvariable nllNormal2(const dvector & x, const dvar_vector & mu, const dvariable & tau)
01440   {
01441     RETURN_ARRAYS_INCREMENT();
01442     dvariable nll=0;  
01443     double n=double(x.size());
01444     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01445     RETURN_ARRAYS_DECREMENT();
01446     return nll;
01447   } 
01448   dvariable nllNormal2(const dvar_vector & x, const dvar_vector & mu, const dvariable & tau)
01449   {
01450     RETURN_ARRAYS_INCREMENT();
01451     dvariable nll=0;  
01452     double n=double(x.size());
01453     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01454     RETURN_ARRAYS_DECREMENT();
01455     return nll;
01456   } 
01457 
01466   df1b2variable nllNormal2(const df1b2vector & x, const dvector & mu, const double tau)
01467   {
01468     df1b2variable nll=.0;  
01469     double n=double(size_count(x));
01470     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01471     return nll;
01472   } 
01473   df1b2variable nllNormal2(const dvector & x, const df1b2vector & mu, const df1b2variable & tau)
01474   {
01475     df1b2variable nll=.0;  
01476     double n=double(size_count(x));
01477     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01478     return nll;
01479   } 
01480   df1b2variable nllNormal2(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & tau)
01481   {
01482     df1b2variable nll=.0;  
01483     double n=double(size_count(x));
01484     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01485     return nll;
01486   } 
01487 
01488 
01489 
01490 
01491 
01492 
01493 
01495   //
01496   //                                    Binomial
01497   //
01499 
01508   double nllBinomial(const double x, const double n,const double p)
01509   {   
01510     double nll=0;
01511     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01512     return nll;
01513   }
01514   dvariable nllBinomial(const dvariable & x, const double n,const double p)
01515   {   
01516     RETURN_ARRAYS_INCREMENT();
01517     dvariable nll=0;
01518     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01519     RETURN_ARRAYS_DECREMENT();
01520     return nll;
01521   }
01522   dvariable nllBinomial(const double x, const dvariable & n,const dvariable & p)
01523   {   
01524     RETURN_ARRAYS_INCREMENT();
01525     dvariable nll=0;
01526     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01527     RETURN_ARRAYS_DECREMENT();
01528     return nll;
01529   }
01530   dvariable nllBinomial(const dvariable & x, const dvariable & n,const dvariable & p)
01531   {   
01532     RETURN_ARRAYS_INCREMENT();
01533     dvariable nll=0;
01534     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01535     RETURN_ARRAYS_DECREMENT();
01536     return nll;
01537   }
01538 
01547   df1b2variable nllBinomial(const df1b2variable & x, const double n,const double p)
01548   {   
01549     df1b2variable nll=.0;
01550     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01551     return nll;
01552   }
01553   df1b2variable nllBinomial(const double x, const df1b2variable & n,const df1b2variable & p)
01554   {   
01555     df1b2variable nll=.0;
01556     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01557     return nll;
01558   }
01559   df1b2variable nllBinomial(const df1b2variable & x, const df1b2variable & n,const df1b2variable & p)
01560   {   
01561     df1b2variable nll=.0;
01562     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01563     return nll;
01564   }
01565 
01566 
01567 
01568 
01577   dvariable nllBinomial(const dvar_vector & x, const dvector & n,const double p)
01578   {   
01579     RETURN_ARRAYS_INCREMENT();
01580     dvariable nll=0;
01581     nll=-sum(x)*log(p+EPS)-sum(n-x)*log(1.-p+EPS)-sum(log_comb(n,x)); //full likelihood, can drop the last term
01582     RETURN_ARRAYS_DECREMENT();
01583     return nll;
01584   }
01585   dvariable nllBinomial(const dvector & x, const dvar_vector & n,const dvariable & p)
01586   {   
01587     RETURN_ARRAYS_INCREMENT();
01588     dvariable nll=0;
01589     nll=-sum(x)*log(p+EPS)-sum(n-x)*log(1.-p+EPS)-sum(log_comb(n,x)); //full likelihood, can drop the last term
01590     RETURN_ARRAYS_DECREMENT();
01591     return nll;
01592   }
01593   dvariable nllBinomial(const dvar_vector & x, const dvar_vector & n,const dvariable & p)
01594   {   
01595     RETURN_ARRAYS_INCREMENT();
01596     dvariable nll=0;
01597     nll=-sum(x)*log(p+EPS)-sum(n-x)*log(1.-p+EPS)-sum(log_comb(n,x)); //full likelihood, can drop the last term
01598     RETURN_ARRAYS_DECREMENT();
01599     return nll;
01600   }
01601 
01610   df1b2variable nllBinomial(const dvector & x, const df1b2vector & n,const df1b2variable & p)
01611   {   
01612     df1b2variable nll=.0;
01613     nll=-sum(x)*log(p+EPS)-sum(n-x)*log(1.-p+EPS)-sum(log_comb(n,x)); //full likelihood, can drop the last term
01614     return nll;
01615   }
01616   df1b2variable nllBinomial(const df1b2vector & x, const dvector & n,const double p)
01617   {   
01618     df1b2variable nll=.0;
01619     nll=-sum(x)*log(p+EPS)-sum(n-x)*log(1.-p+EPS)-sum(log_comb(n,x)); //full likelihood, can drop the last term
01620     return nll;
01621   }
01622   df1b2variable nllBinomial(const df1b2vector & x, const df1b2vector & n,const df1b2variable & p)
01623   {   
01624     df1b2variable nll=.0;
01625     nll=-sum(x)*log(p+EPS)-sum(n-x)*log(1.-p+EPS)-sum(log_comb(n,x)); //full likelihood, can drop the last term
01626     return nll;
01627   }
01628 
01629 
01630 
01631 
01632 
01633 
01634 
01635 
01637   //
01638   //                                    Lognormal
01639   //
01641 
01650   double nllLognormal(const double x, const double mu, const double sigma)
01651   { 
01652     double nll=0;     
01653     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01654     return nll;
01655   }
01656   dvariable nllLognormal(const dvariable& x, const double mu, const double sigma)
01657   { 
01658     RETURN_ARRAYS_INCREMENT();
01659     dvariable nll=0;    
01660     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01661     RETURN_ARRAYS_DECREMENT();
01662     return nll;
01663   }
01664   dvariable nllLognormal(const double x, const dvariable & mu, const dvariable & sigma)
01665   { 
01666     RETURN_ARRAYS_INCREMENT();
01667     dvariable nll=0;    
01668     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01669     RETURN_ARRAYS_DECREMENT();
01670     return nll;
01671   }
01672   dvariable nllLognormal(const dvariable & x, const dvariable & mu, const double sigma)
01673   { 
01674     RETURN_ARRAYS_INCREMENT();
01675     dvariable nll=0;    
01676     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01677     RETURN_ARRAYS_DECREMENT();
01678     return nll;
01679   }
01680   dvariable nllLognormal(const dvariable & x, const double mu, const dvariable & sigma)
01681   { 
01682     RETURN_ARRAYS_INCREMENT();
01683     dvariable nll=0;    
01684     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01685     RETURN_ARRAYS_DECREMENT();
01686     return nll;
01687   }
01688   dvariable nllLognormal(const dvariable & x, const dvariable & mu, const dvariable & sigma)
01689   { 
01690     RETURN_ARRAYS_INCREMENT();
01691     dvariable nll=0;    
01692     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01693     RETURN_ARRAYS_DECREMENT();
01694     return nll;
01695   }
01696 
01705   df1b2variable nllLognormal(const df1b2variable & x, const double mu, const double sigma)
01706   { 
01707     df1b2variable nll=.0;     
01708     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01709     return nll;
01710   }
01711   df1b2variable nllLognormal(const double x, const df1b2variable & mu, const df1b2variable & sigma)
01712   { 
01713     df1b2variable nll=.0;     
01714     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01715     return nll;
01716   }
01717   df1b2variable nllLognormal(const df1b2variable & x, const df1b2variable & mu, const double sigma)
01718   { 
01719     df1b2variable nll=.0;     
01720     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01721     return nll;
01722   }
01723   df1b2variable nllLognormal(const df1b2variable & x, const double mu, const df1b2variable & sigma)
01724   { 
01725     df1b2variable nll=.0;     
01726     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01727     return nll;
01728   }
01729   df1b2variable nllLognormal(const df1b2variable & x, const df1b2variable & mu, const df1b2variable & sigma)
01730   { 
01731     df1b2variable nll=.0;     
01732     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01733     return nll;
01734   }
01735 
01736 
01737 
01738 
01747   dvariable nllLognormal(const dvar_vector & x, const double mu, const double sigma)
01748   { 
01749     RETURN_ARRAYS_INCREMENT();
01750     dvariable nll=0;  
01751     double n=double(x.size());
01752     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01753     RETURN_ARRAYS_DECREMENT();
01754     return nll;
01755   }  
01756   dvariable nllLognormal(const dvector & x, const dvariable & mu, const dvariable & sigma)
01757   { 
01758     RETURN_ARRAYS_INCREMENT();
01759     dvariable nll=0;  
01760     double n=double(x.size());
01761     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01762     RETURN_ARRAYS_DECREMENT();
01763     return nll;
01764   }  
01765   dvariable nllLognormal(const dvar_vector & x, const dvariable & mu, const dvariable & sigma)
01766   { 
01767     RETURN_ARRAYS_INCREMENT();
01768     dvariable nll=0;  
01769     double n=double(x.size());
01770     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01771     RETURN_ARRAYS_DECREMENT();
01772     return nll;
01773   }  
01774 
01783   df1b2variable nllLognormal(const df1b2vector & x, const double mu, const double sigma)
01784   { 
01785     df1b2variable nll=.0;   
01786     double n=double(size_count(x));
01787     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01788     return nll;
01789   }  
01790   df1b2variable nllLognormal(const dvector & x, const df1b2variable & mu, const df1b2variable & sigma)
01791   { 
01792     df1b2variable nll=.0;   
01793     double n=double(size_count(x));
01794     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01795     return nll;
01796   }  
01797   df1b2variable nllLognormal(const df1b2vector & x, const df1b2variable & mu, const df1b2variable & sigma)
01798   { 
01799     df1b2variable nll=.0;   
01800     double n=double(size_count(x));
01801     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01802     return nll;
01803   }  
01804 
01805 
01806 
01815   dvariable nllLognormal(const dvar_vector & x, const dvector & mu, const double sigma)
01816   { 
01817     RETURN_ARRAYS_INCREMENT();
01818     dvariable nll=0;  
01819     double n=double(x.size());
01820     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01821     RETURN_ARRAYS_DECREMENT();
01822     return nll;
01823   }  
01824   dvariable nllLognormal(const dvector & x, const dvar_vector & mu, const dvariable & sigma)
01825   { 
01826     RETURN_ARRAYS_INCREMENT();
01827     dvariable nll=0;  
01828     double n=double(x.size());
01829     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01830     RETURN_ARRAYS_DECREMENT();
01831     return nll;
01832   }  
01833   dvariable nllLognormal(const dvar_vector & x, const dvar_vector & mu, const dvariable & sigma)
01834   { 
01835     RETURN_ARRAYS_INCREMENT();
01836     dvariable nll=0;  
01837     double n=double(x.size());
01838     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01839     RETURN_ARRAYS_DECREMENT();
01840     return nll;
01841   }  
01842 
01851   df1b2variable nllLognormal(const df1b2vector & x, const dvector & mu, const double sigma)
01852   { 
01853     df1b2variable nll=.0;   
01854     double n=double(size_count(x));
01855     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01856     return nll;
01857   }  
01858   df1b2variable nllLognormal(const dvector & x, const df1b2vector & mu, const df1b2variable & sigma)
01859   { 
01860     df1b2variable nll=.0;   
01861     double n=double(size_count(x));
01862     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01863     return nll;
01864   } 
01865   df1b2variable nllLognormal(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & sigma)
01866   { 
01867     df1b2variable nll=.0;   
01868     double n=double(size_count(x));
01869     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01870     return nll;
01871   } 
01872 
01873 
01874 
01875 
01876 
01877 
01879   //
01880   //                                    Lognormal with tau
01881   //
01883 
01892   dvariable nllLognormal2(const double x, const dvariable & mu, const dvariable & tau)
01893   { 
01894     RETURN_ARRAYS_INCREMENT();
01895     dvariable nll=0;  
01896     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01897     RETURN_ARRAYS_DECREMENT();
01898     return nll;
01899   }
01900   dvariable nllLognormal2(const dvariable & x, const double mu, const double tau)
01901   { 
01902     RETURN_ARRAYS_INCREMENT();
01903     dvariable nll=0;  
01904     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01905     RETURN_ARRAYS_DECREMENT();
01906     return nll;
01907   }
01908   dvariable nllLognormal2(const dvariable & x, const dvariable & mu, const dvariable & tau)
01909   { 
01910     RETURN_ARRAYS_INCREMENT();
01911     dvariable nll=0;  
01912     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01913     RETURN_ARRAYS_DECREMENT();
01914     return nll;
01915   }
01916 
01925   df1b2variable nllLognormal2(const df1b2variable x, const double mu, const double tau)
01926   { 
01927     df1b2variable nll=.0;   
01928     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01929     return nll;
01930   }
01931   df1b2variable nllLognormal2(const double x, const df1b2variable & mu, const df1b2variable & tau)
01932   { 
01933     df1b2variable nll=.0;   
01934     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01935     return nll;
01936   }
01937   df1b2variable nllLognormal2(const df1b2variable x, const df1b2variable & mu, const double tau)
01938   { 
01939     df1b2variable nll=.0;   
01940     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01941     return nll;
01942   }
01943   df1b2variable nllLognormal2(const df1b2variable x, const double mu, const df1b2variable & tau)
01944   { 
01945     df1b2variable nll=.0;   
01946     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01947     return nll;
01948   }
01949   df1b2variable nllLognormal2(const df1b2variable x, const df1b2variable & mu, const df1b2variable & tau)
01950   { 
01951     df1b2variable nll=.0;   
01952     nll= -0.5*log(tau+EPS)+log(x+EPS)+0.5*tau*square(log(x+EPS)-mu)+0.5*log(2.*M_PI);//full likelihood
01953     return nll;
01954   }
01955 
01956 
01957 
01958 
01967   dvariable nllLognormal2(const dvar_vector & x, const double mu, const double tau)
01968   { 
01969     RETURN_ARRAYS_INCREMENT();
01970     dvariable nll=0;
01971     double n=double(x.size());
01972     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
01973     RETURN_ARRAYS_DECREMENT();
01974     return nll;
01975   }
01976   dvariable nllLognormal2(const dvector & x, const dvariable & mu, const dvariable & tau)
01977   { 
01978     RETURN_ARRAYS_INCREMENT();
01979     dvariable nll=0;
01980     double n=double(x.size());
01981     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
01982     RETURN_ARRAYS_DECREMENT();
01983     return nll;
01984   }
01985   dvariable nllLognormal2(const dvar_vector & x, const dvariable & mu, const dvariable & tau)
01986   { 
01987     RETURN_ARRAYS_INCREMENT();
01988     dvariable nll=0;
01989     double n=double(x.size());
01990     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
01991     RETURN_ARRAYS_DECREMENT();
01992     return nll;
01993   }
01994 
02003   df1b2variable nllLognormal2(const dvector & x, const df1b2variable & mu, const df1b2variable & tau)
02004   { 
02005     df1b2variable nll=.0;
02006     double n=double(size_count(x)); 
02007     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02008     return nll;
02009   }
02010   df1b2variable nllLognormal2(const df1b2vector & x, const double mu, const double tau)
02011   { 
02012     df1b2variable nll=.0;
02013     double n=double(size_count(x)); 
02014     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02015     return nll;
02016   }
02017   df1b2variable nllLognormal2(const df1b2vector & x, const df1b2variable & mu, const double tau)
02018   { 
02019     df1b2variable nll=.0;
02020     double n=double(size_count(x)); 
02021     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02022     return nll;
02023   }
02024   df1b2variable nllLognormal2(const df1b2vector & x, const double mu, const df1b2variable & tau)
02025   { 
02026     df1b2variable nll=.0;
02027     double n=double(size_count(x)); 
02028     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02029     return nll;
02030   }
02031   df1b2variable nllLognormal2(const df1b2vector & x, const df1b2variable & mu, const df1b2variable & tau)
02032   { 
02033     df1b2variable nll=.0;
02034     double n=double(size_count(x)); 
02035     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02036     return nll;
02037   }
02038 
02039 
02040 
02041 
02050   dvariable nllLognormal2(const dvar_vector & x, const dvector & mu, const double tau)
02051   { 
02052     RETURN_ARRAYS_INCREMENT();
02053     dvariable nll=0;  
02054     double n=double(x.size());
02055     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02056     RETURN_ARRAYS_DECREMENT();
02057     return nll;
02058   }  
02059   dvariable nllLognormal2(const dvector & x, const dvar_vector & mu, const dvariable & tau)
02060   { 
02061     RETURN_ARRAYS_INCREMENT();
02062     dvariable nll=0;  
02063     double n=double(x.size());
02064     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02065     RETURN_ARRAYS_DECREMENT();
02066     return nll;
02067   }  
02068   dvariable nllLognormal2(const dvar_vector & x, const dvar_vector & mu, const dvariable & tau)
02069   { 
02070     RETURN_ARRAYS_INCREMENT();
02071     dvariable nll=0;  
02072     double n=double(x.size());
02073     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02074     RETURN_ARRAYS_DECREMENT();
02075     return nll;
02076   }  
02077 
02086   df1b2variable nllLognormal2(const df1b2vector & x, const dvector & mu, const double tau)
02087   { 
02088     df1b2variable nll=.0;   
02089     double n=double(size_count(x));
02090     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02091     return nll;
02092   }  
02093   df1b2variable nllLognormal2(const dvector & x, const df1b2vector & mu, const df1b2variable & tau)
02094   { 
02095     df1b2variable nll=.0;   
02096     double n=double(size_count(x));
02097     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02098     return nll;
02099   } 
02100   df1b2variable nllLognormal2(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & tau)
02101   { 
02102     df1b2variable nll=.0;   
02103     double n=double(size_count(x));
02104     nll=-0.5*n*log(tau+EPS)+sum(log(x+EPS))+0.5*tau*norm2(log(x+EPS)-mu)+0.5*n*log(2.*M_PI);
02105     return nll;
02106   } 
02107 
02108 
02109 
02110 
02111 
02112 
02113 
02115   //
02116   //                                    Poisson
02117   //
02119 
02127   double nllPoisson(const double x, const double lambda)
02128   {
02129     double nll=0;
02130     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02131     return nll;
02132   }
02133   dvariable nllPoisson(const dvariable & x, const double lambda)
02134   {
02135     RETURN_ARRAYS_INCREMENT();
02136     dvariable nll=0;
02137     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02138     RETURN_ARRAYS_DECREMENT();
02139     return nll;
02140   }
02141   dvariable nllPoisson(const double x, const dvariable & lambda)
02142   {
02143     RETURN_ARRAYS_INCREMENT();
02144     dvariable nll=0;
02145     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02146     RETURN_ARRAYS_DECREMENT();
02147     return nll;
02148   }
02149   dvariable nllPoisson(const dvariable & x, const dvariable & lambda)
02150   {
02151     RETURN_ARRAYS_INCREMENT();
02152     dvariable nll=0;
02153     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02154     RETURN_ARRAYS_DECREMENT();
02155     return nll;
02156   }
02157 
02165   df1b2variable nllPoisson(const df1b2variable & x, const double lambda)
02166   {
02167     df1b2variable nll=.0;
02168     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02169     return nll;
02170   }
02171   df1b2variable nllPoisson(const double x, const df1b2variable & lambda)
02172   {
02173     df1b2variable nll=.0;
02174     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02175     return nll;
02176   }
02177   df1b2variable nllPoisson(const df1b2variable & x, const df1b2variable & lambda)
02178   {
02179     df1b2variable nll=.0;
02180     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02181     return nll;
02182   }
02183 
02184 
02185 
02193   dvariable nllPoisson(const dvar_vector & x, const double lambda)
02194   {
02195     RETURN_ARRAYS_INCREMENT();
02196     dvariable nll=0;
02197     double n=double(x.size());
02198     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02199     RETURN_ARRAYS_DECREMENT();
02200     return nll;
02201   }  
02202   dvariable nllPoisson(const dvector & x, const dvariable & lambda)
02203   {
02204     RETURN_ARRAYS_INCREMENT();
02205     dvariable nll=0;
02206     double n=double(x.size());
02207     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02208     RETURN_ARRAYS_DECREMENT();
02209     return nll;
02210   }  
02211   dvariable nllPoisson(const dvar_vector & x, const dvariable & lambda)
02212   {
02213     RETURN_ARRAYS_INCREMENT();
02214     dvariable nll=0;
02215     double n=double(x.size());
02216     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02217     RETURN_ARRAYS_DECREMENT();
02218     return nll;
02219   }  
02220 
02228   df1b2variable nllPoisson(const dvector & x, const df1b2variable & lambda)
02229   {
02230     df1b2variable nll=.0;
02231     double n=double(size_count(x)); 
02232     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02233     return nll;
02234   }  
02235   df1b2variable nllPoisson(const df1b2vector & x, const double lambda)
02236   {
02237     df1b2variable nll=.0;
02238     double n=double(size_count(x)); 
02239     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02240     return nll;
02241   }  
02242   df1b2variable nllPoisson(const df1b2vector & x, const df1b2variable & lambda)
02243   {
02244     df1b2variable nll=.0;
02245     double n=double(size_count(x)); 
02246     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02247     return nll;
02248   }  
02249 
02250 
02251 
02252 
02253 
02254 
02256   //
02257   //                                    Inverse Gamma
02258   //
02260 
02269   double nllInverseGamma(const double x, const double a, const double b)
02270   {
02271     double nll=0;  
02272     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02273     return nll;
02274   }
02275   dvariable nllInverseGamma(const dvariable & x, const double a, const double b)
02276   {
02277     RETURN_ARRAYS_INCREMENT();
02278     dvariable nll=0;  
02279     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02280     RETURN_ARRAYS_DECREMENT();
02281     return nll;
02282   }
02283   dvariable nllInverseGamma(const double x, const dvariable & a, const dvariable & b)
02284   {
02285     RETURN_ARRAYS_INCREMENT();
02286     dvariable nll=0;  
02287     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02288     RETURN_ARRAYS_DECREMENT();
02289     return nll;
02290   }
02291   dvariable nllInverseGamma(const dvariable & x, const dvariable & a, const dvariable & b)
02292   {
02293     RETURN_ARRAYS_INCREMENT();
02294     dvariable nll=0;  
02295     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02296     RETURN_ARRAYS_DECREMENT();
02297     return nll;
02298   }
02299 
02308   df1b2variable nllInverseGamma(const df1b2variable & x, const double a, const double b)
02309   {
02310     df1b2variable nll=.0;  
02311     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02312     return nll;
02313   }
02314   df1b2variable nllInverseGamma(const double x, const df1b2variable & a, const df1b2variable & b)
02315   {
02316     df1b2variable nll=.0;  
02317     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02318     return nll;
02319   }
02320   df1b2variable nllInverseGamma(const df1b2variable & x, const df1b2variable & a, const double b)
02321   {
02322     df1b2variable nll=.0;  
02323     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02324     return nll;
02325   }
02326   df1b2variable nllInverseGamma(const df1b2variable & x, const double a, const df1b2variable & b)
02327   {
02328     df1b2variable nll=.0;  
02329     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02330     return nll;
02331   }
02332   df1b2variable nllInverseGamma(const df1b2variable & x, const df1b2variable & a, const df1b2variable & b)
02333   {
02334     df1b2variable nll=.0;  
02335     nll= - a*log(b)+gammln(a)  -(a-1.)*log(x+EPS) + b/(x+EPS);
02336     return nll;
02337   }
02338 
02339 
02340 
02341 
02350   dvariable nllInverseGamma(const dvar_vector & x, const double a, const double b)
02351   {
02352     RETURN_ARRAYS_INCREMENT();
02353     dvariable nll=0;  
02354     double n=double(x.size());
02355     nll= - n*a*log(b)+ n*gammln(a)  -(a-1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02356     RETURN_ARRAYS_DECREMENT();
02357     return nll;
02358   }
02359   dvariable nllInverseGamma(const dvector & x, const dvariable & a, const dvariable & b)
02360   {
02361     RETURN_ARRAYS_INCREMENT();
02362     dvariable nll=0;  
02363     double n=double(x.size());
02364     nll= - n*a*log(b)+ n*gammln(a)  -(a-1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02365     RETURN_ARRAYS_DECREMENT();
02366     return nll;
02367   }
02368   dvariable nllInverseGamma(const dvar_vector & x, const dvariable & a, const dvariable & b)
02369   {
02370     RETURN_ARRAYS_INCREMENT();
02371     dvariable nll=0;  
02372     double n=double(x.size());
02373     nll= - n*a*log(b)+ n*gammln(a)  -(a-1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02374     RETURN_ARRAYS_DECREMENT();
02375     return nll;
02376   }
02377 
02386   df1b2variable nllInverseGamma(const dvector & x, const df1b2variable & a, const df1b2variable & b)
02387   {
02388     df1b2variable nll=.0;  
02389     double n=double(size_count(x)); 
02390     nll= - n*a*log(b)+ n*gammln(a)  -(a-1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02391     return nll;
02392   }
02393   df1b2variable nllInverseGamma(const df1b2vector & x, const double a, const double b)
02394   {
02395     df1b2variable nll=.0;  
02396     double n=double(size_count(x)); 
02397     nll= - n*a*log(b)+ n*gammln(a)  -(a-1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02398     return nll;
02399   }
02400   df1b2variable nllInverseGamma(const df1b2vector & x, const df1b2variable & a, const df1b2variable & b)
02401   {
02402     df1b2variable nll=.0;  
02403     double n=double(size_count(x)); 
02404     nll= - n*a*log(b)+ n*gammln(a)  -(a-1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02405     return nll;
02406   }