orderflow-metrics 0.26.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,16 @@ 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
+
7
17
  ## [0.26.0] - 2026-08-27
8
18
 
9
19
  ### Added
package/dist/index.d.ts CHANGED
@@ -34,6 +34,7 @@ export { bipowerVariation, jumpVariation, relativeJumpVariation, } from "./jumps
34
34
  export { minRV, medRV, realizedQuarticity } from "./robustvol.ts";
35
35
  export type { SignaturePoint } from "./noise.ts";
36
36
  export { noiseVariance, sparseRealizedVariance, volatilitySignature, } from "./noise.ts";
37
+ export { twoScaleRealizedVariance, twoScaleRealizedVolatility, } from "./tsrv.ts";
37
38
  export type { Semivariance } from "./semivar.ts";
38
39
  export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.ts";
39
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;AAC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b,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
@@ -20,6 +20,7 @@ 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
22
  export { noiseVariance, sparseRealizedVariance, volatilitySignature, } from "./noise.js";
23
+ export { twoScaleRealizedVariance, twoScaleRealizedVolatility, } from "./tsrv.js";
23
24
  export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.js";
24
25
  export { shannonEntropy, normalizedEntropy, signEntropy, } from "./entropy.js";
25
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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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.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.",
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
@@ -70,6 +70,10 @@ export {
70
70
  sparseRealizedVariance,
71
71
  volatilitySignature,
72
72
  } from "./noise.ts";
73
+ export {
74
+ twoScaleRealizedVariance,
75
+ twoScaleRealizedVolatility,
76
+ } from "./tsrv.ts";
73
77
  export type { Semivariance } from "./semivar.ts";
74
78
  export {
75
79
  realizedSemivariance,
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
+ }