ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dnorm.cpp
Go to the documentation of this file.
1 #include "statsLib.h"
2 #include <admodel.h>
3 #include <df1b2fun.h>
4 #include <adrndeff.h>
5 
46 dvariable dnorm( const prevariable& x, const double& mu, const double& std )
47 {
48 
49  if( std<=0 )
50  {
51  cerr<<"Standard deviation is less than or equal to zero in "
52  "dnorm(const dvariable& x, const double& mu, const double& std)\n";
53  return 0;
54  }
55 
56  return 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
57 }
58 
59 dvariable dnorm( const prevariable& x, const double& mu, const double& std, bool bLog=true )
60 {
61 
62  if( std<=0 )
63  {
64  cerr<<"Standard deviation is less than or equal to zero in "
65  "dnorm(const dvariable& x, const double& mu, const double& std)\n";
66  return 0;
67  }
68  dvariable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
69  if(!bLog) tmp = mfexp(-tmp);
70  return tmp;
71 }
72 
73 
74 
75 df1b2variable dnorm( const df1b2variable& x, const df1b2variable& mu, const df1b2variable& std, bool bLog=true )
76 {
77 
78  if( value(std)<=0 )
79  {
80  cerr<<"Standard deviation is less than or equal to zero in "
81  "dnorm(const dvariable& x, const double& mu, const double& std)\n";
82  ad_exit(1);
83  //return df1b2variable(0);
84  }
85  df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
86  if(!bLog) tmp = mfexp(-tmp);
87  return tmp;
88 }
89 
90 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std )
91 {
92 
93  if( value(std)<=0 )
94  {
95  cerr<<"Standard deviation is less than or equal to zero in "
96  "dnorm(const df1b2variable& x, const double& mu, const double& std)\n";
97  ad_exit(1);
98  //return df1b2variable(0);
99  }
100  df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
101  return tmp;
102 }
103 
104 
105 df1b2variable dnorm( const df1b2variable& x, const double& mu, const double& std, bool bLog=true )
106 {
107 
108  if( value(std)<=0 )
109  {
110  cerr<<"Standard deviation is less than or equal to zero in "
111  "dnorm(const df1b2variable& x, const double& mu, const double& std)\n";
112  ad_exit(1);
113  //return df1b2variable(0);
114  }
115  df1b2variable tmp = 0.5*log(2.*M_PI)+log(std)+0.5*square(x-mu)/(std*std);
116  if(!bLog) tmp = mfexp(-tmp);
117  return tmp;
118 }
119 
120 
130 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std )
131 {
132 
133  if( std<=0 )
134  {
135  cerr<<"Standard deviation is less than or equal to zero in "
136  "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
137  ad_exit(1);
138  }
139 
141  long n=size_count(x);
142  dvariable SS=norm2(x-mu);
143  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
145  return( tmp );
146 }
147 
148 dvariable dnorm( const dvar_vector& x, const double& mu, const double& std, bool bLog=true )
149 {
150 
151  if( std<=0 )
152  {
153  cerr<<"Standard deviation is less than or equal to zero in "
154  "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
155  ad_exit(1);
156  }
157 
159  long n=size_count(x);
160  dvariable SS=norm2(x-mu);
161  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
162  if(!bLog) tmp = mfexp(-tmp);
164  return( tmp );
165 }
166 
167 df1b2variable dnorm( const df1b2vector& x, const double& mu, const double& std, bool bLog=true )
168 {
169 
170  if( std<=0 )
171  {
172  cerr<<"Standard deviation is less than or equal to zero in "
173  "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
174  ad_exit(1);
175  }
176 
178  long n = size_count(x);
179  df1b2variable SS = norm2(x-mu);
180  df1b2variable tmp = n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
181  if(!bLog) tmp = mfexp(-tmp);
183  return( tmp );
184 }
185 
195 dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std )
196 {
197 
198  if( std<=0 )
199  {
200  cerr<<"Standard deviation is less than or equal to zero in "
201  "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
202  ad_exit(1);
203  }
204 
206  long n=size_count(x);
207  dvariable SS=norm2(x-mu);
208  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
210  return( tmp );
211 }
212 
213 // dvariable dnorm( const dvector& x, const prevariable& mu, const prevariable& std, bool dLog=true )
214 // {
215 
216 // if( std<=0 )
217 // {
218 // cerr<<"Standard deviation is less than or equal to zero in "
219 // "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
220 // ad_exit(1);
221 // }
222 
223 // RETURN_ARRAYS_INCREMENT();
224 // long n=size_count(x);
225 // dvariable SS=norm2(x-mu);
226 // dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
227 // if(!dLog) tmp=mfexp(-tmp);
228 // RETURN_ARRAYS_DECREMENT();
229 // return( tmp );
230 // }
231 
232 // df1b2variable dnorm( const df1b2vector& x, const df1b2variable& mu, const df1b2variable& std, bool dLog=true )
233 // {
234 
235 // if( std<=0 )
236 // {
237 // cerr<<"Standard deviation is less than or equal to zero in "
238 // "dnorm( const dvar_vector& x, const double& mu, const double& std )\n";
239 // ad_exit(1);
240 // }
241 
242 // RETURN_ARRAYS_INCREMENT();
243 // long n=size_count(x);
244 // df1b2variable SS=norm2(x-mu);
245 // df1b2variable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
246 // if(!dLog) tmp= mfexp(-tmp);
247 // RETURN_ARRAYS_DECREMENT();
248 // return( tmp );
249 // }
250 
259 dvariable dnorm( const dvar_vector& residual, const prevariable& std )
260 {
261 
262  if( std<=0 )
263  {
264  cerr<<"Standard deviation is less than or equal to zero in "
265  "dnorm( const dvar_vector& residual, const dvariable& std )\n";
266  ad_exit(1);
267  }
268 
270  long n=size_count(residual);
271  dvariable SS=norm2(residual);
272  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
274  return( tmp );
275 }
276 
285 dvariable dnorm( const dvar_vector& residual, const double& std )
286 {
287 
288  if( std<=0 )
289  {
290  cerr<<"Standard deviation is less than or equal to zero in "
291  "dnorm( const dvar_vector& residual, const double& std )\n";
292  ad_exit(1);
293  }
294 
296  long n=size_count(residual);
297  dvariable SS=norm2(residual);
298  dvariable tmp=n*(0.5*log(2.*M_PI)+log(std))+0.5*SS/(std*std);
300  return( tmp );
301 }
302 
311 dvariable dnorm( const dvar_vector& residual, const dvector& std )
312 {
313 
314  if( min(std)<=0 )
315  {
316  cerr<<"Standard deviation is less than or equal to zero in "
317  "dnorm( const dvar_vector& residual, const dvector& std )\n";
318  ad_exit(1);
319  }
320  if (std.indexmin()!=residual.indexmin() && std.indexmax()!=residual.indexmax())
321  {
322  cerr<<"Indices do not match in "
323  "dnorm( const dvar_vector& residual, const dvector& std )\n";
324  ad_exit(1);
325  }
326 
328  int n=size_count(residual);
329  dvector var=elem_prod(std,std);
330  dvar_vector SS=elem_prod(residual,residual);
331  dvariable tmp = 0.5*n*log(2.*M_PI)+sum(log(std))+sum(elem_div(SS,2.*var));
333  return( tmp );
334 }
335 
336 
337 
345 dvariable dnorm( const dvar_vector& residual )
346 {
348  int n = size_count(residual);
349  dvariable SS = norm2(residual);
350  dvariable nloglike = 0.5*n*log(SS);
352  return(nloglike);
353 }
354 
363 dvariable dnorm( const dmatrix& obs, const dvar_matrix& pred)
364 {
366  int n = size_count(obs);
367  dvariable SS = sum(elem_div(square(obs-pred),0.01+pred));
369  return 0.5*n*log(SS);
370 }
371 
372 
373 
382 dvariable dnorm( const dvar_vector& residual, const dvar_vector std )
383 {
384 
385  if( min(std)<=0 )
386  {
387  cerr<<"Standard deviation is less than or equal to zero in "
388  "dnorm( const dvar_vector& residual, const dvar_vector std )\n";
389  ad_exit(1);
390  }
391  if (std.indexmin()!=residual.indexmin() && std.indexmax()!=residual.indexmax())
392  {
393  cerr<<"Indices do not match in "
394  "dnorm( const dvar_vector& residual, const dvector& std )\n";
395  ad_exit(1);
396  }
397 
398 
400  int n=size_count(residual);
401  dvar_vector var=elem_prod(std,std);
402  dvar_vector SS=elem_prod(residual,residual);
403  dvariable tmp=0.5*n*log(2.*M_PI)+sum(log(std))+sum(elem_div(SS,2.*var));
405  return( tmp );
406 }
Base class for dvariable.
Definition: fvar.hpp:1315
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
Description not yet available.
void RETURN_ARRAYS_DECREMENT(void)
Decrements gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:507
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
Description not yet available.
Definition: df1b2fun.h:953
d3_array elem_div(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements division of a(i, j, k) / b(i, j, k).
Definition: d3arr2a.cpp:112
exitptr ad_exit
Definition: gradstrc.cpp:53
ADMB variable vector.
Definition: fvar.hpp:2172
Description not yet available.
Definition: df1b2fun.h:266
d3_array mfexp(const d3_array &m)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr7.cpp:10
double var(const dvector &vec)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: cranfill.cpp:23
#define min(a, b)
Definition: cbivnorm.cpp:188
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Description not yet available.
Description not yet available.
Definition: fvar.hpp:2819
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
int indexmin() const
Definition: fvar.hpp:2287
Library of statistic functions.
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
dvariable dnorm(const prevariable &x, const double &mu, const double &std)
Definition: dnorm.cpp:46
unsigned int size_count(const dvector &x)
Returns total size of elements in vector x.
Definition: dsize.cpp:17
Description not yet available.
void RETURN_ARRAYS_INCREMENT(void)
Increments gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:474
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define M_PI
Definition: fvar.hpp:92
int indexmax() const
Definition: fvar.hpp:2292
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13