SPB Git

Repository Public

README.md 8.5 KB 126 lines markdown

1# Simulations (`src/sim`)23Pre-approved, declarative interactive components for the `simulation` block. A document picks a `kind`, gives4`params` (numbers), a `seed` and a `config` (strings, numbers, booleans). It can never supply code.56- `registry.ts`: `SIMULATIONS`, `resolveParams`, `validateSimulation`, `paramDefs`, `snapParam`, `describeParams`. Safe on the server.7- `SimulationBlock.tsx`: the client component (`block`, `locale`, `staticOnly?`).8- `core/`: seeded PRNG (mulberry32, Box–Muller, Student-t by ratio), statistics, distributions, the expression parser.9- `kinds/`: one module per kind: `compute(params, seed, config) → model` (pure) and `View({ model, locale, step, idPrefix })` (draws only).10- `ui/`: `Plot` (own SVG: mixed layers, gaps), `Chart` (wrapper around the shared `ChartSvg`), `Readouts`, number formatting.1112Rules: every number shown comes from `compute`; same params + seed + config give the same picture; values out of13bounds are clamped and rounded to the slider step; unknown parameter keys are ignored (and reported by14`validateSimulation`). Config keys accepted by every kind: `autoplay` (boolean, animated kinds: play once when the15block first enters the screen, never under reduced motion) and `locale` (set by the block itself).1617Parameters below are written `key (min … max, step, default)`.1819## moe-routing (stochastic, animated: one frame per token)2021- Params: `experts` (2 … 8, 1, 6), `topK` (1 … 8, 1, 2; capped at `experts`), `tokens` (4 … 12, 1, 8), `temperature` (0.1 … 3, 0.1, 1).22- Config: none.23- Computed: logits `z[t][e] = bias_e + ε`, `bias_e ~ N(0, 0.6²)`, `ε ~ N(0, 1)`; gate `g = softmax(z / τ)`; the k largest24  are selected; weights `w_e = g_e / Σ_selected g`; cumulative load (tokens routed per expert), gate mass kept, normalised25  entropy `H(g) / ln N`, final imbalance `max load / (T·k/N)`.26- Simplification: logits are random, not produced by a trained router.2728## neural-forward (stochastic, animated: one frame per layer)2930- Params: `layers` hidden layers (2 … 4, 1, 2), `width` (2 … 6, 1, 4), `inputs` (2 … 4, 1, 3), `scale` (0.2 … 3, 0.1, 1.5).31- Config: `activation` = `tanh` (default) | `relu` | `sigmoid`.32- 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.33  The network is untrained.3435## kv-cache (deterministic, animated: one frame per generated token)3637- Params: `prompt` P (1 … 8, 1, 4), `newTokens` G (2 … 16, 1, 8), `layers` L (1 … 12, 1, 4).38- Config: none.39- Computed, per attention head, causal attention. Step s sees `n = P + s − 1` tokens.40  Without cache: `L·n` keys/values and `L·n(n+1)/2` query–key scores. With cache: step 1 as above (prompt), then `L` keys/values41  and `L·n` scores per step, `L·(n−1)` reused. Cumulative sums of both. MLP cost not counted.4243## attention (stochastic)4445- Params: `temperature` τ (0.1 … 5, 0.1, 1), `dim` d (4 … 32, 4, 16), `locality` λ (0 … 3, 0.1, 0).46- Config: `tokens` (comma-separated, 2 to 10; default `le,chat,dort,sur,le,tapis` / `the,cat,sleeps,on,the,mat`), `causal` (boolean).47- 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 vector48  (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.49  One head, random projections; λ is a simplified relative-position bias.5051## distribution (deterministic)5253- Config: `family` = `normal` (default) | `student` | `lognormal` | `exponential` | `uniform`. Sliders depend on it:54  - normal: `mu` (−3 … 3, 0.1, 0), `sigma` (0.3 … 3, 0.1, 1), `a`, `b` (−8 … 8, 0.1, −1 / 1)55  - student: `df` (1 … 30, 1, 4), `a`, `b` (−8 … 8, 0.1, −2 / 2)56  - 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)57  - exponential: `lambda` (0.2 … 5, 0.1, 1), `a`, `b` (0 … 10, 0.1, 0 / 1)58  - uniform: `low` (−5 … 3, 0.1, 0), `width` (0.5 … 5, 0.1, 2), `a`, `b` (−6 … 9, 0.1, 0.5 / 1.5)59- Computed: density, `P(a ≤ X ≤ b) = F(b) − F(a)` from the closed-form distribution function (Φ; regularised incomplete60  beta for Student-t), mean and standard deviation of the distribution (undefined for Student-t when ν ≤ 1 / ν ≤ 2).6162## confidence-interval (stochastic)6364- 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).65- Config: none.66- 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.6768## regression (stochastic)6970- 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).71- Config: none.72- Computed: `y = α + βx + ε`, `x ~ U(0, 10)`, `ε ~ N(0, σ²)`; OLS estimates with standard errors, R², residual standard error,73  95 % confidence band of the mean response `ŷ ± t_{0.975, n−2}·s·√(1/n + (x − x̄)²/Sxx)`.7475## correlation (stochastic)7677- Params: `rho` (−0.99 … 0.99, 0.01, 0.6), `n` (10 … 500, 10, 150).78- Config: none.79- Computed: `x = z₁`, `y = ρz₁ + √(1−ρ²)z₂`; sample Pearson r, r², Fisher interval `tanh(atanh r ± 1.96/√(n−3))`.8081## garch (stochastic)8283- 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).84- Config: none.85- Computed: `r_t = σ_t z_t`, `σ²_t = ω + α r²_{t−1} + β σ²_{t−1}`, started at `ω/(1−α−β)`. β is reduced so that86  `α + β ≤ 0.999` (a note is shown). Persistence, unconditional variance and standard deviation, half-life87  `ln 0.5 / ln(α+β)`, sample variance. Gaussian innovations.8889## random-walk (stochastic)9091- Config: `process` = `brownian` (default) | `gbm` | `ou`; `x0` (number; default 0 / 100 / 2).92- Params: `drift` μ, `volatility` σ, `paths` (1 … 20, 1, 8), `horizon` T (1 … 20, 1, 5), and for `ou` only `reversion` θ (0.1 … 5, 0.1, 1).93  - brownian: `drift` (−2 … 2, 0.1, 0.5), `volatility` (0.1 … 3, 0.1, 1)94  - gbm: `drift` (−0.3 … 0.5, 0.01, 0.08), `volatility` (0.05 … 0.8, 0.01, 0.2)95  - ou: `drift` = long-run level (−2 … 2, 0.1, 0), `volatility` (0.1 … 2, 0.1, 0.5)96- Computed: 100 steps per path with the exact transition law. Theoretical mean ± 2 sd:97  brownian `x0 + μt`, `σ√t`; gbm `x0·e^{μt}`, `x0·e^{μt}√(e^{σ²t} − 1)`; ou `μ + (x0 − μ)e^{−θt}`, `σ√((1 − e^{−2θt})/(2θ))`.98  For gbm the band is a guide (the law is log-normal).99100## impulse-response (deterministic)101102- 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).103- Config: `shock` = 1 (default) or 2.104- Computed: `IRF_h = A^h e_j` (non-orthogonalised unit shock), eigenvalue moduli from trace and determinant, real/complex,105  stationarity (both moduli < 1).106107## event-study (stochastic)108109- 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), `events` N (5 … 200, 5, 50).110- Config: `window` W (integer 3 … 20, default 10): days on each side of the event.111- Computed: `AR_{i,t} = σ ε + jump·1{t=0} + drift·1{t>0}`; cross-sectional mean AR and CAR (cumulated from −W), each with112  a ±1.96·SE band where SE is the cross-sectional standard deviation / √N; t statistic on day 0; expected CAR `jump + W·drift`.113  Independent abnormal returns, no market-model estimation error.114115## function-plot (deterministic)116117- Config: `expr` (required), `expr2`, `expr3`, `expr4`; `label`, `label2`…`label4` (legend text); `xMin`, `xMax` (default −5, 5);118  `yMin`, `yMax` (default automatic); `xLabel`, `yLabel`; for each parameter name: `<name>Min`, `<name>Max` (default −5, 5), `<name>Step`.119- Params: one slider per name other than `x` found in the expressions (at most 5), default 1 (clamped to the bounds).120- Expression grammar: numbers, `x`, names (letters and digits), `+ - * / ^` (`^` right-associative, `-x^2 = -(x^2)`), parentheses,121  constants `pi`, `e`, functions `exp log ln log10 sqrt abs sin cos tan tanh sigmoid relu max min pow floor` (`log` is natural).122  At most 300 characters and 32 nesting levels. Parsed by `core/expr.ts` (tokeniser, recursive descent, tree walk):123  no `eval`, no `Function`, no property access.124- Computed: 241 points per curve; non-finite values are gaps; pole crossings are cut; when a few values dwarf the rest the125  vertical axis keeps the central 96 % of the values.126
Files main
Menu

Theme