polars_online.sim.regimes

polars_online.sim.regimes(m: int, *, states: Sequence[Any], transition: Any, n_blocks: int, rows_per_block: int, durations: Sequence[int] | None = None, design: str = 'step', smooth_rows: int = 0, phi: float | Sequence[float] = 0.0, scale_state: float | Sequence[float] = 0.0, async_rates: Sequence[float] | None = None, noise: float = 0.0, cycle_profile: Sequence[float] | None = None, cycle_rows: int | None = None, activity: tuple[float, float] | None = None, seed: int = 0) → dict[str, DataFrame][source]

Simulate a stream of m series whose correlation changes by regime, with its truth.

Use it to measure a detector of changing correlation, such as deco, hmm, corrchange or bocpd, against data whose regimes are known. For a stream with a planted relation to a target, build the frame with numpy, as Example data does.

Parameters

m

The number of series, at least 2.

states

The K regimes’ correlations: K correlation matrices (m x m, unit diagonal, positive semi-definite), or K floats, each an equicorrelation, the one correlation of every pair.

transition

The K x K matrix of probabilities, each row summing to 1, that draws the state of each block from the block before it.

n_blocks, rows_per_block

The number of blocks, and the rows in each, each at least 1. A block holds one state.

durations

One block count of at least 1 per state. Given, each state lasts exactly that many blocks, and the next state is drawn from transition with its diagonal removed: the recurring-state design. Default None: each block’s state is drawn from transition.

design, smooth_rows

"step" (the default) switches at the block boundary. "smooth" moves the correlation matrix linearly over smooth_rows rows centered on the boundary: row t follows (1 - mix) R_before + mix R_after, the states of the blocks before and after the boundary, on both sides of it. smooth_rows defaults to 0, which switches at the boundary as "step" does.

phi

The coefficient of each series’ AR(1) filter, one number for all or one per series, each above -1 and below 1. Default 0: no filter.

scale_state

The returns’ scale per state, as the exponent of its multiplier, with s_t the row’s state index. One number gives the multiplier exp(scale_state * s_t), so a positive number makes the scale rise with the state index. A list gives one exponent per state, and the multiplier exp(scale_state[s_t]). Default 0: one scale.

async_rates

The expected reports per row of each series, each above 0, one per series. A row where series i reported nothing carries null for x_i. Default None: every series reports on every row.

noise

The standard deviation of the noise added to each observed level. Default 0: none.

cycle_profile, cycle_rows

cycle_rows multipliers, each above 0 and at most 1, applied to the off-diagonal of R_t at row t mod cycle_rows: a mix toward the identity, so the matrix stays positive semi-definite. cycle_rows defaults to rows_per_block, and sets session = t // cycle_rows. Default None: no cycle.

activity

(mean, shape), both above 0, for a Gamma-distributed activity per row whose mean is scaled by the row’s scale multiplier. clock is then the cumulative activity. Default None: clock is the row index.

seed

The seed of numpy.random.default_rng. Default 0.

Returns

A dict of three frames, "rows", "truth_rows" and "truth_blocks":

rows

What a consumer sees: entity, the constant "sim"; t, the row index; clock; session, t // cycle_rows (the block index unless cycle_rows is given); the levels x_1 .. x_m (null where a series did not report); and activity (null without activity).

truth_rows

Per row: t, block, state, scale_mult and mix, the weight of the later block’s state in the row’s blend: 0 away from a boundary, rising to 1 across it.

truth_blocks

Per block: block, state, n_rows, and corr, the block’s true correlation matrix as vech, its upper triangle with the diagonal, row by row. That matrix is the mean over the block’s rows of the states’ matrices, before cycle_profile mixes them toward the identity.

Raises

ValueError

When:

  • m is below 2, or n_blocks or rows_per_block is below 1;

  • a state is not a correlation matrix of m series, or an equicorrelation is not above -1 / (m - 1) and below 1;

  • transition is not K x K, or a row is not probabilities that sum to 1, or durations does not give one count of at least 1 per state;

  • design is neither "step" nor "smooth";

  • a phi is not above -1 and below 1, or a phi or scale_state list has the wrong length;

  • cycle_rows is below 1, or cycle_profile has the wrong length or a multiplier not above 0 and at most 1;

  • activity is not two numbers above 0, or async_rates is not m rates above 0.

ModuleNotFoundError

numpy is not installed.

UnstableWarning

A warning, under POLARS_ONLINE_WARN_UNSTABLE=1, on each call.

Examples

sim = po.sim.regimes(
    3, states=[0.2, 0.7], transition=[[0.9, 0.1], [0.1, 0.9]],
    n_blocks=4, rows_per_block=50, phi=0.3, seed=1,
)
rows, truth = sim["rows"], sim["truth_blocks"]
print(rows.height, truth["state"].to_list())               # a state per block
print([round(c[1], 2) for c in truth["corr"].to_list()])   # true corr(x_1, x_2)
200 [0, 0, 1, 1]
[0.2, 0.2, 0.7, 0.7]

See also

Notes

The latent returns are eps_t ~ N(0, R_t), filtered to y_it = phi_i y_i,t-1 + eps_it and scaled by exp(scale_state * s_t) (exp(scale_state[s_t]) for a list). The observed series are their levels, the cumulative sums, plus noise. The truth it reports is the innovation correlation: an AR filter moves the return correlation of a pair with unequal phi, which is what polars_online.corr.fisher_se()’s inflation is about.

A convex combination of two correlation matrices is one, so every matrix along a "smooth" boundary is valid. Noise on the level is what the literature models, and it makes the observed return an MA(1) with a negative first autocorrelation, the microstructure effect rcov exists to undo. Under async_rates, last-observation sampling is one forward_fill away.

Everything is drawn from numpy.random.default_rng(seed) in one order, so two calls with the same seed give byte-identical frames under one numpy version. numpy’s Generator makes no promise across versions.