/** * Generate random samples chosen from the multivariate Gaussian * distribution with mean MU and covariance SIGMA. * * X ~ N(u, Lambda) => Y = B * X + v ~ N(B * u + v, B * Lambda * B') * Therefore, if X ~ N(0, Lambda), * then Y = B * X + MU ~ N(MU, B * Lambda * B'). * We only need to do the eigen decomposition: SIGMA = B * Lambda * B'. * * @param MU a 1D {@code double} array holding the mean vector * * @param d dimensionality of the mean vector * * @param SIGMA a 2D {@code double} array holding the covariance matrix * * @param cases number of d dimensional random samples * * @return cases-by-d sample matrix subject to the multivariate * Gaussian distribution N(MU, SIGMA) * */ Matrix& mvnrnd(double* MU, int d, double** SIGMA, int cases) { return mvnrnd(*new DenseMatrix(MU, d, 2), *new DenseMatrix(SIGMA, d, d), cases); }
bool estimation_init(float param[], float husbands_2[][HUSBANDS_COL], float husbands_3[][HUSBANDS_COL], float husbands_4[][HUSBANDS_COL], float husbands_5[][HUSBANDS_COL], float wives[][WIVES_COL], float wage_moments[][WAGE_MOM_COL], float marr_fer_moments[][MARR_MOM_COL], float emp_moments[][EMP_MOM_COL], float general_moments[][GEN_MOM_COL], float* epsilon_b_arr, float* epsilon_f_arr, float* h_draws_b_arr, float* w_draws_b_arr, uint16_t* w_ability_draw_arr, float* h_draws_arr, float* w_draws_arr) { timeval tv = tic(); std::cout << "Loading files..." << std::endl; if (!load<HUSBANDS_COL>("husbands_2.out", husbands_2, HUSBANDS_ROWS)) { return false; } if (!load<HUSBANDS_COL>("husbands.out", husbands_3, HUSBANDS_ROWS)) { return false; } if (!load<HUSBANDS_COL>("husbands_4.out", husbands_4, HUSBANDS_ROWS)) { return false; } if (!load<HUSBANDS_COL>("husbands_5.out", husbands_5, HUSBANDS_ROWS)) { return false; } if (!load<WIVES_COL>("wives.out", wives, WIVES_ROWS)) { return false; } if (!load<WAGE_MOM_COL>("wage_moments.txt", wage_moments, WAGE_MOM_ROWS)) { return false; } if (!load<MARR_MOM_COL>("marr_fer_moments.txt", marr_fer_moments, MARR_MOM_ROWS)) { return false; } if (!load<EMP_MOM_COL>("emp_moments.txt", emp_moments, EMP_MOM_ROWS)) { return false; } if (!load<GEN_MOM_COL>("general_moments.txt", general_moments, GEN_MOM_ROWS)) { return false; } if (!load<INIT_PARAM_COL>("init_parameters.txt", param, INIT_PARAM_ROWS)) { return false; } // some validations on override parameters if (PARAM_ROWS != PARAM_IDX_ROWS || INIT_PARAM_ROWS < PARAM_IDX_ROWS) { std::cerr << "Invalid number of override parameters (#initial=" << INIT_PARAM_ROWS << ",#override=" << PARAM_ROWS << ",#index=" << PARAM_IDX_ROWS << ")" << std::endl; std::cerr << "Not overriding initial parameters" << std::endl; } else { float override_param[PARAM_ROWS]; if (!load<PARAM_COL>("parameters.txt", override_param, PARAM_ROWS)) { std::cerr << "Not overriding initial parameters" << std::endl; } else { uint16_t override_index[PARAM_IDX_ROWS]; if (!load<PARAM_IDX_COL, uint16_t>("parameters_idx.txt", override_index, PARAM_IDX_ROWS)) { std::cerr << "Not overriding initial parameters" << std::endl; } else { for (uint16_t i = 0; i < PARAM_ROWS; ++i) { if (override_index[i] < INIT_PARAM_ROWS) { param[override_index[i]] = override_param[i]; } else { std::cerr << "Invalid parameter index " << override_index[i] << " at row " << i << ", parameter override is skipped" << std::endl; } } } } } print_toc(tv); tv = tic(); std::cout << "Initializing random shocks..." << std::endl; // start with the same random shoks srand(1234); // random shocks' mean and variance const float myu = 0; const float sig = 1; // random draw from normal distribution, for EMAX calculation for (uint16_t i = 0; i < DRAW_B; ++i) { for (uint16_t j = 0; j < T_MAX; ++j) { for (uint16_t k = 0; k < SCHOOL_GROUPS; ++k) { for (uint16_t s = 0; s < 2; ++s) { epsilon_b(i,j,k,s) = mvnrnd(myu, sig); } } } } // TODO PERF reorder the nested loops and put draws in inner most and merge the F with the B loops // random draw from normal distribution, for shocks realizations in forward solution for (uint16_t i = 0; i < DRAW_F; ++i) { for (uint16_t j = 0; j < T_MAX; ++j) { for (uint16_t k = 0; k < SCHOOL_GROUPS; ++k) { for (uint16_t s = 0; s < 2; ++s) { epsilon_f(i,j,k,s) = mvnrnd(myu, sig); } } } } // random draw from uniform distribution, for backward solution for (uint16_t i = 0; i < DRAW_B; ++i) { for (uint16_t j = 0; j < T_MAX; ++j) { for (uint16_t k = 0; k < SCHOOL_GROUPS; ++k) { // 0-HUSBAND; 1-HUSBAND EXP+SCHOOLING; 2-HUSBAND ABILITY; 3-INITIAL MATCH QUALITY; h_draws_b(i,j,k,h_b_HUSBAND) = rand01(); h_draws_b(i,j,k,h_b_HUSBAND_EXP_SCHOOLING) = rand01(); h_draws_b(i,j,k,h_b_HUSBAND_ABILITY) = (float)randi(MAX_ABILITY_IDX); h_draws_b(i,j,k,h_b_INITIAL_MATCH_QUALITY) = (float)randi(MAX_Q_IDX); } } } for (uint16_t i = 0; i < DRAW_B; ++i) { for (uint16_t j = 0; j < T_MAX; ++j) { for (uint16_t k = 0; k < SCHOOL_GROUPS; ++k) { // 0- wife; 1-wife EXP+SCHOOLING+PREV_STATE; 2-wife ABILITY; 3-INITIAL MATCH QUALITY; 4-JOB OFFER FOR MARRIED MEN AND WOMEN EMAX; 5-JOB OFFER FOR SINGLE MEN EMAX w_draws_b(i,j,k,w_b_WIFE) = rand01(); w_draws_b(i,j,k,WIFE_EXP_SCHOOLING_PREV_STATE) = rand01(); w_draws_b(i,j,k,WIFE_ABILITY) = (float)randi(MAX_ABILITY_IDX); w_draws_b(i,j,k,w_b_INITIAL_MATCH_QUALITY) = (float)randi(MAX_Q_IDX); w_draws_b(i,j,k,JOB_OFFER_MARRIED_MEN_WOMEN) = rand01(); w_draws_b(i,j,k,JOB_OFFER_SINGLE_MEN) = rand01(); } } } // random draw from uniform distribution, for forward solution for (uint16_t i = 0; i < DRAW_F; ++i) { for (uint16_t k = 0; k < SCHOOL_GROUPS; ++k) { // wife ability draw, constant by period w_ability_draw(i,k) = randi(MAX_ABILITY_IDX); } } for (uint16_t i = 0; i < DRAW_F; ++i) { for (uint16_t j = 0; j < T_MAX; ++j) { for (uint16_t k = 0; k < SCHOOL_GROUPS; ++k) { // 0- HUSBAND; 1-HUSBAND EXP+SCHOOLING; 2-HUSBAND ABILITY; 3-INITIAL MATCH QUALITY; 4-MATCH UALITY CHANGE PROBABILITY for (uint16_t s = 0; s < 2; ++s) { h_draws(i,j,k,h_HUSBAND) = rand01(); h_draws(i,j,k,h_HUSBAND_EXP_SCHOOLING) = rand01(); h_draws(i,j,k,h_HUSBAND_ABILITY) = (float)randi(MAX_ABILITY_IDX); h_draws(i,j,k,h_INITIAL_MATCH_QUALITY) = (float)randi(MAX_Q_IDX); h_draws(i,j,k,MATCH_UALITY_CHANGE_PROBABILITY) = rand01(); } } } } for (uint16_t i = 0; i < DRAW_F; ++i) { for (uint8_t j = 0; j < T_MAX; ++j) { for (uint8_t k = 0; k < SCHOOL_GROUPS; ++k) { for (uint8_t s = 0; s < 2; ++s) { // 0 - JOB OFFER, 1 - NEW BORN w_draws(i,j,k,s) = rand01(); } } } } print_toc(tv); return true; }