flexpool-plan 0.4.0

flexpool day-ahead planning: price scenarios from forecast quantiles, a storage LP with a CVaR term, water values from the duals, limit orders and their paper execution. No I/O.
Documentation
//! Price scenarios from forecast quantiles: a Gaussian copula with the errors' own time correlation.

use flexpool_forecast::Forecast;

/// Scenarios drawn per day.
pub const SCENARIOS: usize = 200;

/// Φ⁻¹ at the forecast levels 0.1, 0.25, 0.5, 0.75, 0.9.
const KNOTS: [f64; 5] = [
    -1.281_551_565_544_600_4,
    -0.674_489_750_196_081_7,
    0.0,
    0.674_489_750_196_081_7,
    1.281_551_565_544_600_4,
];

/// SplitMix64 (Steele, Lea & Flood 2014): small, fast and fully deterministic.
struct SplitMix64(u64);

impl SplitMix64 {
    const fn next(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    /// Uniform in (0, 1), never 0 or 1.
    fn uniform(&mut self) -> f64 {
        #[expect(clippy::cast_precision_loss, reason = "53 random bits")]
        let bits = (self.next() >> 11) as f64;
        (bits + 0.5) / 9_007_199_254_740_992.0
    }

    fn normal(&mut self) -> f64 {
        inverse_normal(self.uniform())
    }
}

/// Φ⁻¹(p) by Acklam's rational approximation (relative error < 1.2e-9), for p in (0, 1).
#[must_use]
pub fn inverse_normal(p: f64) -> f64 {
    const A: [f64; 6] = [
        -3.969_683_028_665_376e1,
        2.209_460_984_245_205e2,
        -2.759_285_104_469_687e2,
        1.383_577_518_672_69e2,
        -3.066_479_806_614_716e1,
        2.506_628_277_459_239,
    ];
    const B: [f64; 5] = [
        -5.447_609_879_822_406e1,
        1.615_858_368_580_409e2,
        -1.556_989_798_598_866e2,
        6.680_131_188_771_972e1,
        -1.328_068_155_288_572e1,
    ];
    const C: [f64; 6] = [
        -7.784_894_002_430_293e-3,
        -3.223_964_580_411_365e-1,
        -2.400_758_277_161_838,
        -2.549_732_539_343_734,
        4.374_664_141_464_968,
        2.938_163_982_698_783,
    ];
    const D: [f64; 4] = [
        7.784_695_709_041_462e-3,
        3.224_671_290_700_398e-1,
        2.445_134_137_142_996,
        3.754_408_661_907_416,
    ];
    let tail = |q: f64| {
        (((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5])
            / ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0)
    };
    if p < 0.024_25 {
        tail((-2.0 * p.ln()).sqrt())
    } else if p > 1.0 - 0.024_25 {
        -tail((-2.0 * (1.0 - p).ln()).sqrt())
    } else {
        let q = p - 0.5;
        let r = q * q;
        (((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q
            / (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1.0)
    }
}

/// The price at normal score `z` for quantiles `q`: piecewise linear between the knots, linear
/// beyond them with the outer slopes. At the knots it returns the quantiles exactly.
#[must_use]
pub fn price_at(q: &[f64; 5], z: f64) -> f64 {
    let seg = KNOTS.windows(2).position(|w| z <= w[1]).unwrap_or(3);
    let (z0, z1, q0, q1) = (KNOTS[seg], KNOTS[seg + 1], q[seg], q[seg + 1]);
    q0 + (q1 - q0) * (z - z0) / (z1 - z0)
}

/// `SCENARIOS` price paths for `forecast` (one value per interval), latent AR(1) with `rho` per
/// step, clamped to `[lo, hi]`, deterministic for `seed`.
#[must_use]
pub fn draw(forecast: &Forecast, rho: f64, seed: u64, lo: f64, hi: f64) -> Vec<Vec<f64>> {
    let rho = rho.clamp(0.0, 0.99);
    let innovation = (1.0 - rho * rho).sqrt();
    let mut rng = SplitMix64(seed);
    (0..SCENARIOS)
        .map(|_| {
            let mut z = rng.normal();
            forecast
                .intervals
                .iter()
                .enumerate()
                .map(|(t, i)| {
                    if t > 0 {
                        z = rho * z + innovation * rng.normal();
                    }
                    price_at(&i.q, z).clamp(lo, hi)
                })
                .collect()
        })
        .collect()
}