respy.method_of_simulated_moments#

Estimate models with the method of simulated moments (MSM).

The method of simulated moments is developed by [1], [2], and [3] and an estimation technique where the distance between the moments of the actual data and the moments implied by the model parameters is minimized.

References#

[1]

McFadden, D. (1989). A method of simulated moments for estimation of discrete response models without numerical integration. Econometrica: Journal of the Econometric Society, 995-1026.

[2]

Lee, B. S., & Ingram, B. F. (1991). Simulation estimation of time-series models. Journal of Econometrics, 47(2-3), 197-205.

[3]

Duffie, D., & Singleton, K. (1993). Simulated Moments Estimation of Markov Models of Asset Prices. Econometrica, 61(4), 929-952.

Module Contents#

Functions#

get_moment_errors_func(params, options, calc_moments, ...)

Get the moment errors function for MSM estimation.

moment_errors(params, simulate, calc_moments, ...)

Loss function for MSM estimation.

get_diag_weighting_matrix(empirical_moments[, weights])

Create a diagonal weighting matrix from weights.

get_flat_moments(empirical_moments)

Compute the empirical moments flat indexes.

_harmonize_input(x)

Harmonize different types of inputs by turning all inputs into dicts.

_flatten_index(moments)

Flatten the index as a combination of the former index and the columns.

_create_comparison_plot_data_msm(empirical_moments, ...)

Create pandas.DataFrame for estimagic comparison plots.

_create_tidy_data(moments)

Create tidy data from dict containing pandas.DataFrames and/or pandas.Series.

_reconstruct_input_from_dict(x)

Reconstruct input from dict back to a list or single object.

_return_input(x)

respy.method_of_simulated_moments.get_moment_errors_func(params, options, calc_moments, replace_nans, empirical_moments, weighting_matrix=None, n_simulation_periods=None, return_scalar=True)[source]#

Get the moment errors function for MSM estimation.

Parameters:
paramspandas.DataFrame or pandas.Series

Contains parameters.

optionsdict

Dictionary containing model options.

calc_momentscallable() or list or dict

Function(s) used to calculate simulated moments. Must match structure of empirical moments i.e. if empirical_moments is a list of pandas.DataFrames, calc_moments must be a list of the same length containing functions that correspond to the moments in empirical_moments.

replace_nanscallable() or list or dict or None

Functions(s) specifying how to handle missings in simulated_moments. Must match structure of empirical_moments.

empirical_momentspandas.DataFrame or pandas.Series or dict or list

Contains the empirical moments calculated for the observed data. Moments should be saved to pandas.DataFrame or pandas.Series that can either be passed to the function directly or as items of a list or dictionary. Index of pandas.DataFrames can be of type MultiIndex, but columns cannot.

weighting_matrixnumpy.ndarray, default None

Square matrix of dimension (NxN) with N denoting the number of empirical_moments. Used to weight squared moment errors. Will use identity matrix by default.

n_simulation_periodsint, default None

Dictates the number of periods in the simulated dataset. This option does not affect options["n_periods"] which controls the number of periods for which decision rules are computed.

return_scalarbool, default True

Indicates whether to return the scalar value of weighted square product of moment error vector or dictionary that additionally contains vector of (weighted) moment errors, simulated moments that follow the structure of empirical moments, and simulated as well as empirical moments in a pandas.DataFrame that adheres to a tidy data format. The dictionary will contain the following key and value pairs:

  • “value”: Scalar vale of weighted moment errors (float)

  • “root_contributions”: Moment error vectors multiplied with root of weighting matrix (numpy.ndarray)

  • “simulated_moments”: Simulated moments for given parametrization. Will be in

the same data format as empirical_moments (pandas.Series or pandas.DataFrame or list or dict) - “comparison_plot_data”: A pandas.DataFrame that contains both empirical and simulated moments in a tidy data format (pandas.DataFrame). Data contains the following columns:

  • moment_column: Contains the column names of the moment

DataFrames/Series names. - moment_index: Contains the index of the moment DataFrames/ Series.MultiIndex indices will be joined to one string. - value: Contains moment values. - moment_set: Indicator for each set of moments, will use keys if empirical_moments are specified in a dict. Moments input as lists will be numbered according to position. - kind: Indicates whether moments are empirical or simulated.

Returns:
moment_errors_funccallable()

Function where all arguments except the parameter vector are set.

Raises:
ValueError

