ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmultinom.cpp
Go to the documentation of this file.
1 #include "statsLib.h"
2 
16 {
17  if(x.indexmin() != p.indexmin() || x.indexmax() != p.indexmax())
18  {
19  cerr << "Index bounds do not macth in"
20  " dmultinom(const dvector& x, const dvar_vector& p)\n";
21  ad_exit(1);
22  }
23 
24  double n=sum(x);
25  return -gammln(n+1.)+sum(gammln(x+1.))-x*log(p/sum(p));
26 }
27 
28 double neff(const dvector& obs, const dvar_vector& pred)
29 {
30  dvector resid=value(obs-pred);
31  dvector var=value(elem_prod(1.-pred,pred));
32  return sum(var)/norm2(resid);
33 }
34 
35 // dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp, double &neff)
36 // {
37 // RETURN_ARRAYS_INCREMENT();
38 // dvariable nloglike = dmultinom(o,p,nu,tau2,minp);
39 // int t = o.rowmin();
40 // int T = o.rowmax();
41 // dvector n(t,T);
42 // for(int i = t; i <= T; i++ )
43 // {
44 // n(i) = neff(o(i),p(i));
45 // }
46 // neff = mean(n(i));
47 // RETURN_ARRAYS_DECREMENT();
48 // return(nloglike);
49 // }
50 dvariable dmultinom(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu,double& tau2,const double minp)
51 { // returns the negative loglikelihood
52  /*
53  uses Martell dmvlogistic code for grouping age classes
54  with observed proportions < minp
55  NB minp must be greater than 0, otherwise algorithm returns
56  an error if one of the observed proportions is zero.
57  tau2 returns the median absolute standardized residual
58 
59  FIX ME SM I'm getting an array out of Bounds error in here for gear3
60  has to do with the if statement (if minp >1.-4) because Ncount is only
61  1. I've commented the if statement out for now.
62  */
64  int i,j,k,n;
65  int a = o.colmin();
66  int A=o.colmax();
67  int t=o.rowmin();
68  int T=o.rowmax();
69  dvector tmptau(1,A*T); // vector of residuals
70  int Ncount=1;
71  dvariable Nsamp; // multinomial sample size
72  //FIXME NB Make proc_err into a switch in the control file
73  //add this likelihood description to the documentation.
74  dvariable proc_err=0.009; // allow for process error in the pred.age freq...fixed value based on HCAM assessments
75  nu.initialize();
76  dvariable nloglike=0.;
77  //ofstream ofs("check.tmp");
78 
79  for(i=t; i<=T; i++)
80  {
81  //cout<<"Ok to here 1"<<endl;
82  Nsamp=sum(o(i))/(1.+proc_err*sum(o(i)));
83  n=0;
84  dvector oo = o(i)/sum(o(i));
85  dvar_vector pp = p(i)/sum(p(i));
86 
87  //count # of observations greater than minp (2% is a reasonable number)
88  for(j=a;j<=A;j++)
89  if(oo(j) > minp)n++;
90 
91  ivector iiage(1,n);
92  dvector o1(1,n); o1.initialize();
93  dvar_vector p1(1,n); p1.initialize();
94  k=1;
95  for(j=a;j<=A;j++)
96  {
97  if(oo(j)<=minp)
98  {
99  o1(k)+=oo(j);
100  p1(k)+=pp(j);
101  }
102  else
103  {
104  o1(k)+=oo(j);
105  p1(k)+=pp(j);
106  if(k<=n)iiage(k)=j; //ivector for the grouped residuals
107  if(k<n) k++;
108  }
109  }
110  /*
111  //assign residuals to nu based on iiage index
112  dvar_vector t1 = elem_div(o1-p1,sqrt(elem_prod(p1,1.-p1)/Nsamp));
113  for(j=1;j<=n;j++)
114  {
115  nu(i)(iiage(j))=t1(j);
116  tmptau(Ncount++)=sqrt(square(value(t1(j))));
117  }
118  */
119 
120  //CHANGED Later addition from Viv to prevent crashes if
121  //min(p1) is very small.
122  //if(min(p1)>1e-4)
123  {
124  dvar_vector t1 = elem_div(o1-p1,sqrt(elem_prod(p1,1.-p1)/Nsamp));
125  for(j=1;j<=n;j++)
126  {
127  nu(i)(iiage(j))=t1(j);
128  tmptau(Ncount++)=sqrt(square(value(t1(j))));
129  }
130  }
131  //end of addition.
132  // negative log Mulitinomial with constant is:
133  // r = -1.*(gammln(Nsamp+1)+sum(Nsamp*o1(log(p1))-gammln(Nsamp+1)));
134  // TODO add calculation for effective sample size.
135  /*
136  TODO Neff = sum(elem_prod(p1,1.-p1))/sum(square(o1-p1));
137  for each year. Plot the Nsamp vs Neff and look for a 1:1 slope.
138  */
139 
140  nloglike+=sum(-1.*elem_prod(Nsamp*o1,log(p1))+
141  elem_prod(Nsamp*o1,log(o1)));
142  //cout<<"Ok to here 2"<<endl;
143  }
144 
145  dvector w=sort(tmptau(1,Ncount-1));
146  //cout<<"All good "<<Ncount<<endl;
147  tau2=w(int(Ncount/2.)); //median absolute residual (expected value of 0.67ish)
148 
150  return(nloglike);
151 }
152 
153 
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
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
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
void initialize(void)
Zero initialize allocated dvar_matrix, then saves adjoint function and position data.
Definition: fvar_ma7.cpp:48
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
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
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
dvariable dmultinom(const dvector &x, const dvar_vector &p)
Definition: dmultinom.cpp:15
double neff(const dvector &obs, const dvar_vector &pred)
Definition: dmultinom.cpp:28
int rowmax() const
Definition: fvar.hpp:2929
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
double var(const dvector &vec)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: cranfill.cpp:23
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
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
#define w
void RETURN_ARRAYS_INCREMENT(void)
Increments gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:474
void initialize(const dvector &ww)
Description not yet available.
Definition: fvar_a24.cpp:63
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
int indexmax() const
Definition: fvar.hpp:2292
int rowmin() const
Definition: fvar.hpp:2925
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
int colmax(void) const
Definition: fvar.hpp:2943