spear-kernels 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -1
  2. package/stats.js +74 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spear-kernels",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Closed-form kernels discovered by the SPEAR symbolic regression engine, shipped as JS, CUDA C, PyTorch and WebAssembly.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,10 +13,12 @@
13
13
  "import": "./index.js",
14
14
  "require": "./index.cjs"
15
15
  },
16
+ "./stats": "./stats.js",
16
17
  "./package.json": "./package.json"
17
18
  },
18
19
  "files": [
19
20
  "index.js",
21
+ "stats.js",
20
22
  "index.cjs",
21
23
  "index.d.ts",
22
24
  "README.md"
package/stats.js ADDED
@@ -0,0 +1,74 @@
1
+ // spear-kernels/stats — vertical pack: telecom BER, risk (VaR/CVaR),
2
+ // genome-scale p-values, ad-tech conversion scoring.
3
+ // Built on the parity-audited SPEAR kernels (cdf/probit/pdf).
4
+ // Self-contained ESM. MIT license.
5
+
6
+ import { kernels } from "./index.js";
7
+
8
+ const cdfNode = kernels.gaussian_cdf.precise.eval; // takes a number
9
+ const probitNode = kernels.probit_quantile.precise.eval;
10
+
11
+ /** Normal PDF φ(x) */
12
+ export const pdf = (x) => Math.exp(-0.5 * x * x) / Math.sqrt(2 * Math.PI);
13
+
14
+ /** Normal CDF Φ(x) — SPEAR algebraic form (no native erf needed) */
15
+ export const cdf = (x) => cdfNode(x);
16
+
17
+ /** Probit Φ⁻¹(p) — inverse, p ∈ (0,1) */
18
+ export const probit = (p) => probitNode(p);
19
+
20
+ /** Q-function = 1 − Φ(x) (upper tail) */
21
+ export const qfunc = (x) => 1 - cdf(x);
22
+
23
+ // ---------- Telecom: bit error rates ---------------------------------
24
+ /** BPSK/QPSK BER over AWGN from SNR in dB. Exact closed form via Q(). */
25
+ export const berBpsk = (snrDb) => qfunc(Math.sqrt(2 * Math.pow(10, snrDb / 10)));
26
+
27
+ /** M-QAM approximate BER (square M-QAM, Gray coding). */
28
+ export const berQam = (m, snrDb) => {
29
+ const k = Math.log2(m);
30
+ const snr = Math.pow(10, snrDb / 10);
31
+ return (4 / k) * (1 - 1 / Math.sqrt(m)) * qfunc(Math.sqrt((3 * k * snr) / (m - 1)));
32
+ };
33
+
34
+ // ---------- Risk: parametric VaR / CVaR ------------------------------
35
+ /** Parametric VaR at confidence α (e.g. 0.95, 0.99) for N(μ,σ) P&L. */
36
+ export const varNormal = (mu, sigma, alpha = 0.95) =>
37
+ -(mu - sigma * probit(alpha));
38
+
39
+ /**
40
+ * Portfolio sweep: positions [{mu, sigma, size}] × scenarios.
41
+ * Returns portfolio-level parametric VaR/CVaR assuming independence.
42
+ * Aggregated μ,σ via moments; uses SPEAR probit/cdf internally.
43
+ */
44
+ export function portfolioVar(positions, scenarios = [0.95, 0.99]) {
45
+ let mu = 0, varSum = 0;
46
+ for (const p of positions) {
47
+ mu += p.mu * p.size;
48
+ varSum += p.sigma * p.sigma * p.size * p.size;
49
+ }
50
+ const sigma = Math.sqrt(varSum);
51
+ return scenarios.map((alpha) => {
52
+ const z = probit(alpha);
53
+ const varAlpha = -(mu - sigma * z);
54
+ // Normal CVaR closed form: μ − σ·φ(z)/(1−α)
55
+ const cvarAlpha = -(mu - (sigma * pdf(z)) / (1 - alpha));
56
+ return { alpha, var: varAlpha, cvar: cvarAlpha };
57
+ });
58
+ }
59
+
60
+ // ---------- Bioinformatics: genome-scale p-values --------------------
61
+ /** Two-sided p-value from a z-score. */
62
+ export const pValue = (z) => 2 * qfunc(Math.abs(z));
63
+
64
+ /** Bonferroni correction over n tests: returns significance flags. */
65
+ export function bonferroni(zScores, familyAlpha = 0.05) {
66
+ const threshold = familyAlpha / zScores.length;
67
+ return zScores.map((z) => ({ z, p: pValue(z), significant: pValue(z) < threshold }));
68
+ }
69
+
70
+ // ---------- Ad-tech: conversion probability scoring ------------------
71
+ /** Probit-link conversion probability from a linear score. */
72
+ export const conversionProb = (score) => cdf(score);
73
+
74
+ export default { pdf, cdf, probit, qfunc, berBpsk, berQam, varNormal, portfolioVar, pValue, bonferroni, conversionProb };