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#
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.
Lee, B. S., & Ingram, B. F. (1991). Simulation estimation of time-series models. Journal of Econometrics, 47(2-3), 197-205.
Duffie, D., & Singleton, K. (1993). Simulated Moments Estimation of Markov Models of Asset Prices. Econometrica, 61(4), 929-952.
Module Contents#
Functions#
|
Get the moment errors function for MSM estimation. |
|
Loss function for MSM estimation. |
|
Create a diagonal weighting matrix from weights. |
|
Compute the empirical moments flat indexes. |
Harmonize different types of inputs by turning all inputs into dicts. |
|
|
Flatten the index as a combination of the former index and the columns. |
|
Create pandas.DataFrame for estimagic comparison plots. |
|
Create tidy data from dict containing pandas.DataFrames and/or pandas.Series. |
Reconstruct input from dict back to a list or single object. |
|
- 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:
- params
pandas.DataFrame
orpandas.Series
Contains parameters.
- options
dict
Dictionary containing model options.
- calc_moments
callable()
orlist
ordict
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_nans
callable()
orlist
ordict
orNone
Functions(s) specifying how to handle missings in simulated_moments. Must match structure of empirical_moments.
- empirical_moments
pandas.DataFrame
orpandas.Series
ordict
orlist
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_matrix
numpy.ndarray
, defaultNone
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_periods
int
, defaultNone
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.
- params
- Returns:
- moment_errors_func
callable()
Function where all arguments except the parameter vector are set.
- moment_errors_func
- 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:
- params
pandas.DataFrame
orpandas.Series
Contains model parameters.
- simulate
callable()
Function used to simulate data for MSM estimation.
- calc_moments
dict
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_nans
dict
Dictionary of functions(s) specifying how to handle missings in simulated_moments. Must match length of empirical_moments.
- empirical_moments
dict
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_matrix
numpy.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.
- params
- Returns:
- respy.method_of_simulated_moments.get_diag_weighting_matrix(empirical_moments, weights=None)[source]#
Create a diagonal weighting matrix from weights.
- Parameters:
- empirical_moments
pandas.DataFrame
orpandas.Series
ordict
orlist
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.
- weights
pandas.DataFrame
orpandas.Series
ordict
orlist
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.
- 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_moments
pandas.DataFrame
orpandas.Series
ordict
orlist
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.
- empirical_moments
- Returns:
- flat_empirical_moments
pandas.DataFrame
Vector of empirical_moments with flat index.
- flat_empirical_moments
- 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:
- x
pandas.DataFrame
orpandas.Series
orcallable()
orlist
ordict
- x
- Returns:
- x
dict
- x
- respy.method_of_simulated_moments._flatten_index(moments)[source]#
Flatten the index as a combination of the former index and the columns.
- Parameters:
- moments
dict
- moments
- Returns:
- 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:
- Returns:
- respy.method_of_simulated_moments._create_tidy_data(moments)[source]#
Create tidy data from dict containing pandas.DataFrames and/or pandas.Series.
- Parameters:
- moments
dict
- moments
- Returns:
- respy.method_of_simulated_moments._reconstruct_input_from_dict(x)[source]#
Reconstruct input from dict back to a list or single object.
- Parameters:
- x
dict
- x
- Returns:
- out
pandas.DataFrame
orpandas.Series
orcallable()
orlist
- out