26Type
posfun(Type x, Type eps, Type &penalty) {
29 Type ans = CppAD::CondExpGe(x, eps, x, eps/denom);
30 penalty += CppAD::CondExpGe(x, eps, Type(0), 0.01 * (x - eps) * (x - eps));
177 matrix<Type> ay_Index,
185 int total_n = n_years * n_ages;
187 Eigen::SparseMatrix<Type> B(total_n,total_n);
188 Eigen::SparseMatrix<Type> I(total_n,total_n);
190 Eigen::SparseMatrix<Type> Omega(total_n,total_n);
191 Eigen::SparseMatrix<Type> Q_sparse(total_n, total_n);
193 for(
int n = 0; n < total_n; n++) {
195 Type age = ay_Index(n,0);
196 Type year = ay_Index(n,1);
202 for(
int n1 = 0; n1 < total_n; n1++) {
203 if(ay_Index(n1, 0) == age - 1 && ay_Index(n1, 1) == year)
204 B.coeffRef(n, n1) = rho_y;
212 for(
int n1 = 0; n1 < total_n; n1++) {
213 if(ay_Index(n1, 0) == age && ay_Index(n1, 1) == year - 1)
214 B.coeffRef(n, n1) = rho_a;
219 if(year > 1 && age > 1) {
222 for(
int n1 = 0; n1 < total_n; n1++) {
223 if(ay_Index(n1, 0) == age - 1 && ay_Index(n1, 1) == year - 1)
224 B.coeffRef(n,n1) = rho_c;
234 for(
int i = 0; i < total_n; i++) {
235 for(
int j = 0; j < total_n; j++) {
236 if(i == j) Omega.coeffRef(i,j) = 1/exp(log_sigma2);
237 else Omega.coeffRef(i,j) = Type(0.0);
245 matrix<Type> L(total_n, total_n);
246 matrix<Type> tmp_I_B = I-B;
247 L = tmp_I_B.inverse();
248 vector<Type> d(total_n);
250 for(
int n = 0; n < total_n; n++) {
252 d(n) = exp(log_sigma2);
257 for(
int n1 = 0; n1 < n; n1++) {
258 cumvar += L(n, n1) * d(n1) * L(n, n1);
262 d(n) = (exp(log_sigma2) - cumvar) / pow(L(n, n), 2);
267 for(
int i = 0; i < total_n; i++) {
268 for(
int j = 0; j < total_n; j++) {
269 if(i == j) Omega.coeffRef(i,j) = 1/d(i);
270 else Omega.coeffRef(i,j) = Type(0.0);
276 Eigen::SparseMatrix<Type> B_transpose = B.transpose();
278 Q_sparse = (I - B_transpose) * Omega * (I-B);
Eigen::SparseMatrix< Type > construct_Q(int n_years, int n_ages, matrix< Type > ay_Index, Type rho_y, Type rho_a, Type rho_c, Type log_sigma2, int Var_Param)
Construct a sparse GMRF precision matrix separable along year, age, and cohort.