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));
181 matrix<Type> ay_Index,
189 int total_n = n_years * n_ages;
191 Eigen::SparseMatrix<Type> B(total_n,total_n);
192 Eigen::SparseMatrix<Type> I(total_n,total_n);
194 Eigen::SparseMatrix<Type> Omega(total_n,total_n);
195 Eigen::SparseMatrix<Type> Q_sparse(total_n, total_n);
197 for(
int n = 0; n < total_n; n++) {
199 Type age = ay_Index(n,0);
200 Type year = ay_Index(n,1);
206 for(
int n1 = 0; n1 < total_n; n1++) {
207 if(ay_Index(n1, 0) == age - 1 && ay_Index(n1, 1) == year)
208 B.coeffRef(n, n1) = rho_y;
216 for(
int n1 = 0; n1 < total_n; n1++) {
217 if(ay_Index(n1, 0) == age && ay_Index(n1, 1) == year - 1)
218 B.coeffRef(n, n1) = rho_a;
223 if(year > 1 && age > 1) {
226 for(
int n1 = 0; n1 < total_n; n1++) {
227 if(ay_Index(n1, 0) == age - 1 && ay_Index(n1, 1) == year - 1)
228 B.coeffRef(n,n1) = rho_c;
238 for(
int i = 0; i < total_n; i++) {
239 for(
int j = 0; j < total_n; j++) {
240 if(i == j) Omega.coeffRef(i,j) = 1/exp(log_sigma2);
241 else Omega.coeffRef(i,j) = Type(0.0);
249 matrix<Type> L(total_n, total_n);
250 matrix<Type> tmp_I_B = I-B;
251 L = tmp_I_B.inverse();
252 vector<Type> d(total_n);
254 for(
int n = 0; n < total_n; n++) {
256 d(n) = exp(log_sigma2);
261 for(
int n1 = 0; n1 < n; n1++) {
262 cumvar += L(n, n1) * d(n1) * L(n, n1);
266 d(n) = (exp(log_sigma2) - cumvar) / pow(L(n, n), 2);
271 for(
int i = 0; i < total_n; i++) {
272 for(
int j = 0; j < total_n; j++) {
273 if(i == j) Omega.coeffRef(i,j) = 1/d(i);
274 else Omega.coeffRef(i,j) = Type(0.0);
280 Eigen::SparseMatrix<Type> B_transpose = B.transpose();
282 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.