Behavior#

This class is the base class for all behavior classes. It contains the common methods for all behavior classes.

Constructor#

Behavior(parameters, scenarios, variables[, ...])

Base class for the behavior of the MacroStat model.

Simulation and Behavior#

Behavior.forward()

Forward pass of the behavior.

Behavior.initialize()

Initialize the behavior.

Behavior.step(t, scenario[, params])

Step function of the behavior.

Differentiable Operations#

Behavior.diffwhere(condition, x1, x2)

Where condition that is differentiable with respect to the condition.

Behavior.tanhmask(x)

Convert a variable into 0 (x<0) and 1 (x>0)

Behavior.diffmin(x1, x2)

Smooth approximation to the minimum B: https://mathoverflow.net/questions/35191/a-differentiable-approximation-to-the-minimum-function

Behavior.diffmax(x1, x2)

Smooth approximation to the minimum B: https://mathoverflow.net/questions/35191/a-differentiable-approximation-to-the-minimum-function

Behavior.diffmin_v(x)

Smooth approximation to the minimum.

Behavior.diffmax_v(x)

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