// bootstrap class Boot::Boot(const Data& data, Estimator* estimator, const arma::umat& rep) : data(data), n(data.n), p(data.p), rep(rep), r(rep.n_cols) { coef.set_size(p, r); fitted.set_size(n, r); resid.set_size(n, r); scale.set_size(1, r); arma::uvec indices; Data resamp; // for each replicate, construct the dataset then estimate for (int i = 0; i < r; i++) { indices = rep.col(i); resamp.x = data.x.rows(indices); resamp.y = data.y.elem(indices); (*estimator)(resamp, coef.colptr(i), fitted.colptr(i), resid.colptr(i), scale.colptr(i)); } }
double ung_ssm::bsf_filter(const unsigned int nsim, arma::cube& alpha, arma::mat& weights, arma::umat& indices) { arma::uvec nonzero = arma::find(P1.diag() > 0); arma::mat L_P1(m, m, arma::fill::zeros); if (nonzero.n_elem > 0) { L_P1.submat(nonzero, nonzero) = arma::chol(P1.submat(nonzero, nonzero), "lower"); } std::normal_distribution<> normal(0.0, 1.0); for (unsigned int i = 0; i < nsim; i++) { arma::vec um(m); for(unsigned int j = 0; j < m; j++) { um(j) = normal(engine); } alpha.slice(i).col(0) = a1 + L_P1 * um; } std::uniform_real_distribution<> unif(0.0, 1.0); arma::vec normalized_weights(nsim); double loglik = 0.0; if(arma::is_finite(y(0))) { weights.col(0) = log_obs_density(0, alpha); double max_weight = weights.col(0).max(); weights.col(0) = arma::exp(weights.col(0) - max_weight); double sum_weights = arma::accu(weights.col(0)); if(sum_weights > 0.0){ normalized_weights = weights.col(0) / sum_weights; } else { return -std::numeric_limits<double>::infinity(); } loglik = max_weight + std::log(sum_weights / nsim); } else { weights.col(0).ones(); normalized_weights.fill(1.0 / nsim); } for (unsigned int t = 0; t < n; t++) { arma::vec r(nsim); for (unsigned int i = 0; i < nsim; i++) { r(i) = unif(engine); } indices.col(t) = stratified_sample(normalized_weights, r, nsim); arma::mat alphatmp(m, nsim); for (unsigned int i = 0; i < nsim; i++) { alphatmp.col(i) = alpha.slice(indices(i, t)).col(t); } for (unsigned int i = 0; i < nsim; i++) { arma::vec uk(k); for(unsigned int j = 0; j < k; j++) { uk(j) = normal(engine); } alpha.slice(i).col(t + 1) = C.col(t * Ctv) + T.slice(t * Ttv) * alphatmp.col(i) + R.slice(t * Rtv) * uk; } if ((t < (n - 1)) && arma::is_finite(y(t + 1))) { weights.col(t + 1) = log_obs_density(t + 1, alpha); double max_weight = weights.col(t + 1).max(); weights.col(t + 1) = arma::exp(weights.col(t + 1) - max_weight); double sum_weights = arma::accu(weights.col(t + 1)); if(sum_weights > 0.0){ normalized_weights = weights.col(t + 1) / sum_weights; } else { return -std::numeric_limits<double>::infinity(); } loglik += max_weight + std::log(sum_weights / nsim); } else { weights.col(t + 1).ones(); normalized_weights.fill(1.0/nsim); } } // constant part of the log-likelihood switch(distribution) { case 0 : loglik += arma::uvec(arma::find_finite(y)).n_elem * norm_log_const(phi); break; case 1 : { arma::uvec finite_y(find_finite(y)); loglik += poisson_log_const(y(finite_y), u(finite_y)); } break; case 2 : { arma::uvec finite_y(find_finite(y)); loglik += binomial_log_const(y(finite_y), u(finite_y)); } break; case 3 : { arma::uvec finite_y(find_finite(y)); loglik += negbin_log_const(y(finite_y), u(finite_y), phi); } break; } return loglik; }
double ung_ssm::psi_filter(const ugg_ssm& approx_model, const double approx_loglik, const arma::vec& scales, const unsigned int nsim, arma::cube& alpha, arma::mat& weights, arma::umat& indices) { arma::mat alphahat(m, n + 1); arma::cube Vt(m, m, n + 1); arma::cube Ct(m, m, n + 1); approx_model.smoother_ccov(alphahat, Vt, Ct); conditional_cov(Vt, Ct); std::normal_distribution<> normal(0.0, 1.0); for (unsigned int i = 0; i < nsim; i++) { arma::vec um(m); for(unsigned int j = 0; j < m; j++) { um(j) = normal(engine); } alpha.slice(i).col(0) = alphahat.col(0) + Vt.slice(0) * um; } std::uniform_real_distribution<> unif(0.0, 1.0); arma::vec normalized_weights(nsim); double loglik = 0.0; if(arma::is_finite(y(0))) { weights.col(0) = arma::exp(log_weights(approx_model, 0, alpha) - scales(0)); double sum_weights = arma::accu(weights.col(0)); if(sum_weights > 0.0){ normalized_weights = weights.col(0) / sum_weights; } else { return -std::numeric_limits<double>::infinity(); } loglik = approx_loglik + std::log(sum_weights / nsim); } else { weights.col(0).ones(); normalized_weights.fill(1.0 / nsim); loglik = approx_loglik; } for (unsigned int t = 0; t < n; t++) { arma::vec r(nsim); for (unsigned int i = 0; i < nsim; i++) { r(i) = unif(engine); } indices.col(t) = stratified_sample(normalized_weights, r, nsim); arma::mat alphatmp(m, nsim); // for (unsigned int i = 0; i < nsim; i++) { // alphatmp.col(i) = alpha.slice(i).col(t); // } for (unsigned int i = 0; i < nsim; i++) { alphatmp.col(i) = alpha.slice(indices(i, t)).col(t); //alpha.slice(i).col(t) = alphatmp.col(indices(i, t)); } for (unsigned int i = 0; i < nsim; i++) { arma::vec um(m); for(unsigned int j = 0; j < m; j++) { um(j) = normal(engine); } alpha.slice(i).col(t + 1) = alphahat.col(t + 1) + Ct.slice(t + 1) * (alphatmp.col(i) - alphahat.col(t)) + Vt.slice(t + 1) * um; } if ((t < (n - 1)) && arma::is_finite(y(t + 1))) { weights.col(t + 1) = arma::exp(log_weights(approx_model, t + 1, alpha) - scales(t + 1)); double sum_weights = arma::accu(weights.col(t + 1)); if(sum_weights > 0.0){ normalized_weights = weights.col(t + 1) / sum_weights; } else { return -std::numeric_limits<double>::infinity(); } loglik += std::log(sum_weights / nsim); } else { weights.col(t + 1).ones(); normalized_weights.fill(1.0 / nsim); } } return loglik; }