Skip to main content

sandbox_quant/runtime/
predictor_eval.rs

1#[derive(Debug, Clone, Copy, Default)]
2pub struct PredictorEvalVolState {
3    pub last_price: Option<f64>,
4    pub var: f64,
5    pub ready: bool,
6}
7
8pub fn observe_predictor_eval_volatility(
9    st: &mut PredictorEvalVolState,
10    price: f64,
11    alpha_var: f64,
12) {
13    if price <= f64::EPSILON {
14        return;
15    }
16    if let Some(prev) = st.last_price {
17        if prev > f64::EPSILON {
18            let r = (price / prev).ln();
19            if !st.ready {
20                st.var = r * r;
21                st.ready = true;
22            } else {
23                let a = alpha_var.clamp(0.0, 1.0);
24                st.var = (1.0 - a) * st.var + a * (r * r);
25            }
26        }
27    }
28    st.last_price = Some(price);
29}
30
31pub fn predictor_eval_scale(st: &PredictorEvalVolState, min_sigma: f64) -> f64 {
32    if st.ready {
33        st.var.max(0.0).sqrt().max(min_sigma.max(1e-8))
34    } else {
35        min_sigma.max(1e-8)
36    }
37}