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_msm_func(params, options, calc_moments, replace_nans, empirical_moments, weighting_matrix, n_simulation_periods=None, return_scalar=True, return_simulated_moments=False, return_comparison_plot_data=False)

Get the MSM function.

msm(params, simulate, calc_moments, replace_nans, empirical_moments, weighting_matrix, return_scalar, return_simulated_moments, return_comparison_plot_data)

Loss function for MSM estimation.

get_diag_weighting_matrix(empirical_moments, weights=None)

Create a diagonal weighting matrix from weights.

get_flat_moments(empirical_moments)

Compute the empirical moments flat indexes.

_harmonize_input(data)

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

_flatten_index(data)

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

_create_comparison_plot_data_msm(empirical_moments, simulated_moments, moment_set_labels)

Create pandas.DataFrame for estimagic’s comparison plot.

_create_tidy_data(data, moment_set_labels)

Create tidy data from list of pandas.DataFrames.

_reconstruct_inputs(inputs, dict_keys=None)

Reconstruct inputs from lists back to a dictionary or single object.

respy.method_of_simulated_moments.get_msm_func(params, options, calc_moments, replace_nans, empirical_moments, weighting_matrix, n_simulation_periods=None, return_scalar=True, return_simulated_moments=False, return_comparison_plot_data=False)[source]

Get the MSM function.

Parameters
paramspandas.DataFrame or pandas.Series

Contains parameters.

optionsdict

Dictionary containing model options.

calc_momentscallable() or list

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

Functions(s) specifying how to handle missings in simulated_moments. Must match structure of empirical_moments. Exception: If only one replacement function is specified, it will be used on all sets of simulated 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

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

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 moment error vector (False) or weighted square product of moment error vectors (True).

return_simulated_moments: bool, default False

Indicates whether simulated moments should be returned with other output. If True will return simulated moments of the same type as empirical_moments.

return_comparison_plot_data: bool, default False

Indicator for whether a pandas.DataFrame with empirical and simulated moments for the visualization with estimagic should be returned. 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
——-
msm_func: callable

MSM function where all arguments except the parameter vector are set.

respy.method_of_simulated_moments.msm(params, simulate, calc_moments, replace_nans, empirical_moments, weighting_matrix, return_scalar, return_simulated_moments, return_comparison_plot_data)[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_momentslist

List 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_nanslist

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

empirical_momentslist

Contains the empirical moments calculated for the observed data. Each item in the list 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 moment error vector (False) or weighted square product of moment error vector (True).

return_simulated_moments: bool

Indicates whether simulated moments should be returned with other output. If True will return simulated moments of the same type as empirical_moments.

return_comparison_plot_data: list

Will output moments in a tidy data format if True. Expects a list as input where the first element is a boolean indicating whether to return the comparison plot data. The second element in the list can be a list of keys used to identify sets of moments which otherwise will be numbered.

Returns
outpandas.Series or float or tuple

Scalar or moment error vector depending on value of return_scalar. Will be a tuple containing simulated moments of same type as empirical_moments or a tidy pandas.DataFrame if either return_simulated_moments or the first element in return_comparison_plot_data is True.

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 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(data)[source]

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

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

  • Dictionaries will be sorted according to keys and then turn into a list containing the dictionary entries.

respy.method_of_simulated_moments._flatten_index(data)[source]

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

respy.method_of_simulated_moments._create_comparison_plot_data_msm(empirical_moments, simulated_moments, moment_set_labels)[source]

Create pandas.DataFrame for estimagic’s comparison plot.

respy.method_of_simulated_moments._create_tidy_data(data, moment_set_labels)[source]

Create tidy data from list of pandas.DataFrames.

respy.method_of_simulated_moments._reconstruct_inputs(inputs, dict_keys=None)[source]

Reconstruct inputs from lists back to a dictionary or single object.