Brock-Mirman stochastic growth
The Brock-Mirman economy.
- Class:
BrockMirmanGrowthSystem - Import:
from skcomplex.systems.economics.macroeconomics import BrockMirmanGrowthSystem - Field:
economics/macroeconomics - Topics:
optimal growth,representative agent,markov chain,productivity shocks,discrete map - References:
- Caiani et al. 2016 §1.1.4
- Brock and Mirman 1972
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.economics.macroeconomics import BrockMirmanGrowthSystem
trajectory = BrockMirmanGrowthSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
K |
0.2 |
alpha |
0.3333333333333333 |
beta |
0.95 |
z_low |
0.9 |
z_high |
1.1 |
persistence |
0.9 |
regime |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | none |
| Laws | update |
| Randomness | unknown |
| Domains | integer, real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class BrockMirmanGrowthSystem(BaseSystem):
"""The Brock-Mirman economy.
It is the optimal growth of a representative agent whose productivity is Markov. A representative consumer maximises
`E_0 sum_t beta^t ln C_t` subject to `K_{t+1} = Y_t - C_t`, with full depreciation and the output
`Y_t = z_t K_t^alpha` of a representative firm, whose productivity `z_t` follows a Markov chain. The solution is the
linear consumption rule `C_t = (1 - alpha beta) Y_t`, `C = 0.68 Y` for `alpha = 1/3` and `beta = 0.95`, so the
capital follows `K_{t+1} = alpha beta z_t K_t^alpha` and `ln K` is a stationary autoregression around
`(ln(alpha beta) + E ln z) / (1 - alpha)`. This is the standard model that Caiani et al. (2016, Section 1.1.4, Eqs.
1.3 to 1.9) compare with an agent-based economy. The source leaves the chain open, so the productivity takes two
levels that stay with the probability `persistence`.
The system has the capital `economy.K` and the regime `economy.regime`. The observables of the system are
`economy.productivity`, `economy.output`, and `economy.consumption`.
Args:
K:
The initial capital.
alpha:
The capital share of the technology.
beta:
The discount factor of the household.
z_low:
The low productivity.
z_high:
The high productivity.
persistence:
The probability that the productivity keeps its level from one period to the next.
regime:
The initial regime, 0 for the low and 1 for the high productivity.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.economics.macroeconomics import BrockMirmanGrowthSystem
>>> BrockMirmanGrowthSystem(beta=0.9).prepare().system_.elements[0].beta
0.9
"""
field = 'economics/macroeconomics'
topics = ('optimal growth', 'representative agent', 'markov chain', 'productivity shocks', 'discrete map')
references = ('Caiani et al. 2016 §1.1.4', 'Brock and Mirman 1972')
until = 200
observables = tuple(
[
Observable(name, function, element='economy')
for name, function in (
('productivity', StochasticGrowthElement.get_productivity),
('output', StochasticGrowthElement.get_output),
('consumption', StochasticGrowthElement.get_consumption),
)
],
)
def __init__(
self,
K: float = 0.2,
*,
alpha: float = 1 / 3,
beta: float = 0.95,
z_low: float = 0.9,
z_high: float = 1.1,
persistence: float = 0.9,
regime: int = 0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.K = K
self.alpha = alpha
self.beta = beta
self.z_low = z_low
self.z_high = z_high
self.persistence = persistence
self.regime = regime
@staticmethod
def _draw_regime(regime: Expression, chain: Sequence[Sequence[float]]) -> Expression:
"""Return the next regime drawn from the row of the Markov chain that the current regime selects."""
weights = [
sum(where(regime == row, chain[row][column], 0) for row in range(len(chain)))
for column in range(len(chain[0]))
]
return categorical('productivity', weights)
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the components built from the model parameters."""
super()._prepare(context)
chain = ((self.persistence, 1 - self.persistence), (1 - self.persistence, self.persistence))
self.system_ = System(
self.label,
elements=[
StochasticGrowthElement(
'economy',
K=self.K,
regime=self.regime,
alpha=self.alpha,
beta=self.beta,
z_low=self.z_low,
z_high=self.z_high,
),
],
interactions=[
Interaction(
'saving',
roles={'target': 'economy'},
laws=lambda s: [
UpdateLaw(
s.target.K,
s.target.alpha * s.target.beta * StochasticGrowthElement.get_output(s.target),
),
],
),
Interaction(
'productivity',
roles={'target': 'economy'},
laws=lambda s: [UpdateLaw(s.target.regime, self._draw_regime(s.target.regime, chain))],
),
],
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
StochasticGrowthElement, fromElement
Interactions:
Interaction
class StochasticGrowthElement(Element):
"""Representative economy with capital and a productivity regime.
Its technology is the Cobb-Douglas `Y = z K^alpha`. The productivity `z` is `z_low` in regime 0 and `z_high` in
regime 1, and a Markov chain of the system moves the regime. With logarithmic utility and full depreciation, the
optimal consumption is `C = (1 - alpha beta) Y` and the capital of the next period is the output saved,
`K' = alpha beta z K^alpha`.
Args:
label:
The label of the economy.
K:
The capital.
regime:
The productivity regime, 0 for low and 1 for high.
alpha:
The capital share of the technology.
beta:
The discount factor of the household.
z_low:
The productivity of regime 0.
z_high:
The productivity of regime 1.
Examples:
>>> from skcomplex.systems.economics.macroeconomics import StochasticGrowthElement
>>> StochasticGrowthElement('economy', beta=0.9).beta
0.9
"""
def __init__(
self,
label: str,
*,
K: Annotated[float, RealSet.open(0, oo)] = 0.2,
regime: Annotated[float, NN] = 0.0,
alpha: Annotated[float, RealSet.open(0, 1)] = 1.0 / 3,
beta: Annotated[float, RealSet.open(0, 1)] = 0.95,
z_low: Annotated[float, RealSet.open(0, oo)] = 0.9,
z_high: Annotated[float, RealSet.open(0, oo)] = 1.1,
) -> None:
super().__init__(label)
self.K = K
self.regime = regime
self.alpha = alpha
self.beta = beta
self.z_low = z_low
self.z_high = z_high
@staticmethod
def get_productivity(s: SimpleNamespace) -> Expression:
"""Return the productivity of the current regime.
Args:
s:
The namespace of the economy.
Returns:
z:
The productivity.
"""
return s.z_low + (s.z_high - s.z_low) * s.regime
@staticmethod
def get_output(s: SimpleNamespace) -> Expression:
"""Return the output `z K^alpha`.
Args:
s:
The namespace of the economy.
Returns:
output:
The output.
"""
return StochasticGrowthElement.get_productivity(s) * s.K**s.alpha
@staticmethod
def get_consumption(s: SimpleNamespace) -> Expression:
"""Return the optimal consumption `(1 - alpha beta) Y`.
Args:
s:
The namespace of the economy.
Returns:
consumption:
The consumption.
"""
return (1 - s.alpha * s.beta) * StochasticGrowthElement.get_output(s)
class Interaction(BaseInteraction):
"""An interaction whose laws run once for the elements its roles bind.
It states the laws that change the values of the elements its roles bind, such as gravity on a ball in
`Interaction('gravity', roles={'target': 'ball'}, laws=lambda s: [RateLaw(s.target.x.diff(s.t, 2), -9.81)])`. When a
role binds a group, the laws run once for every member, as arrays. With `among` and `along`, they run once for every
edge of a network and read `s.source` and `s.target`, the members at its two ends, or once for every hyperedge or
random small group and read `s.member`, `s.others`, and `s.members`. With `within`, they run once for every pair of
members closer than a radius and read `s.first` and `s.second`. A subclass takes its roles, typed `Role`, and its
parameters as arguments of its `__init__` and sets `laws_` in its `_prepare`. It states predation between two
populations, a reaction between two chemicals, and an infection along the contacts of a network.
Read more in the user guide, under Interactions.
Args:
label:
The name of the interaction, unique among the components of its system, such as `'predation'`.
roles:
The element each role binds, by the name of the role, such as `{'target': 'ball'}`, for a rule written
without a class, or `None`.
laws:
The function of the namespace that returns the laws of a rule written without a class, such as
`lambda s: [RateLaw(s.target.n.diff(s.t), s.target.n)]`, the laws themselves when they read no element, or
`None`.
among:
The group whose members sit at the ends of the edges, such as `'people'`, or `None` to bind elements by
their roles only. A subclass annotates it as a role, and a rule written without a class binds it as one.
along:
`True` for the edges of the network the group lives on, the name of a layer of a network with several, such
as `'contacts'`, `'hyperedges'` for its hyperedges, `('hyperedges', k)` for its hyperedges of `k` members,
`RandomPartition(k)` for random groups of `k` members drawn at every step, `'neighborhoods'` for every
member with the members around it, or `None` for no edges. On the edges, the laws read the member of an
element that sits on them as `s.edge`, and on the hyperedges as `s.hyperedge`.
within:
A `RadiusNeighborhood(r)` for laws that run once for every pair of members of the roles `first` and `second`
closer than `r`, which read `s.first` and `s.second` and give the laws of both, such as the repulsion of
walkers of two crowds, or `None` for no pairs. With `among`, both members come from that group. The laws may
give the energy of the pair as `ObjectiveLaw(V)` in place of its forces, and each member then feels the
force `-dV/dx` on its position `x`.
See Also:
BaseInteraction: The base of every interaction.
RandomPartition: Groups the members of a group at random at every step.
TrainingInteraction: Trains the parameters of a nested system.
Examples:
>>> from skcomplex.core.components.interactions import Interaction
>>> from skcomplex.core.components.laws import RateLaw
>>> birth = Interaction('birth', roles={'target': 'fish'}, laws=lambda s: [RateLaw(s.target.n.diff(s.t), 1)])
>>> birth.roles
{'target': 'fish'}
"""
def __init__(
self,
label: str,
*,
roles: Mapping[str, object] | None = None,
laws: Callable[[SimpleNamespace], Sequence[BaseLaw]] | Sequence[BaseLaw] | None = None,
among: str | None = None,
along: bool | str | tuple[str, int] | RandomPartition | None = None,
within: RadiusNeighborhood | None = None,
) -> None:
super().__init__(label)
self.roles = roles
self.laws = laws
self.among = among
self.along = along
self.within = within
def _get_declared(self) -> Mapping[str, _DeclaredVariableSpec]:
"""Return the numeric arguments as parameters, and `roles` and `among` as roles.
With `within`, `among` binds both members of a pair, `first` and `second`.
"""
declared = dict(super()._get_declared())
for name, binding in (self.roles or {}).items():
declared[name] = _DeclaredVariableSpec(binding, RoleSpec())
if self.among is not None and self.within is not None:
for name in PAIR_ROLES:
declared.setdefault(name, _DeclaredVariableSpec(self.among, RoleSpec()))
elif self.among is not None and 'among' not in declared and 'among' not in self._get_specs():
declared['among'] = _DeclaredVariableSpec(self.among, RoleSpec())
return declared
@classmethod
def _get_state_arguments(cls, role: RoleSpec) -> tuple[str, ...]:
"""Return the arguments that name the states a role reads.
The mapping `roles` of bindings names none.
"""
return tuple(name for name in super()._get_state_arguments(role) if name != 'roles')
def _get_binding(self, role: str) -> object:
"""Return what a role binds, from `roles` or from the argument of its name.
Args:
role:
The name of the role.
Returns:
binding:
The label, labels, or entries the role binds, or `None` when it binds nothing.
"""
if self.roles is not None and role in self.roles:
return self.roles[role]
if self.among is not None and self.within is not None and role in PAIR_ROLES:
return self.among
return super()._get_binding(role)
def _check_arguments(self) -> None:
"""Raise an error for roles, laws, or groups the interaction cannot take.
Raises:
ValidationError:
A role of `roles` takes the name of an argument, `laws` is neither a function nor a list of laws, or
`along` names hyperedges of fewer than two members.
"""
clashes = sorted(set(self.roles or {}) & {'label', 'roles', 'laws', 'among', 'along'})
if clashes:
error_msg = f'The role {clashes[0]!r} of {self.label!r} takes the name of an argument. Rename the role.'
raise ValidationError(error_msg)
if self.laws is not None and not callable(self.laws) and not isinstance(self.laws, list | tuple):
error_msg = (
f'The laws of {self.label!r} are {self.laws!r}. Give a function that takes the bound elements as '
'`s` and returns the laws, such as `laws=lambda s: [RateLaw(s.target.n.diff(s.t), s.target.n)]`.'
)
raise ValidationError(error_msg)
if isinstance(self.along, tuple) and not self._is_sized(self.along):
error_msg = (
f'{self.label!r} runs along {self.along!r}. Give the hyperedges of one size as a whole number of 2 or '
"more, such as `along=('hyperedges', 3)`."
)
raise ValidationError(error_msg)
if isinstance(self.along, RandomPartition):
self.along._check()
grouped = isinstance(self.along, RandomPartition) or self.along == NEIGHBORHOODS
if grouped and self.among is None:
error_msg = f'{self.label!r} runs along groups of members. Name their group with `among`.'
raise ValidationError(error_msg)
super()._check_arguments()
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the laws of `laws` for a binding.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
if self.laws is not None:
self.laws_ = list(self.laws(context) if callable(self.laws) else self.laws)
@staticmethod
def _is_sized(along: tuple[object, ...]) -> bool:
"""Return whether `along` names hyperedges of a whole size of at least two."""
if len(along) != MIN_SIZE or along[0] != HYPEREDGES:
return False
size = along[1]
return isinstance(size, int) and not isinstance(size, bool) and size >= MIN_SIZE
def _get_draws(self) -> bool:
"""Return whether the interaction draws, as it does along random groups."""
return super()._get_draws() or isinstance(self.along, RandomPartition)
def _runs_on_edges(self) -> bool:
"""Return whether the interaction runs along edges."""
grouped = self._runs_on_hyperedges() or self._get_grouping() is not None
return self.along is not None and self.along is not False and not grouped
def _runs_on_hyperedges(self) -> bool:
"""Return whether the interaction runs along the hyperedges of a network."""
return self.along == HYPEREDGES or (isinstance(self.along, tuple) and self.along[0] == HYPEREDGES)
def _get_hyperedge_size(self) -> int | None:
"""Return the size `along` gives the hyperedges, or `None` for every size."""
return int(self.along[-1]) if isinstance(self.along, tuple) and self._runs_on_hyperedges() else None
def _get_within(self) -> RadiusNeighborhood | None:
"""Return the neighbourhood of the pairs the laws run for, or `None` for no pairs."""
return self.within
def _get_layer(self) -> str | None:
"""Return the layer `along` names, or `None` for no layer."""
if not isinstance(self.along, str) or self.along == NEIGHBORHOODS or self._runs_on_hyperedges():
return None
return self.along
def _get_grouping(self) -> tuple[str, int, str] | None:
"""Return the kind, size, and reduction of the groups `along` names, or `None`."""
if isinstance(self.along, RandomPartition):
return ('partition', self.along.size, 'sum')
if self.along == NEIGHBORHOODS:
return ('neighborhood', 0, 'sum')
return None
Equations
The equations are those that symbolic returns for the system with its default parameters.
economy.K: K_economy(t + 1) == ((z_high_economy - z_low_economy)*regime_economy(t) + z_low_economy)*alpha_economy*beta_economy*K_economy(t)^alpha_economy
economy.regime: regime_economy(t + 1) == where(categorical_productivity_productivity*(where(regime_economy(t) == 1, 0.9, 0) + where(regime_economy(t) == 0, 0.9, 0) + where(regime_economy(t) == 1, 0.09999999999999998, 0) + where(regime_economy(t) == 0, 0.09999999999999998, 0)) >= where(regime_economy(t) == 0, 0.9, 0) + where(regime_economy(t) == 1, 0.09999999999999998, 0), 1, 0)