/ concept-collection / timeseries-compressibility
Sign in
concept-collection / timeseries-compressibility
timeseries-compressibility / src / entropy / index.ts
51 lines · 1.8 KBBlameHistoryRaw
1/**
2 * In-browser unbiased estimation of H(z_{M+1} | z_1..z_M) — the entropy
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 */
8import { unbiasedEntropy } from './estimator'
9import { ConditionalChain } from './model'
10import { Rng } from './rng'
12export { unbiasedEntropy } from './estimator'
13export { ConditionalChain, truncatedStdNormal } from './model'
14export { Rng } from './rng'
15export { ndtr, ndtri, erfc } from './normal'
17export const N0 = 128
18export const R_EXPONENT = 1.5
19export const REPS_PER_PAST = 8
20export const THIN = 1
22/** The conditioning window M at a given kernel length, as in the CLI. */
23export function defaultPast(kernelLength: number): number {
24 return Math.max(512, 4 * kernelLength)
27/** The seed for one independent past, derived so past i is reproducible
28 * whether or not the run was stopped and resumed in between. */
29export function pastSeed(baseSeed: number, pastIndex: number): number {
30 return (baseSeed + Math.imul(0x9e3779b9, pastIndex + 1)) >>> 0
33/** One independent past's unbiased estimate: a fresh stationary chain,
34 * averaged over reps randomized-telescoping realizations run back-to-back
35 * on it. Averaging these over pasts estimates H(z_{M+1} | z_1..z_M). */
36export function estimateOnePast(
37 kernel: Float64Array,
38 sigma: number,
39 past: number,
40 seed: number,
41 onRep?: (repsDone: number) => void,
42): number {
43 const rng = new Rng(seed)
44 const chain = new ConditionalChain(kernel, sigma, past, rng, THIN)
45 let sum = 0
46 for (let rep = 0; rep < REPS_PER_PAST; rep++) {
47 sum += unbiasedEntropy(chain.draw, N0, R_EXPONENT, rng)
48 onRep?.(rep + 1)
49 }
50 return sum / REPS_PER_PAST
moveopenescclose