ADMB Documentation  11.1.1920
 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 df1b2variable dnorm( const df1b2variable& x, const df1b2variable& mu, const df1b2variable& std, bool bLog=true )
00074 {
00075 
00076   if( value(std)<=0 ) 
00077   {
00078     cerr<<"Standard deviation is less than or equal to zero in "
00079     "dnorm(const dvariable& x, const double& mu, const double& std)\n";
00080     ad_exit(1);
00081     //return df1b2variable(0);
00082   }
00083   df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00084   if(!bLog)     tmp = mfexp(-tmp);
00085   return tmp;
00086 }
00087 
00088 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std )
00089 {
00090 
00091   if( value(std)<=0 ) 
00092   {
00093     cerr<<"Standard deviation is less than or equal to zero in "
00094     "dnorm(const df1b2variable& x, const double& mu, const double& std)\n";
00095     ad_exit(1);
00096     //return df1b2variable(0);
00097   }
00098   df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00099   return tmp;
00100 }
00101 
00102 
00103 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std, bool bLog=true )
00104 {
00105 
00106   if( value(std)<=0 ) 
00107   {
00108     cerr<<"Standard deviation is less than or equal to zero in "
00109     "dnorm(const df1b2variable& x, const double& mu, const double& std)\n";
00110     ad_exit(1);
00111     //return df1b2variable(0);
00112   }
00113   df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
00114   if(!bLog)     tmp = mfexp(-tmp);
00115   return tmp;
00116 }
00117 
00118 
00128 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std )
00129 {
00130   
00131   if( std<=0 ) 
00132   {
00133     cerr<<"Standard deviation is less than or equal to zero in "
00134     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00135     ad_exit(1);
00136   }
00137   
00138   RETURN_ARRAYS_INCREMENT();
00139   long n=size_count(x);
00140   dvariable SS=norm2(x-mu);
00141   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00142   RETURN_ARRAYS_DECREMENT();
00143   return( tmp );
00144 }
00145 
00146 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std, bool bLog=true )
00147 {
00148   
00149   if( std<=0 ) 
00150   {
00151     cerr<<"Standard deviation is less than or equal to zero in "
00152     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00153     ad_exit(1);
00154   }
00155   
00156   RETURN_ARRAYS_INCREMENT();
00157   long n=size_count(x);
00158   dvariable SS=norm2(x-mu);
00159   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00160   if(!bLog) tmp = mfexp(-tmp);
00161   RETURN_ARRAYS_DECREMENT();
00162   return( tmp );
00163 }
00164 
00165 df1b2variable dnorm( const df1b2vector& x, const double& mu, const double& std, bool bLog=true )
00166 {
00167   
00168   if( std<=0 ) 
00169   {
00170     cerr<<"Standard deviation is less than or equal to zero in "
00171     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00172     ad_exit(1);
00173   }
00174   
00175   RETURN_ARRAYS_INCREMENT();
00176   long n             = size_count(x);
00177   df1b2variable SS   = norm2(x-mu);
00178   df1b2variable tmp = n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00179   if(!bLog) tmp      = mfexp(-tmp);
00180   RETURN_ARRAYS_DECREMENT();
00181   return( tmp );
00182 }
00183 
00193 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std )
00194 {
00195   
00196   if( std<=0 ) 
00197   {
00198     cerr<<"Standard deviation is less than or equal to zero in "
00199     "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00200     ad_exit(1);
00201   }
00202   
00203   RETURN_ARRAYS_INCREMENT();
00204   long n=size_count(x);
00205   dvariable SS=norm2(x-mu);
00206   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00207   RETURN_ARRAYS_DECREMENT();
00208   return( tmp );
00209 }
00210 
00211 // dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std, bool dLog=true )
00212 // {
00213   
00214 //  if( std<=0 ) 
00215 //  {
00216 //    cerr<<"Standard deviation is less than or equal to zero in "
00217 //    "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00218 //    ad_exit(1);
00219 //  }
00220   
00221 //  RETURN_ARRAYS_INCREMENT();
00222 //  long n=size_count(x);
00223 //  dvariable SS=norm2(x-mu);
00224 //  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00225 //  if(!dLog) tmp=mfexp(-tmp);
00226 //  RETURN_ARRAYS_DECREMENT();
00227 //  return( tmp );
00228 // }
00229 
00230 // df1b2variable dnorm( const df1b2vector& x, const df1b2variable& mu, const df1b2variable& std, bool dLog=true )
00231 // {
00232   
00233 //  if( std<=0 ) 
00234 //  {
00235 //    cerr<<"Standard deviation is less than or equal to zero in "
00236 //    "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
00237 //    ad_exit(1);
00238 //  }
00239   
00240 //  RETURN_ARRAYS_INCREMENT();
00241 //  long n=size_count(x);
00242 //  df1b2variable SS=norm2(x-mu);
00243 //  df1b2variable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00244 //  if(!dLog) tmp= mfexp(-tmp);
00245 //  RETURN_ARRAYS_DECREMENT();
00246 //  return( tmp );
00247 // }
00248 
00257 dvariable dnorm( const dvar_vector& residual, const prevariable& std )
00258 {
00259   
00260   if( std<=0 ) 
00261   {
00262     cerr<<"Standard deviation is less than or equal to zero in "
00263     "dnorm( const dvar_vector& residual, const dvariable& std )\n";
00264     ad_exit(1);
00265   }
00266   
00267   RETURN_ARRAYS_INCREMENT();
00268   long n=size_count(residual);
00269   dvariable SS=norm2(residual);
00270   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00271   RETURN_ARRAYS_DECREMENT();
00272   return( tmp );
00273 }
00274 
00283 dvariable dnorm( const dvar_vector& residual, const double& std )
00284 {   
00285   
00286   if( std<=0 ) 
00287   {
00288     cerr<<"Standard deviation is less than or equal to zero in "
00289     "dnorm( const dvar_vector& residual, const double& std )\n";
00290     ad_exit(1);
00291   }
00292   
00293   RETURN_ARRAYS_INCREMENT();
00294   long n=size_count(residual);
00295   dvariable SS=norm2(residual);
00296   dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
00297   RETURN_ARRAYS_DECREMENT();
00298   return( tmp );
00299 }
00300 
00309 dvariable dnorm( const dvar_vector& residual, const dvector& std )
00310 {
00311   
00312   if( min(std)<=0 ) 
00313   {
00314     cerr<<"Standard deviation is less than or equal to zero in "
00315     "dnorm( const dvar_vector& residual, const dvector& std )\n";
00316     ad_exit(1);
00317   }
00318   if (std.indexmin()!=residual.indexmin() && std.indexmax()!=residual.indexmax())
00319   {
00320     cerr<<"Indices do not match in "
00321     "dnorm( const dvar_vector& residual, const dvector& std )\n";
00322     ad_exit(1);
00323   }
00324   
00325   RETURN_ARRAYS_INCREMENT();
00326   int n=size_count(residual);
00327   dvector var=elem_prod(std,std);
00328   dvar_vector SS=elem_prod(residual,residual);
00329   dvariable tmp = 0.5*n*log(2.*M_PI)+sum(log(std))+sum(elem_div(SS,2.*var));
00330   RETURN_ARRAYS_DECREMENT();
00331   return( tmp );
00332 }
00333 
00334 
00335 
00343 dvariable dnorm( const dvar_vector& residual )
00344 {
00345   RETURN_ARRAYS_INCREMENT();
00346   int n              = size_count(residual);
00347   dvariable SS       = norm2(residual);
00348   dvariable nloglike = 0.5*n*log(SS);
00349   RETURN_ARRAYS_DECREMENT();
00350   return(nloglike);
00351 }
00352 
00361 dvariable dnorm( const dmatrix& obs, const dvar_matrix& pred)
00362 {
00363   RETURN_ARRAYS_INCREMENT();
00364   int n = size_count(obs);
00365   dvariable SS = sum(elem_div(square(obs-pred),0.01+pred));
00366   RETURN_ARRAYS_DECREMENT();
00367   return 0.5*n*log(SS);
00368 }
00369 
00370 
00371 
00380 dvariable dnorm( const dvar_vector& residual, const dvar_vector std )
00381 {
00382   
00383   if( min(std)<=0 ) 
00384   {
00385     cerr<<"Standard deviation is less than or equal to zero in "
00386     "dnorm( const dvar_vector& residual, const dvar_vector std )\n";
00387     ad_exit(1);
00388   }
00389   if (std.indexmin()!=residual.indexmin() && std.indexmax()!=residual.indexmax())
00390   {
00391     cerr<<"Indices do not match in "
00392     "dnorm( const dvar_vector& residual, const dvector& std )\n";
00393     ad_exit(1);
00394   }
00395   
00396   
00397   RETURN_ARRAYS_INCREMENT();
00398   int n=size_count(residual);
00399   dvar_vector var=elem_prod(std,std);
00400   dvar_vector SS=elem_prod(residual,residual);
00401   dvariable tmp=0.5*n*log(2.*M_PI)+sum(log(std))+sum(elem_div(SS,2.*var));
00402   RETURN_ARRAYS_DECREMENT();
00403   return( tmp );
00404 }