respy
#
This is the entry-point to the respy package.
Include only imports which should be available using
import respy as rp
rp.<func>
Subpackages#
Submodules#
Package Contents#
Functions#
|
Return parameters, options and data (optional) of an example model. |
|
Get parameter constraints for the estimation compatible with estimagic. |
|
Get the criterion function for maximum likelihood estimation. |
|
Create a diagonal weighting matrix from weights. |
|
Compute the empirical moments flat indexes. |
|
Get the moment errors function for MSM estimation. |
|
Get the simulation function. |
|
Get the solve function. |
- respy.get_example_model(model, with_data=True)[source]#
Return parameters, options and data (optional) of an example model.
- Parameters:
- model
str
Choose one model name in
{"robinson_crusoe_basic", "robinson_crusoe_extended", kw_94_one", "kw_94_two", "kw_94_three", "kw_97_basic", "kw_97_extended" "kw_2000"}
.- with_databool
Whether the accompanying data set should be returned. For some data sets, real data can be provided, for others, a simulated data set will be produced.
- model
- respy.get_parameter_constraints(model)[source]#
Get parameter constraints for the estimation compatible with estimagic.
For more information, see the documentation of estimagic.
- Parameters:
- model
str
Choose one model name in
{"robinson_crusoe_basic", "robinson_crusoe_extended", kw_94_one", "kw_94_two", "kw_94_three", "kw_97_basic", "kw_97_extended" "kw_2000"}
.
- model
- Returns:
Examples
>>> constr = rp.get_parameter_constraints("robinson_crusoe_basic") >>> constr [{'loc': 'shocks_sdcorr', 'type': 'sdcorr'}]
- respy.get_log_like_func(params, options, df, return_scalar=True)[source]#
Get the criterion function for maximum likelihood estimation.
Return a version of the likelihood functions in respy where all arguments except the parameter vector are fixed with
functools.partial()
. Thus the function can be directly passed into an optimizer or a function for taking numerical derivatives.- Parameters:
- params
pandas.DataFrame
DataFrame containing model parameters.
- options
dict
Dictionary containing model options.
- df
pandas.DataFrame
The model is fit to this dataset.
- return_scalarbool, default
False
Indicator for whether the mean log likelihood should be returned. If False will return a dictionary with the following key and value pairs: - “value”: mean log likelihood (float) - “contributions”: log likelihood contributions (numpy.array) - “comparison_plot_data” : DataFrame with various contributions for the visualization with estimagic. Data contains the following columns:
identifier
: Individual identifiers derived from input df.period
: Periods derived from input df.choice
: Choice thatvalue
is connected to.value
: Value of log likelihood contribution.kind
: Kind of contribution (e.g choice or wage).type
and log_type_probability`: Will be included in models with
types.
- params
- Returns:
- criterion_function
log_like()
Criterion function where all arguments except the parameter vector are set.
- criterion_function
- Raises:
AssertionError
If data has not the expected format.
Examples
>>> import respy as rp >>> params, options, data = rp.get_example_model("robinson_crusoe_basic")
At default the function returns the log likelihood as a scalar value.
>>> log_like = rp.get_log_like_func(params=params, options=options, df=data) >>> scalar = log_like(params)
Alternatively, a dictionary containing the log likelihood, as well as log likelihood contributions and a
pandas.DataFrame
can be returned.>>> log_like = rp.get_log_like_func(params=params, options=options, df=data, ... return_scalar=False ... ) >>> outputs = log_like(params) >>> outputs.keys() dict_keys(['value', 'contributions', 'comparison_plot_data'])
- respy.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.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.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.get_simulate_func(params, options, method='n_step_ahead_with_sampling', df=None, n_simulation_periods=None)[source]#
Get the simulation function.
Return
simulate()
where all arguments except the parameter vector are fixed withfunctools.partial()
. Thus, the function can be directly passed into an optimizer for estimation with simulated method of moments or other techniques.- Parameters:
- params
pandas.DataFrame
DataFrame containing model parameters.
- options
dict
Dictionary containing model options.
- method{“n_step_ahead_with_sampling”, “n_step_ahead_with_data”, “one_step_ahead”}
The simulation method which can be one of three and is explained in more detail in
simulate()
.- df
pandas.DataFrame
orNone
, defaultNone
DataFrame containing one or multiple observations per individual.
- n_simulation_periods
int
orNone
, defaultNone
Simulate data for a number of periods. This options does not affect
options["n_periods"]
which controls the number of periods for which decision rules are computed.
- params
- Returns:
- simulate_function
simulate()
Simulation function where all arguments except the parameter vector are set.
- simulate_function
Examples
>>> import respy as rp >>> params, options = rp.get_example_model("robinson_crusoe_basic", with_data=False) >>> simulate = rp.get_simulate_func(params, options) >>> data = simulate(params)
- respy.get_solve_func(params, options)[source]#
Get the solve function.
This function takes a model specification and returns the state space of the model along with components of the solution such as covariates, non-pecuniary rewards, wages, continuation values and expected value functions as attributes of the class.
- Parameters:
- params
pandas.DataFrame
DataFrame containing parameter series.
- options
dict
Dictionary containing model attributes which are not optimized.
- params
- Returns:
- solve
solve()
Function with partialed arguments.
- solve
Examples
>>> import respy as rp >>> params, options = rp.get_example_model("robinson_crusoe_basic", with_data=False) >>> solve = rp.get_solve_func(params, options) >>> state_space = solve(params)