forked from TsaiCH/Fishery-GPEDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpEDMtmb_v5_linearscaling.cpp
53 lines (52 loc) · 1.2 KB
/
gpEDMtmb_v5_linearscaling.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
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_VECTOR(y);
DATA_MATRIX(xcpue);
DATA_MATRIX(xcatch);
DATA_SCALAR(ysigma2);
PARAMETER_VECTOR(logW);
PARAMETER(logtau);
PARAMETER(logg);
PARAMETER(logb);
//
vector<Type> W = exp(logW);
Type b = exp(logb);
//
Type tau = (ysigma2-0.001)/(1+exp(-logtau))+0.001;
Type g = (ysigma2-0.001)/(1+exp(-logg))+0.001;
REPORT(ysigma2);
//
matrix<Type> X(xcpue.rows(),xcpue.cols());
X = xcpue-(xcatch*b);
//
vector<Type> v1(X.cols());
vector<Type> v2(X.cols());
matrix<Type> K(X.rows(),X.rows());
//
for(int i=0; i<K.rows(); i++) {
for(int j=0; j<K.rows(); j++) {
v1 = X.row(i)-X.row(j);
v1 = v1.abs();
v2 = (W*v1)*(W*v1);
K(i,j) = tau*exp(-sum(v2));
}
}
matrix<Type> G(K);
G.fill(0.0);
vector<Type> gvec(K.rows());
gvec.fill(g);
G.diagonal() = gvec;
K = K+G;
Type nll = density::MVNORM_t<Type>(K)(y);
REPORT(K);
vector<Type> v3 = W*W;
Type v4 = (-0.5)*sum(v3)/(3.14159/2);
Type v5 = dbeta(tau/(ysigma2*1.01),Type(1.1),Type(1.1),true);
Type v6 = dbeta(g/(ysigma2*1.01),Type(1.1),Type(1.1),true);
nll -= v4;
nll -= v5;
nll -= v6;
return nll;
}