use crate::mathx;
use crate::params::ModelParams;
const TICKS_PER_SESSION: usize = 390;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct NameVariance {
pub weight: f64,
pub beta: f64,
pub sector: usize,
pub garch_variance: f64,
pub market_cap: f64,
}
impl NameVariance {
pub fn new(
weight: f64,
beta: f64,
sector: usize,
garch_variance: f64,
market_cap: f64,
) -> Self {
NameVariance { weight, beta, sector, garch_variance, market_cap }
}
}
pub fn intraday_variance_factor() -> f64 {
static FACTOR: std::sync::OnceLock<f64> = std::sync::OnceLock::new();
*FACTOR.get_or_init(|| {
let mut total = 0.0;
for i in 0..TICKS_PER_SESSION {
let t = i as f64 / TICKS_PER_SESSION as f64;
let m = crate::market::hours::intraday_vol(t);
total += m * m;
}
total / TICKS_PER_SESSION as f64
})
}
const INV_SQRT_TWO_PI: f64 = 0.3989422804014327;
const SQRT_TWO: f64 = std::f64::consts::SQRT_2;
fn standard_normal_pdf(c: f64) -> f64 {
INV_SQRT_TWO_PI * mathx::exp(-0.5 * c * c)
}
fn standard_normal_upper_tail(c: f64) -> f64 {
0.5 * mathx::erfc(c / SQRT_TWO)
}
pub fn amplifier_moments(slope: f64, threshold: f64, regime_ratio: f64) -> (f64, f64) {
if slope == 0.0 || !(regime_ratio > 0.0) {
return (1.0, 1.0);
}
let a = slope * regime_ratio;
let c = threshold / regime_ratio;
let phi = standard_normal_pdf(c);
let q = standard_normal_upper_tail(c);
let m2 = 2.0 * (c * phi + q);
let m3 = 2.0 * (c * c + 2.0) * phi;
let m4 = 2.0 * ((c * c * c + 3.0 * c) * phi + 3.0 * q);
let linear = m3 - c * m2;
let quadratic = m4 - 2.0 * c * m3 + c * c * m2;
(
1.0 + a * linear,
1.0 + 2.0 * a * linear + a * a * quadratic,
)
}
pub fn transmission_loadings(
beta_w: f64,
lag_multiplier: f64,
tilt: f64,
injection: f64,
) -> (f64, f64) {
let carried = beta_w * lag_multiplier;
let up = carried + injection;
let down = carried * (1.0 + tilt) + injection;
((up + down) * 0.5, (up * up + down * down) * 0.5)
}
fn idio_sigma_daily(p: &ModelParams, name: &NameVariance) -> f64 {
let daily_sigma = mathx::sqrt(mathx::max(name.garch_variance, p.idio_sigma_floor));
daily_sigma
* crate::market::factors::idio_scale_for(p, name.beta)
* crate::market::factors::cap_size_multiplier_with(p, name.market_cap)
}
fn name_noise_variance(
p: &ModelParams,
name: &NameVariance,
factor_variance: f64,
sector_sigma: f64,
k: f64,
) -> f64 {
let loading = crate::market::factors::sector_loading_for(p, name.beta);
let idio = idio_sigma_daily(p, name);
k * (name.beta * name.beta * factor_variance
+ loading * loading * sector_sigma * sector_sigma
+ idio * idio)
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct NameConstants {
pub(crate) loading: f64,
pub(crate) idio_unit: f64,
}
impl NameConstants {
pub(crate) fn of(p: &ModelParams, name: &NameVariance) -> Self {
NameConstants {
loading: crate::market::factors::sector_loading_for(p, name.beta),
idio_unit: crate::market::factors::idio_scale_for(p, name.beta)
* crate::market::factors::cap_size_multiplier_with(p, name.market_cap),
}
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn name_noise_variance_scaled(
p: &ModelParams,
c: &NameConstants,
beta: f64,
garch_variance: f64,
factor_variance: f64,
sector_sigma: f64,
idio_ratio: f64,
k: f64,
) -> f64 {
let idio = mathx::sqrt(mathx::max(garch_variance, p.idio_sigma_floor)) * c.idio_unit;
k * (beta * beta * factor_variance
+ c.loading * c.loading * sector_sigma * sector_sigma
+ idio * idio * idio_ratio)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn name_session_variance(
p: &ModelParams,
c: &NameConstants,
beta: f64,
garch_variance: f64,
factor_variance: f64,
sector_sigma: f64,
idio_ratio: f64,
jump_rate_scale: f64,
k: f64,
) -> f64 {
let (lambda_m, lambda_i) = jump_intensities(p, jump_rate_scale);
let mu = p.jump_mean_market;
let sig = p.jump_sigma_market;
let market_jump = lambda_m * (mu * mu + sig * sig) - (lambda_m * mu) * (lambda_m * mu);
name_noise_variance_scaled(p, c, beta, garch_variance, factor_variance, sector_sigma, idio_ratio, k)
+ market_jump
+ lambda_i * p.jump_sigma_idio * p.jump_sigma_idio
+ p.endogenous_news_intensity * p.endogenous_news_sigma * p.endogenous_news_sigma
}
fn jump_intensities(p: &ModelParams, rate_scale: f64) -> (f64, f64) {
if p.jump_vix_coupling == 0.0 {
(p.jump_intensity_market, p.jump_intensity_idio)
} else if p.jump_idio_vix_decoupled != 0.0 {
(p.jump_intensity_market * rate_scale, p.jump_intensity_idio)
} else {
(
p.jump_intensity_market * rate_scale,
p.jump_intensity_idio * rate_scale,
)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct IndexVarianceTerms {
pub factor_raw: f64,
pub sector_raw: f64,
pub idio_raw: f64,
pub crash_raw: f64,
pub crisis_raw: f64,
pub tilt_raw: f64,
pub k: f64,
pub market_jump: f64,
pub idio_jump: f64,
pub news: f64,
}
impl IndexVarianceTerms {
pub fn total(&self) -> f64 {
self.k
* (self.factor_raw + self.sector_raw + self.idio_raw + self.crash_raw
+ self.crisis_raw + self.tilt_raw)
+ self.market_jump
+ self.idio_jump
+ self.news
}
}
#[allow(clippy::too_many_arguments)]
pub fn index_conditional_variance(
p: &ModelParams,
names: &[NameVariance],
sector_count: usize,
factor_variance: f64,
sector_sigma: f64,
jump_rate_scale: f64,
crisis_spike: f64,
prev_day_down: bool,
k: f64,
) -> f64 {
index_conditional_variance_terms(
p, names, sector_count, factor_variance, sector_sigma, jump_rate_scale,
crisis_spike, prev_day_down, k)
.total()
}
#[allow(clippy::too_many_arguments)]
pub fn index_conditional_variance_terms(
p: &ModelParams,
names: &[NameVariance],
sector_count: usize,
factor_variance: f64,
sector_sigma: f64,
jump_rate_scale: f64,
crisis_spike: f64,
prev_day_down: bool,
k: f64,
) -> IndexVarianceTerms {
let sigmas = vec![sector_sigma; sector_count];
index_conditional_variance_terms_with_states(
p, names, sector_count, factor_variance, &sigmas, &[], &[], jump_rate_scale,
crisis_spike, prev_day_down, k)
}
#[allow(clippy::too_many_arguments)]
pub fn index_conditional_variance_terms_with_states(
p: &ModelParams,
names: &[NameVariance],
sector_count: usize,
factor_variance: f64,
sector_sigmas: &[f64],
jump_excitations: &[f64],
idio_ratios: &[f64],
jump_rate_scale: f64,
crisis_spike: f64,
prev_day_down: bool,
k: f64,
) -> IndexVarianceTerms {
let mut beta_w = 0.0;
let mut weight_sq = 0.0;
let mut idio_var = 0.0;
let mut excited_sq = 0.0;
let mut sector_loaded_total = 0.0;
let mut sector_loaded = vec![0.0; sector_count];
for (index, name) in names.iter().enumerate() {
beta_w += name.weight * name.beta;
weight_sq += name.weight * name.weight;
let idio = idio_sigma_daily(p, name);
match idio_ratios.get(index) {
Some(r) => idio_var += name.weight * name.weight * idio * idio * r,
None => idio_var += name.weight * name.weight * idio * idio,
}
if name.sector < sector_count {
let loaded = name.weight * crate::market::factors::sector_loading_for(p, name.beta);
sector_loaded[name.sector] += loaded;
sector_loaded_total += loaded;
}
if let Some(h) = jump_excitations.get(index) {
excited_sq += name.weight * name.weight * h;
}
}
let mut sector_var = 0.0;
for (s, loaded) in sector_loaded.iter().enumerate() {
let sector_sigma = sector_sigmas[s];
sector_var += loaded * loaded * sector_sigma * sector_sigma;
}
let factor_raw = beta_w * beta_w * factor_variance;
let regime_ratio = mathx::sqrt(factor_variance) / p.market_factor_sigma;
let amplifier_ratio = if p.crash_amplifier_conditional_sigma == 0.0 {
regime_ratio
} else if regime_ratio > 0.0 {
1.0
} else {
0.0
};
let (amp_1, amp_2) =
amplifier_moments(p.crash_amplifier_slope, p.crash_amplifier_threshold, amplifier_ratio);
let crash_raw = factor_raw * (amp_2 - 1.0);
let lag_multiplier = if p.market_beta_down_asym_lag == 0.0 || !prev_day_down {
1.0
} else {
1.0 + p.market_beta_down_asym_lag
};
let crisis_raw = if crisis_spike == 0.0 {
0.0
} else {
let q = p.crisis_blend_source;
let b = beta_w + q * p.crisis_blend_gain * crisis_spike;
let c = sector_loaded_total * crisis_spike * (1.0 - q);
let h = 1.0 - crisis_spike * (1.0 - q);
factor_variance * ((b * b - beta_w * beta_w) * amp_2 + 2.0 * b * c * amp_1 + c * c)
+ (h * h - 1.0) * sector_var
};
let tilt_raw = if p.market_beta_down_asym == 0.0 && lag_multiplier == 1.0 {
0.0
} else {
let injection = p.crisis_blend_source * p.crisis_blend_gain * crisis_spike;
let b = beta_w + injection;
let c = sector_loaded_total * crisis_spike * (1.0 - p.crisis_blend_source);
let (p1, p2) = transmission_loadings(
beta_w, lag_multiplier, p.market_beta_down_asym, injection);
factor_variance * ((p2 - b * b) * amp_2 + 2.0 * c * (p1 - b) * amp_1)
};
let (lambda_m, lambda_i) = jump_intensities(p, jump_rate_scale);
let mu = p.jump_mean_market;
let sig = p.jump_sigma_market;
let market_jump = lambda_m * (mu * mu + sig * sig) - (lambda_m * mu) * (lambda_m * mu);
let idio_jump = weight_sq * lambda_i * p.jump_sigma_idio * p.jump_sigma_idio;
let idio_jump = if excited_sq != 0.0 {
idio_jump + excited_sq * lambda_i * p.jump_sigma_idio * p.jump_sigma_idio
} else {
idio_jump
};
let news = weight_sq * p.endogenous_news_intensity
* p.endogenous_news_sigma
* p.endogenous_news_sigma;
IndexVarianceTerms {
factor_raw,
sector_raw: sector_var,
idio_raw: idio_var,
crash_raw,
crisis_raw,
tilt_raw,
k,
market_jump,
idio_jump,
news,
}
}
const RESTING_VARIANCE_PASSES: usize = 32;
pub fn resting_garch_variances(
p: &ModelParams,
names: &[NameVariance],
sector_base_variances: &[f64],
factor_variance: f64,
sector_sigma: f64,
k: f64,
) -> Vec<f64> {
let mut v: Vec<f64> = names.iter().map(|n| n.garch_variance).collect();
let shock_share = p.garch_alpha + p.garch_gamma / 2.0;
for _ in 0..RESTING_VARIANCE_PASSES {
for (i, name) in names.iter().enumerate() {
let base = sector_base_variances.get(i).copied().unwrap_or(v[i]);
let beta_i = crate::market::garch::garch_beta_for(p, name.market_cap);
if !(beta_i < 1.0) {
continue;
}
let probe = NameVariance { garch_variance: v[i], ..*name };
let r2 = name_noise_variance(p, &probe, factor_variance, sector_sigma, k);
let rest = (p.garch_omega + shock_share * r2) / (1.0 - beta_i);
v[i] = mathx::max(
mathx::min(rest, base * p.garch_ceiling_multiple),
base * p.garch_floor_multiple,
);
}
}
v
}
pub fn index_unconditional_variance(
p: &ModelParams,
names: &[NameVariance],
sector_count: usize,
sector_base_variances: &[f64],
) -> f64 {
let k = intraday_variance_factor();
let factor_variance = p.market_factor_sigma * p.market_factor_sigma;
let sector_sigma = p.sector_factor_sigma;
let resting = resting_garch_variances(
p, names, sector_base_variances, factor_variance, sector_sigma, k);
let at_rest: Vec<NameVariance> = names
.iter()
.enumerate()
.map(|(i, n)| NameVariance { garch_variance: resting[i], ..*n })
.collect();
if p.market_beta_down_asym_lag == 0.0 {
return index_conditional_variance(
p, &at_rest, sector_count, factor_variance, sector_sigma, 1.0, 0.0, false, k);
}
let unlagged = index_conditional_variance(
p, &at_rest, sector_count, factor_variance, sector_sigma, 1.0, 0.0, false, k);
let lagged = index_conditional_variance(
p, &at_rest, sector_count, factor_variance, sector_sigma, 1.0, 0.0, true, k);
0.5 * (unlagged + lagged)
}
pub fn vix_from_variance(premium: f64, variance: f64) -> f64 {
(1.0 + premium) * 100.0 * mathx::sqrt(252.0 * variance)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::params::PT_V18;
#[test]
fn sqrt_two_is_the_double_the_literal_was() {
assert_eq!(SQRT_TWO.to_bits(), 0x3FF6_A09E_667F_3BCD);
#[allow(clippy::approx_constant)]
let literal: f64 = 1.4142135623730951;
assert_eq!(SQRT_TWO.to_bits(), literal.to_bits());
}
fn roster() -> Vec<NameVariance> {
(0..8)
.map(|i| NameVariance {
weight: 1.0 / 8.0,
beta: 0.8 + 0.05 * i as f64,
sector: i % 3,
garch_variance: 0.000225,
market_cap: 2.0e10,
})
.collect()
}
#[test]
fn the_intraday_factor_is_the_curves_own_second_moment() {
let k = intraday_variance_factor();
let integral = 1.0 + 2.0 / 25.0 + 1.0 / 225.0;
assert!((k - integral).abs() < 5e-4, "K = {k}, integral = {integral}");
assert!(k > 1.0, "the curve must inflate variance, not deflate it");
}
#[test]
fn each_term_enters_at_the_size_the_identity_gives_it() {
let names = roster();
let k = intraday_variance_factor();
let mut p = PT_V18;
p.sector_factor_sigma = 0.0;
p.jump_intensity_market = 0.0;
p.jump_intensity_idio = 0.0;
p.endogenous_news_intensity = 0.0;
p.garch_omega = 0.0;
p.crash_amplifier_slope = 0.0;
let v_f = p.market_factor_sigma * p.market_factor_sigma;
let only_factor =
index_conditional_variance(&p, &names, 3, v_f, 0.0, 1.0, 0.0, false, k);
let beta_w: f64 = names.iter().map(|n| n.weight * n.beta).sum();
let idio: f64 = names
.iter()
.map(|n| {
let s = idio_sigma_daily(&p, n);
n.weight * n.weight * s * s
})
.sum();
let d = p.market_beta_down_asym;
let tilt_2 = (1.0 + (1.0 + d) * (1.0 + d)) / 2.0;
let want = k * (beta_w * beta_w * v_f * tilt_2 + idio);
assert!((only_factor - want).abs() < 1e-18, "{only_factor} vs {want}");
let lagged =
index_conditional_variance(&p, &names, 3, v_f, 0.0, 1.0, 0.0, true, k);
let l = 1.0 + p.market_beta_down_asym_lag;
let want_lagged = k * (beta_w * beta_w * v_f * l * l * tilt_2 + idio);
assert!((lagged - want_lagged).abs() < 1e-18, "{lagged} vs {want_lagged}");
let mut with_news = p.clone();
with_news.endogenous_news_intensity = 0.05;
with_news.endogenous_news_sigma = 0.02;
let news = index_conditional_variance(&with_news, &names, 3, v_f, 0.0, 1.0, 0.0, false, k)
- only_factor;
let weight_sq: f64 = names.iter().map(|n| n.weight * n.weight).sum();
assert!((news - weight_sq * 0.05 * 0.02 * 0.02).abs() < 1e-20, "news {news}");
}
#[test]
fn the_intraday_curve_does_not_reach_the_jumps() {
let names = roster();
let k = intraday_variance_factor();
let mut p = PT_V18;
p.sector_factor_sigma = 0.0;
p.endogenous_news_intensity = 0.0;
let v_f = p.market_factor_sigma * p.market_factor_sigma;
let at_k = index_conditional_variance(&p, &names, 3, v_f, 0.0, 1.0, 0.0, false, k);
let at_one = index_conditional_variance(&p, &names, 3, v_f, 0.0, 1.0, 0.0, false, 1.0);
let noise_at_one = (at_k - at_one) / (k - 1.0);
let jumps = at_one - noise_at_one;
assert!(jumps > 0.0, "the jump block vanished: {jumps}");
let (lm, li) = jump_intensities(&p, 1.0);
let mu = p.jump_mean_market;
let sg = p.jump_sigma_market;
let weight_sq: f64 = names.iter().map(|n| n.weight * n.weight).sum();
let want = lm * (mu * mu + sg * sg) - (lm * mu) * (lm * mu)
+ weight_sq * li * p.jump_sigma_idio * p.jump_sigma_idio;
assert!((jumps - want).abs() < 1e-15, "{jumps} vs {want}");
}
#[test]
fn the_resting_variance_iteration_has_stopped_moving() {
let names = roster();
let bases = vec![0.000225; names.len()];
let k = intraday_variance_factor();
let p = PT_V18;
let v_f = p.market_factor_sigma * p.market_factor_sigma;
let settled = resting_garch_variances(
&p, &names, &bases, v_f, p.sector_factor_sigma, k);
let restarted: Vec<NameVariance> = names
.iter()
.enumerate()
.map(|(i, n)| NameVariance { garch_variance: settled[i], ..*n })
.collect();
let again = resting_garch_variances(
&p, &restarted, &bases, v_f, p.sector_factor_sigma, k);
for (i, (a, b)) in settled.iter().zip(again.iter()).enumerate() {
assert!((a - b).abs() <= a.abs() * 1e-12, "name {i}: {a} then {b}");
}
}
#[test]
fn the_process_rests_below_the_variance_the_roster_opens_at() {
let names = roster();
let bases = vec![0.000225; names.len()];
let k = intraday_variance_factor();
let p = PT_V18;
let settled = resting_garch_variances(
&p, &names, &bases, p.market_factor_sigma * p.market_factor_sigma,
p.sector_factor_sigma, k);
for (i, v) in settled.iter().enumerate() {
assert!(*v < 0.000225, "name {i} rests at {v}, at or above its seed");
}
}
fn roster_with_a_stray_sector() -> Vec<NameVariance> {
let mut names = roster();
names[3].sector = 99;
names[5].market_cap = 4.0e11;
names[6].garch_variance = 0.0009;
names
}
fn the_sum_as_it_was_written(
p: &ModelParams,
names: &[NameVariance],
sector_count: usize,
factor_variance: f64,
sector_sigma: f64,
jump_rate_scale: f64,
k: f64,
) -> f64 {
let mut beta_w = 0.0;
let mut weight_sq = 0.0;
let mut idio_var = 0.0;
let mut sector_loaded = vec![0.0; sector_count];
for name in names {
beta_w += name.weight * name.beta;
weight_sq += name.weight * name.weight;
let idio = idio_sigma_daily(p, name);
idio_var += name.weight * name.weight * idio * idio;
if name.sector < sector_count {
sector_loaded[name.sector] +=
name.weight * crate::market::factors::sector_loading_for(p, name.beta);
}
}
let mut sector_var = 0.0;
for loaded in sector_loaded.iter() {
sector_var += loaded * loaded * sector_sigma * sector_sigma;
}
let noise = k * (beta_w * beta_w * factor_variance + sector_var + idio_var);
let (lambda_m, lambda_i) = jump_intensities(p, jump_rate_scale);
let mu = p.jump_mean_market;
let sig = p.jump_sigma_market;
let market_jump = lambda_m * (mu * mu + sig * sig) - (lambda_m * mu) * (lambda_m * mu);
let idio_jump = weight_sq * lambda_i * p.jump_sigma_idio * p.jump_sigma_idio;
let news = weight_sq * p.endogenous_news_intensity
* p.endogenous_news_sigma
* p.endogenous_news_sigma;
noise + market_jump + idio_jump + news
}
fn parameter_spread() -> Vec<(&'static str, ModelParams)> {
let mut uncoupled = PT_V18;
uncoupled.jump_vix_coupling = 0.0;
let mut noisy = PT_V18;
noisy.endogenous_news_intensity = 0.35;
noisy.endogenous_news_sigma = 0.031;
noisy.jump_intensity_idio = 0.0;
let mut no_jumps = PT_V18;
no_jumps.jump_intensity_market = 0.0;
no_jumps.jump_intensity_idio = 0.0;
vec![
("pt-v18", PT_V18),
("uncoupled jumps", uncoupled),
("news on", noisy),
("no jumps", no_jumps),
]
}
#[test]
fn the_calm_regime_reproduces_the_sum_it_replaced() {
let k_curve = intraday_variance_factor();
for (label, mut p) in parameter_spread() {
p.crash_amplifier_slope = 0.0;
p.market_beta_down_asym = 0.0;
p.market_beta_down_asym_lag = 0.0;
for names in [roster(), roster_with_a_stray_sector()] {
for §or_count in &[3usize, 5] {
for i in 0..=12 {
let factor_variance = 1.0e-6 + i as f64 * 8.3e-5;
for j in 0..=6 {
let sector_sigma = j as f64 * 0.0061;
for &scale in &[0.0, 0.25, 1.0, 1.7, 4.9, 27.5] {
for &k in &[1.0, k_curve, 2.25] {
for &lagged in &[false, true] {
let want = the_sum_as_it_was_written(
&p, &names, sector_count, factor_variance,
sector_sigma, scale, k);
let terms = index_conditional_variance_terms(
&p, &names, sector_count, factor_variance,
sector_sigma, scale, 0.0, lagged, k);
assert_eq!(terms.crash_raw, 0.0, "{label}: amplifier off");
assert_eq!(terms.crisis_raw, 0.0, "{label}: no crisis");
assert_eq!(terms.tilt_raw, 0.0, "{label}: no tilt");
assert_eq!(
terms.total(), want,
"{label}: sectors {sector_count}, v_f \
{factor_variance}, sigma_s {sector_sigma}, \
scale {scale}, k {k}, lagged {lagged}");
assert_eq!(
index_conditional_variance(
&p, &names, sector_count, factor_variance,
sector_sigma, scale, 0.0, lagged, k),
want,
"{label}: the summing form moved");
}
}
}
}
}
}
}
}
}
#[test]
fn each_term_carries_the_block_its_name_says() {
let k = intraday_variance_factor();
let p = PT_V18;
let names = roster_with_a_stray_sector();
const SPIKE: f64 = 0.61;
let (v_f, sigma_s, scale) = (0.00021, 0.0093, 1.6);
let terms = index_conditional_variance_terms(
&p, &names, 3, v_f, sigma_s, scale, SPIKE, false, k);
let beta_w: f64 = names.iter().map(|n| n.weight * n.beta).sum();
assert_eq!(terms.factor_raw, beta_w * beta_w * v_f, "factor_raw");
let mut loaded = [0.0; 3];
for n in names.iter() {
if n.sector < 3 {
loaded[n.sector] +=
n.weight * crate::market::factors::sector_loading_for(&p, n.beta);
}
}
let mut sector_var = 0.0;
for l in loaded.iter() {
sector_var += l * l * sigma_s * sigma_s;
}
assert_eq!(terms.sector_raw, sector_var, "sector_raw");
let mut idio_var = 0.0;
for n in names.iter() {
let s = idio_sigma_daily(&p, n);
idio_var += n.weight * n.weight * s * s;
}
assert_eq!(terms.idio_raw, idio_var, "idio_raw");
let s = mathx::sqrt(v_f) / p.market_factor_sigma;
let (amp_1, amp_2) =
amplifier_moments(p.crash_amplifier_slope, p.crash_amplifier_threshold, s);
assert!(amp_2 > amp_1 && amp_1 > 1.0, "the moments ordered wrong: {amp_1}, {amp_2}");
assert_eq!(terms.crash_raw, beta_w * beta_w * v_f * (amp_2 - 1.0), "crash_raw");
let l_w: f64 = names
.iter()
.filter(|n| n.sector < 3)
.map(|n| n.weight * crate::market::factors::sector_loading_for(&p, n.beta))
.sum();
let q = p.crisis_blend_source;
let b = beta_w + q * p.crisis_blend_gain * SPIKE;
let c = l_w * SPIKE * (1.0 - q);
let h = 1.0 - SPIKE * (1.0 - q);
assert_eq!(
terms.crisis_raw,
v_f * ((b * b - beta_w * beta_w) * amp_2 + 2.0 * b * c * amp_1 + c * c)
+ (h * h - 1.0) * sector_var,
"crisis_raw");
assert_eq!(terms.k, k, "k is the argument, not a recomputation");
let (lm, li) = jump_intensities(&p, scale);
let (mu, sg) = (p.jump_mean_market, p.jump_sigma_market);
assert_eq!(terms.market_jump,
lm * (mu * mu + sg * sg) - (lm * mu) * (lm * mu), "market_jump");
let weight_sq: f64 = names.iter().map(|n| n.weight * n.weight).sum();
assert_eq!(terms.idio_jump,
weight_sq * li * p.jump_sigma_idio * p.jump_sigma_idio, "idio_jump");
assert_eq!(terms.news,
weight_sq * p.endogenous_news_intensity
* p.endogenous_news_sigma * p.endogenous_news_sigma, "news");
let no_sector = index_conditional_variance_terms(
&p, &names, 3, v_f, 0.0, scale, SPIKE, false, k);
assert_eq!(no_sector.sector_raw, 0.0,
"the sector block did not follow its sigma");
assert_eq!(no_sector.idio_raw, terms.idio_raw,
"the sector sigma reached idio_raw");
assert_eq!(no_sector.factor_raw, terms.factor_raw,
"the sector sigma reached factor_raw");
let mut quiet = p.clone();
quiet.endogenous_news_intensity = 0.0;
let no_news = index_conditional_variance_terms(
&quiet, &names, 3, v_f, sigma_s, scale, SPIKE, false, k);
assert_eq!(no_news.news, 0.0,
"the news block did not follow its intensity");
assert_eq!(no_news.idio_jump, terms.idio_jump,
"the news intensity reached idio_jump");
let no_factor = index_conditional_variance_terms(
&p, &names, 3, 0.0, sigma_s, scale, SPIKE, false, k);
assert_eq!(no_factor.factor_raw, 0.0,
"the factor block did not follow its variance");
assert_eq!(no_factor.sector_raw, terms.sector_raw,
"the factor variance reached sector_raw");
let mut unamplified = p.clone();
unamplified.crash_amplifier_slope = 0.0;
let flat = index_conditional_variance_terms(
&unamplified, &names, 3, v_f, sigma_s, scale, SPIKE, false, k);
assert_eq!(flat.crash_raw, 0.0, "the amplifier block did not follow its slope");
assert_eq!(flat.factor_raw, terms.factor_raw, "the slope reached factor_raw");
assert_eq!(flat.sector_raw, terms.sector_raw, "the slope reached sector_raw");
assert!(flat.crisis_raw > 0.0 && flat.crisis_raw < terms.crisis_raw,
"the blend must survive the amplifier being off, and be smaller \
without it: {} against {}", flat.crisis_raw, terms.crisis_raw);
let calm = index_conditional_variance_terms(
&p, &names, 3, v_f, sigma_s, scale, 0.0, false, k);
assert_eq!(calm.crisis_raw, 0.0, "the blend block did not follow its spike");
assert_eq!(calm.crash_raw, terms.crash_raw, "the spike reached crash_raw");
assert_eq!(calm.sector_raw, terms.sector_raw, "the spike reached sector_raw");
assert_eq!(calm.factor_raw, terms.factor_raw, "the spike reached factor_raw");
assert!(terms.total() > calm.total(),
"a crisis must cost more variance than no crisis");
}
#[test]
fn the_blend_at_source_zero_takes_from_the_sector_and_gives_to_the_market() {
let k = intraday_variance_factor();
let mut p = PT_V18;
p.crisis_blend_source = 0.0;
p.crash_amplifier_slope = 0.0;
let names = roster();
let (v_f, sigma_s, spike) = (0.00021, 0.0093, 0.55);
let terms =
index_conditional_variance_terms(&p, &names, 3, v_f, sigma_s, 1.0, spike, false, k);
let beta_w: f64 = names.iter().map(|n| n.weight * n.beta).sum();
let l_w: f64 = names
.iter()
.map(|n| n.weight * crate::market::factors::sector_loading_for(&p, n.beta))
.sum();
let c = l_w * spike;
let h = 1.0 - spike;
let want = v_f * (2.0 * beta_w * c + c * c) + (h * h - 1.0) * terms.sector_raw;
assert_eq!(terms.crisis_raw, want, "the source-zero branch");
assert!(terms.crash_raw == 0.0, "the amplifier was switched off");
let attenuation = (h * h - 1.0) * terms.sector_raw;
assert!(attenuation < 0.0, "the attenuation must be negative: {attenuation}");
assert!((attenuation / terms.sector_raw + 0.7975).abs() < 1e-9,
"1 - (1 - 0.55)^2 is 0.7975 of the sector block");
}
fn integrate_against_the_normal(f: impl Fn(f64) -> f64) -> f64 {
const HALF_WIDTH: f64 = 12.0;
const PANELS: usize = 240_000;
let h = 2.0 * HALF_WIDTH / PANELS as f64;
let mut total = 0.0;
for i in 0..=PANELS {
let z = -HALF_WIDTH + i as f64 * h;
let w = if i == 0 || i == PANELS {
1.0
} else if i % 2 == 1 {
4.0
} else {
2.0
};
total += w * f(z) * standard_normal_pdf(z);
}
total * h / 3.0
}
#[test]
fn the_amplifier_moments_are_the_moments_they_claim() {
for &m in &[0.05, 0.2, 0.5] {
for &t in &[1.5, 2.0, 3.0] {
for &s in &[0.3, 0.6, 0.965, 1.0, 1.32, 1.5, 2.04, 3.0] {
let amp = |z: f64| {
let mag = z.abs() * s;
if mag > t { 1.0 + (mag - t) * m } else { 1.0 }
};
let want_1 = integrate_against_the_normal(|z| z * z * amp(z));
let want_2 = integrate_against_the_normal(|z| z * z * amp(z) * amp(z));
let (got_1, got_2) = amplifier_moments(m, t, s);
assert!((got_1 - want_1).abs() < 1e-9 * want_1,
"E[z^2 A] at m {m}, T {t}, s {s}: {got_1} against {want_1}");
assert!((got_2 - want_2).abs() < 1e-9 * want_2,
"E[z^2 A^2] at m {m}, T {t}, s {s}: {got_2} against {want_2}");
}
}
}
assert_eq!(amplifier_moments(0.0, 2.0, 1.0), (1.0, 1.0));
assert_eq!(amplifier_moments(0.2, 2.0, 0.0), (1.0, 1.0));
}
#[test]
fn the_amplifier_costs_more_the_deeper_the_regime() {
let p = PT_V18;
let mut last = 0.0;
for i in 1..=40 {
let s = i as f64 * 0.075;
let (_, amp_2) =
amplifier_moments(p.crash_amplifier_slope, p.crash_amplifier_threshold, s);
assert!(amp_2 >= last, "the moment fell from {last} at s {s}");
assert!(amp_2 >= 1.0, "the amplifier cannot reduce variance: {amp_2}");
if s >= 0.4 {
assert!(amp_2 > last, "the moment stopped rising at s {s}: {amp_2}");
}
last = amp_2;
}
let (_, quiet) =
amplifier_moments(p.crash_amplifier_slope, p.crash_amplifier_threshold, 0.3);
assert!(quiet - 1.0 < 1e-6, "silent means silent: {quiet}");
let (_, at_anchor) =
amplifier_moments(p.crash_amplifier_slope, p.crash_amplifier_threshold, 1.0);
assert!(at_anchor > 1.03 && at_anchor < 1.08,
"the amplifier at the anchor reads {at_anchor}");
}
#[test]
fn the_conditional_normaliser_is_flat_in_the_regime() {
let k = intraday_variance_factor();
let names = roster();
let mut p = crate::params::PT_V19;
assert_eq!(p.crash_amplifier_conditional_sigma, 1.0,
"pt-v19 is the preset this dial was added for");
let base = p.market_factor_sigma * p.market_factor_sigma;
let at = |p: &ModelParams, ratio: f64| {
index_conditional_variance_terms(
p, &names, 3, base * ratio, p.sector_factor_sigma, 1.0, 0.0, false, k)
};
let anchor = at(&p, 1.0);
let flat = anchor.crash_raw / anchor.factor_raw;
assert!(flat > 0.0, "the amplifier must still cost something: {flat}");
let tol = 8.0 * f64::EPSILON;
let slope = (anchor.factor_raw + anchor.crash_raw) / base;
for &ratio in &[0.25, 0.5, 1.0, 2.0, 3.74, 9.0, 21.68, 40.0] {
let terms = at(&p, ratio);
let share = terms.crash_raw / terms.factor_raw;
assert!((share - flat).abs() <= tol * flat,
"the amplifier's share moved at v_f = {ratio} x base: \
{share} against {flat}");
let got = (terms.factor_raw + terms.crash_raw) / (base * ratio);
assert!((got - slope).abs() <= tol * slope,
"the amplified factor block stopped being linear in v_f \
at {ratio} x base: {got} against {slope}");
}
p.crash_amplifier_conditional_sigma = 0.0;
let shallow = at(&p, 1.0);
let deep = at(&p, 21.68);
assert!(deep.crash_raw / deep.factor_raw
> 4.0 * shallow.crash_raw / shallow.factor_raw,
"the baseline normaliser's share barely moved: {} against {}",
deep.crash_raw / deep.factor_raw,
shallow.crash_raw / shallow.factor_raw);
}
struct NoNoise;
impl crate::rng::Rng for NoNoise {
fn next_f64(&mut self) -> f64 {
0.5
}
fn next_normal(&mut self) -> f64 {
0.0
}
}
#[test]
fn the_regime_terms_reproduce_the_index_the_tick_builds() {
let k = intraday_variance_factor();
let names = roster();
for &source in &[1.0, 0.0] {
for &(tilt, lag, lagged) in &[
(0.0, 0.0, false), (0.025, 0.0, false), (0.025, 0.375, false), (0.025, 0.375, true), (0.2, 1.1, true), ] {
for &spike in &[0.0, 0.42, 0.98] {
for &v_f in &[3.0e-5, 5.766e-5, 2.4e-4] {
let mut p = PT_V18;
p.crisis_blend_source = source;
p.market_beta_down_asym = tilt;
p.market_beta_down_asym_lag = lag;
let sigma_tick = mathx::sqrt(v_f) / mathx::sqrt(390.0);
let index_at = |z: f64| {
let mut total = 0.0;
for (i, n) in names.iter().enumerate() {
let company = crate::market::factors::FactorCompany {
id: format!("N{i}"),
sector: format!("S{}", n.sector),
beta: Some(n.beta),
market_cap: n.market_cap,
avg_volume: 1.0e7,
shares_outstanding: 1.0e9,
short_interest: 0.0,
float: 1.0e9,
garch_variance: n.garch_variance,
last_daily_return: Some(0.0),
};
let shared = crate::market::factors::SharedFactors {
market_factor: z * sigma_tick,
sector_factors: (0..3)
.map(|s| (format!("S{s}"), 0.0))
.collect(),
crisis_spike: spike,
prev_day_down: lagged,
market_sigma_tick: sigma_tick,
crisis_epicentre: None,
};
let out = crate::market::factors::calculate_live_factors(
&company, &[], 0.0, 1.0, &shared, &p, &mut NoNoise);
total += n.weight * out.random_noise;
}
total
};
let index_at = |z: f64| {
let base = index_at(z);
if source == 1.0 || spike == 0.0 {
base
} else {
let leak: f64 = names
.iter()
.map(|n| {
n.weight
* crate::market::factors::sector_loading_for(&p, n.beta)
})
.sum::<f64>()
* z * sigma_tick * spike * (1.0 - source);
base + leak
}
};
let offset = index_at(0.0);
if tilt == 0.0 {
assert_eq!(offset, 0.0, "no tilt, no recentring");
}
let per_tick = integrate_against_the_normal(|z| {
let centred = index_at(z) - offset;
centred * centred
});
let measured_daily = 390.0 * per_tick;
let terms = index_conditional_variance_terms(
&p, &names, 3, v_f, 0.0, 1.0, spike, lagged, k);
let want = terms.factor_raw + terms.crash_raw + terms.crisis_raw
+ terms.tilt_raw;
assert!(
(want - measured_daily).abs() < 1e-9 * measured_daily,
"source {source}, tilt {tilt}, lag {lag}, lagged {lagged}, \
spike {spike}, v_f {v_f}: identity {want} \
against the tick's own {measured_daily}");
let mean = integrate_against_the_normal(index_at);
let drift_share = (390.0 * mean) * (390.0 * mean) / measured_daily;
if tilt == 0.0 {
assert!(mean.abs() < 1e-14,
"an untilted transmission has a mean: {mean}");
}
if tilt == 0.025 && lag == 0.375 {
assert!(drift_share < 0.005,
"the recentring residual is {:.4} of the market block \
at lagged {lagged}, spike {spike}, v_f {v_f}",
drift_share);
}
}
}
}
}
}
#[test]
fn the_conditional_normaliser_reproduces_the_index_the_tick_builds() {
let k = intraday_variance_factor();
let names = roster();
for &source in &[1.0, 0.0] {
for &(tilt, lag, lagged) in &[
(0.0, 0.0, false), (0.025, 0.375, false), (0.025, 0.375, true), ] {
for &spike in &[0.0, 0.98] {
for &v_f in &[1.7e-5, 5.766e-5, 1.25e-3] {
let mut p = PT_V18;
p.crash_amplifier_conditional_sigma = 1.0;
p.crisis_blend_source = source;
p.market_beta_down_asym = tilt;
p.market_beta_down_asym_lag = lag;
let sigma_tick = mathx::sqrt(v_f) / mathx::sqrt(390.0);
let index_at = |z: f64| {
let mut total = 0.0;
for (i, n) in names.iter().enumerate() {
let company = crate::market::factors::FactorCompany {
id: format!("N{i}"),
sector: format!("S{}", n.sector),
beta: Some(n.beta),
market_cap: n.market_cap,
avg_volume: 1.0e7,
shares_outstanding: 1.0e9,
short_interest: 0.0,
float: 1.0e9,
garch_variance: n.garch_variance,
last_daily_return: Some(0.0),
};
let shared = crate::market::factors::SharedFactors {
market_factor: z * sigma_tick,
sector_factors: (0..3)
.map(|s| (format!("S{s}"), 0.0))
.collect(),
crisis_spike: spike,
prev_day_down: lagged,
market_sigma_tick: sigma_tick,
crisis_epicentre: None,
};
let out = crate::market::factors::calculate_live_factors(
&company, &[], 0.0, 1.0, &shared, &p, &mut NoNoise);
total += n.weight * out.random_noise;
}
total
};
let index_at = |z: f64| {
let base = index_at(z);
if source == 1.0 || spike == 0.0 {
base
} else {
let leak: f64 = names
.iter()
.map(|n| {
n.weight
* crate::market::factors::sector_loading_for(&p, n.beta)
})
.sum::<f64>()
* z * sigma_tick * spike * (1.0 - source);
base + leak
}
};
let offset = index_at(0.0);
let per_tick = integrate_against_the_normal(|z| {
let centred = index_at(z) - offset;
centred * centred
});
let measured_daily = 390.0 * per_tick;
let terms = index_conditional_variance_terms(
&p, &names, 3, v_f, 0.0, 1.0, spike, lagged, k);
let want = terms.factor_raw + terms.crash_raw + terms.crisis_raw
+ terms.tilt_raw;
assert!(
(want - measured_daily).abs() < 1e-9 * measured_daily,
"source {source}, tilt {tilt}, lag {lag}, lagged {lagged}, \
spike {spike}, v_f {v_f}: identity {want} \
against the tick's own {measured_daily}");
if v_f > 1.0e-3 {
let mut stale = p;
stale.crash_amplifier_conditional_sigma = 0.0;
let stale_terms = index_conditional_variance_terms(
&stale, &names, 3, v_f, 0.0, 1.0, spike, lagged, k);
let stale_want = stale_terms.factor_raw + stale_terms.crash_raw
+ stale_terms.crisis_raw + stale_terms.tilt_raw;
assert!(stale_want > 1.5 * measured_daily,
"a read-back on the OLD normaliser reads \
{stale_want} against the tick's {measured_daily}, \
which is close enough that this test proves \
nothing about which branch was taken");
}
}
}
}
}
}
#[test]
fn the_anchor_averages_over_the_coin_it_cannot_read() {
let names = roster();
let bases: Vec<f64> = names.iter().map(|n| n.garch_variance).collect();
let p = crate::params::PT_V19;
let k = intraday_variance_factor();
let v_f = p.market_factor_sigma * p.market_factor_sigma;
let resting = resting_garch_variances(
&p, &names, &bases, v_f, p.sector_factor_sigma, k);
let at_rest: Vec<NameVariance> = names
.iter()
.enumerate()
.map(|(i, n)| NameVariance { garch_variance: resting[i], ..*n })
.collect();
let unlagged = index_conditional_variance(
&p, &at_rest, 3, v_f, p.sector_factor_sigma, 1.0, 0.0, false, k);
let lagged = index_conditional_variance(
&p, &at_rest, 3, v_f, p.sector_factor_sigma, 1.0, 0.0, true, k);
let got = index_unconditional_variance(&p, &names, 3, &bases);
assert_eq!(got, 0.5 * (unlagged + lagged), "the average is the average");
let vix = |v: f64| vix_from_variance(p.vix_variance_premium, v);
let shortfall = vix(got) / vix(unlagged) - 1.0;
println!(
"anchor: unlagged {:.4}, lagged {:.4}, mean {:.4}; the unlagged face is \
{:.2}% low", vix(unlagged), vix(lagged), vix(got), 100.0 * shortfall);
assert!(lagged > unlagged, "the lagged face must be the larger");
assert!(shortfall > 0.05,
"if the two faces agreed this would not be worth branching on: \
{:.4}", shortfall);
let mut no_wire = p.clone();
no_wire.market_beta_down_asym_lag = 0.0;
let single = index_conditional_variance(
&no_wire, &at_rest, 3, v_f, p.sector_factor_sigma, 1.0, 0.0, false, k);
assert_eq!(
index_unconditional_variance(&no_wire, &names, 3, &bases), single,
"a preset without the lagged wire must evaluate once, on the bits");
}
#[test]
fn the_recentring_residual_is_the_size_it_is_claimed_to_be() {
let k = intraday_variance_factor();
let names = roster();
let p = crate::params::PT_V19;
let base_v = p.market_factor_sigma * p.market_factor_sigma;
let mut worst_block = 0.0f64;
let mut worst_total = 0.0f64;
let mut at_anchor = (0.0f64, 0.0f64);
for &fvar_ratio in &[1.0, 1.742, 2.246, 4.162, 9.0] {
let v_f = base_v * fvar_ratio;
let sigma_tick = mathx::sqrt(v_f) / mathx::sqrt(390.0);
let spike = 0.0;
let index_at = |z: f64| {
let mut total = 0.0;
for (i, n) in names.iter().enumerate() {
let company = crate::market::factors::FactorCompany {
id: format!("N{i}"),
sector: format!("S{}", n.sector),
beta: Some(n.beta),
market_cap: n.market_cap,
avg_volume: 1.0e7,
shares_outstanding: 1.0e9,
short_interest: 0.0,
float: 1.0e9,
garch_variance: n.garch_variance,
last_daily_return: Some(0.0),
};
let shared = crate::market::factors::SharedFactors {
market_factor: z * sigma_tick,
sector_factors: (0..3).map(|s| (format!("S{s}"), 0.0)).collect(),
crisis_spike: spike,
prev_day_down: true,
market_sigma_tick: sigma_tick,
crisis_epicentre: None,
};
let out = crate::market::factors::calculate_live_factors(
&company, &[], 0.0, 1.0, &shared, &p, &mut NoNoise);
total += n.weight * out.random_noise;
}
total
};
let offset = index_at(0.0);
let block = 390.0
* integrate_against_the_normal(
|z| (index_at(z) - offset) * (index_at(z) - offset));
let mean_day = 390.0 * integrate_against_the_normal(index_at);
let terms = index_conditional_variance_terms(
&p, &names, 3, v_f, p.sector_factor_sigma, 1.0, spike, true, k);
let of_block = mean_day * mean_day / block;
let of_total = mean_day * mean_day / terms.total();
if fvar_ratio == 1.0 {
at_anchor = (of_block, of_total);
}
worst_block = mathx::max(worst_block, of_block);
worst_total = mathx::max(worst_total, of_total);
println!(
"fvar x{fvar_ratio}: drift {:.6e}/day, {:.4}% of the market block, \
{:.4}% of V_t", mean_day, 100.0 * of_block, 100.0 * of_total);
}
assert!(at_anchor.0 > 0.0, "the residual vanished at the anchor");
assert!(at_anchor.0 < 0.004, "at the anchor it is {:.5} of the block", at_anchor.0);
assert!(at_anchor.1 < 0.003, "at the anchor it is {:.5} of V_t", at_anchor.1);
assert!(worst_block < 0.008,
"the worst on the ladder is {:.5} of the block", worst_block);
assert!(worst_total < 0.008,
"the worst on the ladder is {:.5} of V_t", worst_total);
}
#[test]
fn the_step_across_the_threshold_is_the_blend_gains_and_nothing_else() {
let k = intraday_variance_factor();
let names = roster();
let pins = [
(1.00, 0.931, 19.53),
(1.50, 1.742, 29.29),
(1.75, 2.246, 34.17),
(2.50, 4.162, 48.82),
];
let read = |p: &ModelParams, fvar_ratio: f64, vix: f64| {
let base_v = p.market_factor_sigma * p.market_factor_sigma;
let v_f = base_v * fvar_ratio;
let spike = crate::market::tick::crisis_spike_for(p, vix, 0.0);
let at = |q: &ModelParams, s: f64| {
index_conditional_variance_terms(
q, &names, 3, v_f, p.sector_factor_sigma, 1.0, s, false, k).total()
};
let mut calm = p.clone();
calm.crash_amplifier_slope = 0.0;
(spike, at(p, spike), at(p, 0.0), at(&calm, 0.0))
};
let shipped = crate::params::PT_V19;
assert_eq!(shipped.crisis_blend_gain, 0.0,
"this half is about the derived zero; the preset now carries {}",
shipped.crisis_blend_gain);
let mut ratios = Vec::new();
for &(x, fvar_ratio, vix) in pins.iter() {
let (spike, regime, no_spike, calm) = read(&shipped, fvar_ratio, vix);
if vix > shipped.crisis_vix_threshold {
assert!(spike >= shipped.crisis_blend_cap,
"x {x} at VIX {vix} must saturate the spike; it read {spike}");
} else {
assert_eq!(spike, 0.0, "x {x} at VIX {vix} is not a crisis");
}
assert_eq!(regime.to_bits(), no_spike.to_bits(),
"x {x}: a spike of {spike} moved the read-back at gain \
0.0, {regime:e} against {no_spike:e}");
ratios.push(regime / calm);
}
for (i, &r) in ratios.iter().enumerate() {
assert!(r > 1.0 && r < 1.06,
"pin {i} reads {r:.4}, outside the amplifier's own band");
}
for i in 1..ratios.len() {
let between = ratios[i] / ratios[i - 1];
assert!(between < 1.02,
"pin {i} steps {between:.4}x over pin {}, which is a step \
the shipped preset has no term for", i - 1);
}
let mut geared = shipped;
geared.crisis_blend_gain = PT_V18.crisis_blend_gain;
assert!(geared.crisis_blend_gain > 0.0, "pt-v18 ships a blend");
let mut geared_ratios = Vec::new();
for &(_x, fvar_ratio, vix) in pins.iter() {
let (_spike, regime, _no_spike, calm) = read(&geared, fvar_ratio, vix);
geared_ratios.push(regime / calm);
}
assert_eq!(geared_ratios[0].to_bits(), ratios[0].to_bits());
assert_eq!(geared_ratios[1].to_bits(), ratios[1].to_bits());
let step = geared_ratios[2] / geared_ratios[1];
assert!(step > 2.0 && step < 4.0,
"the step across the threshold reads {step:.2}x, where the \
loop-gain run's realised variance stepped 2.78x \
(3.992 over 1.438)");
assert!(geared_ratios[3] > geared_ratios[2],
"and it keeps rising above the threshold");
}
#[test]
fn the_conversion_is_one_point_per_per_cent_annualised() {
let v = 0.01 * 0.01;
assert!((vix_from_variance(0.0, v) - 100.0 * mathx::sqrt(252.0) * 0.01).abs() < 1e-12);
assert!((vix_from_variance(0.25, v) - 1.25 * vix_from_variance(0.0, v)).abs() < 1e-12);
}
}