-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_production_model.cpp
79 lines (66 loc) · 1.95 KB
/
simple_production_model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data
DATA_VECTOR(obs_B);
DATA_VECTOR(obs_Y);
// Parameters
PARAMETER_VECTOR(logTMB_B);
PARAMETER_VECTOR(logitTMB_F);
PARAMETER(logTMB_K);
PARAMETER(logTMB_r);
PARAMETER(logsigmaF); // process error in F (random walk)
PARAMETER(logsigmaB1); // process error in B
PARAMETER(logsigmaB2); // observation error in B (assumes q=1 for survey)
PARAMETER(logsigmaY); // observation error in F
// Derived quantities
int nY = obs_B.size(); // number of observations
Type TMB_K=exp(logTMB_K);
Type TMB_r=exp(logTMB_r);
Type sigmaF=exp(logsigmaF);
Type sigmaB1=exp(logsigmaB1);
Type sigmaB2=exp(logsigmaB2);
Type sigmaY=exp(logsigmaY);
vector<Type> TMB_B=exp(logTMB_B);
vector<Type> TMB_F=invlogit(logitTMB_F);
vector<Type> pred_Y(nY-1);
vector<Type> pred_B(nY);
pred_Y.setZero();
pred_B.setZero();
Type ans=0; // likelihood
// compute time series
for(int y=1; y<nY; y++){
pred_Y(y-1) = TMB_B(y-1) * TMB_F(y-1);
pred_B(y) = TMB_B(y-1) + TMB_r * TMB_B(y-1) * (1.0 - TMB_B(y-1) / TMB_K) - pred_Y(y-1);
}
// process error in B
for(int y=1; y<nY; y++){
ans -= dnorm(log(TMB_B(y)), log(pred_B(y)), sigmaB1, true);
}
// random walk in F
for(int y=1; y<(nY-1); y++){
ans -= dnorm(logitTMB_F(y), logitTMB_F(y-1), sigmaF, true);
}
// catch likelihood
for(int y=0; y<(nY-1); y++){
ans -= dnorm(obs_Y(y), pred_Y(y), pred_Y(y)*sigmaY, true);
}
// likelihood for biomass observations (survey with q=1)
for(int y=1; y<nY; y++){
ans -= dnorm(obs_B(y), TMB_B(y), TMB_B(y)*sigmaB2, true);
}
// penalty for B0 not equal to K
ans -= dnorm(logTMB_B(0), logTMB_K, Type(0.01), true);
REPORT(pred_Y);
REPORT(pred_B);
ADREPORT(sigmaF);
ADREPORT(sigmaB1);
ADREPORT(sigmaB2);
ADREPORT(sigmaY);
ADREPORT(TMB_K);
ADREPORT(TMB_r);
ADREPORT(TMB_B);
ADREPORT(TMB_F);
return ans;
}