template <class Type>
Type f(Type x)
{
return Type(2) / (Type(1) + exp(-Type(2) * x)) - Type(1);
}
template <class Type>
Type objective_function<Type>::operator()()
{
int timeSteps = obs.dim[1];
int stateDim = obs.dim[0];
Type rho = f(transf_rho);
Type rhoObs = f(transf_rhoObs);
for (int i = 0; i < stateDim; i++)
for (int j = 0; j < stateDim; j++) {
cov(i, j) = pow(rho, Type(abs(i - j))) * sds[i] * sds[j];
covObs(i, j) = pow(rhoObs, Type(abs(i - j))) * sdObs[i] * sdObs[j];
}
Type ans = 0;
Type huge = 10;
for (
int i = 0; i < stateDim; i++) ans -=
dnorm(u(i, 0), Type(0), huge,
true);
for (int i = 1; i < timeSteps; i++)
ans += neg_log_density(u.col(i) - u.col(i - 1));
for (int i = 0; i < timeSteps; i++) {
ans += neg_log_densityObs(obs.col(i) - u.col(i),
keep.col(i));
}
return ans;
}
License: