orderflow-metrics 0.25.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 +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/noise.d.ts +51 -0
- package/dist/noise.d.ts.map +1 -0
- package/dist/noise.js +89 -0
- package/dist/noise.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +6 -0
- package/src/noise.ts +103 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,18 @@ 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
|
+
|
|
7
19
|
## [0.25.0] - 2026-08-26
|
|
8
20
|
|
|
9
21
|
### Added
|
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,8 @@ 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";
|
|
35
37
|
export type { Semivariance } from "./semivar.ts";
|
|
36
38
|
export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.ts";
|
|
37
39
|
export { shannonEntropy, normalizedEntropy, signEntropy, } from "./entropy.ts";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,k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AwB,EACxB,mBAAmB,GACpB,MAAM,eAAe,CAAC;AACvB,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,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,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,7 @@ 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";
|
|
22
23
|
export { realizedSemivariance, downsideVarianceRatio, signedJumpVariation, } from "./semivar.js";
|
|
23
24
|
export { shannonEntropy, normalizedEntropy, signEntropy, } from "./entropy.js";
|
|
24
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":[],"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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B,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/noise.d.ts
ADDED
|
@@ -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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AAE,sBAAsB,CAAC,OAAO,EAAE,IAAI,CAAC;KACxD,CAAC,CAAC,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "orderflow-metrics",
|
|
3
|
-
"version": "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.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
|
@@ -64,6 +64,12 @@ 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";
|
|
67
73
|
export type { Semivariance } from "./semivar.ts";
|
|
68
74
|
export {
|
|
69
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
|
+
}
|