Simulations (src/sim)
Pre-approved, declarative interactive components for the simulation block. A document picks a kind, gives
params (numbers), a seed and a config (strings, numbers, booleans). It can never supply code.
registry.ts:SIMULATIONS,resolveParams,validateSimulation,paramDefs,snapParam,describeParams. Safe on the server.SimulationBlock.tsx: the client component (block,locale,staticOnly?).core/: seeded PRNG (mulberry32, Box–Muller, Student-t by ratio), statistics, distributions, the expression parser.kinds/: one module per kind:compute(params, seed, config) → model(pure) andView({ model, locale, step, idPrefix })(draws only).ui/:Plot(own SVG: mixed layers, gaps),Chart(wrapper around the sharedChartSvg),Readouts, number formatting.
Rules: every number shown comes from compute; same params + seed + config give the same picture; values out of
bounds are clamped and rounded to the slider step; unknown parameter keys are ignored (and reported by
validateSimulation). Config keys accepted by every kind: autoplay (boolean, animated kinds: play once when the
block first enters the screen, never under reduced motion) and locale (set by the block itself).
Parameters below are written key (min … max, step, default).
moe-routing (stochastic, animated: one frame per token)
- Params:
experts(2 … 8, 1, 6),topK(1 … 8, 1, 2; capped atexperts),tokens(4 … 12, 1, 8),temperature(0.1 … 3, 0.1, 1). - Config: none.
- Computed: logits
z[t][e] = bias_e + ε,bias_e ~ N(0, 0.6²),ε ~ N(0, 1); gateg = softmax(z / τ); the k largest are selected; weightsw_e = g_e / Σ_selected g; cumulative load (tokens routed per expert), gate mass kept, normalised entropyH(g) / ln N, final imbalancemax load / (T·k/N). - Simplification: logits are random, not produced by a trained router.
neural-forward (stochastic, animated: one frame per layer)
- Params:
layershidden layers (2 … 4, 1, 2),width(2 … 6, 1, 4),inputs(2 … 4, 1, 3),scale(0.2 … 3, 0.1, 1.5). - Config:
activation=tanh(default) |relu|sigmoid. - Computed:
a_l = f(W_l a_{l−1} + b_l);W ~ N(0, (scale/√fan_in)²),b ~ N(0, 0.1²), inputs~ U(−1, 1); two linear outputs. The network is untrained.
kv-cache (deterministic, animated: one frame per generated token)
- Params:
promptP (1 … 8, 1, 4),newTokensG (2 … 16, 1, 8),layersL (1 … 12, 1, 4). - Config: none.
- Computed, per attention head, causal attention. Step s sees
n = P + s − 1tokens. Without cache:L·nkeys/values andL·n(n+1)/2query–key scores. With cache: step 1 as above (prompt), thenLkeys/values andL·nscores per step,L·(n−1)reused. Cumulative sums of both. MLP cost not counted.
attention (stochastic)
- Params:
temperatureτ (0.1 … 5, 0.1, 1),dimd (4 … 32, 4, 16),localityλ (0 … 3, 0.1, 0). - Config:
tokens(comma-separated, 2 to 10; defaultle,chat,dort,sur,le,tapis/the,cat,sleeps,on,the,mat),causal(boolean). - Computed:
A_ij = softmax_j((q_i·k_j/√d − λ|i−j|)/τ),q = W_q e,k = W_k e,W ~ N(0, 1/d); embedding = word vector (same word, same vector) + 0.5 × position vector, all N(0, 1). Rows sum to 1. Also mean normalised row entropy and mean self-weight. One head, random projections; λ is a simplified relative-position bias.
distribution (deterministic)
- Config:
family=normal(default) |student|lognormal|exponential|uniform. Sliders depend on it:- normal:
mu(−3 … 3, 0.1, 0),sigma(0.3 … 3, 0.1, 1),a,b(−8 … 8, 0.1, −1 / 1) - student:
df(1 … 30, 1, 4),a,b(−8 … 8, 0.1, −2 / 2) - lognormal:
mu(−1 … 1.5, 0.1, 0.5),sigma(0.1 … 1.5, 0.05, 0.5),a,b(0 … 10, 0.1, 1 / 3) - exponential:
lambda(0.2 … 5, 0.1, 1),a,b(0 … 10, 0.1, 0 / 1) - uniform:
low(−5 … 3, 0.1, 0),width(0.5 … 5, 0.1, 2),a,b(−6 … 9, 0.1, 0.5 / 1.5)
- normal:
- Computed: density,
P(a ≤ X ≤ b) = F(b) − F(a)from the closed-form distribution function (Φ; regularised incomplete beta for Student-t), mean and standard deviation of the distribution (undefined for Student-t when ν ≤ 1 / ν ≤ 2).
confidence-interval (stochastic)
- Params:
n(2 … 200, 1, 20),m(10 … 100, 5, 50),level(0.5 … 0.99, 0.01, 0.95),mu(−5 … 5, 0.5, 0),sigma(0.5 … 5, 0.5, 1). - Config: none.
- Computed: m samples of size n from N(μ, σ²); interval
x̄ ± t_{(1+c)/2, n−1}·s/√n; observed coverage, number of misses, mean width.
regression (stochastic)
- Params:
slopeβ (−3 … 3, 0.1, 1.5),interceptα (−5 … 5, 0.5, 1),sigma(0 … 6, 0.1, 2),n(5 … 200, 1, 40). - Config: none.
- Computed:
y = α + βx + ε,x ~ U(0, 10),ε ~ N(0, σ²); OLS estimates with standard errors, R², residual standard error, 95 % confidence band of the mean responseŷ ± t_{0.975, n−2}·s·√(1/n + (x − x̄)²/Sxx).
correlation (stochastic)
- Params:
rho(−0.99 … 0.99, 0.01, 0.6),n(10 … 500, 10, 150). - Config: none.
- Computed:
x = z₁,y = ρz₁ + √(1−ρ²)z₂; sample Pearson r, r², Fisher intervaltanh(atanh r ± 1.96/√(n−3)).
garch (stochastic)
- Params:
omega(0.01 … 1, 0.01, 0.05),alpha(0 … 0.5, 0.01, 0.1),beta(0 … 0.99, 0.01, 0.85),n(100 … 600, 50, 400). - Config: none.
- Computed:
r_t = σ_t z_t,σ²_t = ω + α r²_{t−1} + β σ²_{t−1}, started atω/(1−α−β). β is reduced so thatα + β ≤ 0.999(a note is shown). Persistence, unconditional variance and standard deviation, half-lifeln 0.5 / ln(α+β), sample variance. Gaussian innovations.
random-walk (stochastic)
- Config:
process=brownian(default) |gbm|ou;x0(number; default 0 / 100 / 2). - Params:
driftμ,volatilityσ,paths(1 … 20, 1, 8),horizonT (1 … 20, 1, 5), and forouonlyreversionθ (0.1 … 5, 0.1, 1).- brownian:
drift(−2 … 2, 0.1, 0.5),volatility(0.1 … 3, 0.1, 1) - gbm:
drift(−0.3 … 0.5, 0.01, 0.08),volatility(0.05 … 0.8, 0.01, 0.2) - ou:
drift= long-run level (−2 … 2, 0.1, 0),volatility(0.1 … 2, 0.1, 0.5)
- brownian:
- Computed: 100 steps per path with the exact transition law. Theoretical mean ± 2 sd:
brownian
x0 + μt,σ√t; gbmx0·e^{μt},x0·e^{μt}√(e^{σ²t} − 1); ouμ + (x0 − μ)e^{−θt},σ√((1 − e^{−2θt})/(2θ)). For gbm the band is a guide (the law is log-normal).
impulse-response (deterministic)
- Params:
a11(0.6),a12(0.2),a21(0.3),a22(0.5), each −1.2 … 1.2 step 0.05;horizon(5 … 40, 1, 20). - Config:
shock= 1 (default) or 2. - Computed:
IRF_h = A^h e_j(non-orthogonalised unit shock), eigenvalue moduli from trace and determinant, real/complex, stationarity (both moduli < 1).
event-study (stochastic)
- Params:
jump% (−5 … 5, 0.1, 2),drift% per day (−0.5 … 0.5, 0.01, 0.1),sigma% (0.5 … 5, 0.1, 2),eventsN (5 … 200, 5, 50). - Config:
windowW (integer 3 … 20, default 10): days on each side of the event. - Computed:
AR_{i,t} = σ ε + jump·1{t=0} + drift·1{t>0}; cross-sectional mean AR and CAR (cumulated from −W), each with a ±1.96·SE band where SE is the cross-sectional standard deviation / √N; t statistic on day 0; expected CARjump + W·drift. Independent abnormal returns, no market-model estimation error.
function-plot (deterministic)
- Config:
expr(required),expr2,expr3,expr4;label,label2…label4(legend text);xMin,xMax(default −5, 5);yMin,yMax(default automatic);xLabel,yLabel; for each parameter name:<name>Min,<name>Max(default −5, 5),<name>Step. - Params: one slider per name other than
xfound in the expressions (at most 5), default 1 (clamped to the bounds). - Expression grammar: numbers,
x, names (letters and digits),+ - * / ^(^right-associative,-x^2 = -(x^2)), parentheses, constantspi,e, functionsexp log ln log10 sqrt abs sin cos tan tanh sigmoid relu max min pow floor(logis natural). At most 300 characters and 32 nesting levels. Parsed bycore/expr.ts(tokeniser, recursive descent, tree walk): noeval, noFunction, no property access. - Computed: 241 points per curve; non-finite values are gaps; pole crossings are cut; when a few values dwarf the rest the vertical axis keeps the central 96 % of the values.