If replacement function cannot be broadcast (1:1 or 1:N) to simulated moments.

ValueError

If the number of functions to compute the simulated moments does not match the number of empirical moments.

respy.method_of_simulated_moments.moment_errors(params, simulate, calc_moments, replace_nans, empirical_moments, weighting_matrix, return_scalar, are_empirical_moments_dict)[source]#

Loss function for MSM estimation.

Parameters:
paramspandas.DataFrame or pandas.Series

Contains model parameters.

simulatecallable()

Function used to simulate data for MSM estimation.

calc_momentsdict

Dictionary of function(s) used to calculate simulated moments. Must match length of empirical_moments i.e. calc_moments contains a moments function for each item in empirical_moments.

replace_nansdict

Dictionary of functions(s) specifying how to handle missings in simulated_moments. Must match length of empirical_moments.

empirical_momentsdict

Contains the empirical moments calculated for the observed data. Each item in the dict constitutes a set of moments saved to a pandas.DataFrame or pandas.Series. Index of pandas.DataFrames can be of type MultiIndex, but columns cannot.

weighting_matrixnumpy.ndarray

Square matrix of dimension (NxN) with N denoting the number of empirical_moments. Used to weight squared moment errors.

return_scalarbool

Indicates whether to return the scalar value of weighted square product of moment error vector or dictionary that additionally contains vector of (root weighted) moment errors, simulated moments that follow the structure of empirical moments, and simulated as well as empirical moments in a pandas.DataFrame that adheres to a tidy data format.

are_empirical_moments_dictbool

Indicates whether empirical_moments are originally saved to a dict. Used for return of simulated moments in the same form when return_scalar is False.

Returns:
outfloat or dict
respy.method_of_simulated_moments.get_diag_weighting_matrix(empirical_moments, weights=None)[source]#

Create a diagonal weighting matrix from weights.

Parameters:
empirical_momentspandas.DataFrame or pandas.Series or dict or list

Contains the empirical moments calculated for the observed data. Moments should be saved to pandas.DataFrame or pandas.Series that can either be passed to the function directly or as items of a list or dictionary.

weightspandas.DataFrame or pandas.Series or dict or list

Contains weights (usually variances) of empirical moments. Must match structure of empirical_moments i.e. if empirical_moments is a list of pandas.DataFrames, weights must be list of pandas.DataFrames as well where each DataFrame entry contains the weight for the corresponding moment in empirical_moments.

Returns:
numpy.ndarray

Array contains a diagonal weighting matrix.

respy.method_of_simulated_moments.get_flat_moments(empirical_moments)[source]#

Compute the empirical moments flat indexes.

Parameters:
empirical_momentspandas.DataFrame or pandas.Series or dict or list

containing pandas.DataFrame or pandas.Series. Contains the empirical moments calculated for the observed data. Moments should be saved to pandas.DataFrame or pandas.Series that can either be passed to the function directly or as items of a list or dictionary.

Returns:
flat_empirical_momentspandas.DataFrame

Vector of empirical_moments with flat index.

respy.method_of_simulated_moments._harmonize_input(x)[source]#

Harmonize different types of inputs by turning all inputs into dicts.

  • pandas.DataFrames/Series and callable functions will turn into a dict containing a single item (i.e. the input).

  • Dictionaries will be left as is.

Parameters:
xpandas.DataFrame or pandas.Series or callable() or list or dict
Returns:
xdict
respy.method_of_simulated_moments._flatten_index(moments)[source]#

Flatten the index as a combination of the former index and the columns.

Parameters:
momentsdict
Returns:
pandas.DataFrame
respy.method_of_simulated_moments._create_comparison_plot_data_msm(empirical_moments, simulated_moments)[source]#

Create pandas.DataFrame for estimagic comparison plots.

Returned object contains empirical and simulated moments.

Parameters:
empirical_momentsdict
simulated_momentsdict
Returns:
pandas.DataFrame
respy.method_of_simulated_moments._create_tidy_data(moments)[source]#

Create tidy data from dict containing pandas.DataFrames and/or pandas.Series.

Parameters:
momentsdict
Returns:
pandas.DataFrame
respy.method_of_simulated_moments._reconstruct_input_from_dict(x)[source]#

Reconstruct input from dict back to a list or single object.

Parameters:
xdict
Returns:
outpandas.DataFrame or pandas.Series or callable() or list
respy.method_of_simulated_moments._return_input(x)[source]#