6b485dfCall R the entropy rate, and put the estimate button under its readoutJeremy Magland 2 * In-browser unbiased estimation of H(z_{M+1} | z_1..z_M) — the entropy
3f857e3Estimate the reference rate R in the browserJeremy Magland 3 * rate R — for the app's model. Hand-synced TypeScript port of the sibling
4 * timeseries-entropy package (see the headers of estimator.ts / model.ts);
5 * defaults match its CLI so browser runs and `timeseries-entropy` runs
6 * target the same estimand with the same variance behavior.
7 */
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 8import { integratedAutocorrTime, unbiasedEntropy } from './estimator'
3f857e3Estimate the reference rate R in the browserJeremy Magland 9import { ConditionalChain } from './model'
10import { Rng } from './rng'
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 12export { unbiasedEntropy, integratedAutocorrTime } from './estimator'
3f857e3Estimate the reference rate R in the browserJeremy Magland 13export { ConditionalChain, truncatedStdNormal } from './model'
14export { Rng } from './rng'
15export { ndtr, ndtri, erfc } from './normal'
fec2284Show analytic entropy-rate prediction alongside the Monte-Carlo estimateJeremy Magland 16export { predictEntropyRate, gaussUniformEntropy, logSpectrumMean } from './theory'
3f857e3Estimate the reference rate R in the browserJeremy Magland 17
18export const N0 = 128
19export const R_EXPONENT = 1.5
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 20/** Per-past realization budget at thin = 1; the resolved thin divides it. */
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 22/** Draws taken at thin = 1 to measure each chain's autocorrelation time. */
23export const PROBE = 512
24/** Cap on the auto-resolved thin (bounds cost; also the probe cannot
25 * resolve times much beyond PROBE / 10). */
26export const THIN_CAP = 64
3f857e3Estimate the reference rate R in the browserJeremy Magland 27
28/** The conditioning window M at a given kernel length, as in the CLI. */
29export function defaultPast(kernelLength: number): number {
30 return Math.max(512, 4 * kernelLength)
31}
33/** The seed for one independent past, derived so past i is reproducible
34 * whether or not the run was stopped and resumed in between. */
35export function pastSeed(baseSeed: number, pastIndex: number): number {
36 return (baseSeed + Math.imul(0x9e3779b9, pastIndex + 1)) >>> 0
37}
40 * One independent past's unbiased estimate: a fresh stationary chain,
41 * auto-thinned to its measured mixing, averaged over randomized-telescoping
42 * realizations run back-to-back on it. Averaging these over pasts estimates
43 * H(z_{M+1} | z_1..z_M).
44 *
45 * Auto-thinning mirrors thin='auto' in the Python package: a PROBE-draw
46 * pilot at thin = 1 estimates the chain's integrated autocorrelation time
47 * tau, the chain then takes ceil(tau) sweeps per draw (capped at THIN_CAP),
48 * and REPS_PER_PAST acts as a budget — realizations = max(1, budget/thin) —
49 * so per-past cost stays roughly flat. Without this, slowly mixing chains
50 * (narrowband kernels x large sigma) make the level corrections decay too
51 * slowly for R_EXPONENT and the estimator's variance is infinite: still
52 * unbiased, but rare realizations of hundreds of bits.
53 */
3f857e3Estimate the reference rate R in the browserJeremy Magland 54export function estimateOnePast(
55 kernel: Float64Array,
56 sigma: number,
57 past: number,
58 seed: number,
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 59 onRep?: (repsDone: number, reps: number) => void,
61 const rng = new Rng(seed)
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 62 const chain = new ConditionalChain(kernel, sigma, past, rng, 1)
63 const tau = integratedAutocorrTime(chain.draw(PROBE))
64 chain.thin = Math.min(THIN_CAP, Math.max(1, Math.ceil(tau)))
65 const reps = Math.max(1, Math.round(REPS_PER_PAST / chain.thin))
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 67 for (let rep = 0; rep < reps; rep++) {
3f857e3Estimate the reference rate R in the browserJeremy Magland 68 sum += unbiasedEntropy(chain.draw, N0, R_EXPONENT, rng)
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 69 onRep?.(rep + 1, reps)
581f213Auto-thin entropy chains to their measured autocorrelation timeJeremy Magland 71 return sum / reps