orderflow-metrics 0.24.0 → 0.26.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.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,30 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
4
4
  This project follows [Semantic Versioning](https://semver.org/); pre-1.0 the
5
5
  public API may still change between minor versions.
6
6
 
7
+ ## [0.26.0] - 2026-08-27
8
+
9
+ ### Added
10
+ - Microstructure-noise-aware realized variance (`noise`) — `noiseVariance`
11
+ (variance of the microstructure noise, ≈ RV_finest / 2n; Zhang, Mykland &
12
+ Aït-Sahalia 2005), `sparseRealizedVariance` (RV on a coarser grid, averaged
13
+ over every offset by subsampling so no data is wasted — `step = 1` reproduces
14
+ plain RV, larger steps suppress noise bias) and `volatilitySignature` (RV as a
15
+ function of sampling step: the classic signature plot whose blow-up at fine
16
+ steps is the fingerprint of microstructure noise). Test suite included.
17
+ (Python: 0.14.0.)
18
+
19
+ ## [0.25.0] - 2026-08-26
20
+
21
+ ### Added
22
+ - Jump-robust realized variance (`robustvol`) — `minRV` and `medRV`, integrated
23
+ variance estimators built from the minimum / median of neighbouring absolute
24
+ returns (Andersen, Dobrev & Schaumburg, 2012). Like bipower variation they
25
+ strip discrete jumps out of realized variance, but more robustly — MedRV also
26
+ shrugs off isolated zero returns and outliers. Plus `realizedQuarticity`
27
+ ((n/3)·Σrᵢ⁴, Barndorff-Nielsen & Shephard 2002), the integrated-quarticity
28
+ estimate that sets the standard error of realized variance and standardises
29
+ jump tests. Test suite included. (Python: 0.13.0.)
30
+
7
31
  ## [0.24.0] - 2026-08-25
8
32
 
9
33
  ### Changed
package/dist/index.d.ts CHANGED
@@ -31,6 +31,9 @@ export type { HurstOptions } from "./hurst.ts";
31
31
  export { hurstExponent } from "./hurst.ts";
32
32
  export { realizedSkewness, realizedKurtosis } from "./moments.ts";
33
33
  export { bipowerVariation, jumpVariation, relativeJumpVariation, } from "./jumps.ts";
34
+ export { minRV, medRV, realizedQuarticity } from "./robustvol.ts";
35
+ export type { SignaturePoint } from "./noise.ts";
36
+ export { noiseVariance, sparseRealizedVariance, volatilitySignature, } from "./noise.ts";
34
37
  export type { Semivariance } from "./semivar.ts";
35
38
  export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.ts";
36
39
  export { shannonEntropy, normalizedEntropy, signEntropy, } from "./entropy.ts";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChE,YAAY,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EACL,IAAI,EACJ,cAAc,EACd,cAAc,EACd,iBAAiB,GAClB,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,cAAc,EACd,WAAW,EACX,UAAU,EACV,UAAU,GACX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACrE,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACnD,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAA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AC3C,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,qBAAqB,GACtB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAClE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EACL,aAAa,EACb,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,WAAW,GACZ,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,OAAO,EACP,IAAI,EACJ,YAAY,EACZ,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,YAAY,GACb,MAAM,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -18,6 +18,8 @@ export { parkinsonVolatility, garmanKlassVolatility, rogersSatchellVolatility, y
18
18
  export { hurstExponent } from "./hurst.js";
19
19
  export { realizedSkewness, realizedKurtosis } from "./moments.js";
20
20
  export { bipowerVariation, jumpVariation, relativeJumpVariation, } from "./jumps.js";
21
+ export { minRV, medRV, realizedQuarticity } from "./robustvol.js";
22
+ export { noiseVariance, sparseRealizedVariance, volatilitySignature, } from "./noise.js";
21
23
  export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.js";
22
24
  export { shannonEntropy, normalizedEntropy, signEntropy, } from "./entropy.js";
23
25
  export { Welford, Ewma, EwmaVariance, RollingWindow, } from "./online.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"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AAiB,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Microstructure-noise-aware realized variance.
3
+ *
4
+ * At the finest sampling frequency, realized variance is badly biased upward:
5
+ * observed prices are the "true" price plus microstructure noise (bid-ask
6
+ * bounce, discreteness, latency), and squaring tiny bounce returns pumps up the
7
+ * sum. Sample more coarsely and the noise averages out — but you throw away
8
+ * data. These tools let you see, quantify, and reduce that bias.
9
+ *
10
+ * noiseVariance — the variance of the noise itself, ≈ RV_finest / 2n
11
+ * (Zhang, Mykland & Aït-Sahalia, 2005)
12
+ * sparseRealizedVariance — RV on a coarser grid, averaged over every offset
13
+ * (subsampling), so no data is wasted
14
+ * volatilitySignature — RV as a function of sampling step: the classic
15
+ * "signature plot" whose blow-up at fine steps is the
16
+ * visual fingerprint of microstructure noise
17
+ *
18
+ * Every function takes a series of (fine-grid) returns.
19
+ */
20
+ /**
21
+ * Estimate the variance of the microstructure noise. Under the additive-noise
22
+ * model the finest-grid realized variance is dominated by noise and converges
23
+ * to 2n·(noise variance), so the noise variance is estimated as RV / (2n).
24
+ * Returns 0 for an empty series.
25
+ */
26
+ export declare function noiseVariance(returns: readonly number[]): number;
27
+ /**
28
+ * Realized variance on a grid `step` times coarser than the raw returns,
29
+ * averaged over all `step` possible starting offsets (subsampling) so every
30
+ * observation is used. `step = 1` reproduces plain realized variance; larger
31
+ * steps suppress microstructure-noise bias at the cost of resolution. Returns 0
32
+ * for `step < 1`, an empty series, or a step too large to form any block.
33
+ */
34
+ export declare function sparseRealizedVariance(returns: readonly number[], step: number): number;
35
+ /** One point of a volatility-signature curve. */
36
+ export interface SignaturePoint {
37
+ /** Sampling step (in raw-return units). */
38
+ step: number;
39
+ /** Subsampled realized variance at that step. */
40
+ realizedVariance: number;
41
+ }
42
+ /**
43
+ * The volatility signature: subsampled realized variance at each sampling step
44
+ * in `steps`. Plotted against the step, the curve typically starts high (noise
45
+ * inflated) at step 1 and settles toward the true integrated variance as the
46
+ * step grows — the shape that diagnoses how much microstructure noise a series
47
+ * carries. Steps are used as given (deduplication and ordering are the caller's
48
+ * choice).
49
+ */
50
+ export declare function volatilitySignature(returns: readonly number[], steps: readonly number[]): SignaturePoint[];
51
+ //# sourceMappingURL=noise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noise.d.ts","sourceRoot":"","sources":["../src/noise.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AASH;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAIhE;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,IAAI,EAAE,MAAM,GACX,MAAM,CA2BR;AAED,iDAAiD;AACjD,MAAM,WAAW,cAAc;IAC7B,2CAA2C;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,KAAK,EAAE,SAAS,MAAM,EAAE,GACvB,cAAc,EAAE,CAKlB"}
package/dist/noise.js ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Microstructure-noise-aware realized variance.
3
+ *
4
+ * At the finest sampling frequency, realized variance is badly biased upward:
5
+ * observed prices are the "true" price plus microstructure noise (bid-ask
6
+ * bounce, discreteness, latency), and squaring tiny bounce returns pumps up the
7
+ * sum. Sample more coarsely and the noise averages out — but you throw away
8
+ * data. These tools let you see, quantify, and reduce that bias.
9
+ *
10
+ * noiseVariance — the variance of the noise itself, ≈ RV_finest / 2n
11
+ * (Zhang, Mykland & Aït-Sahalia, 2005)
12
+ * sparseRealizedVariance — RV on a coarser grid, averaged over every offset
13
+ * (subsampling), so no data is wasted
14
+ * volatilitySignature — RV as a function of sampling step: the classic
15
+ * "signature plot" whose blow-up at fine steps is the
16
+ * visual fingerprint of microstructure noise
17
+ *
18
+ * Every function takes a series of (fine-grid) returns.
19
+ */
20
+ /** Σ rᵢ² over the full, finest grid (local helper). */
21
+ function realizedVarAll(returns) {
22
+ let s = 0;
23
+ for (const r of returns)
24
+ s += r * r;
25
+ return s;
26
+ }
27
+ /**
28
+ * Estimate the variance of the microstructure noise. Under the additive-noise
29
+ * model the finest-grid realized variance is dominated by noise and converges
30
+ * to 2n·(noise variance), so the noise variance is estimated as RV / (2n).
31
+ * Returns 0 for an empty series.
32
+ */
33
+ export function noiseVariance(returns) {
34
+ const n = returns.length;
35
+ if (n < 1)
36
+ return 0;
37
+ return realizedVarAll(returns) / (2 * n);
38
+ }
39
+ /**
40
+ * Realized variance on a grid `step` times coarser than the raw returns,
41
+ * averaged over all `step` possible starting offsets (subsampling) so every
42
+ * observation is used. `step = 1` reproduces plain realized variance; larger
43
+ * steps suppress microstructure-noise bias at the cost of resolution. Returns 0
44
+ * for `step < 1`, an empty series, or a step too large to form any block.
45
+ */
46
+ export function sparseRealizedVariance(returns, step) {
47
+ const n = returns.length;
48
+ const k = Math.floor(step);
49
+ if (k < 1 || n < 1)
50
+ return 0;
51
+ if (k === 1)
52
+ return realizedVarAll(returns);
53
+ // cumulative log-prices P[0..n], P[i] = Σ returns[0..i-1]
54
+ const P = new Array(n + 1);
55
+ P[0] = 0;
56
+ for (let i = 0; i < n; i++)
57
+ P[i + 1] = P[i] + returns[i];
58
+ let total = 0;
59
+ let grids = 0;
60
+ for (let g = 0; g < k; g++) {
61
+ let s = 0;
62
+ let blocks = 0;
63
+ for (let idx = g; idx + k <= n; idx += k) {
64
+ const d = P[idx + k] - P[idx];
65
+ s += d * d;
66
+ blocks++;
67
+ }
68
+ if (blocks > 0) {
69
+ total += s;
70
+ grids++;
71
+ }
72
+ }
73
+ return grids > 0 ? total / grids : 0;
74
+ }
75
+ /**
76
+ * The volatility signature: subsampled realized variance at each sampling step
77
+ * in `steps`. Plotted against the step, the curve typically starts high (noise
78
+ * inflated) at step 1 and settles toward the true integrated variance as the
79
+ * step grows — the shape that diagnoses how much microstructure noise a series
80
+ * carries. Steps are used as given (deduplication and ordering are the caller's
81
+ * choice).
82
+ */
83
+ export function volatilitySignature(returns, steps) {
84
+ return steps.map((step) => ({
85
+ step,
86
+ realizedVariance: sparseRealizedVariance(returns, step),
87
+ }));
88
+ }
89
+ //# sourceMappingURL=noise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noise.js","sourceRoot":"","sources":["../src/noise.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,uDAAuD;AACvD,SAAS,cAAc,CAAC,OAA0B;IAChD,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,MAAM,CAAC,IAAI,OAAO;QAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpC,OAAO,CAAC,CAAC;AACX,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,OAA0B;IACtD,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IACzB,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC;IACpB,OAAO,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAC3C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CACpC,OAA0B,EAC1B,IAAY;IAEZ,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IACzB,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3B,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC;IAC7B,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,cAAc,CAAC,OAAO,CAAC,CAAC;IAE5C,0DAA0D;IAC1D,MAAM,CAAC,GAAG,IAAI,KAAK,CAAS,CAAC,GAAG,CAAC,CAAC,CAAC;IACnC,CA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
1
+ /**
2
+ * Jump-robust realized variance and realized quarticity.
3
+ *
4
+ * Plain realized variance (RV = Σ rᵢ²) is inflated by discrete jumps. Like
5
+ * bipower variation, MinRV and MedRV estimate only the *continuous* part of
6
+ * variance, but using the minimum / median of neighbouring absolute returns —
7
+ * which is even more robust to jumps (and, for MedRV, to occasional zero
8
+ * returns and isolated outliers) than the product form of bipower variation.
9
+ * Andersen, Dobrev & Schaumburg (2012).
10
+ *
11
+ * MinRV = (π/(π−2)) · (n/(n−1)) · Σ min(|rᵢ₋₁|, |rᵢ|)²
12
+ * MedRV = (π/(6−4√3+π)) · (n/(n−2)) · Σ med(|rᵢ₋₁|, |rᵢ|, |rᵢ₊₁|)²
13
+ *
14
+ * Realized quarticity (RQ = (n/3) · Σ rᵢ⁴) estimates the integrated quarticity
15
+ * ∫σ⁴ — the quantity that sets the standard error of realized variance and
16
+ * appears in the denominator of jump tests. Barndorff-Nielsen & Shephard (2002).
17
+ *
18
+ * Each function takes a return series and returns a non-negative number.
19
+ */
20
+ /**
21
+ * MinRV — jump-robust integrated variance from the squared minimum of adjacent
22
+ * absolute returns. A single jump inflates one return but is paired with a
23
+ * smaller neighbour, so `min` discards it. Returns 0 for fewer than two returns.
24
+ */
25
+ export declare function minRV(returns: readonly number[]): number;
26
+ /**
27
+ * MedRV — jump-robust integrated variance from the squared median of three
28
+ * consecutive absolute returns. The median ignores a lone jump *and* a lone
29
+ * near-zero return, making it the most robust of the three continuous-variance
30
+ * estimators here. Returns 0 for fewer than three returns.
31
+ */
32
+ export declare function medRV(returns: readonly number[]): number;
33
+ /**
34
+ * Realized quarticity: (n/3) · Σ rᵢ⁴, an estimate of the integrated quarticity
35
+ * ∫σ⁴ used to form the standard error of realized variance and to standardise
36
+ * jump tests. Returns 0 for an empty series.
37
+ */
38
+ export declare function realizedQuarticity(returns: readonly number[]): number;
39
+ //# sourceMappingURL=robustvol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"robustvol.d.ts","sourceRoot":"","sources":["../src/robustvol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAOH;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAWxD;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAaxD;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CASrE"}
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Jump-robust realized variance and realized quarticity.
3
+ *
4
+ * Plain realized variance (RV = Σ rᵢ²) is inflated by discrete jumps. Like
5
+ * bipower variation, MinRV and MedRV estimate only the *continuous* part of
6
+ * variance, but using the minimum / median of neighbouring absolute returns —
7
+ * which is even more robust to jumps (and, for MedRV, to occasional zero
8
+ * returns and isolated outliers) than the product form of bipower variation.
9
+ * Andersen, Dobrev & Schaumburg (2012).
10
+ *
11
+ * MinRV = (π/(π−2)) · (n/(n−1)) · Σ min(|rᵢ₋₁|, |rᵢ|)²
12
+ * MedRV = (π/(6−4√3+π)) · (n/(n−2)) · Σ med(|rᵢ₋₁|, |rᵢ|, |rᵢ₊₁|)²
13
+ *
14
+ * Realized quarticity (RQ = (n/3) · Σ rᵢ⁴) estimates the integrated quarticity
15
+ * ∫σ⁴ — the quantity that sets the standard error of realized variance and
16
+ * appears in the denominator of jump tests. Barndorff-Nielsen & Shephard (2002).
17
+ *
18
+ * Each function takes a return series and returns a non-negative number.
19
+ */
20
+ // π / (π − 2): the MinRV scaling constant (from E[min(|Z₁|,|Z₂|)²], Z ~ N(0,1)).
21
+ const MIN_RV_SCALE = Math.PI / (Math.PI - 2);
22
+ // π / (6 − 4√3 + π): the MedRV scaling constant (from E[med(|Z₁|,|Z₂|,|Z₃|)²]).
23
+ const MED_RV_SCALE = Math.PI / (6 - 4 * Math.sqrt(3) + Math.PI);
24
+ /**
25
+ * MinRV — jump-robust integrated variance from the squared minimum of adjacent
26
+ * absolute returns. A single jump inflates one return but is paired with a
27
+ * smaller neighbour, so `min` discards it. Returns 0 for fewer than two returns.
28
+ */
29
+ export function minRV(returns) {
30
+ const n = returns.length;
31
+ if (n < 2)
32
+ return 0;
33
+ let s = 0;
34
+ for (let i = 1; i < n; i++) {
35
+ const a = Math.abs(returns[i - 1]);
36
+ const b = Math.abs(returns[i]);
37
+ const m = a < b ? a : b;
38
+ s += m * m;
39
+ }
40
+ return MIN_RV_SCALE * (n / (n - 1)) * s;
41
+ }
42
+ /**
43
+ * MedRV — jump-robust integrated variance from the squared median of three
44
+ * consecutive absolute returns. The median ignores a lone jump *and* a lone
45
+ * near-zero return, making it the most robust of the three continuous-variance
46
+ * estimators here. Returns 0 for fewer than three returns.
47
+ */
48
+ export function medRV(returns) {
49
+ const n = returns.length;
50
+ if (n < 3)
51
+ return 0;
52
+ let s = 0;
53
+ for (let i = 1; i < n - 1; i++) {
54
+ const a = Math.abs(returns[i - 1]);
55
+ const b = Math.abs(returns[i]);
56
+ const c = Math.abs(returns[i + 1]);
57
+ // median of a, b, c without allocating an array
58
+ const med = Math.max(Math.min(a, b), Math.min(Math.max(a, b), c));
59
+ s += med * med;
60
+ }
61
+ return MED_RV_SCALE * (n / (n - 2)) * s;
62
+ }
63
+ /**
64
+ * Realized quarticity: (n/3) · Σ rᵢ⁴, an estimate of the integrated quarticity
65
+ * ∫σ⁴ used to form the standard error of realized variance and to standardise
66
+ * jump tests. Returns 0 for an empty series.
67
+ */
68
+ export function realizedQuarticity(returns) {
69
+ const n = returns.length;
70
+ if (n < 1)
71
+ return 0;
72
+ let s = 0;
73
+ for (const r of returns) {
74
+ const r2 = r * r;
75
+ s += r2 * r2;
76
+ }
77
+ return (n / 3) * s;
78
+ }
79
+ //# sourceMappingURL=robustvol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"robustvol.js","sourceRoot":"","sources":["../src/robustvol.ts"],"names":[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-metrics",
3
- "version": "0.24.0",
4
- "description": "Microstructure metrics in dependency-free TypeScript — OFI, VPIN, information-driven bars, market impact (square-root & Almgren-Chriss), markouts, implementation shortfall, high-low spread estimators (Corwin-Schultz, Abdi-Ranaldo), range-based volatility (Parkinson, Garman-Klass, Rogers-Satchell, Yang-Zhang), Hurst exponent, realized skewness & kurtosis, bipower variation & jump detection, realized semivariance & signed jump variation, order-flow entropy, online/streaming estimators (Welford, EWMA, rolling window), realized covariance/correlation/beta, Kyle's lambda, trade-sign classification, Amihud illiquidity.",
3
+ "version": "0.26.0",
4
+ "description": "Microstructure metrics in dependency-free TypeScript — OFI, VPIN, information-driven bars, market impact (square-root & Almgren-Chriss), markouts, implementation shortfall, high-low spread estimators (Corwin-Schultz, Abdi-Ranaldo), range-based volatility (Parkinson, Garman-Klass, Rogers-Satchell, Yang-Zhang), Hurst exponent, realized skewness & kurtosis, bipower variation & jump detection, jump-robust variance (MinRV, MedRV) & realized quarticity, realized semivariance & signed jump variation, order-flow entropy, online/streaming estimators (Welford, EWMA, rolling window), realized covariance/correlation/beta, microstructure-noise-robust variance (subsampling, volatility signature), Kyle's lambda, trade-sign classification, Amihud illiquidity.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
package/src/index.ts CHANGED
@@ -63,6 +63,13 @@ export {
63
63
  jumpVariation,
64
64
  relativeJumpVariation,
65
65
  } from "./jumps.ts";
66
+ export { minRV, medRV, realizedQuarticity } from "./robustvol.ts";
67
+ export type { SignaturePoint } from "./noise.ts";
68
+ export {
69
+ noiseVariance,
70
+ sparseRealizedVariance,
71
+ volatilitySignature,
72
+ } from "./noise.ts";
66
73
  export type { Semivariance } from "./semivar.ts";
67
74
  export {
68
75
  realizedSemivariance,
package/src/noise.ts ADDED
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Microstructure-noise-aware realized variance.
3
+ *
4
+ * At the finest sampling frequency, realized variance is badly biased upward:
5
+ * observed prices are the "true" price plus microstructure noise (bid-ask
6
+ * bounce, discreteness, latency), and squaring tiny bounce returns pumps up the
7
+ * sum. Sample more coarsely and the noise averages out — but you throw away
8
+ * data. These tools let you see, quantify, and reduce that bias.
9
+ *
10
+ * noiseVariance — the variance of the noise itself, ≈ RV_finest / 2n
11
+ * (Zhang, Mykland & Aït-Sahalia, 2005)
12
+ * sparseRealizedVariance — RV on a coarser grid, averaged over every offset
13
+ * (subsampling), so no data is wasted
14
+ * volatilitySignature — RV as a function of sampling step: the classic
15
+ * "signature plot" whose blow-up at fine steps is the
16
+ * visual fingerprint of microstructure noise
17
+ *
18
+ * Every function takes a series of (fine-grid) returns.
19
+ */
20
+
21
+ /** Σ rᵢ² over the full, finest grid (local helper). */
22
+ function realizedVarAll(returns: readonly number[]): number {
23
+ let s = 0;
24
+ for (const r of returns) s += r * r;
25
+ return s;
26
+ }
27
+
28
+ /**
29
+ * Estimate the variance of the microstructure noise. Under the additive-noise
30
+ * model the finest-grid realized variance is dominated by noise and converges
31
+ * to 2n·(noise variance), so the noise variance is estimated as RV / (2n).
32
+ * Returns 0 for an empty series.
33
+ */
34
+ export function noiseVariance(returns: readonly number[]): number {
35
+ const n = returns.length;
36
+ if (n < 1) return 0;
37
+ return realizedVarAll(returns) / (2 * n);
38
+ }
39
+
40
+ /**
41
+ * Realized variance on a grid `step` times coarser than the raw returns,
42
+ * averaged over all `step` possible starting offsets (subsampling) so every
43
+ * observation is used. `step = 1` reproduces plain realized variance; larger
44
+ * steps suppress microstructure-noise bias at the cost of resolution. Returns 0
45
+ * for `step < 1`, an empty series, or a step too large to form any block.
46
+ */
47
+ export function sparseRealizedVariance(
48
+ returns: readonly number[],
49
+ step: number,
50
+ ): number {
51
+ const n = returns.length;
52
+ const k = Math.floor(step);
53
+ if (k < 1 || n < 1) return 0;
54
+ if (k === 1) return realizedVarAll(returns);
55
+
56
+ // cumulative log-prices P[0..n], P[i] = Σ returns[0..i-1]
57
+ const P = new Array<number>(n + 1);
58
+ P[0] = 0;
59
+ for (let i = 0; i < n; i++) P[i + 1] = P[i] + returns[i];
60
+
61
+ let total = 0;
62
+ let grids = 0;
63
+ for (let g = 0; g < k; g++) {
64
+ let s = 0;
65
+ let blocks = 0;
66
+ for (let idx = g; idx + k <= n; idx += k) {
67
+ const d = P[idx + k] - P[idx];
68
+ s += d * d;
69
+ blocks++;
70
+ }
71
+ if (blocks > 0) {
72
+ total += s;
73
+ grids++;
74
+ }
75
+ }
76
+ return grids > 0 ? total / grids : 0;
77
+ }
78
+
79
+ /** One point of a volatility-signature curve. */
80
+ export interface SignaturePoint {
81
+ /** Sampling step (in raw-return units). */
82
+ step: number;
83
+ /** Subsampled realized variance at that step. */
84
+ realizedVariance: number;
85
+ }
86
+
87
+ /**
88
+ * The volatility signature: subsampled realized variance at each sampling step
89
+ * in `steps`. Plotted against the step, the curve typically starts high (noise
90
+ * inflated) at step 1 and settles toward the true integrated variance as the
91
+ * step grows — the shape that diagnoses how much microstructure noise a series
92
+ * carries. Steps are used as given (deduplication and ordering are the caller's
93
+ * choice).
94
+ */
95
+ export function volatilitySignature(
96
+ returns: readonly number[],
97
+ steps: readonly number[],
98
+ ): SignaturePoint[] {
99
+ return steps.map((step) => ({
100
+ step,
101
+ realizedVariance: sparseRealizedVariance(returns, step),
102
+ }));
103
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Jump-robust realized variance and realized quarticity.
3
+ *
4
+ * Plain realized variance (RV = Σ rᵢ²) is inflated by discrete jumps. Like
5
+ * bipower variation, MinRV and MedRV estimate only the *continuous* part of
6
+ * variance, but using the minimum / median of neighbouring absolute returns —
7
+ * which is even more robust to jumps (and, for MedRV, to occasional zero
8
+ * returns and isolated outliers) than the product form of bipower variation.
9
+ * Andersen, Dobrev & Schaumburg (2012).
10
+ *
11
+ * MinRV = (π/(π−2)) · (n/(n−1)) · Σ min(|rᵢ₋₁|, |rᵢ|)²
12
+ * MedRV = (π/(6−4√3+π)) · (n/(n−2)) · Σ med(|rᵢ₋₁|, |rᵢ|, |rᵢ₊₁|)²
13
+ *
14
+ * Realized quarticity (RQ = (n/3) · Σ rᵢ⁴) estimates the integrated quarticity
15
+ * ∫σ⁴ — the quantity that sets the standard error of realized variance and
16
+ * appears in the denominator of jump tests. Barndorff-Nielsen & Shephard (2002).
17
+ *
18
+ * Each function takes a return series and returns a non-negative number.
19
+ */
20
+
21
+ // π / (π − 2): the MinRV scaling constant (from E[min(|Z₁|,|Z₂|)²], Z ~ N(0,1)).
22
+ const MIN_RV_SCALE = Math.PI / (Math.PI - 2);
23
+ // π / (6 − 4√3 + π): the MedRV scaling constant (from E[med(|Z₁|,|Z₂|,|Z₃|)²]).
24
+ const MED_RV_SCALE = Math.PI / (6 - 4 * Math.sqrt(3) + Math.PI);
25
+
26
+ /**
27
+ * MinRV — jump-robust integrated variance from the squared minimum of adjacent
28
+ * absolute returns. A single jump inflates one return but is paired with a
29
+ * smaller neighbour, so `min` discards it. Returns 0 for fewer than two returns.
30
+ */
31
+ export function minRV(returns: readonly number[]): number {
32
+ const n = returns.length;
33
+ if (n < 2) return 0;
34
+ let s = 0;
35
+ for (let i = 1; i < n; i++) {
36
+ const a = Math.abs(returns[i - 1]);
37
+ const b = Math.abs(returns[i]);
38
+ const m = a < b ? a : b;
39
+ s += m * m;
40
+ }
41
+ return MIN_RV_SCALE * (n / (n - 1)) * s;
42
+ }
43
+
44
+ /**
45
+ * MedRV — jump-robust integrated variance from the squared median of three
46
+ * consecutive absolute returns. The median ignores a lone jump *and* a lone
47
+ * near-zero return, making it the most robust of the three continuous-variance
48
+ * estimators here. Returns 0 for fewer than three returns.
49
+ */
50
+ export function medRV(returns: readonly number[]): number {
51
+ const n = returns.length;
52
+ if (n < 3) return 0;
53
+ let s = 0;
54
+ for (let i = 1; i < n - 1; i++) {
55
+ const a = Math.abs(returns[i - 1]);
56
+ const b = Math.abs(returns[i]);
57
+ const c = Math.abs(returns[i + 1]);
58
+ // median of a, b, c without allocating an array
59
+ const med = Math.max(Math.min(a, b), Math.min(Math.max(a, b), c));
60
+ s += med * med;
61
+ }
62
+ return MED_RV_SCALE * (n / (n - 2)) * s;
63
+ }
64
+
65
+ /**
66
+ * Realized quarticity: (n/3) · Σ rᵢ⁴, an estimate of the integrated quarticity
67
+ * ∫σ⁴ used to form the standard error of realized variance and to standardise
68
+ * jump tests. Returns 0 for an empty series.
69
+ */
70
+ export function realizedQuarticity(returns: readonly number[]): number {
71
+ const n = returns.length;
72
+ if (n < 1) return 0;
73
+ let s = 0;
74
+ for (const r of returns) {
75
+ const r2 = r * r;
76
+ s += r2 * r2;
77
+ }
78
+ return (n / 3) * s;
79
+ }