Optimize the RcppArmadillo code #include <RcppArmadillo.h> //…
- User
Optimize the RcppArmadillo code
#include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] #include
using namespace arma;
//' Penalty for SSDiD //' @description //' The regulazation term eta^2 for SSDiD estimator without s2. //' @param N Double. //' @param deg Double. The default value of 0.9 is proposed in the paper in Remark 3.2. //' @return Numeric. The penalty term without s2. double penalty(double N, double pen = 0.9) { return std::pow(N, -pen); }
//' Computes the base synthetic diff-in-diff or diff-in-diff estimate. //' @description //' The armadillo implementation of
base_estimatoralgorithm. //' The bottom right cell is the only cell being treated: W_it = 1. //' @param Y : Numeric arma::mat. A submatrix of outcomes with one treated obs in the bottom right corner. //' @param coh_weights : Numeric arma::vec. The vector of cohort weights. It might be the number of units in cohorts or //' the cohort weights, usually just cohort population. //' @param penalty : double. //' @param s2 : double or -1.0. The upper bound estimate of noise variance. //' @param type : std::string. Type of the estimator should besdidordid. //' //' @return NumericVector size of 2. The first element is the treatment effect, //' the second one is the asymptotic variance. // [[Rcpp::export]] Rcpp::NumericVector base_estimator( const arma::mat& Y, const arma::vec& coh_weights, double penalty, // eta^2 double s2 = -1.0, // -1.0 is a substitute for NULL std::string type = "sdid" ) { int j_c = Y.n_rows - 1; int t_c = Y.n_cols - 1;// Check dimensions if (static_cast(Y.n_rows) != static_cast(coh_weights.n_elem)) { Rcpp::stop("The number of cohorts does not coincide in Y matrix and coh_weights vector"); } if (type != "did" && type != "sdid") { Rcpp::stop("The 'type' argument should be either 'sdid' or 'did'"); }
// s2 should be either -1.0 (NULL) or positive if (!(s2 == -1.0 || s2 > 0)) { Rcpp::stop("s2 should be either NULL (-1.0) or positive"); }
double N = arma::sum(coh_weights.subvec(0, j_c - 1)); arma::vec pi = coh_weights.subvec(0, j_c - 1) / N;
// Extract Y_c (j_c x t_c) arma::mat Y_c = Y.submat(0, 0, j_c - 1, t_c - 1); arma::vec Y_j0 = Y.submat(j_c, 0, j_c, t_c - 1).t(); arma::vec Y_t0 = Y.submat(0, t_c, j_c - 1, t_c); double Y_j0_t0 = Y(j_c, t_c);
arma::vec lambda_weights, gamma_weights;
if (type == "did") { lambda_weights = arma::ones(t_c) / t_c; gamma_weights = pi; } else { arma::vec ones_t = arma::ones(t_c); arma::vec ones_j = arma::ones(j_c);
// Gamma - unit weights arma::mat Sigma_tc = diagmat(penalty / pi); // It is not exactly a cov matix // Parts of gradient for gamma optimization problem arma::vec grad_gamma1 = 2 * (Y_c * (-Y_j0)); double grad_gamma2 = 2 * arma::sum(-Y_j0); arma::vec gamma_grad = arma::zeros(j_c + 2); gamma_grad.subvec(0, j_c - 1) = grad_gamma1; gamma_grad(j_c) = grad_gamma2; gamma_grad(j_c + 1) = -1; arma::mat block_1 = 2 * Y_c * Y_c.t() + 2 * Sigma_tc; arma::vec block_2 = 2 * (Y_c * ones_t); arma::mat gamma_hess = arma::zeros(j_c + 2, j_c + 2); gamma_hess.submat(0, 0, j_c - 1, j_c - 1) = block_1; gamma_hess.submat(0, j_c, j_c - 1, j_c) = block_2; gamma_hess.submat(0, j_c + 1, j_c - 1, j_c + 1) = ones_j; gamma_hess.submat(j_c, 0, j_c, j_c - 1) = block_2.t(); gamma_hess(j_c, j_c) = 2 * t_c; gamma_hess.submat(j_c + 1, 0, j_c + 1, j_c - 1) = ones_j.t(); // This prevents the code from breaking, // forcing to use DiD when hessian of the objective function is singular. if (arma::rcond(gamma_hess) < 2.5e-16) { Rcpp::warning("The hessian for unit weights is near singular. Doing DiD unit weights."); gamma_weights = pi; } else { arma::vec solution = arma::solve(gamma_hess, -gamma_grad, solve_opts::likely_sympd); gamma_weights = solution.subvec(0, j_c - 1); } // Lambda - time weights double diag_val_jc = penalty * (1.0 / j_c) * arma::sum(1.0 / pi); arma::mat Sigma_jc = diag_val_jc * arma::eye(t_c, t_c); arma::vec grad_lambda_1 = 2 * (Y_c.t() * (-Y_t0)); double grad_lambda_2 = 2 * arma::sum(-Y_t0); arma::vec lambda_grad = arma::zeros(t_c + 2); lambda_grad.subvec(0, t_c - 1) = grad_lambda_1; lambda_grad(t_c) = grad_lambda_2; lambda_grad(t_c + 1) = -1; arma::mat blockl_1 = 2 * Y_c.t() * Y_c + 2 * Sigma_jc; arma::vec blockl_2 = 2 * (Y_c.t() * ones_j); arma::mat lambda_hess = arma::zeros(t_c + 2, t_c + 2); lambda_hess.submat(0, 0, t_c - 1, t_c - 1) = blockl_1; lambda_hess.submat(0, t_c, t_c - 1, t_c) = blockl_2; lambda_hess.submat(0, t_c + 1, t_c - 1, t_c + 1) = ones_t; lambda_hess.submat(t_c, 0, t_c, t_c - 1) = blockl_2.t(); lambda_hess(t_c, t_c) = 2 * j_c; lambda_hess.submat(t_c + 1, 0, t_c + 1, t_c - 1) = ones_t.t(); if (arma::rcond(lambda_hess) < 2.5e-16) { Rcpp::warning("The hessian for time weights is near singular. Doing DiD time weights"); lambda_weights = arma::ones<vec>(t_c) / t_c; } else { arma::vec solution = arma::solve(lambda_hess, -lambda_grad, solve_opts::likely_sympd); lambda_weights = solution.subvec(0, t_c - 1); } Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.