diff --git a/matlab/ms-sbvar/ms_estimation.m b/matlab/ms-sbvar/ms_estimation.m index c591f4f98..81873741f 100644 --- a/matlab/ms-sbvar/ms_estimation.m +++ b/matlab/ms-sbvar/ms_estimation.m @@ -68,6 +68,6 @@ mexErrCheck('ms_estimation', err); [options_, oo_] = set_ms_estimation_file(options_, oo_); [err, oo_.ms.A0, oo_.ms.Aplus, oo_.ms.Zeta, oo_.ms.Q] = ... - mex_ms_convert_free_parameters(options_.ms.estimation_file_tag, oo_.ms.maxparams); + mex_ms_convert_free_parameters({{'file_tag',options_.ms.estimation_file_tag}}, oo_.ms.maxparams); mexErrCheck('mex_ms_convert_free_parameters', err); end diff --git a/matlab/ms-sbvar/ms_forecast.m b/matlab/ms-sbvar/ms_forecast.m index 07c08fde4..8993e3395 100644 --- a/matlab/ms-sbvar/ms_forecast.m +++ b/matlab/ms-sbvar/ms_forecast.m @@ -39,30 +39,36 @@ clean_files_for_second_type_of_mex(M_, options_, 'forecast') forecastdir = [options_.ms.output_file_tag filesep 'Forecast']; create_dir(forecastdir); -opt = {options_.ms.estimation_file_tag, ... - 'seed', options_.DynareRandomStreams.seed, ... - 'horizon', options_.ms.horizon, ... - 'number_observations',options_.ms.forecast_data_obs ... - 'error_bands', options_.ms.error_bands, ... - 'percentiles', options_.ms.error_band_percentiles, ... - 'thin', options_.ms.thinning_factor }; +opt = { ... + {'file_tag', options_.ms.estimation_file_tag}, ... + {'seed', options_.DynareRandomStreams.seed}, ... + {'horizon', options_.ms.horizon}, ... + {'number_observations', options_.ms.forecast_data_obs}, ... + {'error_bands', options_.ms.error_bands}, ... + {'percentiles', options_.ms.error_band_percentiles}, ... + {'thin', options_.ms.thinning_factor} + }; -[err, forecast] = mex_ms_forecast(opt{:},'free_parameters',oo_.ms.maxparams,'shocks_per_parameter', options_.ms.shock_draws); +[err, forecast] = mex_ms_forecast([opt(:)', {{'free_parameters',oo_.ms.maxparams}, ... + {'shocks_per_parameter', options_.ms.shock_draws}}]); mexErrCheck('mex_ms_forecast ergodic ', err); plot_ms_forecast(M_,options_,forecast,'Forecast',options_.graph_save_formats,options_.TeX); -[err, regime_forecast] = mex_ms_forecast(opt{:},'free_parameters',oo_.ms.maxparams,'shocks_per_parameter', options_.ms.shock_draws,'regimes'); +[err, regime_forecast] = mex_ms_forecast([opt(:)', {{'free_parameters',oo_.ms.maxparams}, ... + {'shocks_per_parameter', options_.ms.shock_draws}, {'regimes'}}]); mexErrCheck('mex_ms_forecast ergodic regimes', err); save([forecastdir filesep 'ergodic_forecast.mat'], 'forecast', 'regime_forecast'); if exist(options_.ms.mh_file,'file') > 0 - [err, forecast] = mex_ms_forecast(opt{:},'free_parameters',oo_.ms.maxparams,'shocks_per_parameter', options_.ms.shocks_per_parameter, ... - 'simulation_file',options_.ms.mh_file,'parameter_uncertainty'); + [err, forecast] = mex_ms_forecast([opt(:)', {{'free_parameters',oo_.ms.maxparams}, ... + {'shocks_per_parameter', options_.ms.shocks_per_parameter}, ... + {'simulation_file', options_.ms.mh_file}, {'parameter_uncertainty'}}]); mexErrCheck('mex_ms_forecast bayesian ', err); plot_ms_forecast(M_,options_,forecast,'Forecast w/ Parameter Uncertainty',options_.graph_save_formats,options_.TeX); - [err, regime_forecast] = mex_ms_forecast(opt{:},'free_parameters',oo_.ms.maxparams,'shocks_per_parameter', options_.ms.shocks_per_parameter, ... - 'simulation_file',options_.ms.mh_file,'parameter_uncertainty','regimes'); + [err, regime_forecast] = mex_ms_forecast([opt(:)', {{'free_parameters',oo_.ms.maxparams}, ... + {'shocks_per_parameter', options_.ms.shocks_per_parameter}, ... + {'simulation_file', options_.ms.mh_file}, {'parameter_uncertainty','regimes'}}]); mexErrCheck('mex_ms_forecast bayesian regimes ', err); save([forecastdir filesep 'bayesian_forecast.mat'], 'forecast', 'regime_forecast'); end diff --git a/matlab/ms-sbvar/ms_irf.m b/matlab/ms-sbvar/ms_irf.m index a3fa4c730..17fb87f26 100644 --- a/matlab/ms-sbvar/ms_irf.m +++ b/matlab/ms-sbvar/ms_irf.m @@ -40,19 +40,21 @@ clean_files_for_second_type_of_mex(M_, options_, 'irf') irfdir = [options_.ms.output_file_tag filesep 'IRF']; create_dir(irfdir); -opt = {options_.ms.estimation_file_tag, ... - 'seed', options_.DynareRandomStreams.seed, ... - 'horizon', options_.ms.horizon, ... - 'filtered', options_.ms.filtered_probabilities, ... - 'error_bands', options_.ms.error_bands, ... - 'percentiles', options_.ms.error_band_percentiles, ... - 'thin', options_.ms.thinning_factor }; +opt = { ... + {'file_tag', options_.ms.estimation_file_tag}, ... + {'seed', options_.DynareRandomStreams.seed}, ... + {'horizon', options_.ms.horizon}, ... + {'filtered', options_.ms.filtered_probabilities}, ... + {'error_bands', options_.ms.error_bands}, ... + {'percentiles', options_.ms.error_band_percentiles}, ... + {'thin', options_.ms.thinning_factor} + }; -[err, irf] = mex_ms_irf(opt{:}, 'free_parameters', oo_.ms.maxparams, 'shocks_per_parameter', options_.ms.shock_draws); +[err, irf] = mex_ms_irf([opt(:)', {{'free_parameters', oo_.ms.maxparams}, {'shocks_per_parameter', options_.ms.shock_draws}}]); mexErrCheck('mex_ms_irf ergodic ', err); plot_ms_irf(M_,options_,irf,options_.varobs,'Ergodic Impulse Responses',varlist); -[err, regime_irfs] = mex_ms_irf(opt{:}, 'free_parameters',oo_.ms.maxparams,'shocks_per_parameter', options_.ms.shock_draws,'regimes'); +[err, regime_irfs] = mex_ms_irf([opt(:)', {{'free_parameters',oo_.ms.maxparams}, {'shocks_per_parameter', options_.ms.shock_draws}, {'regimes'}}]); mexErrCheck('mex_ms_irf ergodic regimes ',err); for i=1:size(regime_irfs,1) plot_ms_irf(M_,options_,squeeze(regime_irfs(i,:,:,:)),options_.varobs,['Ergodic ' ... @@ -61,13 +63,13 @@ end save([irfdir filesep 'ergodic_irf.mat'], 'irf', 'regime_irfs'); if exist(options_.ms.mh_file,'file') > 0 - [err, irf] = mex_ms_irf(opt{:}, 'shocks_per_parameter', options_.ms.shocks_per_parameter, ... - 'parameter_uncertainty','simulation_file',options_.ms.mh_file); + [err, irf] = mex_ms_irf([opt(:)', {{'shocks_per_parameter', options_.ms.shocks_per_parameter}, ... + {'parameter_uncertainty'},{'simulation_file',options_.ms.mh_file}}]); mexErrCheck('mex_ms_irf bayesian ',err); plot_ms_irf(M_,options_,irf,options_.varobs,'Impulse Responses with Parameter Uncertainty',varlist); - [err, regime_irfs] = mex_ms_irf(opt{:}, 'shocks_per_parameter', options_.ms.shocks_per_parameter, ... - 'simulation_file',options_.ms.mh_file,'parameter_uncertainty','regimes'); + [err, regime_irfs] = mex_ms_irf([opt(:)', {{'shocks_per_parameter', options_.ms.shocks_per_parameter}, ... + {'simulation_file',options_.ms.mh_file},{'parameter_uncertainty'},{'regimes'}}]); mexErrCheck('mex_ms_irf bayesian regimes ',err); for i=1:size(regime_irfs,1) plot_ms_irf(M_,options_,squeeze(regime_irfs(i,:,:,:)),options_.varobs,['Impulse ' ... diff --git a/matlab/ms-sbvar/ms_variance_decomposition.m b/matlab/ms-sbvar/ms_variance_decomposition.m index 74a36e54e..f0a89ca3c 100644 --- a/matlab/ms-sbvar/ms_variance_decomposition.m +++ b/matlab/ms-sbvar/ms_variance_decomposition.m @@ -41,28 +41,34 @@ create_dir(vddir); % NOTICE THAT VARIANCE DECOMPOSITION DEFAULTS TO USING THE MEAN, NOT MEDIAN OR BANDED -opt = {options_.ms.estimation_file_tag, ... - 'seed', options_.DynareRandomStreams.seed, ... - 'horizon', options_.ms.horizon, ... - 'filtered', options_.ms.filtered_probabilities, ... - 'error_bands', options_.ms.error_bands, ... - 'percentiles', options_.ms.error_band_percentiles, ... - 'thin', options_.ms.thinning_factor, ... - 'mean'}; +opt = { + {'file_tag', options_.ms.estimation_file_tag}, ... + {'seed', options_.DynareRandomStreams.seed}, ... + {'horizon', options_.ms.horizon}, ... + {'filtered', options_.ms.filtered_probabilities}, ... + {'error_bands', options_.ms.error_bands}, ... + {'percentiles', options_.ms.error_band_percentiles}, ... + {'thin', options_.ms.thinning_factor}, ... + {'mean'} ... + }; -[err, vd] = mex_ms_variance_decomposition(opt{:},'free_parameters',oo_.ms.maxparams,'shocks',options_.ms.shock_draws); +[err, vd] = mex_ms_variance_decomposition([opt(:)', {{'free_parameters',oo_.ms.maxparams}, ... + {'shocks', options_.ms.shock_draws}}]); mexErrCheck('mex_ms_variance_decomposition ergodic ', err); plot_ms_variance_decomposition(M_,options_,vd, 'Ergodic Variance Decomposition',options_.graph_save_formats,options_.TeX); -[err, regime_vd] = mex_ms_variance_decomposition(opt{:},'free_parameters',oo_.ms.maxparams,'shocks',options_.ms.shock_draws,'regimes'); +[err, regime_vd] = mex_ms_variance_decomposition([opt(:)', {{'free_parameters',oo_.ms.maxparams}, ... + {'shocks', options_.ms.shock_draws}, {'regimes'}}]); mexErrCheck('mex_ms_variance_decomposition ergodic regimes', err); save([vddir filesep 'ergodic_vd.mat'], 'vd', 'regime_vd'); if exist(options_.ms.mh_file,'file') > 0 - [err, vd] = mex_ms_variance_decomposition(opt{:},'simulation_file',options_.ms.mh_file,'shocks',options_.ms.shocks_per_parameter,'parameter_uncertainty'); + [err, vd] = mex_ms_variance_decomposition([opt(:)', {{'simulation_file',options_.ms.mh_file}, ... + {'shocks', options_.ms.shocks_per_parameter}, {'parameter_uncertainty'}}]); mexErrCheck('mex_ms_variance_decomposition bayesian ', err); - [err, regime_vd] = mex_ms_variance_decomposition(opt{:},'simulation_file',options_.ms.mh_file,'shocks',options_.ms.shocks_per_parameter,'parameter_uncertainty','regimes'); + [err, regime_vd] = mex_ms_variance_decomposition([opt(:)', {{'simulation_file',options_.ms.mh_file}, ... + {'shocks', options_.ms.shocks_per_parameter}, {'parameter_uncertainty'}, {'regimes'}}]); mexErrCheck('mex_ms_variance_decomposition bayesian regimes ', err); save([vddir filesep 'bayesian_vd.mat'], 'vd', 'regime_vd'); end diff --git a/mex/sources/ms-sbvar/mex_ms_convert_free_parameters.cc b/mex/sources/ms-sbvar/mex_ms_convert_free_parameters.cc index 6e2b55846..89d846df4 100644 --- a/mex/sources/ms-sbvar/mex_ms_convert_free_parameters.cc +++ b/mex/sources/ms-sbvar/mex_ms_convert_free_parameters.cc @@ -37,9 +37,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { - using namespace std; - - char *input_buf; double *free_parameters; int nvars, npre, nstates, nfree; double *aplus = NULL, *a0 = NULL, *zeta = NULL, *q = NULL; @@ -48,23 +45,18 @@ mexFunction(int nlhs, mxArray *plhs[], SbvarOption *options = NULL; /* input must be a string */ - if (mxIsChar(prhs[0]) != 1) - DYN_MEX_FUNC_ERR_MSG_TXT("First argument has to be a string to the init_filename."); + if (nrhs !=2) + DYN_MEX_FUNC_ERR_MSG_TXT("This function takes exactly two arguments"); if (!mxIsDouble(prhs[1])) DYN_MEX_FUNC_ERR_MSG_TXT("Second argument is a vector of free parameters"); if (nlhs < 4) DYN_MEX_FUNC_ERR_MSG_TXT("You must specify at least four output arguments [err,A0,Aplus,Zeta]"); - // copy the string data from prhs[0] into a C string input_ buf. */ - input_buf = mxArrayToString(prhs[0]); - if (input_buf == NULL) - DYN_MEX_FUNC_ERR_MSG_TXT("Could not convert input to string."); - // second element should be vector of free parameters */ free_parameters = mxGetPr(prhs[1]); - model = initialize_model_and_options(input_buf, &options, nrhs, prhs, &nstates, &nvars, &npre, &nfree); + model = initialize_model_and_options(&options, prhs, &nstates, &nvars, &npre, &nfree); if (model == NULL || options == NULL) DYN_MEX_FUNC_ERR_MSG_TXT("There was a problem initializing the model, can not continue"); diff --git a/mex/sources/ms-sbvar/mex_ms_forecast.cc b/mex/sources/ms-sbvar/mex_ms_forecast.cc index 08d5eabbe..5a27ca3a4 100644 --- a/mex/sources/ms-sbvar/mex_ms_forecast.cc +++ b/mex/sources/ms-sbvar/mex_ms_forecast.cc @@ -36,10 +36,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { - - using namespace std; - - char *input_buf; double *out_buf; int i, j, k, s, nfree, nstates, nvars, npre, T; @@ -52,16 +48,12 @@ mexFunction(int nlhs, mxArray *plhs[], int type = F_FREE; // Check the left hand and right hand side arguments to make sure they conform - if (mxIsChar(prhs[0]) != 1) - DYN_MEX_FUNC_ERR_MSG_TXT("First argument has to be a string to the init_filename."); + if (nrhs != 1) + DYN_MEX_FUNC_ERR_MSG_TXT("ms_forecast takes one cell array as an input argument."); if (nlhs != 2) DYN_MEX_FUNC_ERR_MSG_TXT("You must specify two output arguments."); - // copy the string data from prhs[0] into a C string input_ buf. */ - input_buf = mxArrayToString(prhs[0]); - if (input_buf == NULL) - DYN_MEX_FUNC_ERR_MSG_TXT("Could not convert input to string."); - model = initialize_model_and_options(input_buf, &options, nrhs, prhs, &nstates, &nvars, &npre, &nfree); + model = initialize_model_and_options(&options, prhs, &nstates, &nvars, &npre, &nfree); if (model == NULL || options == NULL) DYN_MEX_FUNC_ERR_MSG_TXT("There was a problem initializing the model, can not continue"); diff --git a/mex/sources/ms-sbvar/mex_ms_irf.cc b/mex/sources/ms-sbvar/mex_ms_irf.cc index fe0131562..ada450487 100644 --- a/mex/sources/ms-sbvar/mex_ms_irf.cc +++ b/mex/sources/ms-sbvar/mex_ms_irf.cc @@ -36,8 +36,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { - - char *input_buf; double *out_buf; int i, j, k, s, nfree, nstates, nvars, npre; @@ -50,16 +48,13 @@ mexFunction(int nlhs, mxArray *plhs[], int type = F_FREE, ergodic = 1; // Check the left hand and right hand side arguments to make sure they conform - if (mxIsChar(prhs[0]) != 1) - DYN_MEX_FUNC_ERR_MSG_TXT("First argument has to be a string to the init_filename."); + if (nrhs != 1) + DYN_MEX_FUNC_ERR_MSG_TXT("ms_irf takes one cell array as an input argument."); if (nlhs != 2) DYN_MEX_FUNC_ERR_MSG_TXT("You must specify two output arguments."); - // copy the string data from prhs[0] into a C string input_ buf. */ - input_buf = mxArrayToString(prhs[0]); - if (input_buf == NULL) - DYN_MEX_FUNC_ERR_MSG_TXT("Could not convert input to string."); - model = initialize_model_and_options(input_buf, &options, nrhs, prhs, &nstates, &nvars, &npre, &nfree); + // copy the string data from prhs[0] into a C string input_ buf. + model = initialize_model_and_options(&options, prhs, &nstates, &nvars, &npre, &nfree); if (model == NULL || options == NULL) DYN_MEX_FUNC_ERR_MSG_TXT("There was a problem initializing the model, can not continue"); diff --git a/mex/sources/ms-sbvar/mex_ms_sbvar.cc b/mex/sources/ms-sbvar/mex_ms_sbvar.cc index da2706c81..9d27590a3 100644 --- a/mex/sources/ms-sbvar/mex_ms_sbvar.cc +++ b/mex/sources/ms-sbvar/mex_ms_sbvar.cc @@ -348,205 +348,182 @@ initialize_sbvar_options(char *file_tag) } int -set_options(SbvarOption *options, int nrhs, const mxArray *prhs[]) +set_options(SbvarOption *options, const mxArray *prhs[]) { if (options == NULL) options = initialize_sbvar_options((char *) NULL); - int i, buf_len, buf_len2; - char *input_buf = NULL; double *temp_buf; bool shocks_passed = false; - - /* deal with optional arguments */ - for (i = 0; i < nrhs; i++) + int num_options = mxGetN(prhs[0]); + for (int i = 1; i < num_options; i++) { - if (mxIsChar(prhs[i]) == 1) + mxArray *this_option = mxGetCell(prhs[0],i); + char *option_name_c = mxArrayToString(mxGetCell(this_option,0)); + string option_name (option_name_c); + mxArray *this_option_value = NULL; + if (mxGetN(this_option) > 1) + this_option_value = mxGetCell(this_option,1); + + if (option_name == "horizon") + if (this_option_value && mxIsNumeric(this_option_value)) + { + temp_buf = (double *) mxGetData(this_option_value); + options->horizon = (int) temp_buf[0]; + } + else + { + cout << "You must pass an integer after specifying the 'horizon' option" << endl; + return 1; + } + else if (option_name == "filtered") + options->filtered_probabilities = true; + else if (option_name == "error_bands") { - buf_len = mxGetN(prhs[i])*sizeof(mxChar)+1; - input_buf = (char *) mxMalloc(buf_len); - mxGetString(prhs[i], input_buf, buf_len); + free(options->percentiles); + options->num_percentiles = 3; + options->percentiles = new double[3]; + options->percentiles[0] = 0.16; + options->percentiles[1] = 0.5; + options->percentiles[2] = 0.84; - if (strstr(input_buf, "horizon")) + // Check if the user specified to turn off error bands + if (this_option_value && mxIsNumeric(this_option_value)) { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) + temp_buf = (double *) mxGetData(this_option_value); + if (temp_buf[0] == 0) { - temp_buf = (double *) mxGetData(prhs[i+1]); - options->horizon = (int) temp_buf[0]; - } - else - { - printf("You must pass an integer after specifying the 'horizon' option"); - return 1; + options->num_percentiles = 1; + options->percentiles = new double[1]; + options->percentiles[0] = 0.50; } } - else if (strstr(input_buf, "filtered")) - options->filtered_probabilities = true; - else if (strstr(input_buf, "error_bands")) + } + else if (option_name == "median") + { + free(options->percentiles); + options->num_percentiles = 1; + options->percentiles = new double[1]; + options->percentiles[0] = 0.5; + } + else if (option_name == "percentiles") + if (this_option_value) + { + options->num_percentiles = mxGetN(this_option_value) + > mxGetM(this_option_value) ? mxGetN(this_option_value) + : mxGetM(this_option_value); + options->percentiles = mxGetPr(this_option_value); + } + else + { + cout << "You must pass a vector after the 'percentiles' argument with the " + << "percentiles that you want to have computed, ex " + << "'percentiles',[.16 .5 .84]" << endl; + return 1; + } + else if (option_name == "parameter_uncertainty") + { + options->parameter_uncertainty = true; + if (shocks_passed == false) + options->shocks = 1; + } + else if (option_name == "shocks" || option_name == "shocks_per_parameter") + if (this_option_value && mxIsNumeric(this_option_value)) + { + temp_buf = (double *) mxGetData(this_option_value); + options->shocks = (int) temp_buf[0]; + shocks_passed = true; + } + else + { + cout << "You must pass an integer after specifying the 'shocks' option" << endl; + return 1; + } + else if (option_name == "thin") + if (this_option_value && mxIsNumeric(this_option_value)) + { + temp_buf = (double *) mxGetData(this_option_value); + options->thin = (int) temp_buf[0]; + } + else + { + cout << "You must pass an integer after specifying the 'thin' option" << endl; + return 1; + } + else if (option_name == "simulation_file") + { + char *posterior_filename = mxArrayToString(this_option_value); + strcpy(options->simulation_filename, posterior_filename); + if (!(options->simulation_file = fopen(posterior_filename, "rt"))) { - free(options->percentiles); - options->num_percentiles = 3; - options->percentiles = new double[3]; - options->percentiles[0] = 0.16; - options->percentiles[1] = 0.5; - options->percentiles[2] = 0.84; - - // Check if the specified to turn off error bands - if (nrhs > i+1) - if (mxIsNumeric(prhs[i+1])) - { - temp_buf = (double *) mxGetData(prhs[i+1]); - if (temp_buf[0] == 0) - { - options->num_percentiles = 1; - options->percentiles = new double[1]; - options->percentiles[0] = 0.50; - } - } - - } - else if (strstr(input_buf, "median")) - { - free(options->percentiles); - options->num_percentiles = 1; - options->percentiles = new double[1]; - options->percentiles[0] = 0.5; - - } - else if (strstr(input_buf, "percentiles")) - { - if (nrhs >= i+1) - { - options->num_percentiles = mxGetN(prhs[i+1]) > mxGetM(prhs[i+1]) ? mxGetN(prhs[i+1]) : mxGetM(prhs[i+1]); - options->percentiles = mxGetPr(prhs[i+1]); - } - else - { - printf("You must pass a vector after the 'percentiles' argument with the percentiles that you want to have computed, ex 'percentiles',[.16 .5 .84]"); - return 1; - - } - - } - else if (strstr(input_buf, "parameter_uncertainty")) - { - options->parameter_uncertainty = true; - if (shocks_passed == false) - options->shocks = 1; - - } - else if (strstr(input_buf, "shocks") || strstr(input_buf, "shocks_per_parameter")) - { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) - { - temp_buf = (double *) mxGetData(prhs[i+1]); - options->shocks = (int) temp_buf[0]; - shocks_passed = true; - } - else - { - printf("You must pass an integer after specifying the 'shocks' option"); - return 1; - } - - } - else if (strstr(input_buf, "thin")) - { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) - { - temp_buf = (double *) mxGetData(prhs[i+1]); - options->thin = (int) temp_buf[0]; - } - else - { - printf("You must pass an integer after specifying the 'thin' option"); - return 1; - } - - } - else if (strstr(input_buf, "simulation_file")) - { - buf_len2 = mxGetN(prhs[i+1])*sizeof(mxChar)+1; - char *posterior_filename = (char *) mxMalloc(buf_len2); - mxGetString(prhs[i+1], posterior_filename, buf_len2); - - strcpy(options->simulation_filename, posterior_filename); - - if (!(options->simulation_file = fopen(posterior_filename, "rt"))) - { - printf("The posterior simulation file does not exist"); - return 1; - } - - } - else if (strstr(input_buf, "regimes")) - { - options->regimes = true; - - } - else if (strstr(input_buf, "regime")) - { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) - { - temp_buf = (double *) mxGetData(prhs[i+1]); - options->regime = (int) temp_buf[0]; - } - else - { - printf("You must pass an integer after specifying the 'regime' option, or alternatively you can specify 'regimes'"); - return 1; - } - - } - else if (strstr(input_buf, "number_observations")) - { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) - { - temp_buf = (double *) mxGetData(prhs[i+1]); - options->number_observations = (int) temp_buf[0]; - } - else - { - printf("You must pass an integer after specifying the 'regime' option, or alternatively you can specify 'regimes'"); - return 1; - } - } - else if (strstr(input_buf, "free_parameters")) - { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) - { - options->num_parameters = mxGetM(prhs[i+1]); - options->free_parameters = mxGetPr(prhs[i+1]); - } - else - { - printf("You must pass a vector of free parameters after specifying 'free_parameters'"); - return 1; - } - - } - else if (strstr(input_buf, "mean")) - { - options->mean = true; - options->num_percentiles = 0; - options->percentiles = (double *) NULL; - - } - else if (strstr(input_buf, "seed")) - { - if (nrhs >= i+1 && mxIsNumeric(prhs[i+1])) - { - temp_buf = (double *) mxGetData(prhs[i+1]); - options->seed = (long) temp_buf[0]; - } - else - { - printf("You must pass an integer after specifying the 'seed' option"); - return 1; - } + cout << "Can not open posterior file " << options->simulation_file + << " for reading. " << endl; + return 1; } + mxFree(posterior_filename); + } + else if (option_name == "regimes") + options->regimes = true; + else if (option_name == "regime") + if (this_option_value && mxIsNumeric(this_option_value)) + { + temp_buf = (double *) mxGetData(this_option_value); + options->regime = (int) temp_buf[0]; + } + else + { + cout << "You must pass an integer after specifying the 'regime' " + << "option, or alternatively you can specify 'regimes'" << endl; + return 1; + } + else if (option_name == "number_observations") + if (this_option_value && mxIsNumeric(this_option_value)) + { + temp_buf = (double *) mxGetData(this_option_value); + options->number_observations = (int) temp_buf[0]; + } + else + { + cout << "You must pass an integer after specifying the " + << "'number_observations' option" << endl; + return 1; + } + else if (option_name == "free_parameters") + if (this_option_value && mxIsNumeric(this_option_value)) + { + options->num_parameters = mxGetM(this_option_value); + options->free_parameters = mxGetPr(this_option_value); + } + else + { + cout << "You must pass a vector of free parameters after " + << "specifying 'free_parameters'" << endl; + return 1; + } + else if (option_name == "mean") + { + options->mean = true; + options->num_percentiles = 0; + options->percentiles = (double *) NULL; } + else if (option_name == "seed") + if (this_option_value && mxIsNumeric(this_option_value)) + { + temp_buf = (double *) mxGetData(this_option_value); + options->seed = (long) temp_buf[0]; + } + else + { + cout << "You must pass an integer after specifying the 'seed' option" << endl; + return 1; + } + else + { + cout << "set_options error: option '" << option_name << "' not matched" << endl; + return 1; + } + mxFree(option_name_c); } // End Optional Arguments return 0; } @@ -583,16 +560,14 @@ print_sbvar_options(SbvarOption *options) } TStateModel * -initialize_model_and_options(char *name, SbvarOption **options, int nrhs, const mxArray *prhs[], int *nstates, int *nvars, int *npre, int *nfree) +initialize_model_and_options(SbvarOption **options, const mxArray *prhs[], int *nstates, int *nvars, int *npre, int *nfree) { - - using namespace std; - + char *name; TStateModel *model = (TStateModel *) NULL; - int ret; SbvarOption *opt; // Initialize the StateSpace Model with the initialization file + name = mxArrayToString(mxGetCell(mxGetCell(prhs[0],0),1)); model = initialize_ms_model(name); if (model == NULL) { @@ -607,7 +582,8 @@ initialize_model_and_options(char *name, SbvarOption **options, int nrhs, const // Process the rest of the options opt = initialize_sbvar_options(name); - if (set_options(opt, nrhs, prhs) > 0) + mxFree(name); + if (set_options(opt, prhs) > 0) { cout << "There was a problem with the options passed." << endl; return (TStateModel *) NULL; diff --git a/mex/sources/ms-sbvar/mex_ms_sbvar.h b/mex/sources/ms-sbvar/mex_ms_sbvar.h index 11703a23c..edca7a296 100644 --- a/mex/sources/ms-sbvar/mex_ms_sbvar.h +++ b/mex/sources/ms-sbvar/mex_ms_sbvar.h @@ -61,13 +61,13 @@ typedef struct sbvar_options_t { int file_exist(char *filename); char* CreateFilenameFromTag(const char *fmt,const char *tag,const char *dir); SbvarOption * initialize_sbvar_options(char *file_tag); -int set_options(SbvarOption *options, int nrhs, const mxArray *prhs[]); +int set_options(SbvarOption *options, const mxArray *prhs[]); int print_sbvar_options(SbvarOption *options); TStateModel * initialize_ms_model(char *filename); int get_var_dimensions(TStateModel *model, int *nstates, int *nvars, int *npre, int *nfree); int set_parameters_in_VAR(TStateModel *model, double *free_parameters); int convert_free_parameters_to_VAR(TStateModel *model, double *free_parameters, double *A0, double *Aplus, double *Zeta, double *Q ); -TStateModel * initialize_model_and_options(char *name,SbvarOption **options, int nrhs, const mxArray *prhs[], int *nstates, int *nvars, int *npre, int *nfree); +TStateModel * initialize_model_and_options(SbvarOption **options, const mxArray *prhs[], int *nstates, int *nvars, int *npre, int *nfree); #endif -#endif \ No newline at end of file +#endif diff --git a/mex/sources/ms-sbvar/mex_ms_variance_decomposition.cc b/mex/sources/ms-sbvar/mex_ms_variance_decomposition.cc index 9d8e813a6..2ef5f5d8b 100644 --- a/mex/sources/ms-sbvar/mex_ms_variance_decomposition.cc +++ b/mex/sources/ms-sbvar/mex_ms_variance_decomposition.cc @@ -35,8 +35,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { - - char *input_buf; double *out_buf; int i, j, k, s, nfree, nstates, nvars, npre; @@ -49,16 +47,12 @@ mexFunction(int nlhs, mxArray *plhs[], int type = F_FREE, ergodic = 1; /* Check the left hand and right hand side arguments to make sure they conform */ - if (mxIsChar(prhs[0]) != 1) - DYN_MEX_FUNC_ERR_MSG_TXT("First argument has to be a string to the init_filename."); + if (nrhs != 1) + DYN_MEX_FUNC_ERR_MSG_TXT("ms_variance_decomposition takes one cell array as an input argument."); if (nlhs != 2) DYN_MEX_FUNC_ERR_MSG_TXT("You must specify two output arguments."); - // copy the string data from prhs[0] into a C string input_ buf. */ - input_buf = mxArrayToString(prhs[0]); - if (input_buf == NULL) - DYN_MEX_FUNC_ERR_MSG_TXT("Could not convert input to string."); - model = initialize_model_and_options(input_buf, &options, nrhs, prhs, &nstates, &nvars, &npre, &nfree); + model = initialize_model_and_options(&options, prhs, &nstates, &nvars, &npre, &nfree); if (model == NULL || options == NULL) DYN_MEX_FUNC_ERR_MSG_TXT("There was a problem initializing the model, can not continue");