ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
baranov.cpp
Go to the documentation of this file.
1 #include "statsLib.h"
2 
51 double get_ft(const double& ct, const double& m, const dvector& va, const dvector& ba)
52 {
53  double ft;
54  //initial guess for ft
55  ft=ct/(va*(ba*exp(-m/2.)));
56 
57  for(int i=1;i<=50;i++)
58  {
59  dvector f = ft*va;
60  dvector z = m+f;
61  dvector s = exp(-z);
62  dvector o = (1.-s);
63 
64  dvector t1 = elem_div(f,z);
65  dvector t2 = elem_prod(t1,o);
66  dvector t3 = elem_prod(o,ba);
67  //predicted catch
68  double pct = t2*ba;
69 
70  //derivative of catch wrt ft
71  double dct = sum(
72  elem_div(t3,z)
73  - elem_div(elem_prod(f,t3),square(z))
74  + elem_prod(elem_prod(t1,s),ba));
75 
76  ft -= (pct-ct)/dct; //newton step
77  //if(fabs(pct-ct)<1.e-9) break; //do not use for dvariables
78  }
79 
80  return(ft);
81 }
82 
94 dvector get_ft( dvector& ct,const double& m, const dmatrix& V,const dvector& ba )
95 {
96  /* ARGUMENTS:
97  ct is the observed catch for each fleet
98  m is the natural mortality rate
99  va is the vulnerability row fleet, col age va(1,ngear,1,nage)
100  ba is the start of year biomass at age
101  */
102 
103  int i,a,A;
104  double minsurv = 0.05;
105  int ng=size_count(ct); //number of gears
106  a=ba.indexmin(); A=ba.indexmax();
107  dvector ft(1,ng); ft=0;
108  dvector ctmp(1,ng);
109  dvector ct_hat(1,ng); //predicted catch
110  dvector dct_hat(1,ng); //derivative
111  dvector zage(a,A);
112  dvector sage(a,A);
113  dvector ominus(a,A);
114  dmatrix F(1,ng,a,A);
115 
116 
117  //initial guess for ft=ct/(0.98 Bt);
118  ctmp=ct;
119 
120  for(i=1;i<=ng;i++)
121  {
122  ft(i) = ctmp(i)/(0.98*ba*V(i)*exp(-0.5*m));
123  if(1.-ft(i)<minsurv)
124  {
125  ft(i)=1.-minsurv;
126  ctmp=ft(i)*ba*V(i)*exp(-0.5*m);
127  }
128  }
129  ct=ctmp; //don't do this for the differentiable version.
130 
131  //now solve baranov catch equation iteratively.
132  for(int iter=1; iter<=17; iter++)
133  {
134  for(i=1;i<=ng;i++)F(i)=ft(i)*V(i);
135  zage=m+colsum(F);
136  sage=mfexp(-zage);
137  ominus=(1.-sage);
138 
139  for(i=1;i<=ng;i++)
140  {
141  dvector t1 = elem_div(F(i),zage);
142  dvector t2 = elem_prod(t1,ominus);
143  dvector t3 = elem_prod(ominus,ba);
144 
145  ct_hat(i) = t2*ba;
146 
147  dct_hat(i) = sum(
148  elem_div(t3,zage)
149  - elem_div(elem_prod(F(i),t3),square(zage))
150  + elem_prod(elem_prod(t1,sage),ba));
151 
152  ft(i) -= (ct_hat(i)-ctmp(i))/dct_hat(i);
153  }
154  //cout<<iter<<"\t"<<ft<<"\t"<<ct_hat-ct<<endl;
155  }
156  //cout<<ft<<"\t\t"<<ct<<"\t\t"<<ctmp<<endl;
157 
158  return(ft);
159 }
160 
174 dvector get_ft( dvector& ct,const double& m, const dmatrix& V,const dvector& na, const dvector& wa )
175 {
176  /* ARGUMENTS:
177  ct is the observed catch for each fleet
178  m is the natural mortality rate
179  va is the vulnerability row fleet, col age va(1,ngear,1,nage)
180  na is the start of year numbers at age
181  wa is the mean weight-at-age
182  */
183 
184  int i,a,A;
185  double minsurv = 0.05;
186  int ng=size_count(ct); //number of gears
187  a=na.indexmin(); A=na.indexmax();
188  dvector ft(1,ng); ft=0;
189  dvector ctmp(1,ng);
190  dvector ct_hat(1,ng); //predicted catch
191  dvector dct_hat(1,ng); //derivative
192  dvector ba(a,A); //biomass at age
193  dvector ca(a,A); //catch-at-age in numbers
194  dvector zage(a,A);
195  dvector sage(a,A);
196  dvector ominus(a,A);
197  dmatrix F(1,ng,a,A);
198 
199 
200  //initial guess for ft=ct/(0.98 Bt);
201  ctmp=ct;
202  ba = elem_prod(na,wa);
203  for(i=1;i<=ng;i++)
204  {
205  ft(i) = ctmp(i)/(0.98*ba*V(i)*exp(-0.5*m));
206  if(1.-ft(i)<minsurv)
207  {
208  ft(i)=1.-minsurv;
209  ctmp(i)=ft(i)*ba*V(i)*exp(-0.5*m);
210  }
211  }
212  ct=ctmp; //don't do this for the differentiable version.
213 
214  //now solve baranov catch equation iteratively.
215  for(int iter=1; iter<=17; iter++)
216  {
217  for(i=1;i<=ng;i++)F(i)=ft(i)*V(i);
218  zage=m+colsum(F);
219  sage=mfexp(-zage);
220  ominus=(1.-sage);
221 
222  for(i=1;i<=ng;i++)
223  {
224  dvector t1 = elem_div(F(i),zage);
225  dvector t2 = elem_prod(t1,ominus);
226  dvector t3 = elem_prod(ominus,na);
227  ca = elem_prod(t2,na);
228 
229  ct_hat(i) = ca*wa;
230 
231  dvector t4 = ft(i)*square(V(i));
232 
233  dvector t5 = elem_div(elem_prod(elem_prod(V(i),ominus),na),zage)
234  - elem_div(elem_prod(elem_prod(t4,ominus),na),square(zage))
235  + elem_div(elem_prod(elem_prod(t4,sage),na),zage);
236  dct_hat(i) = t5*wa;
237 
238  ft(i) -= (ct_hat(i)-ctmp(i))/dct_hat(i);
239  }
240  //cout<<iter<<"\t"<<ft<<"\t"<<ct_hat-ctmp<<endl;
241  //SJDM, this algorithm does converge niceley for multiple fleets
242  }
243  //cout<<ft<<"\t\t"<<ct<<"\t\t"<<ctmp<<endl;
244  //cout<<ct<<endl;
245  return(ft);
246 }
247 
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
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
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
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 get_ft(const double &ct, const double &m, const dvector &va, const dvector &ba)
Definition: baranov.cpp:51
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
double colsum(const dmatrix &m, int col)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat27.cpp:10
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
Description not yet available.
Definition: fvar.hpp:2819
Library of statistic functions.
unsigned int size_count(const dvector &x)
Returns total size of elements in vector x.
Definition: dsize.cpp:17
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16