Behavior#
This class is the base class for all behavior classes. It contains the common methods for all behavior classes.
Constructor#
|
Base class for the behavior of the MacroStat model. |
Simulation and Behavior#
|
Forward pass of the behavior. |
|
Initialize the behavior. |
|
Step function of the behavior. |
Differentiable Operations#
|
Where condition that is differentiable with respect to the condition. |
|
Convert a variable into 0 (x<0) and 1 (x>0) |
|
Smooth approximation to the minimum B: https://mathoverflow.net/questions/35191/a-differentiable-approximation-to-the-minimum-function |
|
Smooth approximation to the minimum B: https://mathoverflow.net/questions/35191/a-differentiable-approximation-to-the-minimum-function |
|
Smooth approximation to the minimum. |
|
Smooth approximation to the maximum for a tensor. |
Parameter shocks and constraints#
During every simulation step, Behavior.apply_parameter_shocks()
applies any active scenario shocks to the parameter tensors and then
re-runs every LinearConstraint
returned by the model’s parameters. This second pass keeps adding-up
identities intact when scenarios shock free parameters in a
constrained group, and the operation is differentiable so both
autograd and finite-difference Jacobians see the residual relationship
at every timestep. See Constraints for details.
Notes#
The Behavior class is designed to be a flexible base class that allows users to implement their own model behavior while maintaining a consistent interface. The key methods that users need to implement are initialize() and step(), which define the model’s initialization and simulation behavior respectively.
Example#
A typical implementation of a behavior class might look like:
>>> class MyBehavior(Behavior):
... def initialize(self):
... # Initialize state variables
... self.state['x'] = torch.zeros(1)
...
... def step(self, t, scenario):
... # Update state variables
... self.state['x'] = self.state['x'] + 1