Download web/src/lib/stats.js from keefereuther/confidence-intervals: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/spaces/keefereuther/confidence-intervals/resolve/main/web/src/lib/stats.js
- Command line
-
hf download hf://spaces/keefereuther/confidence-intervals/web/src/lib/stats.js
-
curl -L -o stats.js https://huggingface.co/spaces/keefereuther/confidence-intervals/resolve/main/web/src/lib/stats.js
5.25 kB
| /** Box–Muller normal samples (independent of R’s RNG). */ | |
| export function rnorm(n, mean = 0, sd = 1) { | |
| const out = new Array(n) | |
| for (let i = 0; i < n; i += 2) { | |
| const u1 = Math.random() | |
| const u2 = Math.random() | |
| const mag = sd * Math.sqrt(-2 * Math.log(u1)) | |
| const z0 = mag * Math.cos(2 * Math.PI * u2) + mean | |
| out[i] = z0 | |
| if (i + 1 < n) { | |
| out[i + 1] = mag * Math.sin(2 * Math.PI * u2) + mean | |
| } | |
| } | |
| return out | |
| } | |
| export function mean(arr) { | |
| if (!arr.length) return NaN | |
| let s = 0 | |
| for (const v of arr) s += v | |
| return s / arr.length | |
| } | |
| /** Bessel n−1 SD like R’s `sd()`. */ | |
| export function sampleSd(arr) { | |
| const n = arr.length | |
| if (n < 2) return 0 | |
| const m = mean(arr) | |
| let s = 0 | |
| for (const v of arr) s += (v - m) ** 2 | |
| return Math.sqrt(s / (n - 1)) | |
| } | |
| export function dnorm(x, mu = 0, sigma = 1) { | |
| return ( | |
| (1 / (sigma * Math.sqrt(2 * Math.PI))) * | |
| Math.exp(-0.5 * ((x - mu) / sigma) ** 2) | |
| ) | |
| } | |
| /** | |
| * Inverse standard normal quantile (Φ⁻¹), Acklam’s approximation. | |
| */ | |
| export function qnorm(p) { | |
| if (p <= 0 || p >= 1 || Number.isNaN(p)) return NaN | |
| const a1 = -3.969683028665376e1 | |
| const a2 = 2.209460984245205e2 | |
| const a3 = -2.759285104469687e2 | |
| const a4 = 1.38357751867269e2 | |
| const a5 = -3.066479806614716e1 | |
| const a6 = 2.506628277459239 | |
| const b1 = -5.447609897822406e1 | |
| const b2 = 1.615858368580409e2 | |
| const b3 = -1.556989798598866e2 | |
| const b4 = 6.680131188771972e1 | |
| const b5 = -1.328068155288572e1 | |
| const c1 = -7.784894002430293e-3 | |
| const c2 = -0.322396441657 | |
| const c3 = -2.400758277161838 | |
| const c4 = -2.549732539343734 | |
| const c5 = 4.374664141464968 | |
| const c6 = 2.938163982698783 | |
| const d1 = 7.784695709041462e-3 | |
| const d2 = 0.322467129098 | |
| const d3 = 2.445134137142996 | |
| const d4 = 3.754408661907416 | |
| const plow = 0.02425 | |
| const phigh = 1 - plow | |
| let q | |
| if (p < plow) { | |
| q = Math.sqrt(-2 * Math.log(p)) | |
| return ( | |
| (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / | |
| ((((d1 * q + d2) * q + d3) * q + d4) * q + 1) | |
| ) | |
| } | |
| if (p > phigh) { | |
| q = Math.sqrt(-2 * Math.log(1 - p)) | |
| return ( | |
| -((((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / | |
| ((((d1 * q + d2) * q + d3) * q + d4) * q + 1)) | |
| ) | |
| } | |
| q = p - 0.5 | |
| const r = q * q | |
| return ( | |
| (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * | |
| q / | |
| (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1) | |
| ) | |
| } | |
| /** R default `hist()` Sturges nclass */ | |
| export function nclassSturges(n) { | |
| if (n < 2) return 1 | |
| return Math.ceil(Math.log2(n)) + 1 | |
| } | |
| /** Simple “pretty” breaks between lo and hi (~R pretty for display). */ | |
| export function prettyBreaks(lo, hi, nBreaks) { | |
| if (lo === hi) return [lo - 0.5, lo, lo + 0.5] | |
| const raw = (hi - lo) / Math.max(1, nBreaks - 1) | |
| const exp = Math.floor(Math.log10(raw)) | |
| const base = 10 ** exp | |
| const candidates = [1, 2, 5, 10].map((m) => m * base) | |
| let step = candidates[0] | |
| for (const c of candidates) { | |
| if (c >= raw) { | |
| step = c | |
| break | |
| } | |
| step = c | |
| } | |
| const start = Math.floor(lo / step) * step | |
| const out = [] | |
| let x = start | |
| while (x <= hi + step * 0.501) { | |
| out.push(x) | |
| x += step | |
| } | |
| if (out.length < 2) { | |
| return [lo, hi] | |
| } | |
| return out | |
| } | |
| /** | |
| * Histogram density heights like R `hist(..., freq=FALSE)`: | |
| * density_i = count_i / (n * bin_width). | |
| */ | |
| export function histDensity(values) { | |
| const n = values.length | |
| if (!n) return { breaks: [], heights: [], mids: [] } | |
| const vmin = Math.min(...values) | |
| const vmax = Math.max(...values) | |
| const nb = Math.min(nclassSturges(n), Math.max(1, n)) | |
| let breaks = prettyBreaks(vmin, vmax, nb) | |
| if (breaks.length < 2) { | |
| breaks = [vmin - 1e-6, vmax + 1e-6] | |
| } | |
| const counts = new Array(breaks.length - 1).fill(0) | |
| for (const v of values) { | |
| let j = -1 | |
| for (let i = 0; i < counts.length; i++) { | |
| const lo = breaks[i] | |
| const hi = breaks[i + 1] | |
| const last = i === counts.length - 1 | |
| if ((v >= lo && v < hi) || (last && v >= lo && v <= hi)) { | |
| j = i | |
| break | |
| } | |
| } | |
| if (j < 0) { | |
| if (v < breaks[0]) j = 0 | |
| else j = counts.length - 1 | |
| } | |
| counts[j] += 1 | |
| } | |
| const heights = counts.map((c, i) => { | |
| const w = breaks[i + 1] - breaks[i] | |
| return c / (n * w) | |
| }) | |
| const mids = counts.map((_, i) => (breaks[i] + breaks[i + 1]) / 2) | |
| return { breaks, heights, mids, counts } | |
| } | |
| /** R `quantile(..., type = 7)` for a single probability p in (0,1). */ | |
| export function quantile7(values, p) { | |
| const x = [...values].sort((a, b) => a - b) | |
| const n = x.length | |
| if (!n) return NaN | |
| const h = (n - 1) * p + 1 | |
| const lo = Math.floor(h) - 1 | |
| const hi = Math.ceil(h) - 1 | |
| const w = h - Math.floor(h) | |
| if (lo < 0) return x[0] | |
| if (hi >= n) return x[n - 1] | |
| if (lo === hi) return x[lo] | |
| return x[lo] * (1 - w) + x[hi] * w | |
| } | |
| export function sampleWithReplace(arr, rng = Math.random) { | |
| const n = arr.length | |
| const out = new Array(n) | |
| for (let i = 0; i < n; i++) out[i] = arr[(rng() * n) | 0] | |
| return out | |
| } | |
| export function bootstrapMeans(sample, B) { | |
| const out = new Array(B) | |
| for (let b = 0; b < B; b++) { | |
| const boot = sampleWithReplace(sample) | |
| out[b] = mean(boot) | |
| } | |
| return out | |
| } | |