orderflow-metrics 0.25.0 → 0.27.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,28 @@ 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.27.0] - 2026-08-28
8
+
9
+ ### Added
10
+ - Two-Scale Realized Variance (`tsrv`) — `twoScaleRealizedVariance` and
11
+ `twoScaleRealizedVolatility`, a *consistent*, microstructure-noise-corrected
12
+ estimator of integrated variance (Zhang, Mykland & Aït-Sahalia 2005). It
13
+ combines a slow subsampled RV with the fast all-ticks RV and subtracts the
14
+ noise bias — the estimator-grade sequel to the diagnostics in the `noise`
15
+ module. Test suite included. (Python: 0.15.0.)
16
+
17
+ ## [0.26.0] - 2026-08-27
18
+
19
+ ### Added
20
+ - Microstructure-noise-aware realized variance (`noise`) — `noiseVariance`
21
+ (variance of the microstructure noise, ≈ RV_finest / 2n; Zhang, Mykland &
22
+ Aït-Sahalia 2005), `sparseRealizedVariance` (RV on a coarser grid, averaged
23
+ over every offset by subsampling so no data is wasted — `step = 1` reproduces
24
+ plain RV, larger steps suppress noise bias) and `volatilitySignature` (RV as a
25
+ function of sampling step: the classic signature plot whose blow-up at fine
26
+ steps is the fingerprint of microstructure noise). Test suite included.
27
+ (Python: 0.14.0.)
28
+
7
29
  ## [0.25.0] - 2026-08-26
8
30
 
9
31
  ### Added
package/dist/index.d.ts CHANGED
@@ -32,6 +32,9 @@ export { hurstExponent } from "./hurst.ts";
32
32
  export { realizedSkewness, realizedKurtosis } from "./moments.ts";
33
33
  export { bipowerVariation, jumpVariation, relativeJumpVariation, } from "./jumps.ts";
34
34
  export { minRV, medRV, realizedQuarticity } from "./robustvol.ts";
35
+ export type { SignaturePoint } from "./noise.ts";
36
+ export { noiseVariance, sparseRealizedVariance, volatilitySignature, } from "./noise.ts";
37
+ export { twoScaleRealizedVariance, twoScaleRealizedVolatility, } from "./tsrv.ts";
35
38
  export type { Semivariance } from "./semivar.ts";
36
39
  export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.ts";
37
40
  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,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;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AAE,MAAM,YAAY,CAAC;AAC3C,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,OAAO,EACL,wBAAwB,EACxB,0BAA0B,GAC3B,MAAM,WAAW,CAAC;AACnB,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
@@ -19,6 +19,8 @@ export { hurstExponent } from "./hurst.js";
19
19
  export { realizedSkewness, realizedKurtosis } from "./moments.js";
20
20
  export { bipowerVariation, jumpVariation, relativeJumpVariation, } from "./jumps.js";
21
21
  export { minRV, medRV, realizedQuarticity } from "./robustvol.js";
22
+ export { noiseVariance, sparseRealizedVariance, volatilitySignature, } from "./noise.js";
23
+ export { twoScaleRealizedVariance, twoScaleRealizedVolatility, } from "./tsrv.js";
22
24
  export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.js";
23
25
  export { shannonEntropy, normalizedEntropy, signEntropy, } from "./entropy.js";
24
26
  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":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhE,OAAO,EACL,IAAI,EACJ,cAAc,EACd,cAAc,EACd,iBAAiB,GAClB,MAAM,WAAW,CAAC;AAEnB,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;AAErE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEjE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE7D,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,iBAAiB,EACjB,OAAO,EACP,cAAc,GACf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAE7E,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MA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AAM,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"}
