ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmvlogistic.cpp
Go to the documentation of this file.
1 #include "statsLib.h"
2 
15 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p,dvar_matrix& nu, double& tau2,const double minp)
16 { //returns the negative loglikelihood using the MLE for the variance
17  /*
18  This is a modified version of the dmvlogistic negative log likelihood
19  where proportions at age less than minp are pooled into the consecutive
20  age-classes. See last paragraph in Appendix A of Richards, Schnute and
21  Olsen 1997.
22 
23  NB minp must be greater than 0, otherwise this algorithm returns an
24  error if one of the observed proportions is zero.
25 
26  -1) first count the number of observations for each year > minp
27  -2) normalized observed and predicted age-proportions
28  -3) loop over ages, and check if observed proportion is < minp
29  -if yes, then add observed proprtion to bin k
30  -if no then add observed proportion to bin k and increment
31  bin k if k is currently less than the number of bins.
32  -4) do the same grouping for the predicted proportions.
33  -5) use ivector iiage to correctly assign residuals into nu
34 
35  FEB 8, 2011. Fixed a bug in the variance calculation &
36  likelihood scaling that was discovered at the 2011 Hake
37  assessment STAR panel in Seattle.
38  */
40  int i,j,k,n;
41  int age_counts=0;
42  int a = o.colmin();
43  int A=o.colmax();
44  double tiny=0.001/(A-a+1);
45  int t=o.rowmin();
46  int T=o.rowmax();
47  dvariable tau_hat2; //mle of the variance
48  //dvar_matrix nu(t,T,a,A);
49  nu.initialize();
50 
51  for(i=t; i<=T; i++)
52  {
53  n=0;
54  dvector oo = o(i)/sum(o(i));
55  dvar_vector pp = p(i)/sum(p(i));
56 
57  //count # of observations greater than minp (2% is a reasonable number)
58  for(j=a;j<=A;j++)
59  if(oo(j) > minp)n++;
60 
61  ivector iiage(1,n);
62  dvector o1(1,n); o1.initialize();
63  dvar_vector p1(1,n); p1.initialize();
64  k=1;
65  for(j=a;j<=A;j++)
66  {
67  if(oo(j)<=minp)
68  {
69  o1(k)+=oo(j);
70  p1(k)+=pp(j);
71  }
72  else
73  {
74  o1(k)+=oo(j);
75  p1(k)+=pp(j);
76  if(k<=n)iiage(k)=j; //ivector for the grouped residuals
77  if(k<n) k++;
78  }
79  }
80 
81  //assign residuals to nu based on iiage index
82  dvar_vector t1 = log(o1)-log(p1) - mean(log(o1)-log(p1));
83 
84  for(j=1;j<=n;j++)
85  nu(i)(iiage(j))=t1(j);
86 
87  age_counts += n-1;
88  }
89  //Depricated Wrong Variance & likelihood calculation.
90  //tau_hat2 = 1./(age_counts*(T-t+1))*norm2(nu);
91  //dvariable nloglike =(age_counts*(T-t+1))*log(tau_hat2);
92 
93  //Feb 8, 2011 Fixed variance & likelihood
94  tau_hat2 = 1./(age_counts)*norm2(nu);
95  dvariable nloglike =(age_counts)*log(tau_hat2);
96  tau2=value(tau_hat2); //mle of the variance
98  return(nloglike);
99 }
100 
101 //The following function has been depricated.
102 dvariable dmvlogistic(const dmatrix o, const dvar_matrix& p, double& tau2)
103 {
104 
105  //returns the negative loglikelihood using the MLE for the variance
107 
108  int a = o.colmin();
109  int A=o.colmax();
110  double tiny=0.001/(A-a+1);
111  int t=o.rowmin();
112  int T=o.rowmax();
113  dvariable tau_hat2; //mle of the variance
114  dvar_matrix nu(t,T,a,A);
115  for(int i=t; i<=T; i++)
116  {
117  dvector o1=o(i)/sum(o(i));
118  dvar_vector p1=p(i)/sum(p(i));
119  //p1=log(p1)-mean(p1);
120  //p1=log(p1)-mean(log(p1));
121  //p1 = exp(p1)/sum(exp(p1));
122  nu(i) = log(o1+tiny)-log(p1+tiny) - mean(log(o1+tiny)-log(p1+tiny));
123  }
124  tau_hat2 = 1./((A-a)*(T-t+1))*norm2(nu);
125  dvariable nloglike =((A-a)*(T-t+1))*log(tau_hat2);
126  tau2=value(tau_hat2); //mle of the variance
128  return(nloglike);
129 }
130 
void RETURN_ARRAYS_DECREMENT(void)
Decrements gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:507
Vector of double precision numbers.
Definition: dvector.h:50
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
ADMB variable vector.
Definition: fvar.hpp:2172
double mean(const dvector &vec)
Returns computed mean of vec.
Definition: cranfill.cpp:43
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int rowmax() const
Definition: fvar.hpp:2929
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
Library of statistic functions.
const double tiny
dvariable dmvlogistic(const dmatrix o, const dvar_matrix &p, dvar_matrix &nu, double &tau2, const double minp)
Definition: dmvlogistic.cpp:15
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
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 rowmin() const
Definition: fvar.hpp:2925
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