ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dnorm.cpp
Go to the documentation of this file.
00001 #include "statsLib.h"
00002 #include <admodel.h>
00003 #include <df1b2fun.h>
00004 #include <adrndeff.h> 
00005 
00046 dvariable dnorm( const prevariable& x, const double& mu, const double& std )
00047 {
00048 
00049   if( std<=0 ) 
00050   {
00051     cerr<<"Standard deviation is less than or equal to zero in "
00052     "dnorm(const dvariable& x, const double& mu, const double& std)\n";
00053     return 0;
00054   }
00055 
00056   return 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00057 }
00058 
00059 dvariable dnorm( const prevariable& x, const double& mu, const double& std, bool bLog=true )
00060 {
00061 
00062   if( std<=0 ) 
00063   {
00064     cerr<<"Standard deviation is less than or equal to zero in "
00065     "dnorm(const dvariable& x, const double& mu, const double& std)\n";
00066     return 0;
00067   }
00068   dvariable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00069   if(!bLog)  tmp = mfexp(-tmp);
00070   return tmp;
00071 }
00072 
00073 
00074 
00075 df1b2variable dnorm( const df1b2variable& x, const df1b2variable& mu, const df1b2variable& std, bool bLog=true )
00076 {
00077 
00078   if( value(std)<=0 ) 
00079   {
00080     cerr<<"Standard deviation is less than or equal to zero in "
00081     "dnorm(const dvariable& x, const double& mu, const double& std)\n";
00082     ad_exit(1);
00083     //return df1b2variable(0);
00084   }
00085   df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00086   if(!bLog)     tmp = mfexp(-tmp);
00087   return tmp;
00088 }
00089 
00090 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std )
00091 {
00092 
00093   if( value(std)<=0 ) 
00094   {
00095     cerr<<"Standard deviation is less than or equal to zero in "
00096     "dnorm(const df1b2variable& x, const double& mu, const double& std)\n";
00097     ad_exit(1);
00098     //return df1b2variable(0);
00099   }
00100   df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00101   return tmp;
00102 }
00103 
00104 
00105 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std, bool bLog=true )
00106 {
00107 
00108   if( value(std)<=0 ) 
00109   {
00110     cerr<<"Standard deviation is less than or equal to zero in "
00111     "dnorm(const df1b2variable& x, const double& mu, const double& std)\n";
00112     ad_exit(1);
00113     //return df1b2variable(0);
00114   }
00115   df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00116   if(!bLog)     tmp = mfexp(-tmp);
00117   return tmp;
00118 }
00119 
00120 
00130 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std )
00131 {
00132   
00133   if( std<=0 ) 
00134   {
00135     cerr<<"Standard deviation is less than or equal to zero in "
00136     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00137     ad_exit(1);
00138   }
00139   
00140   RETURN_ARRAYS_INCREMENT();
00141   long n=size_count(x);
00142   dvariable SS=norm2(x-mu);
00143   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00144   RETURN_ARRAYS_DECREMENT();
00145   return( tmp );
00146 }
00147 
00148 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std, bool bLog=true )
00149 {
00150   
00151   if( std<=0 ) 
00152   {
00153     cerr<<"Standard deviation is less than or equal to zero in "
00154     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00155     ad_exit(1);
00156   }
00157   
00158   RETURN_ARRAYS_INCREMENT();
00159   long n=size_count(x);
00160   dvariable SS=norm2(x-mu);
00161   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00162   if(!bLog) tmp = mfexp(-tmp);
00163   RETURN_ARRAYS_DECREMENT();
00164   return( tmp );
00165 }
00166 
00167 df1b2variable dnorm( const df1b2vector& x, const double& mu, const double& std, bool bLog=true )
00168 {
00169   
00170   if( std<=0 ) 
00171   {
00172     cerr<<"Standard deviation is less than or equal to zero in "
00173     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00174     ad_exit(1);
00175   }
00176   
00177   RETURN_ARRAYS_INCREMENT();
00178   long n             = size_count(x);
00179   df1b2variable SS   = norm2(x-mu);
00180   df1b2variable tmp = n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00181   if(!bLog) tmp      = mfexp(-tmp);
00182   RETURN_ARRAYS_DECREMENT();
00183   return( tmp );
00184 }
00185 
00195 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std )
00196 {
00197   
00198   if( std<=0 ) 
00199   {
00200     cerr<<"Standard deviation is less than or equal to zero in "
00201     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00202     ad_exit(1);
00203   }
00204   
00205   RETURN_ARRAYS_INCREMENT();
00206   long n=size_count(x);
00207   dvariable SS=norm2(x-mu);
00208   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00209   RETURN_ARRAYS_DECREMENT();
00210   return( tmp );
00211 }
00212 
00213 // dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std, bool dLog=true )
00214 // {
00215   
00216 //  if( std<=0 ) 
00217 //  {
00218 //    cerr<<"Standard deviation is less than or equal to zero in "
00219 //    "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00220 //    ad_exit(1);
00221 //  }
00222   
00223 //  RETURN_ARRAYS_INCREMENT();
00224 //  long n=size_count(x);
00225 //  dvariable SS=norm2(x-mu);
00226 //  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00227 //  if(!dLog) tmp=mfexp(-tmp);
00228 //  RETURN_ARRAYS_DECREMENT();
00229 //  return( tmp );
00230 // }
00231 
00232 // df1b2variable dnorm( const df1b2vector& x, const df1b2variable& mu, const df1b2variable& std, bool dLog=true )
00233 // {
00234   
00235 //  if( std<=0 ) 
00236 //  {
00237 //    cerr<<"Standard deviation is less than or equal to zero in "
00238 //    "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00239 //    ad_exit(1);
00240 //  }
00241   
00242 //  RETURN_ARRAYS_INCREMENT();
00243 //  long n=size_count(x);
00244 //  df1b2variable SS=norm2(x-mu);
00245 //  df1b2variable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00246 //  if(!dLog) tmp= mfexp(-tmp);
00247 //  RETURN_ARRAYS_DECREMENT();
00248 //  return( tmp );
00249 // }
00250 
00259 dvariable dnorm( const dvar_vector& residual, const prevariable& std )
00260 {
00261   
00262   if( std<=0 ) 
00263   {
00264     cerr<<"Standard deviation is less than or equal to zero in "
00265     "dnorm( const dvar_vector& residual, const dvariable& std )\n";
00266     ad_exit(1);
00267   }
00268   
00269   RETURN_ARRAYS_INCREMENT();
00270   long n=size_count(residual);
00271   dvariable SS=norm2(residual);
00272   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00273   RETURN_ARRAYS_DECREMENT();
00274   return( tmp );
00275 }
00276 
00285 dvariable dnorm( const dvar_vector& residual, const double& std )
00286 {   
00287   
00288   if( std<=0 ) 
00289   {
00290     cerr<<"Standard deviation is less than or equal to zero in "
00291     "dnorm( const dvar_vector& residual, const double& std )\n";
00292     ad_exit(1);
00293   }
00294   
00295   RETURN_ARRAYS_INCREMENT();
00296   long n=size_count(residual);
00297   dvariable SS=norm2(residual);
00298   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00299   RETURN_ARRAYS_DECREMENT();
00300   return( tmp );
00301 }
00302 
00311 dvariable dnorm( const dvar_vector& residual, const dvector& std )
00312 {
00313   
00314   if( min(std)<=0 ) 
00315   {
00316     cerr<<"Standard deviation is less than or equal to zero in "
00317     "dnorm( const dvar_vector& residual, const dvector& std )\n";
00318     ad_exit(1);
00319   }
00320   if (std.indexmin()!=residual.indexmin() && std.indexmax()!=residual.indexmax())
00321   {
00322     cerr<<"Indices do not match in "
00323     "dnorm( const dvar_vector& residual, const dvector& std )\n";
00324     ad_exit(1);
00325   }
00326   
00327   RETURN_ARRAYS_INCREMENT();
00328   int n=size_count(residual);
00329   dvector var=elem_prod(std,std);
00330   dvar_vector SS=elem_prod(residual,residual);
00331   dvariable tmp = 0.5*n*log(2.*M_PI)+sum(log(std))+sum(elem_div(SS,2.*var));
00332   RETURN_ARRAYS_DECREMENT();
00333   return( tmp );
00334 }
00335 
00336 
00337 
00345 dvariable dnorm( const dvar_vector& residual )
00346 {
00347   RETURN_ARRAYS_INCREMENT();
00348   int n              = size_count(residual);
00349   dvariable SS       = norm2(residual);
00350   dvariable nloglike = 0.5*n*log(SS);
00351   RETURN_ARRAYS_DECREMENT();
00352   return(nloglike);
00353 }
00354 
00363 dvariable dnorm( const dmatrix& obs, const dvar_matrix& pred)
00364 {
00365   RETURN_ARRAYS_INCREMENT();
00366   int n = size_count(obs);
00367   dvariable SS = sum(elem_div(square(obs-pred),0.01+pred));
00368   RETURN_ARRAYS_DECREMENT();
00369   return 0.5*n*log(SS);
00370 }
00371 
00372 
00373 
00382 dvariable dnorm( const dvar_vector& residual, const dvar_vector std )
00383 {
00384   
00385   if( min(std)<=0 ) 
00386   {
00387     cerr<<"Standard deviation is less than or equal to zero in "
00388     "dnorm( const dvar_vector& residual, const dvar_vector std )\n";
00389     ad_exit(1);
00390   }
00391   if (std.indexmin()!=residual.indexmin() && std.indexmax()!=residual.indexmax())
00392   {
00393     cerr<<"Indices do not match in "
00394     "dnorm( const dvar_vector& residual, const dvector& std )\n";
00395     ad_exit(1);
00396   }
00397   
00398   
00399   RETURN_ARRAYS_INCREMENT();
00400   int n=size_count(residual);
00401   dvar_vector var=elem_prod(std,std);
00402   dvar_vector SS=elem_prod(residual,residual);
00403   dvariable tmp=0.5*n*log(2.*M_PI)+sum(log(std))+sum(elem_div(SS,2.*var));
00404   RETURN_ARRAYS_DECREMENT();
00405   return( tmp );
00406 }