@@ -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,CA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BAAmB,CACjC,OAA0B,EAC1B,KAAwB;IAExB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC1B,IAAI;QACJ,gBAAgB,EAAE,sBAAsB,CAAC,OAAO,EAAE,IAAI,CAAC;KACxD,CAAC,CAAC,CAAC;AACN,CAAC"}
package/dist/tsrv.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Two-Scale Realized Variance (TSRV) — Zhang, Mykland & Aït-Sahalia (2005).
3
+ *
4
+ * Plain realized variance is biased upward by microstructure noise, and sparse
5
+ * (subsampled) realized variance reduces that bias but doesn't remove it. TSRV
6
+ * removes it: it combines two sampling scales — a slow, subsampled RV and the
7
+ * fast all-ticks RV (which is essentially a pure measurement of the noise) — and
8
+ * subtracts a bias correction, yielding a *consistent* estimator of integrated
9
+ * variance that uses every observation.
10
+ *
11
+ * TSRV = (1 − n̄/n)⁻¹ · ( RV_sparse(K) − (n̄/n) · RV_all )
12
+ *
13
+ * where RV_all is the finest-grid Σrᵢ², RV_sparse(K) is realized variance on a
14
+ * grid K times coarser averaged over all K offsets (subsampling), and
15
+ * n̄ = (n − K + 1)/K is the average number of returns per slow subgrid. The
16
+ * leading factor is the small-sample bias adjustment. `K` (the slow scale) is
17
+ * best read off a volatility signature plot (see the `noise` module).
18
+ */
19
+ /**
20
+ * Two-scale realized variance: a microstructure-noise-consistent estimator of
21
+ * integrated variance. `slowScale` (K ≥ 2) is the coarse sampling factor.
22
+ * Returns plain realized variance for `slowScale < 2` (no second scale) and 0
23
+ * for fewer than two returns. In heavy-noise / very-small-sample cases the
24
+ * estimate can be slightly negative, like any bias-corrected variance
25
+ * estimator; floor it yourself if you need a non-negative value.
26
+ */
27
+ export declare function twoScaleRealizedVariance(returns: readonly number[], slowScale: number): number;
28
+ /**
29
+ * Two-scale realized volatility: the square root of `twoScaleRealizedVariance`,
30
+ * floored at 0 (a negative small-sample estimate is treated as zero variance).
31
+ */
32
+ export declare function twoScaleRealizedVolatility(returns: readonly number[], slowScale: number): number;
33
+ //# sourceMappingURL=tsrv.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tsrv.d.ts","sourceRoot":"","sources":["../src/tsrv.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAUH;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,SAAS,EAAE,MAAM,GAChB,MAAM,CAYR;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,SAAS,EAAE,MAAM,GAChB,MAAM,CAGR"}
package/dist/tsrv.js ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Two-Scale Realized Variance (TSRV) — Zhang, Mykland & Aït-Sahalia (2005).
3
+ *
4
+ * Plain realized variance is biased upward by microstructure noise, and sparse
5
+ * (subsampled) realized variance reduces that bias but doesn't remove it. TSRV
6
+ * removes it: it combines two sampling scales — a slow, subsampled RV and the
7
+ * fast all-ticks RV (which is essentially a pure measurement of the noise) — and
8
+ * subtracts a bias correction, yielding a *consistent* estimator of integrated
9
+ * variance that uses every observation.
10
+ *
11
+ * TSRV = (1 − n̄/n)⁻¹ · ( RV_sparse(K) − (n̄/n) · RV_all )
12
+ *
13
+ * where RV_all is the finest-grid Σrᵢ², RV_sparse(K) is realized variance on a
14
+ * grid K times coarser averaged over all K offsets (subsampling), and
15
+ * n̄ = (n − K + 1)/K is the average number of returns per slow subgrid. The
16
+ * leading factor is the small-sample bias adjustment. `K` (the slow scale) is
17
+ * best read off a volatility signature plot (see the `noise` module).
18
+ */
19
+ import { sparseRealizedVariance } from "./noise.js";
20
+ function realizedVarAll(returns) {
21
+ let s = 0;
22
+ for (const r of returns)
23
+ s += r * r;
24
+ return s;
25
+ }
26
+ /**
27
+ * Two-scale realized variance: a microstructure-noise-consistent estimator of
28
+ * integrated variance. `slowScale` (K ≥ 2) is the coarse sampling factor.
29
+ * Returns plain realized variance for `slowScale < 2` (no second scale) and 0
30
+ * for fewer than two returns. In heavy-noise / very-small-sample cases the
31
+ * estimate can be slightly negative, like any bias-corrected variance
32
+ * estimator; floor it yourself if you need a non-negative value.
33
+ */
34
+ export function twoScaleRealizedVariance(returns, slowScale) {
35
+ const n = returns.length;
36
+ const k = Math.floor(slowScale);
37
+ if (n < 2)
38
+ return 0;
39
+ if (k < 2)
40
+ return realizedVarAll(returns);
41
+ const rvSparse = sparseRealizedVariance(returns, k);
42
+ const rvAll = realizedVarAll(returns);
43
+ const nBar = (n - k + 1) / k;
44
+ const adj = 1 - nBar / n;
45
+ if (adj <= 0)
46
+ return rvSparse; // too few observations for the correction
47
+ return (rvSparse - (nBar / n) * rvAll) / adj;
48
+ }
49
+ /**
50
+ * Two-scale realized volatility: the square root of `twoScaleRealizedVariance`,
51
+ * floored at 0 (a negative small-sample estimate is treated as zero variance).
52
+ */
53
+ export function twoScaleRealizedVolatility(returns, slowScale) {
54
+ const v = twoScaleRealizedVariance(returns, slowScale);
55
+ return Math.sqrt(v > 0 ? v : 0);
56
+ }
57
+ //# sourceMappingURL=tsrv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tsrv.js","sourceRoot":"","sources":["../src/tsrv.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,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;;;;;;;GAOG;AACH,MAAM,UAAU,wBAAwB,CACtC,OAA0B,EAC1B,SAAiB;IAEjB,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IACzB,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAChC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC;IACpB,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,cAAc,CAAC,OAAO,CAAC,CAAC;IAE1C,MAAM,QAAQ,GAAG,sBAAsB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IACpD,MAAM,KAAK,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAC7B,MAAM,GAAG,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC;IACzB,IAAI,GAAG,IAAI,CAAC;QAAE,OAAO,QAAQ,CAAC,CAAC,0CAA0C;IACzE,OAAO,CAAC,QAAQ,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,GAAG,CAAC;AAC/C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CACxC,OAA0B,EAC1B,SAAiB;IAEjB,MAAM,CAAC,GAAG,wBAAwB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACvD,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAClC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "orderflow-metrics",
3
- "version": "0.25.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, Kyle's lambda, trade-sign classification, Amihud illiquidity.",
3
+ "version": "0.27.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, two-scale realized variance), 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
@@ -64,6 +64,16 @@ export {
64
64
  relativeJumpVariation,
65
65
  } from "./jumps.ts";
