macrostat.models package¶
Submodules¶
macrostat.models.model module¶
Generic model class as a wrapper to specific implementations
- class macrostat.models.model.Model(parameters: dict, hyper_parameters: dict, name: str = 'model')[source]¶
Bases:
objectA class representing a macroeconomic model.
This class provides a wrapper for users to write their underlying model behavior while maintaining a uniformly accessible interface. Specifically, the user is expected to adapt the model.simulate() function to their needs, respecting only that the return of that function is a pandas dataframe.
- output¶
None, or the latest simulation run for given parameters
- Type:
pd.DataFrame
Example
A general workflow for a model might look like
>>> model = Model(parameters, hyper_parameters) >>> output = model.simulate() >>> model.save()
- classmethod load(path=None)[source]¶
Class method to load a model instance from a pickled file.
- Parameters:
path – path to the targeted Sampler
optional – path to the targeted Sampler
Notes
Note
This implementation is dependent on your pickling version
- save(path=None)[source]¶
Save the model object as a pickled file
- Parameters:
path – path where the model will be stored. If it is None then the model’s name will be used and the file stored in the working directory.
optional – path where the model will be stored. If it is None then the model’s name will be used and the file stored in the working directory.
Notes
Note
This implementation is dependent on your pickling version
- simulate(*args, **kwargs) DataFrame[source]¶
Simulate a model run using the stored parameters
This function is designed to be overwritten by the user’s specific implementation of their model. Note that it is expected for the user to set the ‘’self.output’’ attribute to the output generated.
- Returns:
output – Output of the model. Generically it should have a “time”-like index and variables across the columns
- Return type:
pd.DataFrame
macrostat.models.torchmodel module¶
Generic model class for models implemented using PyTorch
- class macrostat.models.torchmodel.TorchModel(parameters: dict = None, hyper_parameters: dict = None, name: str = 'torchmodel')[source]¶
Bases:
Model,ModuleGeneric model class for models implemented using PyTorch
This class provides a wrapper for users to write their underlying model behavior while maintaining a uniformly accessible interface. Specifically, the user is expected to adapt the model.forward() function to their needs. The use of PyTorch allows for automatic differentiation which has computational advantages compared to finite differences.
- output¶
None, or the latest simulation run for given parameters
- Type:
pd.DataFrame
Example
A general workflow for a model might look like
>>> model = Model(parameters, hyper_parameters) >>> output = model.simulate() >>> model.save()
- diffmax(x1, x2)[source]¶
Smooth approximation to the minimum B: https://mathoverflow.net/questions/35191/a-differentiable-approximation-to-the-minimum-function
- Requires:
self.hyper_parameters[‘max_constant’] as a large number
- diffmax_v(x)[source]¶
Smooth approximation to the maximum for a tensor. See diffmax
- Requires:
self.hyper_parameters[‘max_constant’] as a large number
- diffmin(x1, x2)[source]¶
Smooth approximation to the minimum B: https://mathoverflow.net/questions/35191/a-differentiable-approximation-to-the-minimum-function
- Requires:
self.hyper_parameters[‘min_constant’] as a large number
- diffmin_v(x)[source]¶
Smooth approximation to the minimum. See diffmin
- Requires:
self.hyper_parameters[‘min_constant’] as a large number
- diffwhere(condition, x1, x2)[source]¶
Where condition that is differentiable with respect to the condition.
- Requires:
self.hyper_parameters[‘diffwhere’] = True self.hyper_parameters[‘sigmoid_constant’] as a large number
Note: For non-NaN/inf, where(x > eps, z, y) is (x - eps > 0) * (z - y) + y, so we can use the sigmoid function to approximate the where function.
- Parameters:
condition (torch.Tensor) – Condition to be evaluated expressed as x - eps
x1 (torch.Tensor) – Value to be returned if condition is True
x2 (torch.Tensor) – Value to be returned if condition is False
- forward(*args, **kwargs)[source]¶
Run the model forward through time, e.g. the loop over timesteps goes here.
This method is called by the simulation method and by the pytorch autograd system. Generally, it shouldn’t be called directly by the user.
- initialize_simulation()[source]¶
Initialize the model’s state variables before running the simulation For instance, if one wants to load in a model state to start from.
- simulate() DataFrame[source]¶
Simulate a model run using the stored parameters
This function is designed to be overwritten by the user’s specific implementation of their model. Note that it is expected for the user to set the ‘’self.output’’ attribute to the output generated.
The function will run ‘’self.initialize_simulation’’ to set up the model and then run the forward pass of the model. Furthermore, in the pure simulation case, we omit the gradient calculation.
- Returns:
output – Output of the model. Generically it should have a “time”-like index and variables across the columns
- Return type:
pd.DataFrame