ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
qfc_est.cpp
Go to the documentation of this file.
00001 
00039   #include "qfclib.h"
00040 
00041 
00042 
00043 
00051   dvar_vector logitProp(const dvar_vector& a)
00052   {
00053     int dim; 
00054     dim=a.size()+1;
00055     dvar_vector p(1,dim);
00056     dvar_vector expa=mfexp(a);
00057     p(1,dim-1)=expa/(1.+sum(expa));
00058     //p(dim)=1.-sum(p(1,dim-1));  //original version was buggy for the last term
00059     p(dim)=1./(1.+sum(expa));
00060     return p;
00061   }
00062   df1b2vector logitProp(const df1b2vector& a)
00063   {
00064     int dim; 
00065     dim=size_count(a)+1;
00066     df1b2vector p(1,dim);
00067     df1b2vector expa=mfexp(a);
00068     p(1,dim-1)=expa/(1.+sum(expa));
00069     //p(dim)=1.-sum(p(1,dim-1));  //original version was buggy for the last term
00070     p(dim)=1./(1.+sum(expa));
00071     return p;
00072   }
00073 
00074 
00075 
00076 
00082   dvar_vector invLogitProp(const dvar_vector& p)
00083   {
00084     int dim; 
00085     dim=p.size()-1;
00086     dvar_vector a(1,dim); 
00087     dvar_vector lp(1,dim+1);
00088   
00089     lp=log(p+EPS);//take log for each p
00090     a=lp(1,dim)-lp(dim+1);//each subtract the last one
00091     return a;
00092   }
00093   df1b2vector invLogitProp(const df1b2vector& p)
00094   {
00095     int dim; 
00096     dim=size_count(p)-1;
00097     df1b2vector a(1,dim); 
00098     df1b2vector lp(1,dim+1);
00099   
00100     lp=log(p+EPS);//take log for each p
00101     a=lp(1,dim)-lp(dim+1);//each subtract the last one
00102     return a;
00103   }
00104 
00105 
00106 
00107 
00108 
00115   dvar_vector normalize_p(dvar_vector& p, dvariable fpen)
00116   {
00117     dvariable psum=sum(p);
00118     p/=psum; // Now the p will sum to 1
00119     fpen+=1000.*square(log(psum)); //penalty
00120     return p;
00121   }
00122   df1b2vector normalize_p(df1b2vector& p, df1b2variable fpen)
00123   {
00124     df1b2variable psum=sum(p);
00125     p/=psum; // Now the p will sum to 1
00126     fpen+=1000.*square(log(psum)); //penalty
00127     return p;
00128   }
00129 
00130 
00131 
00132 
00142   dvar_vector posfun(dvar_vector& x,const dvector& eps, dvariable& pen) //x and eps both are vector
00143   { 
00144     for(int i=x.indexmin();i<=x.indexmax();i++)
00145     {
00146       x(i)=posfun(x(i),eps(i),pen);
00147     }
00148     return x;
00149   }
00150   df1b2vector posfun(df1b2vector& x,const dvector& eps, df1b2variable& pen) //x and eps both are vector
00151   { 
00152     for(int i=x.indexmin();i<=x.indexmax();i++)
00153     {
00154       x(i)=posfun(x(i),eps(i),pen);
00155     }
00156     return x;
00157   }
00158 
00159 
00160 
00161 
00172   dvar_matrix posfun(dvar_matrix& x,const dmatrix & eps, dvariable& pen)
00173   {
00174     for(int i=x.rowmin();i<=x.rowmax();i++)
00175     {
00176       for(int j=x.colmin();j<=x.colmax();j++)
00177       {
00178         x(i,j)=posfun(x(i,j),eps(i,j),pen);
00179       }
00180     }
00181     return x;
00182   }
00183   df1b2matrix posfun(df1b2matrix& x,const dmatrix & eps, df1b2variable& pen)
00184   {
00185     for(int i=x.rowmin();i<=x.rowmax();i++)
00186     {
00187       for(int j=x.colmin();j<=x.colmax();j++)
00188       {
00189         x(i,j)=posfun(x(i,j),eps(i,j),pen);
00190       }
00191     }
00192     return x;
00193   }
00194 
00195 
00196 
00197 
00198   
00199  
00210   dvariable mf_upper_bound2(const dvariable & x,const double fmax, dvariable & fpen)
00211   {
00212     if (value(x)<=fmax) return x;
00213     else
00214     {
00215       fpen+=.01*square(x-fmax);
00216       return x-x/(2.-fmax/x)+fmax;
00217     }
00218   }
00219 
00220   df1b2variable mf_upper_bound2(const df1b2variable & x,const double fmax, df1b2variable & fpen)
00221   {
00222     if (value(x)<=fmax) return x;
00223     else
00224     {
00225       fpen+=.01*square(x-fmax);
00226       return x-x/(2.-fmax/x)+fmax;
00227     }
00228   }
00229 
00230 
00231 
00232 
00233 
00243   dvar_vector boundp(const dvar_vector & x, const double fmin, const double fmax, const dvariable & fpen )
00244   {
00245     dvar_vector t(x.indexmin(),x.indexmax());
00246     for(int i=x.indexmin();i<=x.indexmax();i++)
00247       t(i)=boundp(x(i),fmin,fmax,fpen);
00248     return (t);
00249   }
00250   df1b2vector boundp(const df1b2vector & x, const double fmin, const double fmax, const df1b2variable & fpen )
00251   {
00252     df1b2vector t(x.indexmin(),x.indexmax());
00253     for(int i=x.indexmin();i<=x.indexmax();i++)
00254       t(i)=boundp(x(i),fmin,fmax,fpen);
00255     return (t);
00256   }
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00271   df1b2vector log_comb(const df1b2vector & n,const df1b2vector & x)
00272   {
00273     df1b2vector tmp(n.indexmin(),n.indexmax());
00274     for(int i=n.indexmin();i<=n.indexmax();i++)
00275       tmp(i)=log_comb(n(i),x(i));
00276     return tmp;
00277   }
00278   df1b2vector log_comb(const df1b2vector & n,const dvector & x)
00279   {
00280     df1b2vector tmp(n.indexmin(),n.indexmax());
00281     for(int i=n.indexmin();i<=n.indexmax();i++)
00282       tmp(i)=log_comb(n(i),x(i));
00283     return tmp;
00284   }
00285   df1b2vector log_comb(const dvector & n,const df1b2vector & x)
00286   {
00287     df1b2vector tmp(n.indexmin(),n.indexmax());
00288     for(int i=n.indexmin();i<=n.indexmax();i++)
00289       tmp(i)=log_comb(n(i),x(i));
00290     return tmp;
00291   }  
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00300   //
00301   //                                    Negative Binomial 
00302   //
00304 
00315   double  nllNegativeBinomial(const double obs, const double m, const double s )
00316   {
00317     double nll=0; 
00318     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00319     return nll;
00320   }
00321   dvariable  nllNegativeBinomial(const dvariable & obs, const double m, const double s )
00322   {
00323     RETURN_ARRAYS_INCREMENT();
00324     dvariable nll=0; 
00325     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00326     RETURN_ARRAYS_DECREMENT();
00327     return nll;
00328   } 
00329   dvariable  nllNegativeBinomial(const double obs, const dvariable & m, const dvariable & s)
00330   {
00331     RETURN_ARRAYS_INCREMENT();
00332     dvariable nll=0; 
00333     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00334     RETURN_ARRAYS_DECREMENT();
00335     return nll;
00336   } 
00337   dvariable  nllNegativeBinomial(const dvariable & obs, const dvariable & m, const double s)
00338   {
00339     RETURN_ARRAYS_INCREMENT();
00340     dvariable nll=0; 
00341     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00342     RETURN_ARRAYS_DECREMENT();
00343     return nll;
00344   }
00345   dvariable  nllNegativeBinomial(const dvariable & obs, const double m, const dvariable & s)
00346   {
00347     RETURN_ARRAYS_INCREMENT();
00348     dvariable nll=0; 
00349     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00350     RETURN_ARRAYS_DECREMENT();
00351     return nll;
00352   }
00353   dvariable  nllNegativeBinomial(const dvariable & obs, const dvariable & m, const dvariable & s)
00354   {
00355     RETURN_ARRAYS_INCREMENT();
00356     dvariable nll=0; 
00357     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00358     RETURN_ARRAYS_DECREMENT();
00359     return nll;
00360   }
00361 
00372   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const double m, const double s)
00373   {
00374     df1b2variable nll=.0; 
00375     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00376     return nll;
00377   }
00378   df1b2variable  nllNegativeBinomial(const double obs, const df1b2variable & m, const df1b2variable & s)
00379   {
00380     df1b2variable nll=.0; 
00381     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00382     return nll;
00383   }
00384   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const df1b2variable & m, const double s)
00385   {
00386     df1b2variable nll=.0; 
00387     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00388     return nll;
00389   }
00390   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const double m, const df1b2variable & s)
00391   {
00392     df1b2variable nll=.0; 
00393     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00394     return nll;
00395   }
00396   df1b2variable  nllNegativeBinomial(const df1b2variable & obs, const df1b2variable & m, const df1b2variable & s)
00397   {
00398     df1b2variable nll=.0; 
00399     nll= -gammln(obs+s)+ gammln(s)- s*log(s/(m+s)+EPS)- obs*log(m/(m+s)+EPS)+ gammln(obs+1.);
00400     return nll;
00401   }
00402 
00403 
00404 
00405 
00406 
00417   dvariable  nllNegativeBinomial(const dvector & obs, const dvariable & m, const dvariable & s)
00418   {
00419     RETURN_ARRAYS_INCREMENT();
00420     dvariable nll=0; 
00421     double n=double(obs.size());
00422     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.));
00423     RETURN_ARRAYS_DECREMENT();
00424     return nll;
00425   }
00426   dvariable  nllNegativeBinomial(const dvar_vector & obs, const double m, const double s)
00427   {
00428     RETURN_ARRAYS_INCREMENT();
00429     dvariable nll=0; 
00430     double n=double(obs.size());
00431     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.));
00432     RETURN_ARRAYS_DECREMENT();
00433     return nll;
00434   }
00435   dvariable  nllNegativeBinomial(const dvar_vector & obs, const dvariable & m, const dvariable & s)
00436   {
00437     RETURN_ARRAYS_INCREMENT();
00438     dvariable nll=0; 
00439     double n=double(obs.size());
00440     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.));
00441     RETURN_ARRAYS_DECREMENT();
00442     return nll;
00443   }
00444 
00455   df1b2variable  nllNegativeBinomial(const df1b2vector & obs, const double m, const double s)
00456   {
00457     df1b2variable nll=.0; 
00458     double n=double(size_count(obs));
00459     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.));
00460     return nll;
00461   }
00462   df1b2variable  nllNegativeBinomial(const dvector & obs, const df1b2variable & m, const df1b2variable & s)
00463   {
00464     df1b2variable nll=.0; 
00465     double n=double(size_count(obs));
00466     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.));
00467     return nll;
00468   }
00469   df1b2variable  nllNegativeBinomial(const df1b2vector & obs, const df1b2variable & m, const df1b2variable & s)
00470   {
00471     df1b2variable nll=.0; 
00472     double n=double(size_count(obs));
00473     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.));
00474     return nll;
00475   }
00476 
00477 
00478 
00479 
00481   //
00482   //                                    Negative Binomial with tau
00483   //
00485 
00495   double nllNegativeBinomial2(const double obs, const double m, const double tau)
00496   {
00497     return -log_density_negbinomial (obs,m,tau); //use admb built in function
00498   } 
00499   dvariable nllNegativeBinomial2(const double obs, const dvariable & m, const dvariable & tau)
00500   {
00501     return -1.*log_negbinomial_density(obs,m,tau); //use admb built in function
00502   }   
00503   dvariable nllNegativeBinomial2(const dvariable & obs, const double m, const double tau)
00504   {
00505     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00506   }   
00507   dvariable nllNegativeBinomial2(const dvariable & obs, const dvariable & m, const double tau)
00508   {
00509     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00510   } 
00511   dvariable nllNegativeBinomial2(const dvariable & obs, const double m, const dvariable & tau)
00512   {
00513     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00514   } 
00515   dvariable nllNegativeBinomial2(const dvariable & obs, const dvariable & m, const dvariable & tau)
00516   {
00517     return nllNegativeBinomial(obs, m,m/(tau-1.+EPS));
00518   } 
00519 
00529   df1b2variable nllNegativeBinomial2(const df1b2variable & obs, const double m, const double tau)
00530   {
00531     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00532   }
00533   df1b2variable nllNegativeBinomial2(const double obs, const df1b2variable & m, const df1b2variable & tau)
00534   {
00535     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00536   }
00537   df1b2variable nllNegativeBinomial2(const df1b2variable & obs, const df1b2variable & m, const df1b2variable & tau)
00538   {
00539     return nllNegativeBinomial(obs,m,m/(tau-1.+EPS));
00540   }
00541 
00542 
00543 
00544 
00545 
00546 
00548   //
00549   //                                    Multinomial
00550   //
00552 
00560   double  nllMultiNomial(const dvector & obsN, const dvector & p)
00561   {
00562     double nll=0;  
00563     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00564     return nll;
00565   } 
00566   dvariable  nllMultiNomial(const dvector & obsN, const dvar_vector & p)
00567   {
00568     RETURN_ARRAYS_INCREMENT(); 
00569     dvariable nll=0;  
00570     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00571     RETURN_ARRAYS_DECREMENT();
00572     return nll;
00573   }
00574   dvariable  nllMultiNomial(const dvar_vector & obsN, const dvector & p)
00575   {
00576     RETURN_ARRAYS_INCREMENT(); 
00577     dvariable nll=0;  
00578     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00579     RETURN_ARRAYS_DECREMENT();
00580     return nll;
00581   }   
00582   dvariable  nllMultiNomial(const dvar_vector & obsN, const dvar_vector & p)
00583   {
00584     RETURN_ARRAYS_INCREMENT(); 
00585     dvariable nll=0;  
00586     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00587     RETURN_ARRAYS_DECREMENT();
00588     return nll;
00589   }    
00590 
00598   df1b2variable  nllMultiNomial(const df1b2vector & obsN, const dvector & p)
00599   {
00600     df1b2variable nll=.0;  
00601     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00602     return nll;
00603   }
00604   df1b2variable  nllMultiNomial(const dvector & obsN, const df1b2vector & p)
00605   {
00606     df1b2variable nll=.0;  
00607     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00608     return nll;
00609   }  
00610   df1b2variable  nllMultiNomial(const df1b2vector & obsN, const df1b2vector & p)
00611   {
00612     df1b2variable nll=.0;  
00613     nll= -1.*(obsN*log(p+EPS) + gammln(sum(obsN)+1.) - sum(gammln(obsN+1.))); //full likelihood
00614     return nll;
00615   }
00616 
00617 
00618 
00619 
00621   //
00622   //                                    Dirichlet
00623   //
00625 
00633   double nllDirichlet(const dvector & p, const dvector & shape)
00634   {
00635     double nll=0;  
00636     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00637     return nll;
00638   }
00639   dvariable nllDirichlet(const dvar_vector & p, const dvector & shape)
00640   {
00641     RETURN_ARRAYS_INCREMENT();
00642     dvariable nll=0;  
00643     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00644     RETURN_ARRAYS_DECREMENT();
00645     return nll;
00646   } 
00647   dvariable nllDirichlet(const dvector & p, const dvar_vector & shape)
00648   {
00649     RETURN_ARRAYS_INCREMENT();
00650     dvariable nll=0;  
00651     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00652     RETURN_ARRAYS_DECREMENT();
00653     return nll;
00654   }
00655   dvariable nllDirichlet(const dvar_vector & p, const dvar_vector & shape)
00656   {
00657     RETURN_ARRAYS_INCREMENT();
00658     dvariable nll=0;  
00659     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00660     RETURN_ARRAYS_DECREMENT();
00661     return nll;
00662   }
00663 
00671   df1b2variable nllDirichlet(const df1b2vector & p, const dvector & shape)
00672   {
00673     df1b2variable nll=.0;  
00674     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00675     return nll;
00676   }  
00677   df1b2variable nllDirichlet(const dvector & p, const df1b2vector & shape)
00678   {
00679     df1b2variable nll=.0;  
00680     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00681     return nll;
00682   }
00683   df1b2variable nllDirichlet(const df1b2vector & p, const df1b2vector & shape)
00684   {
00685     df1b2variable nll=.0;  
00686     nll= -1.*(gammln(sum(shape)) - sum(gammln(shape)) + (shape-1.)*log(p+EPS));
00687     return nll;
00688   }  
00689 
00690  
00691 
00692 
00694   //
00695   //                                    Gamma
00696   //
00698 
00708   double nllGamma(const double & x, const double a, const double b)
00709   {
00710     return -1.*log_gamma_density(x,a,b);  //call admb built in function
00711   }
00712   dvariable nllGamma(const dvariable & x, const double a, const double b)
00713   {
00714     RETURN_ARRAYS_INCREMENT();
00715     dvariable nll=0;  
00716     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00717     RETURN_ARRAYS_DECREMENT();
00718     return nll;
00719   } 
00720   dvariable nllGamma(const double x, const dvariable & a, const dvariable & b)
00721   {
00722     RETURN_ARRAYS_INCREMENT();
00723     dvariable nll=0;  
00724     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00725     RETURN_ARRAYS_DECREMENT();
00726     return nll;
00727   } 
00728   dvariable nllGamma(const dvariable & x, const dvariable & a, const double b)
00729   {
00730     RETURN_ARRAYS_INCREMENT();
00731     dvariable nll=0;  
00732     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00733     RETURN_ARRAYS_DECREMENT();
00734     return nll;
00735   }
00736   dvariable nllGamma(const dvariable & x, const double a, const dvariable & b)
00737   {
00738     RETURN_ARRAYS_INCREMENT();
00739     dvariable nll=0;  
00740     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00741     RETURN_ARRAYS_DECREMENT();
00742     return nll;
00743   }
00744   dvariable nllGamma(const dvariable & x, const dvariable & a, const dvariable & b)
00745   {
00746     RETURN_ARRAYS_INCREMENT();
00747     dvariable nll=0;  
00748     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00749     RETURN_ARRAYS_DECREMENT();
00750     return nll;
00751   }
00752 
00762   df1b2variable nllGamma(const df1b2variable & x, const double a, const double b)
00763   {
00764     df1b2variable nll=.0;  
00765     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00766     return nll;
00767   }
00768   df1b2variable nllGamma(const double x, const df1b2variable & a, const df1b2variable & b)
00769   {
00770     df1b2variable nll=.0;  
00771     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00772     return nll;
00773   }
00774   df1b2variable nllGamma(const df1b2variable & x, const df1b2variable & a, const double b)
00775   {
00776     df1b2variable nll=.0;  
00777     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00778     return nll;
00779   }
00780   df1b2variable nllGamma(const df1b2variable & x, const double a, const df1b2variable & b)
00781   {
00782     df1b2variable nll=.0;  
00783     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00784     return nll;
00785   }
00786   df1b2variable nllGamma(const df1b2variable & x, const df1b2variable & a, const df1b2variable & b)
00787   {
00788     df1b2variable nll=.0;  
00789     nll= gammln(a) - a*log(b) -(a-1.)*log(x+EPS) + b*x;  
00790     return nll;
00791   }
00792 
00793 
00794 
00803   double nllGamma(const dvector & x, const double a, const double b)
00804   {
00805     double nll=0;  
00806     double n=double(x.size());
00807     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00808     return nll;
00809   }
00810   dvariable nllGamma(const dvar_vector & x, const double a, const double b)
00811   {
00812     RETURN_ARRAYS_INCREMENT();
00813     dvariable nll=0;  
00814     double n=double(x.size());
00815     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00816     RETURN_ARRAYS_DECREMENT();
00817     return nll;
00818   } 
00819   dvariable nllGamma(const dvector & x, const dvariable & a, const dvariable & b)
00820   {
00821     RETURN_ARRAYS_INCREMENT();
00822     dvariable nll=0;  
00823     double n=double(x.size());
00824     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00825     RETURN_ARRAYS_DECREMENT();
00826     return nll;
00827   }
00828   dvariable nllGamma(const dvar_vector & x, const dvariable & a, const dvariable & b)
00829   {
00830     RETURN_ARRAYS_INCREMENT();
00831     dvariable nll=0;  
00832     double n=double(x.size());
00833     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00834     RETURN_ARRAYS_DECREMENT();
00835     return nll;
00836   }
00837 
00846   df1b2variable nllGamma(const df1b2vector & x, const double a, const double b)
00847   {
00848     df1b2variable nll=.0;  
00849     double n=double(size_count(x));
00850     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00851     return nll;
00852   }  
00853   df1b2variable nllGamma(const dvector & x, const df1b2variable & a, const df1b2variable & b)
00854   {
00855     df1b2variable nll=.0;  
00856     double n=double(size_count(x));
00857     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00858     return nll;
00859   }    
00860   df1b2variable nllGamma(const df1b2vector & x, const df1b2variable & a, const df1b2variable & b)
00861   {
00862     df1b2variable nll=.0;  
00863     double n=double(size_count(x));
00864     nll=n*gammln(a) - n*a*log(b) -(a-1.)*sum(log(x+EPS)) + b*sum(x);  
00865     return nll;
00866   }  
00867 
00868 
00869 
00870 
00872   //
00873   //                                    Beta
00874   //
00876    
00885   double nllBeta(const double x, const double a, const double b)
00886   {
00887     double nll=0;  
00888     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00889     return nll;
00890   }
00891   dvariable nllBeta(const dvariable & x, const double a, const double b)
00892   {
00893     RETURN_ARRAYS_INCREMENT();
00894     dvariable nll=0;  
00895     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00896     RETURN_ARRAYS_DECREMENT();
00897     return nll;
00898   }   
00899   dvariable nllBeta(const double x, const dvariable & a, const dvariable & b)
00900   {
00901     RETURN_ARRAYS_INCREMENT();
00902     dvariable nll=0;  
00903     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00904     RETURN_ARRAYS_DECREMENT();
00905     return nll;
00906   } 
00907   dvariable nllBeta(const dvariable & x, const dvariable & a, const double b)
00908   {
00909     RETURN_ARRAYS_INCREMENT();
00910     dvariable nll=0;  
00911     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00912     RETURN_ARRAYS_DECREMENT();
00913     return nll;
00914   }  
00915   dvariable nllBeta(const dvariable & x, const double a, const dvariable & b)
00916   {
00917     RETURN_ARRAYS_INCREMENT();
00918     dvariable nll=0;  
00919     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00920     RETURN_ARRAYS_DECREMENT();
00921     return nll;
00922   }  
00923   dvariable nllBeta(const dvariable & x, const dvariable & a, const dvariable & b)
00924   {
00925     RETURN_ARRAYS_INCREMENT();
00926     dvariable nll=0;  
00927     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00928     RETURN_ARRAYS_DECREMENT();
00929     return nll;
00930   }  
00931 
00940   df1b2variable nllBeta(const df1b2variable & x, const double a, const double b)
00941   {
00942     df1b2variable nll=.0;  
00943     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00944     return nll;
00945   }   
00946   df1b2variable nllBeta(const df1b2variable & x, const df1b2variable & a, const double b)
00947   {
00948     df1b2variable nll=.0;  
00949     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00950     return nll;
00951   } 
00952   df1b2variable nllBeta(const df1b2variable & x, const double a, const df1b2variable & b)
00953   {
00954     df1b2variable nll=.0;  
00955     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00956     return nll;
00957   } 
00958   df1b2variable nllBeta(const double x, const df1b2variable & a, const df1b2variable & b)
00959   {
00960     df1b2variable nll=.0;  
00961     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00962     return nll;
00963   } 
00964   df1b2variable nllBeta(const df1b2variable & x, const df1b2variable & a, const df1b2variable & b)
00965   {
00966     df1b2variable nll=.0;  
00967     nll=gammln(a)+gammln(b)-gammln(a+b)-(a-1.)*log(x+EPS)-(b-1.)*log(1.-x+EPS);  
00968     return nll;
00969   } 
00970 
00971 
00972 
00981   dvariable nllBeta(const dvar_vector & x, const double a, const double b)
00982   {
00983     RETURN_ARRAYS_INCREMENT();
00984     dvariable nll=0;  
00985     double n=double(x.size());
00986     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
00987     RETURN_ARRAYS_DECREMENT();
00988     return nll;
00989   }
00990   dvariable nllBeta(const dvector & x, const dvariable & a, const dvariable & b)
00991   {
00992     RETURN_ARRAYS_INCREMENT();
00993     dvariable nll=0;  
00994     double n=double(x.size());
00995     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
00996     RETURN_ARRAYS_DECREMENT();
00997     return nll;
00998   }
00999   dvariable nllBeta(const dvar_vector & x, const dvariable & a, const dvariable & b)
01000   {
01001     RETURN_ARRAYS_INCREMENT();
01002     dvariable nll=0;  
01003     double n=double(x.size());
01004     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01005     RETURN_ARRAYS_DECREMENT();
01006     return nll;
01007   }
01008   
01017   df1b2variable nllBeta(const df1b2vector & x, const double a, const double b)
01018   {
01019     df1b2variable nll=.0;  
01020     double n=double(size_count(x));
01021     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01022     return nll;
01023   }  
01024   df1b2variable nllBeta(const dvector & x, const df1b2variable & a, const df1b2variable & b)
01025   {
01026     df1b2variable nll=.0;  
01027     double n=double(size_count(x));
01028     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01029     return nll;
01030   }   
01031   df1b2variable nllBeta(const df1b2vector & x, const df1b2variable & a, const df1b2variable & b)
01032   {
01033     df1b2variable nll=.0;  
01034     double n=double(size_count(x));
01035     nll=n*gammln(a)+n*gammln(b)-n*gammln(a+b)-(a-1.)*sum(log(x+EPS))-(b-1.)*sum(log(1.-x+EPS));  
01036     return nll;
01037   } 
01038 
01039 
01040 
01041 
01043   //
01044   //                                    Normal
01045   //
01047 
01056   double nllNormal(const double x, const double mu, const double sigma)
01057   {
01058     double nll=0;  
01059     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01060     return nll;
01061   }
01062   dvariable nllNormal(const dvariable & x, const double mu, const double sigma)
01063   {
01064     RETURN_ARRAYS_INCREMENT();
01065     dvariable nll=0;  
01066     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01067     RETURN_ARRAYS_DECREMENT();
01068     return nll;
01069   }
01070   dvariable nllNormal(const double x, const dvariable & mu, const dvariable & sigma)
01071   {
01072     RETURN_ARRAYS_INCREMENT();
01073     dvariable nll=0;  
01074     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01075     RETURN_ARRAYS_DECREMENT();
01076     return nll;
01077   } 
01078   dvariable nllNormal(const dvariable & x, const dvariable & mu, const double sigma)
01079   {
01080     RETURN_ARRAYS_INCREMENT();
01081     dvariable nll=0;  
01082     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01083     RETURN_ARRAYS_DECREMENT();
01084     return nll;
01085   } 
01086   dvariable nllNormal(const dvariable & x, const double mu, const dvariable & sigma)
01087   {
01088     RETURN_ARRAYS_INCREMENT();
01089     dvariable nll=0;  
01090     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01091     RETURN_ARRAYS_DECREMENT();
01092     return nll;
01093   }
01094   dvariable nllNormal(const dvariable & x, const dvariable & mu, const dvariable & sigma)
01095   {
01096     RETURN_ARRAYS_INCREMENT();
01097     dvariable nll=0;  
01098     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01099     RETURN_ARRAYS_DECREMENT();
01100     return nll;
01101   }
01102 
01111   df1b2variable nllNormal(const df1b2variable & x, const double mu, const double sigma)
01112   {
01113     df1b2variable nll=.0;  
01114     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01115     return nll;
01116   }
01117   df1b2variable nllNormal(const df1b2variable & x, const df1b2variable & mu, const double sigma)
01118   {
01119     df1b2variable nll=.0;  
01120     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01121     return nll;
01122   }
01123   df1b2variable nllNormal(const df1b2variable & x, const double mu, const df1b2variable & sigma)
01124   {
01125     df1b2variable nll=.0;  
01126     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01127     return nll;
01128   }
01129   df1b2variable nllNormal(const double x, const df1b2variable & mu, const df1b2variable & sigma)
01130   {
01131     df1b2variable nll=.0;  
01132     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01133     return nll;
01134   }
01135   df1b2variable nllNormal(const df1b2variable & x, const df1b2variable & mu, const df1b2variable & sigma)
01136   {
01137     df1b2variable nll=.0;  
01138     nll=log(sigma*sqrt(2.*M_PI)+EPS) +0.5*square((x-mu)/(sigma+EPS));  
01139     return nll;
01140   }
01141 
01142 
01143 
01152   double nllNormal(const dvector & x, const double mu, const double sigma)
01153   {
01154     double nll=0;  
01155     double n=double(x.size());
01156     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01157     return nll;
01158   }
01159   dvariable nllNormal(const dvar_vector& x, const double mu, const double sigma)
01160   {
01161     RETURN_ARRAYS_INCREMENT();
01162     dvariable nll=0;  
01163     double n=double(x.size());
01164     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01165     RETURN_ARRAYS_DECREMENT();
01166     return nll;
01167   }
01168   dvariable nllNormal(const dvector & x, const dvariable & mu, const dvariable & sigma)
01169   {
01170     RETURN_ARRAYS_INCREMENT();
01171     dvariable nll=0;  
01172     double n=double(x.size());
01173     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01174     RETURN_ARRAYS_DECREMENT();
01175     return nll;
01176   }
01177   dvariable nllNormal(const dvar_vector & x, const dvariable & mu, const dvariable & sigma)
01178   {
01179     RETURN_ARRAYS_INCREMENT();
01180     dvariable nll=0;  
01181     double n=double(x.size());
01182     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01183     RETURN_ARRAYS_DECREMENT();
01184     return nll;
01185   }
01186  
01195   df1b2variable nllNormal(const df1b2vector & x, const double mu, const double sigma)
01196   {
01197     df1b2variable nll=.0;  
01198     double n=double(size_count(x));
01199     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01200     return nll;
01201   }
01202   df1b2variable nllNormal(const dvector & x, const df1b2variable & mu, const df1b2variable & sigma)
01203   {
01204     df1b2variable nll=.0;  
01205     double n=double(size_count(x));
01206     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01207     return nll;
01208   }
01209   df1b2variable nllNormal(const df1b2vector & x, const df1b2variable & mu, const df1b2variable & sigma)
01210   {
01211     df1b2variable nll=.0;  
01212     double n=double(size_count(x));
01213     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01214     return nll;
01215   }
01216  
01217 
01218 
01227   dvariable nllNormal(const dvar_vector & x, const dvector & mu, const double sigma)
01228   {
01229     RETURN_ARRAYS_INCREMENT();
01230     dvariable nll=0;  
01231     double n=double(x.size());
01232     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01233     RETURN_ARRAYS_DECREMENT();
01234     return nll;
01235   }   
01236   dvariable nllNormal(const dvector & x, const dvar_vector & mu, const dvariable & sigma)
01237   {
01238     RETURN_ARRAYS_INCREMENT();
01239     dvariable nll=0;  
01240     double n=double(x.size());
01241     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01242     RETURN_ARRAYS_DECREMENT();
01243     return nll;
01244   }
01245   dvariable nllNormal(const dvar_vector & x, const dvar_vector & mu, const dvariable & sigma)
01246   {
01247     RETURN_ARRAYS_INCREMENT();
01248     dvariable nll=0;  
01249     double n=double(x.size());
01250     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01251     RETURN_ARRAYS_DECREMENT();
01252     return nll;
01253   }
01254 
01263   df1b2variable nllNormal(const df1b2vector & x, const dvector & mu, const double sigma)
01264   {
01265     df1b2variable nll=.0;  
01266     double n=double(size_count(x));
01267     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01268     return nll;
01269   }   
01270   df1b2variable nllNormal(const dvector & x, const df1b2vector & mu, const df1b2variable & sigma)
01271   {
01272     df1b2variable nll=.0;  
01273     double n=double(size_count(x));
01274     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01275     return nll;
01276   } 
01277   df1b2variable nllNormal(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & sigma)
01278   {
01279     df1b2variable nll=.0;  
01280     double n=double(size_count(x));
01281     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +0.5*norm2((x-mu)/(sigma+EPS));  
01282     return nll;
01283   } 
01284 
01285 
01286 
01287 
01288 
01289 
01291   //
01292   //                                    Normal with tau
01293   //
01295 
01304   dvariable nllNormal2(const dvariable & x, const double mu, const double tau)
01305   {
01306     RETURN_ARRAYS_INCREMENT();
01307     dvariable nll=0;  
01308     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01309     RETURN_ARRAYS_DECREMENT();
01310     return nll;
01311   }
01312   dvariable nllNormal2(const double x, const dvariable & mu, const dvariable & tau)
01313   {
01314     RETURN_ARRAYS_INCREMENT();
01315     dvariable nll=0;  
01316     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01317     RETURN_ARRAYS_DECREMENT();
01318     return nll;
01319   }
01320   dvariable nllNormal2(const dvariable & x, const dvariable & mu, const dvariable & tau)
01321   {
01322     RETURN_ARRAYS_INCREMENT();
01323     dvariable nll=0;  
01324     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01325     RETURN_ARRAYS_DECREMENT();
01326     return nll;
01327   }
01328 
01337   df1b2variable nllNormal2(const df1b2variable & x, const double mu, const double tau)
01338   {
01339     df1b2variable nll=.0;  
01340     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01341     return nll;
01342   }
01343   df1b2variable nllNormal2(const double x, const df1b2variable & mu, const df1b2variable & tau)
01344   {
01345     df1b2variable nll=.0;  
01346     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01347     return nll;
01348   }
01349   df1b2variable nllNormal2(const df1b2variable & x, const df1b2variable & mu, const df1b2variable & tau)
01350   {
01351     df1b2variable nll=.0;  
01352     nll = -0.5*log(tau+EPS)+0.5*tau*square(x-mu)+0.5*log(2.*M_PI);//full likelihood, can drop last term
01353     return nll;
01354   }
01355 
01356 
01357 
01358 
01367   dvariable nllNormal2(const dvar_vector & x, const double mu, const double tau)
01368   {
01369     RETURN_ARRAYS_INCREMENT();
01370     dvariable nll=0;  
01371     double n=double(x.size());
01372     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
01373     RETURN_ARRAYS_DECREMENT();
01374     return nll;
01375   }
01376   dvariable nllNormal2(const dvector & x, const dvariable & mu, const dvariable & tau)
01377   {
01378     RETURN_ARRAYS_INCREMENT();
01379     dvariable nll=0;  
01380     double n=double(x.size());
01381     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
01382     RETURN_ARRAYS_DECREMENT();
01383     return nll;
01384   }
01385   dvariable nllNormal2(const dvar_vector & x, const dvariable & mu, const dvariable & tau)
01386   {
01387     RETURN_ARRAYS_INCREMENT();
01388     dvariable nll=0;  
01389     double n=double(x.size());
01390     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
01391     RETURN_ARRAYS_DECREMENT();
01392     return nll;
01393   }
01394 
01403   df1b2variable nllNormal2(const df1b2vector & x, const double  mu, const double tau)
01404   {
01405     df1b2variable nll=.0;  
01406     double n=double(size_count(x));
01407     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
01408     return nll;
01409   }
01410   df1b2variable nllNormal2(const dvector & x, const df1b2variable &  mu, const df1b2variable & tau)
01411   {
01412     df1b2variable nll=.0;  
01413     double n=double(size_count(x));
01414     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
01415     return nll;
01416   }
01417   df1b2variable nllNormal2(const df1b2vector & x, const df1b2variable &  mu, const df1b2variable & tau)
01418   {
01419     df1b2variable nll=.0;  
01420     double n=double(size_count(x));
01421     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
01422     return nll;
01423   }
01424 
01425 
01426 
01435   dvariable nllNormal2(const dvar_vector & x, const dvector & mu, const double tau)
01436   {
01437     RETURN_ARRAYS_INCREMENT();
01438     dvariable nll=0;  
01439     double n=double(x.size());
01440     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01441     RETURN_ARRAYS_DECREMENT();
01442     return nll;
01443   } 
01444   dvariable nllNormal2(const dvector & x, const dvar_vector & mu, const dvariable & tau)
01445   {
01446     RETURN_ARRAYS_INCREMENT();
01447     dvariable nll=0;  
01448     double n=double(x.size());
01449     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01450     RETURN_ARRAYS_DECREMENT();
01451     return nll;
01452   } 
01453   dvariable nllNormal2(const dvar_vector & x, const dvar_vector & mu, const dvariable & tau)
01454   {
01455     RETURN_ARRAYS_INCREMENT();
01456     dvariable nll=0;  
01457     double n=double(x.size());
01458     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01459     RETURN_ARRAYS_DECREMENT();
01460     return nll;
01461   } 
01462 
01471   df1b2variable nllNormal2(const df1b2vector & x, const dvector & mu, const double tau)
01472   {
01473     df1b2variable nll=.0;  
01474     double n=double(size_count(x));
01475     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01476     return nll;
01477   } 
01478   df1b2variable nllNormal2(const dvector & x, const df1b2vector & mu, const df1b2variable & tau)
01479   {
01480     df1b2variable nll=.0;  
01481     double n=double(size_count(x));
01482     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01483     return nll;
01484   } 
01485   df1b2variable nllNormal2(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & tau)
01486   {
01487     df1b2variable nll=.0;  
01488     double n=double(size_count(x));
01489     nll = -0.5*n*log(tau+EPS)+0.5*tau*norm2(x-mu)+0.5*n*log(2.*M_PI);
01490     return nll;
01491   } 
01492 
01493 
01494 
01495 
01496 
01497 
01498 
01500   //
01501   //                                    Binomial
01502   //
01504 
01513   double nllBinomial(const double x, const double n,const double p)
01514   {   
01515     double nll=0;
01516     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01517     return nll;
01518   }
01519   dvariable nllBinomial(const dvariable & x, const double n,const double p)
01520   {   
01521     RETURN_ARRAYS_INCREMENT();
01522     dvariable nll=0;
01523     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01524     RETURN_ARRAYS_DECREMENT();
01525     return nll;
01526   }
01527   dvariable nllBinomial(const double x, const dvariable & n,const dvariable & p)
01528   {   
01529     RETURN_ARRAYS_INCREMENT();
01530     dvariable nll=0;
01531     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01532     RETURN_ARRAYS_DECREMENT();
01533     return nll;
01534   }
01535   dvariable nllBinomial(const dvariable & x, const dvariable & n,const dvariable & p)
01536   {   
01537     RETURN_ARRAYS_INCREMENT();
01538     dvariable nll=0;
01539     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01540     RETURN_ARRAYS_DECREMENT();
01541     return nll;
01542   }
01543 
01552   df1b2variable nllBinomial(const df1b2variable & x, const double n,const double p)
01553   {   
01554     df1b2variable nll=.0;
01555     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01556     return nll;
01557   }
01558   df1b2variable nllBinomial(const double x, const df1b2variable & n,const df1b2variable & p)
01559   {   
01560     df1b2variable nll=.0;
01561     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01562     return nll;
01563   }
01564   df1b2variable nllBinomial(const df1b2variable & x, const df1b2variable & n,const df1b2variable & p)
01565   {   
01566     df1b2variable nll=.0;
01567     nll=-x*log(p+EPS)-(n-x)*log(1.-p+EPS)-log_comb(n,x); //full likelihood, can drop the last term
01568     return nll;
01569   }
01570 
01571 
01572 
01573 
01582   dvariable nllBinomial(const dvar_vector & x, const dvector & n,const double p)
01583   {   
01584     RETURN_ARRAYS_INCREMENT();
01585     dvariable nll=0;
01586     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
01587     RETURN_ARRAYS_DECREMENT();
01588     return nll;
01589   }
01590   dvariable nllBinomial(const dvector & x, const dvar_vector & n,const dvariable & p)
01591   {   
01592     RETURN_ARRAYS_INCREMENT();
01593     dvariable nll=0;
01594     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
01595     RETURN_ARRAYS_DECREMENT();
01596     return nll;
01597   }
01598   dvariable nllBinomial(const dvar_vector & x, const dvar_vector & n,const dvariable & p)
01599   {   
01600     RETURN_ARRAYS_INCREMENT();
01601     dvariable nll=0;
01602     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
01603     RETURN_ARRAYS_DECREMENT();
01604     return nll;
01605   }
01606 
01615   df1b2variable nllBinomial(const dvector & x, const df1b2vector & n,const df1b2variable & p)
01616   {   
01617     df1b2variable nll=.0;
01618     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
01619     return nll;
01620   }
01621   df1b2variable nllBinomial(const df1b2vector & x, const dvector & n,const double p)
01622   {   
01623     df1b2variable nll=.0;
01624     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
01625     return nll;
01626   }
01627   df1b2variable nllBinomial(const df1b2vector & x, const df1b2vector & n,const df1b2variable & p)
01628   {   
01629     df1b2variable nll=.0;
01630     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
01631     return nll;
01632   }
01633 
01634 
01635 
01636 
01637 
01638 
01639 
01640 
01642   //
01643   //                                    Lognormal
01644   //
01646 
01655   double nllLognormal(const double x, const double mu, const double sigma)
01656   { 
01657     double nll=0;     
01658     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01659     return nll;
01660   }
01661   dvariable nllLognormal(const dvariable& x, const double mu, const double sigma)
01662   { 
01663     RETURN_ARRAYS_INCREMENT();
01664     dvariable nll=0;    
01665     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01666     RETURN_ARRAYS_DECREMENT();
01667     return nll;
01668   }
01669   dvariable nllLognormal(const double x, const dvariable & mu, const dvariable & sigma)
01670   { 
01671     RETURN_ARRAYS_INCREMENT();
01672     dvariable nll=0;    
01673     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01674     RETURN_ARRAYS_DECREMENT();
01675     return nll;
01676   }
01677   dvariable nllLognormal(const dvariable & x, const dvariable & mu, const double sigma)
01678   { 
01679     RETURN_ARRAYS_INCREMENT();
01680     dvariable nll=0;    
01681     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01682     RETURN_ARRAYS_DECREMENT();
01683     return nll;
01684   }
01685   dvariable nllLognormal(const dvariable & x, const double mu, const dvariable & sigma)
01686   { 
01687     RETURN_ARRAYS_INCREMENT();
01688     dvariable nll=0;    
01689     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01690     RETURN_ARRAYS_DECREMENT();
01691     return nll;
01692   }
01693   dvariable nllLognormal(const dvariable & x, const dvariable & mu, const dvariable & sigma)
01694   { 
01695     RETURN_ARRAYS_INCREMENT();
01696     dvariable nll=0;    
01697     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01698     RETURN_ARRAYS_DECREMENT();
01699     return nll;
01700   }
01701 
01710   df1b2variable nllLognormal(const df1b2variable & x, const double mu, const double sigma)
01711   { 
01712     df1b2variable nll=.0;     
01713     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01714     return nll;
01715   }
01716   df1b2variable nllLognormal(const double x, const df1b2variable & mu, const df1b2variable & sigma)
01717   { 
01718     df1b2variable nll=.0;     
01719     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01720     return nll;
01721   }
01722   df1b2variable nllLognormal(const df1b2variable & x, const df1b2variable & mu, const double sigma)
01723   { 
01724     df1b2variable nll=.0;     
01725     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01726     return nll;
01727   }
01728   df1b2variable nllLognormal(const df1b2variable & x, const double mu, const df1b2variable & sigma)
01729   { 
01730     df1b2variable nll=.0;     
01731     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01732     return nll;
01733   }
01734   df1b2variable nllLognormal(const df1b2variable & x, const df1b2variable & mu, const df1b2variable & sigma)
01735   { 
01736     df1b2variable nll=.0;     
01737     nll=log(sigma*sqrt(2.*M_PI)+EPS)+log(x+EPS)+0.5*square((log(x+EPS)-mu)/(sigma+EPS));//full likelihood
01738     return nll;
01739   }
01740 
01741 
01742 
01743 
01752   dvariable nllLognormal(const dvar_vector & x, const double mu, const double sigma)
01753   { 
01754     RETURN_ARRAYS_INCREMENT();
01755     dvariable nll=0;  
01756     double n=double(x.size());
01757     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01758     RETURN_ARRAYS_DECREMENT();
01759     return nll;
01760   }  
01761   dvariable nllLognormal(const dvector & x, const dvariable & mu, const dvariable & sigma)
01762   { 
01763     RETURN_ARRAYS_INCREMENT();
01764     dvariable nll=0;  
01765     double n=double(x.size());
01766     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01767     RETURN_ARRAYS_DECREMENT();
01768     return nll;
01769   }  
01770   dvariable nllLognormal(const dvar_vector & x, const dvariable & mu, const dvariable & sigma)
01771   { 
01772     RETURN_ARRAYS_INCREMENT();
01773     dvariable nll=0;  
01774     double n=double(x.size());
01775     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01776     RETURN_ARRAYS_DECREMENT();
01777     return nll;
01778   }  
01779 
01788   df1b2variable nllLognormal(const df1b2vector & x, const double mu, const double sigma)
01789   { 
01790     df1b2variable nll=.0;   
01791     double n=double(size_count(x));
01792     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01793     return nll;
01794   }  
01795   df1b2variable nllLognormal(const dvector & x, const df1b2variable & mu, const df1b2variable & sigma)
01796   { 
01797     df1b2variable nll=.0;   
01798     double n=double(size_count(x));
01799     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01800     return nll;
01801   }  
01802   df1b2variable nllLognormal(const df1b2vector & x, const df1b2variable & mu, const df1b2variable & sigma)
01803   { 
01804     df1b2variable nll=.0;   
01805     double n=double(size_count(x));
01806     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01807     return nll;
01808   }  
01809 
01810 
01811 
01820   dvariable nllLognormal(const dvar_vector & x, const dvector & mu, const double sigma)
01821   { 
01822     RETURN_ARRAYS_INCREMENT();
01823     dvariable nll=0;  
01824     double n=double(x.size());
01825     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01826     RETURN_ARRAYS_DECREMENT();
01827     return nll;
01828   }  
01829   dvariable nllLognormal(const dvector & x, const dvar_vector & mu, const dvariable & sigma)
01830   { 
01831     RETURN_ARRAYS_INCREMENT();
01832     dvariable nll=0;  
01833     double n=double(x.size());
01834     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01835     RETURN_ARRAYS_DECREMENT();
01836     return nll;
01837   }  
01838   dvariable nllLognormal(const dvar_vector & x, const dvar_vector & mu, const dvariable & sigma)
01839   { 
01840     RETURN_ARRAYS_INCREMENT();
01841     dvariable nll=0;  
01842     double n=double(x.size());
01843     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01844     RETURN_ARRAYS_DECREMENT();
01845     return nll;
01846   }  
01847 
01856   df1b2variable nllLognormal(const df1b2vector & x, const dvector & mu, const double sigma)
01857   { 
01858     df1b2variable nll=.0;   
01859     double n=double(size_count(x));
01860     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01861     return nll;
01862   }  
01863   df1b2variable nllLognormal(const dvector & x, const df1b2vector & mu, const df1b2variable & sigma)
01864   { 
01865     df1b2variable nll=.0;   
01866     double n=double(size_count(x));
01867     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01868     return nll;
01869   } 
01870   df1b2variable nllLognormal(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & sigma)
01871   { 
01872     df1b2variable nll=.0;   
01873     double n=double(size_count(x));
01874     nll=n*log(sigma*sqrt(2.*M_PI)+EPS) +sum(log(x+EPS))+0.5*norm2((log(x+EPS)-mu)/(sigma+EPS));
01875     return nll;
01876   } 
01877 
01878 
01879 
01880 
01881 
01882 
01884   //
01885   //                                    Lognormal with tau
01886   //
01888 
01897   dvariable nllLognormal2(const double x, const dvariable & mu, const dvariable & tau)
01898   { 
01899     RETURN_ARRAYS_INCREMENT();
01900     dvariable nll=0;  
01901     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
01902     RETURN_ARRAYS_DECREMENT();
01903     return nll;
01904   }
01905   dvariable nllLognormal2(const dvariable & x, const double mu, const double tau)
01906   { 
01907     RETURN_ARRAYS_INCREMENT();
01908     dvariable nll=0;  
01909     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
01910     RETURN_ARRAYS_DECREMENT();
01911     return nll;
01912   }
01913   dvariable nllLognormal2(const dvariable & x, const dvariable & mu, const dvariable & tau)
01914   { 
01915     RETURN_ARRAYS_INCREMENT();
01916     dvariable nll=0;  
01917     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
01918     RETURN_ARRAYS_DECREMENT();
01919     return nll;
01920   }
01921 
01930   df1b2variable nllLognormal2(const df1b2variable x, const double mu, const double tau)
01931   { 
01932     df1b2variable nll=.0;   
01933     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
01934     return nll;
01935   }
01936   df1b2variable nllLognormal2(const double x, const df1b2variable & mu, const df1b2variable & tau)
01937   { 
01938     df1b2variable nll=.0;   
01939     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
01940     return nll;
01941   }
01942   df1b2variable nllLognormal2(const df1b2variable x, const df1b2variable & mu, const double tau)
01943   { 
01944     df1b2variable nll=.0;   
01945     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
01946     return nll;
01947   }
01948   df1b2variable nllLognormal2(const df1b2variable x, const double mu, const df1b2variable & tau)
01949   { 
01950     df1b2variable nll=.0;   
01951     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
01952     return nll;
01953   }
01954   df1b2variable nllLognormal2(const df1b2variable x, const df1b2variable & mu, const df1b2variable & tau)
01955   { 
01956     df1b2variable nll=.0;   
01957     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
01958     return nll;
01959   }
01960 
01961 
01962 
01963 
01972   dvariable nllLognormal2(const dvar_vector & x, const double mu, const double tau)
01973   { 
01974     RETURN_ARRAYS_INCREMENT();
01975     dvariable nll=0;
01976     double n=double(x.size());
01977     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);
01978     RETURN_ARRAYS_DECREMENT();
01979     return nll;
01980   }
01981   dvariable nllLognormal2(const dvector & x, const dvariable & mu, const dvariable & tau)
01982   { 
01983     RETURN_ARRAYS_INCREMENT();
01984     dvariable nll=0;
01985     double n=double(x.size());
01986     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);
01987     RETURN_ARRAYS_DECREMENT();
01988     return nll;
01989   }
01990   dvariable nllLognormal2(const dvar_vector & x, const dvariable & mu, const dvariable & tau)
01991   { 
01992     RETURN_ARRAYS_INCREMENT();
01993     dvariable nll=0;
01994     double n=double(x.size());
01995     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);
01996     RETURN_ARRAYS_DECREMENT();
01997     return nll;
01998   }
01999 
02008   df1b2variable nllLognormal2(const dvector & x, const df1b2variable & mu, const df1b2variable & tau)
02009   { 
02010     df1b2variable nll=.0;
02011     double n=double(size_count(x)); 
02012     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);
02013     return nll;
02014   }
02015   df1b2variable nllLognormal2(const df1b2vector & x, const double mu, const double tau)
02016   { 
02017     df1b2variable nll=.0;
02018     double n=double(size_count(x)); 
02019     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);
02020     return nll;
02021   }
02022   df1b2variable nllLognormal2(const df1b2vector & x, const df1b2variable & mu, const double tau)
02023   { 
02024     df1b2variable nll=.0;
02025     double n=double(size_count(x)); 
02026     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);
02027     return nll;
02028   }
02029   df1b2variable nllLognormal2(const df1b2vector & x, const double mu, const df1b2variable & tau)
02030   { 
02031     df1b2variable nll=.0;
02032     double n=double(size_count(x)); 
02033     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);
02034     return nll;
02035   }
02036   df1b2variable nllLognormal2(const df1b2vector & x, const df1b2variable & mu, const df1b2variable & tau)
02037   { 
02038     df1b2variable nll=.0;
02039     double n=double(size_count(x)); 
02040     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);
02041     return nll;
02042   }
02043 
02044 
02045 
02046 
02055   dvariable nllLognormal2(const dvar_vector & x, const dvector & mu, const double tau)
02056   { 
02057     RETURN_ARRAYS_INCREMENT();
02058     dvariable nll=0;  
02059     double n=double(x.size());
02060     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);
02061     RETURN_ARRAYS_DECREMENT();
02062     return nll;
02063   }  
02064   dvariable nllLognormal2(const dvector & x, const dvar_vector & mu, const dvariable & tau)
02065   { 
02066     RETURN_ARRAYS_INCREMENT();
02067     dvariable nll=0;  
02068     double n=double(x.size());
02069     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);
02070     RETURN_ARRAYS_DECREMENT();
02071     return nll;
02072   }  
02073   dvariable nllLognormal2(const dvar_vector & x, const dvar_vector & mu, const dvariable & tau)
02074   { 
02075     RETURN_ARRAYS_INCREMENT();
02076     dvariable nll=0;  
02077     double n=double(x.size());
02078     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);
02079     RETURN_ARRAYS_DECREMENT();
02080     return nll;
02081   }  
02082 
02091   df1b2variable nllLognormal2(const df1b2vector & x, const dvector & mu, const double tau)
02092   { 
02093     df1b2variable nll=.0;   
02094     double n=double(size_count(x));
02095     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);
02096     return nll;
02097   }  
02098   df1b2variable nllLognormal2(const dvector & x, const df1b2vector & mu, const df1b2variable & tau)
02099   { 
02100     df1b2variable nll=.0;   
02101     double n=double(size_count(x));
02102     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);
02103     return nll;
02104   } 
02105   df1b2variable nllLognormal2(const df1b2vector & x, const df1b2vector & mu, const df1b2variable & tau)
02106   { 
02107     df1b2variable nll=.0;   
02108     double n=double(size_count(x));
02109     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);
02110     return nll;
02111   } 
02112 
02113 
02114 
02115 
02116 
02117 
02118 
02120   //
02121   //                                    Poisson
02122   //
02124 
02132   double nllPoisson(const double x, const double lambda)
02133   {
02134     double nll=0;
02135     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02136     return nll;
02137   }
02138   dvariable nllPoisson(const dvariable & x, const double lambda)
02139   {
02140     RETURN_ARRAYS_INCREMENT();
02141     dvariable nll=0;
02142     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02143     RETURN_ARRAYS_DECREMENT();
02144     return nll;
02145   }
02146   dvariable nllPoisson(const double x, const dvariable & lambda)
02147   {
02148     RETURN_ARRAYS_INCREMENT();
02149     dvariable nll=0;
02150     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02151     RETURN_ARRAYS_DECREMENT();
02152     return nll;
02153   }
02154   dvariable nllPoisson(const dvariable & x, const dvariable & lambda)
02155   {
02156     RETURN_ARRAYS_INCREMENT();
02157     dvariable nll=0;
02158     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02159     RETURN_ARRAYS_DECREMENT();
02160     return nll;
02161   }
02162 
02170   df1b2variable nllPoisson(const df1b2variable & x, const double lambda)
02171   {
02172     df1b2variable nll=.0;
02173     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02174     return nll;
02175   }
02176   df1b2variable nllPoisson(const double x, const df1b2variable & lambda)
02177   {
02178     df1b2variable nll=.0;
02179     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02180     return nll;
02181   }
02182   df1b2variable nllPoisson(const df1b2variable & x, const df1b2variable & lambda)
02183   {
02184     df1b2variable nll=.0;
02185     nll = lambda- x*log(lambda+EPS)+gammln(x+1.0);//full likelihood
02186     return nll;
02187   }
02188 
02189 
02190 
02198   dvariable nllPoisson(const dvar_vector & x, const double lambda)
02199   {
02200     RETURN_ARRAYS_INCREMENT();
02201     dvariable nll=0;
02202     double n=double(x.size());
02203     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02204     RETURN_ARRAYS_DECREMENT();
02205     return nll;
02206   }  
02207   dvariable nllPoisson(const dvector & x, const dvariable & lambda)
02208   {
02209     RETURN_ARRAYS_INCREMENT();
02210     dvariable nll=0;
02211     double n=double(x.size());
02212     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02213     RETURN_ARRAYS_DECREMENT();
02214     return nll;
02215   }  
02216   dvariable nllPoisson(const dvar_vector & x, const dvariable & lambda)
02217   {
02218     RETURN_ARRAYS_INCREMENT();
02219     dvariable nll=0;
02220     double n=double(x.size());
02221     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02222     RETURN_ARRAYS_DECREMENT();
02223     return nll;
02224   }  
02225 
02233   df1b2variable nllPoisson(const dvector & x, const df1b2variable & lambda)
02234   {
02235     df1b2variable nll=.0;
02236     double n=double(size_count(x)); 
02237     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02238     return nll;
02239   }  
02240   df1b2variable nllPoisson(const df1b2vector & x, const double lambda)
02241   {
02242     df1b2variable nll=.0;
02243     double n=double(size_count(x)); 
02244     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02245     return nll;
02246   }  
02247   df1b2variable nllPoisson(const df1b2vector & x, const df1b2variable & lambda)
02248   {
02249     df1b2variable nll=.0;
02250     double n=double(size_count(x)); 
02251     nll = n*lambda- sum(x)*log(lambda+EPS)+sum(gammln(x+1.0));//full likelihood, can drop the last term
02252     return nll;
02253   }  
02254 
02255 
02256 
02257 
02258 
02259 
02261   //
02262   //                                    Inverse Gamma
02263   //
02265 
02274   double nllInverseGamma(const double x, const double a, const double b)
02275   {
02276     double nll=0;  
02277     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02278     return nll;
02279   }
02280   dvariable nllInverseGamma(const dvariable & x, const double a, const double b)
02281   {
02282     RETURN_ARRAYS_INCREMENT();
02283     dvariable nll=0;  
02284     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02285     RETURN_ARRAYS_DECREMENT();
02286     return nll;
02287   }
02288   dvariable nllInverseGamma(const double x, const dvariable & a, const dvariable & b)
02289   {
02290     RETURN_ARRAYS_INCREMENT();
02291     dvariable nll=0;  
02292     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02293     RETURN_ARRAYS_DECREMENT();
02294     return nll;
02295   }
02296   dvariable nllInverseGamma(const dvariable & x, const dvariable & a, const dvariable & b)
02297   {
02298     RETURN_ARRAYS_INCREMENT();
02299     dvariable nll=0;  
02300     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02301     RETURN_ARRAYS_DECREMENT();
02302     return nll;
02303   }
02304 
02313   df1b2variable nllInverseGamma(const df1b2variable & x, const double a, const double b)
02314   {
02315     df1b2variable nll=.0;  
02316     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02317     return nll;
02318   }
02319   df1b2variable nllInverseGamma(const double x, const df1b2variable & a, const df1b2variable & b)
02320   {
02321     df1b2variable nll=.0;  
02322     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02323     return nll;
02324   }
02325   df1b2variable nllInverseGamma(const df1b2variable & x, const df1b2variable & a, const double b)
02326   {
02327     df1b2variable nll=.0;  
02328     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02329     return nll;
02330   }
02331   df1b2variable nllInverseGamma(const df1b2variable & x, const double a, const df1b2variable & b)
02332   {
02333     df1b2variable nll=.0;  
02334     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02335     return nll;
02336   }
02337   df1b2variable nllInverseGamma(const df1b2variable & x, const df1b2variable & a, const df1b2variable & b)
02338   {
02339     df1b2variable nll=.0;  
02340     nll= - a*log(b)+gammln(a)  +(a+1.)*log(x+EPS) + b/(x+EPS);
02341     return nll;
02342   }
02343 
02344 
02345 
02346 
02355   dvariable nllInverseGamma(const dvar_vector & x, const double a, const double b)
02356   {
02357     RETURN_ARRAYS_INCREMENT();
02358     dvariable nll=0;  
02359     double n=double(x.size());
02360     nll= - n*a*log(b)+ n*gammln(a)  +(a+1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02361     RETURN_ARRAYS_DECREMENT();
02362     return nll;
02363   }
02364   dvariable nllInverseGamma(const dvector & x, const dvariable & a, const dvariable & b)
02365   {
02366     RETURN_ARRAYS_INCREMENT();
02367     dvariable nll=0;  
02368     double n=double(x.size());
02369     nll= - n*a*log(b)+ n*gammln(a)  +(a+1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02370     RETURN_ARRAYS_DECREMENT();
02371     return nll;
02372   }
02373   dvariable nllInverseGamma(const dvar_vector & x, const dvariable & a, const dvariable & b)
02374   {
02375     RETURN_ARRAYS_INCREMENT();
02376     dvariable nll=0;  
02377     double n=double(x.size());
02378     nll= - n*a*log(b)+ n*gammln(a)  +(a+1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02379     RETURN_ARRAYS_DECREMENT();
02380     return nll;
02381   }
02382 
02391   df1b2variable nllInverseGamma(const dvector & x, const df1b2variable & a, const df1b2variable & b)
02392   {
02393     df1b2variable nll=.0;  
02394     double n=double(size_count(x)); 
02395     nll= - n*a*log(b)+ n*gammln(a)  +(a+1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02396     return nll;
02397   }
02398   df1b2variable nllInverseGamma(const df1b2vector & x, const double a, const double b)
02399   {
02400     df1b2variable nll=.0;  
02401     double n=double(size_count(x)); 
02402     nll= - n*a*log(b)+ n*gammln(a)  +(a+1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02403     return nll;
02404   }
02405   df1b2variable nllInverseGamma(const df1b2vector & x, const df1b2variable & a, const df1b2variable & b)
02406   {
02407     df1b2variable nll=.0;  
02408     double n=double(size_count(x)); 
02409     nll= - n*a*log(b)+ n*gammln(a)  +(a+1.)*sum(log(x+EPS)) + b/sum(x+EPS);
02410     return nll;
02411   }