mfe.realized¶
mfe.realized ¶
mfe.realized — Realized volatility measures for HFT data.
price_filter ¶
price_filter(price: FloatArray, time: FloatArray, time_type: TimeType = TimeType.SECONDS, sampling_type: SamplingType = SamplingType.CALENDAR_TIME, sampling_interval: float | int | FloatArray = 300) -> tuple[FloatArray, FloatArray]
Filter raw tick prices to a regular grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
(N,) array of log or raw prices (function is agnostic)
|
|
required |
time
|
FloatArray
|
|
required |
time_type
|
how timestamps are encoded
|
|
SECONDS
|
sampling_type
|
sampling scheme
|
|
CALENDAR_TIME
|
sampling_interval
|
float | int | FloatArray
|
|
300
|
Returns:
| Type | Description |
|---|---|
(filtered_price, filtered_time) — both (M,) arrays
|
|
Source code in src/mfe/realized/sampling.py
returns_from_prices ¶
Compute log or simple returns from a price series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
(M,) filtered price array
|
|
required |
log
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
(M - 1,) return array
|
|
Source code in src/mfe/realized/sampling.py
refresh_time ¶
refresh_time(prices: list[FloatArray], times: list[FloatArray]) -> tuple[list[FloatArray], FloatArray]
Synchronize K asynchronous price series via refresh-time sampling (Barndorff-Nielsen et al. 2011).
For two assets this is O(N1 + N2) and vectorized. For K > 2 this loops over assets — TODO: Cython for K > 10.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
list of K (N_k,) price arrays
|
|
required |
times
|
list[FloatArray]
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
sync_prices |
list of K (M,) synchronized price arrays
|
|
sync_times |
(M,) refresh times
|
|
Source code in src/mfe/realized/sampling.py
realized_variance ¶
Standard realized variance: sum of squared returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
subsamples
|
number of sub-grids for sub-sampling bias correction
|
|
1
|
Returns:
| Type | Description |
|---|---|
RealizedResult with .value = RV and .subsampled_value = sub-sampled RV
|
|
Source code in src/mfe/realized/variance.py
realized_bipower_variation ¶
realized_bipower_variation(returns: FloatArray, skip: int = 0, subsamples: int = 1) -> RealizedResult
Realized bipower variation (BPV) with optional skip-k extension.
BPV = mu_1^{-2} * sum_{t=skip+2}^{T} |r_t| * |r_{t-skip-1}|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
skip
|
int
|
|
0
|
subsamples
|
sub-sampling replications for bias correction
|
|
1
|
Returns:
| Type | Description |
|---|---|
RealizedResult
|
.value = BPV .debiased_value = BPV * m/(m - skip - 1) where m = number of returns used |
Source code in src/mfe/realized/variance.py
realized_med_variance ¶
Median realized variance: robust to jumps.
MedRV = (pi / (6 - 4*sqrt(3) + pi)) * (M/(M-2)) * sum_{t=2}^{T-1} median(|r_{t-1}|, |r_t|, |r_{t+1}|)^2
Vectorized: uses np.partition (O(N), not O(N log N)) to find the median of each triplet without sorting. ~3x faster than the column_stack approach.
Source code in src/mfe/realized/variance.py
realized_min_variance ¶
Min realized variance: minimum of adjacent pairs of squared returns.
MinRV = (pi / (pi - 2)) * (M/(M-1)) * sum_{t=1}^{T-1} min(|r_t|, |r_{t+1}|)^2
Source code in src/mfe/realized/variance.py
realized_preaveraged_variance ¶
Pre-averaged realized variance (Jacod et al. 2009).
Uses a linear pre-averaging kernel g(x) = min(x, 1-x) with block size k_n = floor(theta * sqrt(n)).
This estimator is consistent even under microstructure noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(M,) log-return array
|
|
required |
theta
|
float
|
|
0.8
|
Source code in src/mfe/realized/variance.py
realized_semivariance ¶
Decompose RV into positive and negative semivariance.
RS+ = sum_{r > 0} r^2, RS- = sum_{r < 0} r^2
Returns (rs_pos, rs_neg).
Source code in src/mfe/realized/variance.py
realized_kernel ¶
realized_kernel(returns: FloatArray, kernel_type: KernelType = KernelType.PARZEN, bandwidth: int | None = None, jitter: bool = True) -> RealizedKernelResult
Realized kernel estimator for quadratic variation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
kernel_type
|
KernelType
|
|
PARZEN
|
bandwidth
|
int | None
|
|
None
|
jitter
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
RealizedKernelResult
|
|
Source code in src/mfe/realized/kernel.py
select_bandwidth ¶
select_bandwidth(returns: FloatArray, kernel_type: KernelType = KernelType.PARZEN, noise_variance: float | None = None, iq_lower_bound: float | None = None) -> int
Optimal bandwidth H for the realized kernel.
H* = c_star * xi^{4/5} * n^{3/5}
where xi = noise_variance / sqrt(IQ), and c_star depends on the kernel.
Source code in src/mfe/realized/kernel.py
realized_quarticity ¶
Realized quarticity: (n/3) * sum r_t^4
Consistent estimator of integrated quarticity IQ = int_0^1 sigma_t^4 dt.
Source code in src/mfe/realized/quarticity.py
realized_tripower_quarticity ¶
Tripower quarticity — robust to occasional jumps.
TPQ = n * mu_{4/3}^{-3} * mean(|r_{t-2}|^{4/3} |r_{t-1}|^{4/3} |r_t|^{4/3})
Source code in src/mfe/realized/quarticity.py
bns_jump_test ¶
Barndorff-Nielsen & Shephard (2006) jump test based on the ratio RV/BPV.
Z = sqrt(n) * (RV/BPV - 1) / sqrt(omega_hat)
Under the null of no jumps, Z -> N(0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(M,) log-return array
|
|
required |
alpha
|
float
|
|
0.05
|
Returns:
| Type | Description |
|---|---|
JumpTestResult
|
|
Source code in src/mfe/realized/jumps.py
estimate_noise_variance ¶
Estimate the microstructure noise variance omega^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(M,) log-return array at the finest available frequency
|
|
required |
method
|
str
|
|
'bandi-russell'
|
Returns:
| Type | Description |
|---|---|
float — noise variance estimate (>= 0)
|
|
Source code in src/mfe/realized/noise.py
realized_covariance ¶
Standard realized covariance matrix from synchronous returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(T, K) matrix of synchronous log-returns
|
|
required |
Returns:
| Type | Description |
|---|---|
RealizedCovarianceResult with .cov = (K, K) realized covariance matrix
|
|
Source code in src/mfe/realized/covariance.py
realized_correlation ¶
Realized correlation matrix from synchronous returns.
Returns (K, K) correlation matrix.
Source code in src/mfe/realized/covariance.py
realized_hayashi_yoshida ¶
realized_hayashi_yoshida(prices: list[FloatArray], times: list[FloatArray]) -> RealizedCovarianceResult
Hayashi-Yoshida realized covariance for K non-synchronously observed assets.
Hayashi, T. & Yoshida, N. (2005): "On Covariance Estimation of Non-Synchronously Observed Diffusion Processes", Bernoulli.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
list of K price arrays (lengths can differ)
|
|
required |
times
|
list[FloatArray]
|
|
required |
Returns:
| Type | Description |
|---|---|
RealizedCovarianceResult with (K, K) covariance matrix.
|
.method = "hayashi-yoshida" |
Notes
Diagonal elements are the standard realized variance of each asset (computed from their own tick data, so no synchronization needed).
K > 2 assets: implemented as O(K^2) bivariate calls. The MATLAB mfe-toolbox has a TODO here for the general case — we implement it.
Source code in src/mfe/realized/covariance.py
realized_covariance_refresh_time ¶
realized_covariance_refresh_time(prices: list[FloatArray], times: list[FloatArray]) -> RealizedCovarianceResult
Realized covariance using refresh-time synchronization.
Barndorff-Nielsen, Hansen, Lunde & Shephard (2011).
Synchronizes K asynchronous tick streams via refresh time, then computes the standard realized covariance on the synchronized returns.
Less efficient than HY (more data loss from synchronization) but gives a positive semi-definite matrix by construction.
Source code in src/mfe/realized/covariance.py
realized_range ¶
realized_range(high: FloatArray, low: FloatArray, open_: FloatArray | None = None, close: FloatArray | None = None) -> RealizedRangeResult
Realized range estimator from sub-interval high/low prices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
high
|
FloatArray
|
|
required |
low
|
FloatArray
|
|
required |
open_
|
(M,) optional — open log-price (used for Yang-Zhang correction)
|
|
None
|
close
|
(M,) optional — close log-price (used for Yang-Zhang correction)
|
|
None
|
Returns:
| Type | Description |
|---|---|
RealizedRangeResult
|
.value — realized range estimate of IV .n_intervals — M .efficiency — ~1.67 vs RV under Brownian motion |
Notes
Input prices can be raw or log — the function uses log(high) - log(low) which equals log(high/low) regardless of whether inputs are already logs. If inputs are already log-prices, pass them directly; the formula is the same either way (log(e^h) - log(e^l) = h - l).
Source code in src/mfe/realized/range_.py
realized_range_from_ticks ¶
realized_range_from_ticks(price: FloatArray, time: FloatArray, interval_seconds: float = 300.0) -> RealizedRangeResult
Compute realized range from raw tick data by aggregating into OHLC bars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
FloatArray
|
|
required |
time
|
FloatArray
|
|
required |
interval_seconds
|
bar width in seconds (default 5 minutes)
|
|
300.0
|
Source code in src/mfe/realized/range_.py
msrv ¶
Multi-Scale Realized Variance (MSRV) — Zhang (2006).
Combines J sub-sampled RVs with optimally chosen weights to achieve the best rate of convergence under i.i.d. microstructure noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
n_scales
|
number of scales J; if None uses min(N^{1/2}, 30)
|
|
None
|
Source code in src/mfe/realized/tsrv.py
realized_quantile_variance ¶
Realized quantile variance — jump-robust quadratic variation estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(T,) log-return array
|
|
required |
tau
|
float
|
|
0.5
|
Returns:
| Type | Description |
|---|---|
RealizedQuantileVarResult
|
.value — RQV estimate of integrated variance |
Source code in src/mfe/realized/quantile_var.py
realized_multivariate_kernel ¶
realized_multivariate_kernel(returns: FloatArray, kernel_type: KernelType = KernelType.PARZEN, bandwidth: int | None = None, jitter: bool = True) -> MultivariateKernelResult
Multivariate realized kernel for the full (K, K) covariance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
kernel_type
|
kernel weight function
|
|
PARZEN
|
bandwidth
|
int | None
|
|
None
|
jitter
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
MultivariateKernelResult
|
.rk — raw (K, K) realized kernel matrix .rk_adjusted — noise-corrected version (PSD enforced) |
Source code in src/mfe/realized/multivariate_kernel.py
covariance ¶
Realized covariance estimators for multivariate HFT data.
Andersen, Bollerslev, Diebold & Labys (2003): synchronous realized covariance. Hayashi & Yoshida (2005): non-synchronous covariance estimator. Barndorff-Nielsen et al. (2011): multivariate realized kernel.
Note on Hayashi-Yoshida for K > 2: The MATLAB realized_hayashi_yoshida.m has a TODO comment for K > 2 assets. We implement the general K-asset case by applying the bivariate HY estimator to each (i, j) pair and assembling the full matrix. This is O(K^2 * max(N_i, N_j)) and correct, but not the most efficient possible implementation for large K.
realized_covariance ¶
Standard realized covariance matrix from synchronous returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(T, K) matrix of synchronous log-returns
|
|
required |
Returns:
| Type | Description |
|---|---|
RealizedCovarianceResult with .cov = (K, K) realized covariance matrix
|
|
Source code in src/mfe/realized/covariance.py
realized_correlation ¶
Realized correlation matrix from synchronous returns.
Returns (K, K) correlation matrix.
Source code in src/mfe/realized/covariance.py
realized_hayashi_yoshida ¶
realized_hayashi_yoshida(prices: list[FloatArray], times: list[FloatArray]) -> RealizedCovarianceResult
Hayashi-Yoshida realized covariance for K non-synchronously observed assets.
Hayashi, T. & Yoshida, N. (2005): "On Covariance Estimation of Non-Synchronously Observed Diffusion Processes", Bernoulli.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
list of K price arrays (lengths can differ)
|
|
required |
times
|
list[FloatArray]
|
|
required |
Returns:
| Type | Description |
|---|---|
RealizedCovarianceResult with (K, K) covariance matrix.
|
.method = "hayashi-yoshida" |
Notes
Diagonal elements are the standard realized variance of each asset (computed from their own tick data, so no synchronization needed).
K > 2 assets: implemented as O(K^2) bivariate calls. The MATLAB mfe-toolbox has a TODO here for the general case — we implement it.
Source code in src/mfe/realized/covariance.py
realized_covariance_refresh_time ¶
realized_covariance_refresh_time(prices: list[FloatArray], times: list[FloatArray]) -> RealizedCovarianceResult
Realized covariance using refresh-time synchronization.
Barndorff-Nielsen, Hansen, Lunde & Shephard (2011).
Synchronizes K asynchronous tick streams via refresh time, then computes the standard realized covariance on the synchronized returns.
Less efficient than HY (more data loss from synchronization) but gives a positive semi-definite matrix by construction.
Source code in src/mfe/realized/covariance.py
jumps ¶
Jump detection tests.
Barndorff-Nielsen & Shephard (2006): "Econometrics of Testing for Jumps in Financial Economics Using Bipower Variation", JFEC.
Also includes the ratio-based test and the min/med variance jump test.
bns_jump_test ¶
Barndorff-Nielsen & Shephard (2006) jump test based on the ratio RV/BPV.
Z = sqrt(n) * (RV/BPV - 1) / sqrt(omega_hat)
Under the null of no jumps, Z -> N(0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(M,) log-return array
|
|
required |
alpha
|
float
|
|
0.05
|
Returns:
| Type | Description |
|---|---|
JumpTestResult
|
|
Source code in src/mfe/realized/jumps.py
kernel ¶
Realized kernel estimator.
Barndorff-Nielsen, Hansen, Lunde & Shephard (2008): "Designing Realized Kernels to Measure the Ex-Post Variation of Equity Prices in the Presence of Noise", Econometrica.
The estimator is: RK = sum_{h=-H}^{H} k(h/(H+1)) * gamma_h where gamma_h = sum_{t>|h|} r_t * r_{t-|h|} (autocovariance of returns).
Key design decisions vs. the MATLAB version: - parameter validation is fully separated from the hot path - bandwidth selection is a standalone function (easily unit-testable) - the inner autocovariance loop is in a Cython extension (_core.pyx); if unavailable we fall back to the numpy path here
select_bandwidth ¶
select_bandwidth(returns: FloatArray, kernel_type: KernelType = KernelType.PARZEN, noise_variance: float | None = None, iq_lower_bound: float | None = None) -> int
Optimal bandwidth H for the realized kernel.
H* = c_star * xi^{4/5} * n^{3/5}
where xi = noise_variance / sqrt(IQ), and c_star depends on the kernel.
Source code in src/mfe/realized/kernel.py
realized_kernel ¶
realized_kernel(returns: FloatArray, kernel_type: KernelType = KernelType.PARZEN, bandwidth: int | None = None, jitter: bool = True) -> RealizedKernelResult
Realized kernel estimator for quadratic variation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
kernel_type
|
KernelType
|
|
PARZEN
|
bandwidth
|
int | None
|
|
None
|
jitter
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
RealizedKernelResult
|
|
Source code in src/mfe/realized/kernel.py
multivariate_kernel ¶
Multivariate Realized Kernel.
Barndorff-Nielsen, Hansen, Lunde & Shephard (2011): "Multivariate Realised Kernels: Consistent Positive Semi-Definite Estimators of the Covariation of Equity Prices with Noise and Non-Synchronous Trading", Journal of Econometrics, 162(2), 149-169.
The multivariate realized kernel generalizes the univariate realized kernel to estimate the entire (K, K) covariance matrix simultaneously from synchronised returns (after refresh-time or calendar-time sampling).
RK_{ij} = sum_{h=-H}^{H} k(h/(H+1)) * Gamma_{h,ij}
where Gamma_{h,ij} = sum_{t>|h|} r_{i,t} * r_{j,t-|h|} is the cross-autocovariance.
The key property: the full (K, K) matrix is positive semi-definite by construction because all kernels are symmetric and the weight function k satisfies k(0)=1 and the matrix {Gamma_h} has a positive-semidefinite kernel-weighted combination.
Contrast with pairwise HY: HY applied to each (i,j) pair is not guaranteed to give a PSD matrix. The multivariate realized kernel IS PSD.
Computational note: The inner loop is O(H * T * K^2). For K=10, T=50K, H=30: ~1.5B ops. The Cython _core.pyx extension covers the K=2 case analytically; for general K we use numpy's matmul broadcasting.
realized_multivariate_kernel ¶
realized_multivariate_kernel(returns: FloatArray, kernel_type: KernelType = KernelType.PARZEN, bandwidth: int | None = None, jitter: bool = True) -> MultivariateKernelResult
Multivariate realized kernel for the full (K, K) covariance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
kernel_type
|
kernel weight function
|
|
PARZEN
|
bandwidth
|
int | None
|
|
None
|
jitter
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
MultivariateKernelResult
|
.rk — raw (K, K) realized kernel matrix .rk_adjusted — noise-corrected version (PSD enforced) |
Source code in src/mfe/realized/multivariate_kernel.py
noise ¶
Microstructure noise variance estimation.
Two approaches: 1. Bandi & Russell (2006): noise_var = -0.5 * mean(r_t * r_{t-1}) 2. Zhang, Mykland & Ait-Sahalia (2005): from the difference between full- frequency RV and a sub-sampled RV.
We default to the Bandi-Russell estimator as in the MATLAB mfe-toolbox.
estimate_noise_variance ¶
Estimate the microstructure noise variance omega^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(M,) log-return array at the finest available frequency
|
|
required |
method
|
str
|
|
'bandi-russell'
|
Returns:
| Type | Description |
|---|---|
float — noise variance estimate (>= 0)
|
|
Source code in src/mfe/realized/noise.py
quantile_var ¶
Realized Quantile Variance.
Christensen, Oomen & Podolskij (2010): "Realised Quantile-Based Estimation of the Integrated Variance", Journal of Econometrics, 159(1), 74-98.
The realized quantile variance uses the quantile of the distribution of intra-period squared returns rather than their sum. It is jump-robust: jumps appear as extreme outliers in the distribution of r_t^2 and are downweighted by choosing an appropriate quantile below 1.
For a given quantile probability tau in (0, 1):
RQV(tau) = c(tau) * mean_{t} ( r_t^2 * 1{r_t^2 <= q_tau} ) * T / floor(tau * T)
where q_tau is the empirical tau-quantile of {r_t^2} and c(tau) is a calibration constant that ensures consistency under a pure diffusion:
c(tau) = 1 / (chi2_cdf(chi2_ppf(tau, df=1), df=1) ← same as tau for chi2(1))
= 1 / tau (asymptotically, to leading order)
More precisely: since r_t^2 / sigma^2 ~ chi2(1) under normality, the expectation of the quantile-truncated version satisfies:
E[r_t^2 * 1{r_t^2 <= q_tau}] = sigma^2 * gamma(3/2, chi2_ppf(tau, 1)/2) / Gamma(3/2)
where gamma is the lower incomplete gamma function. We use this to calibrate.
Practical default: tau = 0.50 (median-based), which gives good jump robustness while retaining reasonable efficiency.
realized_quantile_variance ¶
Realized quantile variance — jump-robust quadratic variation estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(T,) log-return array
|
|
required |
tau
|
float
|
|
0.5
|
Returns:
| Type | Description |
|---|---|
RealizedQuantileVarResult
|
.value — RQV estimate of integrated variance |
Source code in src/mfe/realized/quantile_var.py
quarticity ¶
Realized quarticity and integrated quarticity estimators.
Used as inputs for CLT-based inference on RV and RK.
realized_quarticity ¶
Realized quarticity: (n/3) * sum r_t^4
Consistent estimator of integrated quarticity IQ = int_0^1 sigma_t^4 dt.
Source code in src/mfe/realized/quarticity.py
realized_tripower_quarticity ¶
Tripower quarticity — robust to occasional jumps.
TPQ = n * mu_{4/3}^{-3} * mean(|r_{t-2}|^{4/3} |r_{t-1}|^{4/3} |r_t|^{4/3})
Source code in src/mfe/realized/quarticity.py
range_ ¶
Realized range estimator.
Christensen, K. & Podolskij, M. (2007): "Realized Range-Based Estimation of Integrated Variance", Journal of Econometrics, 141(2), 323-349.
The realized range uses intra-interval high-low price ranges instead of squared returns. Under a continuous semimartingale, the range over a sub-interval [t_{j-1}, t_j] satisfies:
E[(log H_j - log L_j)^2] = 4 * log(2) * IV_j
where H_j (L_j) is the highest (lowest) price in the sub-interval. This is more efficient than squared returns due to the additional information in extreme intra-period prices.
The realized range estimator is: RR = (1 / (4 * log(2))) * sum_j (log H_j - log L_j)^2
Unlike RV it requires OHLC (open/high/low/close) data per sub-interval, which is the standard format from HFT bar data.
Also implements the normalized realized range for noise robustness.
realized_range ¶
realized_range(high: FloatArray, low: FloatArray, open_: FloatArray | None = None, close: FloatArray | None = None) -> RealizedRangeResult
Realized range estimator from sub-interval high/low prices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
high
|
FloatArray
|
|
required |
low
|
FloatArray
|
|
required |
open_
|
(M,) optional — open log-price (used for Yang-Zhang correction)
|
|
None
|
close
|
(M,) optional — close log-price (used for Yang-Zhang correction)
|
|
None
|
Returns:
| Type | Description |
|---|---|
RealizedRangeResult
|
.value — realized range estimate of IV .n_intervals — M .efficiency — ~1.67 vs RV under Brownian motion |
Notes
Input prices can be raw or log — the function uses log(high) - log(low) which equals log(high/low) regardless of whether inputs are already logs. If inputs are already log-prices, pass them directly; the formula is the same either way (log(e^h) - log(e^l) = h - l).
Source code in src/mfe/realized/range_.py
realized_range_from_ticks ¶
realized_range_from_ticks(price: FloatArray, time: FloatArray, interval_seconds: float = 300.0) -> RealizedRangeResult
Compute realized range from raw tick data by aggregating into OHLC bars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
FloatArray
|
|
required |
time
|
FloatArray
|
|
required |
interval_seconds
|
bar width in seconds (default 5 minutes)
|
|
300.0
|
Source code in src/mfe/realized/range_.py
sampling ¶
Price filtering and return computation for HFT tick data.
Implements the sampling schemes from the MATLAB mfe-toolbox realized module: - Calendar-time sampling (fixed clock intervals) - Business-time sampling (fixed tick intervals) - Calendar-uniform (uniform in clock time via interpolation) - Business-uniform (uniform in tick space) - Fixed-grid sampling
All functions operate on raw tick data (price, timestamp) and return a filtered (price, time) pair ready for return computation.
price_filter ¶
price_filter(price: FloatArray, time: FloatArray, time_type: TimeType = TimeType.SECONDS, sampling_type: SamplingType = SamplingType.CALENDAR_TIME, sampling_interval: float | int | FloatArray = 300) -> tuple[FloatArray, FloatArray]
Filter raw tick prices to a regular grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
(N,) array of log or raw prices (function is agnostic)
|
|
required |
time
|
FloatArray
|
|
required |
time_type
|
how timestamps are encoded
|
|
SECONDS
|
sampling_type
|
sampling scheme
|
|
CALENDAR_TIME
|
sampling_interval
|
float | int | FloatArray
|
|
300
|
Returns:
| Type | Description |
|---|---|
(filtered_price, filtered_time) — both (M,) arrays
|
|
Source code in src/mfe/realized/sampling.py
returns_from_prices ¶
Compute log or simple returns from a price series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
(M,) filtered price array
|
|
required |
log
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
(M - 1,) return array
|
|
Source code in src/mfe/realized/sampling.py
refresh_time ¶
refresh_time(prices: list[FloatArray], times: list[FloatArray]) -> tuple[list[FloatArray], FloatArray]
Synchronize K asynchronous price series via refresh-time sampling (Barndorff-Nielsen et al. 2011).
For two assets this is O(N1 + N2) and vectorized. For K > 2 this loops over assets — TODO: Cython for K > 10.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prices
|
list of K (N_k,) price arrays
|
|
required |
times
|
list[FloatArray]
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
sync_prices |
list of K (M,) synchronized price arrays
|
|
sync_times |
(M,) refresh times
|
|
Source code in src/mfe/realized/sampling.py
tsrv ¶
Two-Scale Realized Variance (TSRV).
Zhang, L., Mykland, P.A. & Ait-Sahalia, Y. (2005): "A Tale of Two Time Scales: Determining Integrated Volatility With Noisy High-Frequency Data", JASA, 100(472), 1394-1411.
TSRV is a bias-corrected realized variance that is consistent under i.i.d. microstructure noise. It uses two sampling frequencies:
TSRV = (1 / (1 - n_slow/n_fast)) * (RV_slow - (n_slow/n_fast) * RV_fast)
where: RV_slow = realized variance at a slower (e.g. 5-min) frequency RV_fast = realized variance at the fastest available frequency (all ticks) n_slow = number of slow-scale returns n_fast = number of fast-scale returns
The correction removes the leading noise bias term from RV_fast.
Contrast with preaveraged RV (Jacod et al. 2009): Pre-averaging is an alternative noise-robust estimator that is also consistent and semiparametrically efficient, but TSRV is simpler and easier to interpret as a bias correction of standard RV.
Multi-scale version (MSRV) is implemented as an extension.
Reference
Zhang (2006): "Efficient Estimation of Stochastic Volatility Using Noisy Observations: A Multi-Scale Approach", Bernoulli.
tsrv ¶
Two-Scale Realized Variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(N,) all-tick log-returns (finest available frequency)
|
|
required |
K
|
int | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
TSRVResult
|
.tsrv — the TSRV estimate of integrated variance .rv_fast — all-tick RV (noise-contaminated) .rv_slow — sub-sampled RV at scale K |
Source code in src/mfe/realized/tsrv.py
msrv ¶
Multi-Scale Realized Variance (MSRV) — Zhang (2006).
Combines J sub-sampled RVs with optimally chosen weights to achieve the best rate of convergence under i.i.d. microstructure noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
n_scales
|
number of scales J; if None uses min(N^{1/2}, 30)
|
|
None
|
Source code in src/mfe/realized/tsrv.py
variance ¶
Realized variance estimators.
All functions take log-returns (not prices) as input. The caller is responsible for sampling/filtering via realized.sampling.
References
Andersen & Bollerslev (1998) — realized variance Barndorff-Nielsen & Shephard (2004) — bipower variation Barndorff-Nielsen et al. (2008) — pre-averaged bipower variation Christensen & Podolskij (2007) — realized range Andersen, Dobrev & Schaumburg (2012) — realized min/med variance
realized_variance ¶
Standard realized variance: sum of squared returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
subsamples
|
number of sub-grids for sub-sampling bias correction
|
|
1
|
Returns:
| Type | Description |
|---|---|
RealizedResult with .value = RV and .subsampled_value = sub-sampled RV
|
|
Source code in src/mfe/realized/variance.py
realized_bipower_variation ¶
realized_bipower_variation(returns: FloatArray, skip: int = 0, subsamples: int = 1) -> RealizedResult
Realized bipower variation (BPV) with optional skip-k extension.
BPV = mu_1^{-2} * sum_{t=skip+2}^{T} |r_t| * |r_{t-skip-1}|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
FloatArray
|
|
required |
skip
|
int
|
|
0
|
subsamples
|
sub-sampling replications for bias correction
|
|
1
|
Returns:
| Type | Description |
|---|---|
RealizedResult
|
.value = BPV .debiased_value = BPV * m/(m - skip - 1) where m = number of returns used |
Source code in src/mfe/realized/variance.py
realized_med_variance ¶
Median realized variance: robust to jumps.
MedRV = (pi / (6 - 4*sqrt(3) + pi)) * (M/(M-2)) * sum_{t=2}^{T-1} median(|r_{t-1}|, |r_t|, |r_{t+1}|)^2
Vectorized: uses np.partition (O(N), not O(N log N)) to find the median of each triplet without sorting. ~3x faster than the column_stack approach.
Source code in src/mfe/realized/variance.py
realized_min_variance ¶
Min realized variance: minimum of adjacent pairs of squared returns.
MinRV = (pi / (pi - 2)) * (M/(M-1)) * sum_{t=1}^{T-1} min(|r_t|, |r_{t+1}|)^2
Source code in src/mfe/realized/variance.py
realized_preaveraged_variance ¶
Pre-averaged realized variance (Jacod et al. 2009).
Uses a linear pre-averaging kernel g(x) = min(x, 1-x) with block size k_n = floor(theta * sqrt(n)).
This estimator is consistent even under microstructure noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
(M,) log-return array
|
|
required |
theta
|
float
|
|
0.8
|
Source code in src/mfe/realized/variance.py
realized_semivariance ¶
Decompose RV into positive and negative semivariance.
RS+ = sum_{r > 0} r^2, RS- = sum_{r < 0} r^2
Returns (rs_pos, rs_neg).