66
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";
73
+ export {
74
+ twoScaleRealizedVariance,
75
+ twoScaleRealizedVolatility,
76
+ } from "./tsrv.ts";
67
77
  export type { Semivariance } from "./semivar.ts";
68
78
  export {
69
79
  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
+ }
package/src/tsrv.ts ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Two-Scale Realized Variance (TSRV) — Zhang, Mykland & Aït-Sahalia (2005).
3
+ *
4
+ * Plain realized variance is biased upward by microstructure noise, and sparse
5
+ * (subsampled) realized variance reduces that bias but doesn't remove it. TSRV
6
+ * removes it: it combines two sampling scales — a slow, subsampled RV and the
7
+ * fast all-ticks RV (which is essentially a pure measurement of the noise) — and
8
+ * subtracts a bias correction, yielding a *consistent* estimator of integrated
9
+ * variance that uses every observation.
10
+ *
11
+ * TSRV = (1 − n̄/n)⁻¹ · ( RV_sparse(K) − (n̄/n) · RV_all )
12
+ *
13
+ * where RV_all is the finest-grid Σrᵢ², RV_sparse(K) is realized variance on a
14
+ * grid K times coarser averaged over all K offsets (subsampling), and
15
+ * n̄ = (n − K + 1)/K is the average number of returns per slow subgrid. The
16
+ * leading factor is the small-sample bias adjustment. `K` (the slow scale) is
17
+ * best read off a volatility signature plot (see the `noise` module).
18
+ */
19
+
20
+ import { sparseRealizedVariance } from "./noise.ts";
21
+
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
+ * Two-scale realized variance: a microstructure-noise-consistent estimator of
30
+ * integrated variance. `slowScale` (K ≥ 2) is the coarse sampling factor.
31
+ * Returns plain realized variance for `slowScale < 2` (no second scale) and 0
32
+ * for fewer than two returns. In heavy-noise / very-small-sample cases the
33
+ * estimate can be slightly negative, like any bias-corrected variance
34
+ * estimator; floor it yourself if you need a non-negative value.
35
+ */
36
+ export function twoScaleRealizedVariance(
37
+ returns: readonly number[],
38
+ slowScale: number,
39
+ ): number {
40
+ const n = returns.length;
41
+ const k = Math.floor(slowScale);
42
+ if (n < 2) return 0;
43
+ if (k < 2) return realizedVarAll(returns);
44
+
45
+ const rvSparse = sparseRealizedVariance(returns, k);
46
+ const rvAll = realizedVarAll(returns);
47
+ const nBar = (n - k + 1) / k;
48
+ const adj = 1 - nBar / n;
49
+ if (adj <= 0) return rvSparse; // too few observations for the correction
50
+ return (rvSparse - (nBar / n) * rvAll) / adj;
51
+ }
52
+
53
+ /**
54
+ * Two-scale realized volatility: the square root of `twoScaleRealizedVariance`,
55
+ * floored at 0 (a negative small-sample estimate is treated as zero variance).
56
+ */
57
+ export function twoScaleRealizedVolatility(
58
+ returns: readonly number[],
59
+ slowScale: number,
60
+ ): number {
61
+ const v = twoScaleRealizedVariance(returns, slowScale);
62
+ return Math.sqrt(v > 0 ? v : 0);
63
+ }