-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeavervalidation.cpp
47 lines (38 loc) · 1.43 KB
/
beavervalidation.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
// This file serves as the C++ code for implementation of the beaver example in TMB
// Model and Data: Reynolds, P.S. (1994) Time-series analyses of beaver body temperatures.
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator()()
{
// Data Declarations
DATA_VECTOR(Y); // Temperatue Observations
DATA_VECTOR_INDICATOR(keep, Y); // For one-step predictions
DATA_FACTOR(act); // Indicator for activities,
// 1 for beaver outside of the retreat (high-intensity activity)
// 0 otherwise
// Parameter Declarations
PARAMETER(logitPhi);
PARAMETER(logSdState);
PARAMETER(logSdObs);
PARAMETER_VECTOR(mu);
PARAMETER_VECTOR(X);
// Parameter Transformations
Type phi = Type(2.0) / (Type(1.0) + exp(-logitPhi)) - 1.0;
Type sdState = exp(logSdState);
Type sdObs = exp(logSdObs);
// Initialization of Negative Log Likelihood (nll)
Type nll = 0.0;
// Update nll
nll -= dnorm(X(0), mu(act(0)), sdState/sqrt(1.0-pow(phi,2.0)), true);
// Update nll looping over states X
for(int i = 1; i < X.size(); ++i)
nll -= dnorm(X(i),mu(act(i)) + phi*(X(i-1) - mu(act(i-1))), sdState, true);
// Update nll looping over observations Y
for(int i = 0; i < Y.size(); ++i)
nll -= keep(i) * dnorm(Y(i), X(i), sdObs, true); // For one-step prediction
// Make parameters available to R (for report)
ADREPORT(phi);
ADREPORT(sdState);
ADREPORT(sdObs);
return nll;
}