Skip to content

Engine

The core simulation engine. The simulate() function is the main entry point for running Monte Carlo simulations.

SimulationResult

SimulationResult dataclass

Output of a simulation run.

terminal_inflation = field(default=None, repr=False) class-attribute instance-attribute

Per-path cumulative inflation factor at the final step, shape (n_paths,).

Exposed so callers can deflate any nominal series the engine returns. Deflating the published percentiles instead does not work: the factor varies by path, so the median of the real values is not the median nominal divided by the median inflation.

terminal_wealth_real_percentiles = field(default_factory=dict) class-attribute instance-attribute

Terminal wealth in today's dollars.

terminal_wealth_percentiles is nominal at end_age, which over a multi-decade horizon is a much larger number than the same wealth in present terms. Reporting only the nominal figure invites it to be read as though it were real.

retirement_step property

Index of the first retired month in the time series (0 if the plan starts retired).

Use this rather than recomputing (retirement_age - current_age) * 12, which is negative for a plan that starts in retirement.

simulate

simulate(plan, market, policies, sim_config)

Run a Monte Carlo simulation.

Parameters:

Name Type Description Default
plan PlanConfig

Financial plan configuration.

required
market MarketAssumptions

Market return and inflation assumptions.

required
policies PolicyBundle

Spending, withdrawal, and rebalancing policies.

required
sim_config SimulationConfig

Simulation execution parameters.

required

Returns:

Type Description
SimulationResult

SimulationResult with success probability, percentiles, and time series.

Timeline

Timeline dataclass

Monthly time grid derived from plan ages.

Attributes:

Name Type Description
current_age int

Starting age in years.

retirement_age int

Retirement age in years.

end_age int

End of simulation age in years.

income_end_age int

Age when earned income stops.

n_steps int

Total number of monthly steps.

retirement_step int

Step index when retirement begins (0 when the plan starts retired).

income_end_step int

Step index when income stops (0 when it already has).

from_ages(current_age, retirement_age, end_age, income_end_age=None) classmethod

Create a Timeline from age parameters.

is_retired(step)

Check if the simulation is in the retirement phase at a given step.

has_income(step)

Check if earned income is active at a given step.

age_at(step)

Return the age (as a float) at a given step.

month_of_year(step)

Return the calendar month (1-12) at a given step.

Assumes the simulation starts in January.

SimulationState

SimulationState dataclass

Mutable state for all paths at a given time step.

Uses plain numpy arrays for performance. NOT Pydantic — this is mutated every step in the hot loop.

Attributes:

Name Type Description
positions NDArray[floating[Any]]

(n_paths, n_accounts, n_assets) dollar holdings per asset within each account.

cumulative_inflation NDArray[floating[Any]]

(n_paths,) cumulative inflation factor (starts at 1.0).

is_depleted NDArray[bool_]

(n_paths,) boolean mask for paths with zero total wealth.

step int

Current time step index.

n_paths int

Number of simulation paths.

n_accounts int

Number of accounts.

n_assets int

Number of asset classes.

account_types list[str]

List of account type strings.

balances property

Account balances, shape (n_paths, n_accounts).

Derived from positions by summing over assets.

total_wealth property

Total wealth across all accounts, shape (n_paths,).

initialize(n_paths, initial_balances, account_types, target_weights) classmethod

Create initial simulation state.

Each account's balance is distributed across assets according to target_weights.

Parameters:

Name Type Description Default
n_paths int

Number of simulation paths.

required
initial_balances list[float]

Starting balance for each account.

required
account_types list[str]

Account type labels.

required
target_weights NDArray[floating[Any]]

(n_assets,) target asset allocation weights summing to 1.

required

RNG

make_rng(seed)

Create a deterministic numpy Generator from a seed.

Uses PCG64DXSM for high-quality, reproducible random number generation.