ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
newreg2.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <fvar.hpp>
12 
18  const dvariable& a)
19 {
20  if (obs.indexmin() != pred.indexmin() || obs.indexmax() != pred.indexmax() )
21  {
22  cerr << "Index limits on observed vector are not equal to the Index\n"
23  "limits on the predicted vector in robust_reg_likelihood function\n";
24  }
25 
26  //Need this statement because the function
27  //returns a variable type
30 
31  dvariable v_hat;
32  double width=3.0;
33  double pcon=0.05;
34  double width2=width*width;
35  dvariable a2;
36  a2=a*a;
37  dvar_vector diff = obs-pred; // These are the residuals
38  dvar_vector diff2 = pow(diff,2); // These are the squared residuals
39  v_hat = mean(diff2)+1.e-80; // add 1.e-80 so that a perfect fit wont't
40  // produce log(0).
41  double b=2.*pcon/(width*sqrt(PI)); // This is the weight for the
42  // "robustifying" term
43  dvariable log_likelihood = -sum(log((1.-pcon)*exp(-diff2/(2.*a2*v_hat))
44  + b/(1.+pow(diff2/(width2*a2*v_hat),2))));
45  log_likelihood += 0.5*diff.size()*log(a2*v_hat);
46 
47  // Need this to decrement the stack increment
48  // caused by RETURN_ARRAYS_INCREMENT();
50 
51  return(log_likelihood);
52 }
53 
59  const double a)
60 {
61  if (obs.indexmin() != pred.indexmin() || obs.indexmax() != pred.indexmax() )
62  {
63  cerr << "Index limits on observed vector are not equal to the Index\n"
64  "limits on the predicted vector in robust_reg_likelihood function\n";
65  }
66 
67  //Need this statement because the function
68  //returns a variable type
71 
72  dvariable v_hat;
73  double width=3.0;
74  double pcon=0.05;
75  //double width2=width*width;
76  dvariable a2;
77  a2=a*a;
78  dvar_vector diff = obs-pred; // These are the residuals
79  dvar_vector diff2 = square(diff); // These are the squared residuals
80  v_hat = mean(diff2)+1.e-80; // add 1.e-80 so that a perfect fit wont't
81  // produce log(0).
82  double b=2.*pcon/(width*sqrt(PI)); // This is the weight for the
83  // "robustifying" term
84  dvariable log_likelihood = -sum(log((1.-pcon)*exp(-diff2/(2.*a2*v_hat))
85  + b/(1.+pow(diff2/(a2*v_hat),2))));
86  log_likelihood += 0.5*diff.size()*log(a2*v_hat);
87  // Need this to decrement the stack increment
88  // caused by RETURN_ARRAYS_INCREMENT();
90  return(log_likelihood);
91 }
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
ADMB variable vector.
Definition: fvar.hpp:2172
unsigned int size() const
Definition: fvar.hpp:2297
double mean(const dvector &vec)
Returns computed mean of vec.
Definition: cranfill.cpp:43
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
void RETURN_ARRAYS_INCREMENT()
Definition: gradstrc.cpp:478
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
static _THREAD gradient_structure * _instance
int indexmin() const
Definition: fvar.hpp:2287
void RETURN_ARRAYS_DECREMENT()
Definition: gradstrc.cpp:511
#define PI
Definition: fvar.hpp:95
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
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
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17
dvariable robust_regression(const dvector &obs, const dvar_vector &pred, double a=0.7)
Description not yet available.
Definition: newreg2.cpp:58