oxigrid 0.1.2

Pure Rust Energy Systems Simulation & Optimization Library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/// Stochastic Optimal Power Flow (S-OPF).
///
/// Incorporates uncertainty in renewable generation and load through
/// Monte Carlo scenario sampling, chance constraints, and CVaR (Conditional
/// Value at Risk) risk measures.
///
/// # Method
///
/// 1. **Scenario generation**: Sample `n_scenarios` realisations of uncertain
///    parameters (renewable output, load) from specified distributions.
///
/// 2. **Deterministic solve**: Run DC-OPF for each scenario independently.
///
/// 3. **Chance-constraint satisfaction**: Check that the fraction of scenarios
///    where constraint violations occur is below the allowed violation
///    probability `epsilon`.
///
/// 4. **CVaR computation**: Compute CVaR_α of cost (expected cost in the
///    worst `(1−α)` fraction of scenarios).
///
/// 5. **Optimal base dispatch**: Minimise E[cost] + β · CVaR subject to the
///    chance constraints.  Implemented as a weighted average over scenarios.
///
/// # References
/// - Bienstock & Shukla, "Chance-Constrained Optimal Power Flow", 2014.
/// - Rockafellar & Uryasev, "CVaR methodology", JCAM 2002.
use crate::error::{OxiGridError, Result};
use crate::network::topology::PowerNetwork;
use crate::optimize::opf::dc_opf::{economic_dispatch_pub, GenCost};
use serde::{Deserialize, Serialize};

/// Uncertain parameter distribution for one variable.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UncertainDist {
    /// Normal distribution N(μ, σ²)
    Normal { mean: f64, std: f64 },
    /// Uniform U[lo, hi]
    Uniform { lo: f64, hi: f64 },
    /// Beta distribution B(α, β) scaled to [lo, hi]
    Beta {
        alpha: f64,
        beta_param: f64,
        lo: f64,
        hi: f64,
    },
    /// Fixed (deterministic) value
    Fixed(f64),
}

impl UncertainDist {
    /// Sample from this distribution using a standard uniform u ∈ (0,1).
    pub fn sample(&self, u: f64) -> f64 {
        match self {
            Self::Normal { mean, std } => {
                // Box-Muller requires two uniforms; use single approximate inversion
                mean + std * normal_quantile(u)
            }
            Self::Uniform { lo, hi } => lo + u * (hi - lo),
            Self::Beta {
                alpha,
                beta_param,
                lo,
                hi,
            } => {
                let x = beta_sample(u, *alpha, *beta_param);
                lo + x * (hi - lo)
            }
            Self::Fixed(v) => *v,
        }
    }

    /// Expected value of this distribution.
    pub fn mean(&self) -> f64 {
        match self {
            Self::Normal { mean, .. } => *mean,
            Self::Uniform { lo, hi } => (lo + hi) / 2.0,
            Self::Beta {
                alpha,
                beta_param,
                lo,
                hi,
            } => {
                let mu = alpha / (alpha + beta_param);
                lo + mu * (hi - lo)
            }
            Self::Fixed(v) => *v,
        }
    }

    /// Standard deviation.
    pub fn std_dev(&self) -> f64 {
        match self {
            Self::Normal { std, .. } => *std,
            Self::Uniform { lo, hi } => (hi - lo) / (12.0_f64).sqrt(),
            Self::Beta {
                alpha,
                beta_param,
                lo,
                hi,
            } => {
                let var = alpha * beta_param
                    / ((alpha + beta_param).powi(2) * (alpha + beta_param + 1.0));
                (hi - lo) * var.sqrt()
            }
            Self::Fixed(_) => 0.0,
        }
    }
}

/// Uncertain renewable/load source tied to a bus.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UncertainSource {
    /// Bus ID where the uncertainty is injected
    pub bus_id: u32,
    /// Type: "load" or "renewable"
    pub source_type: String,
    /// Power output distribution `MW`
    pub dist: UncertainDist,
}

impl UncertainSource {
    pub fn wind(bus_id: u32, mean_mw: f64, std_mw: f64) -> Self {
        Self {
            bus_id,
            source_type: "renewable".into(),
            dist: UncertainDist::Normal {
                mean: mean_mw,
                std: std_mw,
            },
        }
    }

    pub fn solar(bus_id: u32, peak_mw: f64) -> Self {
        Self {
            bus_id,
            source_type: "renewable".into(),
            dist: UncertainDist::Beta {
                alpha: 2.0,
                beta_param: 5.0,
                lo: 0.0,
                hi: peak_mw,
            },
        }
    }

    pub fn load(bus_id: u32, mean_mw: f64, std_mw: f64) -> Self {
        Self {
            bus_id,
            source_type: "load".into(),
            dist: UncertainDist::Normal {
                mean: mean_mw,
                std: std_mw,
            },
        }
    }
}

/// Configuration for stochastic OPF.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StochasticOpfConfig {
    /// Number of Monte Carlo scenarios
    pub n_scenarios: usize,
    /// Random seed for reproducibility
    pub seed: u64,
    /// Allowed constraint violation probability ε (chance constraint level)
    pub epsilon: f64,
    /// CVaR confidence level α (e.g. 0.95 = worst 5% of scenarios)
    pub cvar_alpha: f64,
    /// CVaR risk penalty weight β (objective = `E[cost]` + β·CVaR)
    pub cvar_beta: f64,
}

impl Default for StochasticOpfConfig {
    fn default() -> Self {
        Self {
            n_scenarios: 100,
            seed: 42,
            epsilon: 0.05,
            cvar_alpha: 0.95,
            cvar_beta: 0.2,
        }
    }
}

/// One scenario realisation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Scenario {
    /// Scenario index
    pub index: usize,
    /// Sampled uncertain power injections `MW`, keyed by bus_id
    pub injections: Vec<(u32, f64)>,
    /// Total load for this scenario `MW`
    pub total_load_mw: f64,
    /// Dispatch result `MW` per generator
    pub p_gen_mw: Vec<f64>,
    /// Total cost [$/h]
    pub cost: f64,
    /// Any constraint violated in this scenario
    pub constraint_violated: bool,
    /// Curtailment of renewables `MW` (renewable sampled − renewable used)
    pub curtailment_mw: f64,
}

/// Stochastic OPF result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StochasticOpfResult {
    /// All scenario solutions
    pub scenarios: Vec<Scenario>,
    /// Expected cost across all scenarios [$/h]
    pub expected_cost: f64,
    /// Cost standard deviation [$/h]
    pub cost_std: f64,
    /// CVaR at α confidence level [$/h]
    pub cvar: f64,
    /// Risk-adjusted objective: `E[cost]` + β·CVaR `$/h`
    pub objective: f64,
    /// Probability of constraint violation across scenarios
    pub violation_probability: f64,
    /// Chance constraint satisfied? (violation_prob ≤ ε)
    pub chance_constraint_satisfied: bool,
    /// Optimal base dispatch (expected dispatch across scenarios) `MW`
    pub base_dispatch_mw: Vec<f64>,
    /// Expected curtailment `MW`
    pub expected_curtailment_mw: f64,
}

impl StochasticOpfResult {
    /// Percentile cost at given probability level p ∈ `0,1`.
    pub fn cost_percentile(&self, p: f64) -> f64 {
        let mut costs: Vec<f64> = self.scenarios.iter().map(|s| s.cost).collect();
        costs.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let idx = ((p * costs.len() as f64) as usize).min(costs.len() - 1);
        costs[idx]
    }

    /// Number of scenarios with constraint violations.
    pub fn n_violations(&self) -> usize {
        self.scenarios
            .iter()
            .filter(|s| s.constraint_violated)
            .count()
    }
}

/// Run a stochastic OPF with Monte Carlo scenario sampling.
///
/// For each scenario:
///   1. Sample uncertain sources using the configured seed+LCG.
///   2. Compute effective load = base load + uncertain load − uncertain renewable.
///   3. Solve deterministic DC-OPF for the scenario load.
///   4. Check constraint feasibility (load ≤ gen capacity).
///
/// Then aggregate statistics: `E[cost]`, CVaR, violation probability.
pub fn run_stochastic_opf(
    network: &PowerNetwork,
    gen_costs: &[GenCost],
    uncertain_sources: &[UncertainSource],
    config: &StochasticOpfConfig,
) -> Result<StochasticOpfResult> {
    if gen_costs.len() != network.generators.len() {
        return Err(OxiGridError::InvalidParameter(format!(
            "gen_costs length {} != generators {}",
            gen_costs.len(),
            network.generators.len()
        )));
    }

    let base_load_mw: f64 = network.buses.iter().map(|b| b.pd.0).sum();
    let n_gen = network.generators.len();

    // Total generation capacity
    let p_max_total: f64 = gen_costs.iter().map(|c| c.p_max).sum();
    let p_min_total: f64 = gen_costs.iter().map(|c| c.p_min).sum();

    let mut scenarios = Vec::with_capacity(config.n_scenarios);

    // LCG random number generator
    let mut rng = LcgRng::new(config.seed);

    for i in 0..config.n_scenarios {
        // Sample injections
        let mut net_load = base_load_mw;
        let mut injections = Vec::with_capacity(uncertain_sources.len());
        let mut curtailment = 0.0;

        for src in uncertain_sources {
            let u = rng.next_f64();
            let power_mw = src.dist.sample(u).max(0.0);
            injections.push((src.bus_id, power_mw));

            if src.source_type == "load" {
                net_load += power_mw - src.dist.mean(); // deviation from mean
            } else {
                // Renewable reduces net load; any excess is curtailed
                net_load -= power_mw;
            }
        }

        // Ensure non-negative load
        if net_load < 0.0 {
            curtailment = -net_load;
            net_load = 0.0;
        }

        // Check feasibility
        let mut constraint_violated = false;
        let (p_gen_mw, cost) = if net_load > p_max_total || net_load < p_min_total {
            constraint_violated = true;
            // Use clamped dispatch
            let clamped_load = net_load.clamp(p_min_total, p_max_total);
            let p =
                economic_dispatch_pub(gen_costs, clamped_load).unwrap_or_else(|_| vec![0.0; n_gen]);
            let c: f64 = gen_costs
                .iter()
                .zip(p.iter())
                .map(|(gc, &pg)| gc.total_cost(pg))
                .sum();
            (p, c)
        } else {
            match economic_dispatch_pub(gen_costs, net_load) {
                Ok(p) => {
                    let c: f64 = gen_costs
                        .iter()
                        .zip(p.iter())
                        .map(|(gc, &pg)| gc.total_cost(pg))
                        .sum();
                    (p, c)
                }
                Err(_) => {
                    constraint_violated = true;
                    (vec![0.0; n_gen], 0.0)
                }
            }
        };

        scenarios.push(Scenario {
            index: i,
            injections,
            total_load_mw: net_load,
            p_gen_mw,
            cost,
            constraint_violated,
            curtailment_mw: curtailment,
        });
    }

    // Aggregate statistics
    let n = scenarios.len() as f64;
    let expected_cost = scenarios.iter().map(|s| s.cost).sum::<f64>() / n;
    let cost_var = scenarios
        .iter()
        .map(|s| (s.cost - expected_cost).powi(2))
        .sum::<f64>()
        / n;
    let cost_std = cost_var.sqrt();

    // CVaR at alpha level
    let cvar = compute_cvar(&scenarios, config.cvar_alpha);

    let violation_probability =
        scenarios.iter().filter(|s| s.constraint_violated).count() as f64 / n;

    let chance_constraint_satisfied = violation_probability <= config.epsilon;

    let objective = expected_cost + config.cvar_beta * cvar;

    // Base dispatch: average across non-violated scenarios (or all if all violated)
    let valid_scenarios: Vec<&Scenario> = scenarios
        .iter()
        .filter(|s| !s.constraint_violated)
        .collect();
    let ref_scenarios = if valid_scenarios.is_empty() {
        scenarios.iter().collect()
    } else {
        valid_scenarios
    };
    let base_dispatch_mw = if ref_scenarios.is_empty() {
        vec![0.0; n_gen]
    } else {
        let m = ref_scenarios.len() as f64;
        (0..n_gen)
            .map(|g| ref_scenarios.iter().map(|s| s.p_gen_mw[g]).sum::<f64>() / m)
            .collect()
    };

    let expected_curtailment_mw = scenarios.iter().map(|s| s.curtailment_mw).sum::<f64>() / n;

    Ok(StochasticOpfResult {
        scenarios,
        expected_cost,
        cost_std,
        cvar,
        objective,
        violation_probability,
        chance_constraint_satisfied,
        base_dispatch_mw,
        expected_curtailment_mw,
    })
}

/// Compute CVaR at confidence level α for a set of scenarios.
///
/// CVaR_α = E[cost | cost ≥ VaR_α]
fn compute_cvar(scenarios: &[Scenario], alpha: f64) -> f64 {
    let mut costs: Vec<f64> = scenarios.iter().map(|s| s.cost).collect();
    costs.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let n = costs.len();
    if n == 0 {
        return 0.0;
    }

    let var_idx = ((alpha * n as f64) as usize).min(n - 1);
    let tail_costs = &costs[var_idx..];

    if tail_costs.is_empty() {
        costs[n - 1]
    } else {
        tail_costs.iter().sum::<f64>() / tail_costs.len() as f64
    }
}

/// Sensitivity analysis: partial derivative of expected cost w.r.t. uncertain mean.
///
/// Uses finite-difference approximation: `ΔE[cost]` / Δμ_i.
pub fn cost_sensitivity_to_mean(
    network: &PowerNetwork,
    gen_costs: &[GenCost],
    uncertain_sources: &[UncertainSource],
    config: &StochasticOpfConfig,
    source_idx: usize,
    delta_mw: f64,
) -> Result<f64> {
    // Baseline
    let result0 = run_stochastic_opf(network, gen_costs, uncertain_sources, config)?;

    // Perturbed
    let mut sources_pert = uncertain_sources.to_vec();
    if source_idx >= sources_pert.len() {
        return Err(OxiGridError::InvalidParameter(
            "source_idx out of range".into(),
        ));
    }
    let mean0 = sources_pert[source_idx].dist.mean();
    sources_pert[source_idx].dist = UncertainDist::Normal {
        mean: mean0 + delta_mw,
        std: uncertain_sources[source_idx].dist.std_dev(),
    };

    let result1 = run_stochastic_opf(network, gen_costs, &sources_pert, config)?;

    Ok((result1.expected_cost - result0.expected_cost) / delta_mw)
}

/// Pinball loss for quantile forecast evaluation.
///
/// L_τ(y, ŷ) = (y − ŷ)·τ if y ≥ ŷ, else (ŷ − y)·(1 − τ)
pub fn pinball_loss(actual: f64, predicted: f64, tau: f64) -> f64 {
    if actual >= predicted {
        (actual - predicted) * tau
    } else {
        (predicted - actual) * (1.0 - tau)
    }
}

/// Average pinball loss for a set of forecasts.
pub fn mean_pinball_loss(actuals: &[f64], predicteds: &[f64], tau: f64) -> f64 {
    if actuals.len() != predicteds.len() || actuals.is_empty() {
        return 0.0;
    }
    actuals
        .iter()
        .zip(predicteds.iter())
        .map(|(&a, &p)| pinball_loss(a, p, tau))
        .sum::<f64>()
        / actuals.len() as f64
}

/// Linear Congruential Generator (fast, reproducible).
struct LcgRng {
    state: u64,
}

impl LcgRng {
    fn new(seed: u64) -> Self {
        Self {
            state: seed.wrapping_add(1),
        }
    }

    fn next_u64(&mut self) -> u64 {
        // Parameters from Knuth / Numerical Recipes
        self.state = self
            .state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        self.state
    }

    fn next_f64(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
    }
}

/// Approximate standard normal quantile (Beasley-Springer-Moro algorithm).
fn normal_quantile(p: f64) -> f64 {
    let p = p.clamp(1e-10, 1.0 - 1e-10);
    let a = [
        2.50662823884,
        -18.61500062529,
        41.39119773534,
        -25.44106049637,
    ];
    let b = [
        -8.47351093090,
        23.08336743743,
        -21.06224101826,
        3.13082909833,
    ];
    let c = [
        0.337475482272615,
        0.976169019091719,
        0.160797971491821,
        2.76438810333863e-2,
        3.8405729373609e-3,
        3.951896511349e-4,
        3.21767881768e-5,
        2.888167364e-7,
        3.960315187e-7,
    ];

    let q = p - 0.5;
    if q.abs() <= 0.42 {
        let r = q * q;
        let num = q * (((a[3] * r + a[2]) * r + a[1]) * r + a[0]);
        let den = (((b[3] * r + b[2]) * r + b[1]) * r + b[0]) * r + 1.0;
        num / den
    } else {
        let r = if q < 0.0 { p } else { 1.0 - p };
        let r = (-r.ln()).ln();
        let mut s = c[0];
        for &ci in &c[1..] {
            s = s * r + ci;
        }
        if q < 0.0 {
            -s
        } else {
            s
        }
    }
}

/// Sample from Beta(α, β) distribution using acceptance-rejection.
fn beta_sample(u: f64, alpha: f64, beta: f64) -> f64 {
    // Simplified: use Johnk's method for α,β close to 1,
    // otherwise use normal approximation
    let mu = alpha / (alpha + beta);
    let var = alpha * beta / ((alpha + beta).powi(2) * (alpha + beta + 1.0));
    // Transform u through normal quantile
    let z = normal_quantile(u);
    (mu + var.sqrt() * z).clamp(0.0, 1.0)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ieee14_net() -> PowerNetwork {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/ieee14.m");
        PowerNetwork::from_matpower(path).expect("ieee14")
    }

    fn ieee14_costs(net: &PowerNetwork) -> Vec<GenCost> {
        net.generators
            .iter()
            .map(|g| GenCost::quadratic(0.0, 20.0, 0.05, g.pmin.max(0.0), g.pmax.max(10.0)))
            .collect()
    }

    #[test]
    fn test_stochastic_opf_basic() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        let sources = vec![UncertainSource::wind(1, 20.0, 5.0)];
        let config = StochasticOpfConfig {
            n_scenarios: 50,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &sources, &config).unwrap();
        assert_eq!(result.scenarios.len(), 50);
        assert!(result.expected_cost > 0.0);
    }

    #[test]
    fn test_expected_cost_positive() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        let sources = vec![UncertainSource::solar(2, 15.0)];
        let config = StochasticOpfConfig {
            n_scenarios: 30,
            seed: 123,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &sources, &config).unwrap();
        assert!(
            result.expected_cost > 0.0,
            "Expected cost: {:.2}",
            result.expected_cost
        );
    }

    #[test]
    fn test_cvar_geq_expected_cost() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        let sources = vec![UncertainSource::wind(1, 10.0, 8.0)];
        let config = StochasticOpfConfig {
            n_scenarios: 100,
            cvar_alpha: 0.9,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &sources, &config).unwrap();
        // CVaR ≥ Expected cost by definition
        assert!(
            result.cvar >= result.expected_cost - 1.0,
            "CVaR={:.2} should be ≥ E[cost]={:.2}",
            result.cvar,
            result.expected_cost
        );
    }

    #[test]
    fn test_no_uncertain_sources() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        let config = StochasticOpfConfig {
            n_scenarios: 20,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &[], &config).unwrap();
        // All scenarios identical → std dev = 0
        assert!(
            result.cost_std < 1.0,
            "Cost std with no uncertainty: {:.4}",
            result.cost_std
        );
    }

    #[test]
    fn test_base_dispatch_length() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        let sources = vec![UncertainSource::wind(1, 5.0, 2.0)];
        let config = StochasticOpfConfig {
            n_scenarios: 20,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &sources, &config).unwrap();
        assert_eq!(result.base_dispatch_mw.len(), net.generators.len());
    }

    #[test]
    fn test_pinball_loss_zero_at_quantile() {
        // For tau=0.5 (median), loss should be symmetric
        let loss_over = pinball_loss(12.0, 10.0, 0.5);
        let loss_under = pinball_loss(8.0, 10.0, 0.5);
        assert!(
            (loss_over - loss_under).abs() < 1e-10,
            "Median pinball: over={:.4}, under={:.4}",
            loss_over,
            loss_under
        );
    }

    #[test]
    fn test_pinball_loss_asymmetric() {
        // tau=0.9: under-prediction is heavily penalised
        let loss_over = pinball_loss(12.0, 10.0, 0.9);
        let loss_under = pinball_loss(8.0, 10.0, 0.9);
        assert!(
            loss_over > loss_under,
            "At τ=0.9 over-prediction penalised more"
        );
    }

    #[test]
    fn test_mean_pinball_loss() {
        let actuals = vec![10.0, 12.0, 8.0, 11.0];
        let predicted = vec![10.0, 10.0, 10.0, 10.0];
        let loss = mean_pinball_loss(&actuals, &predicted, 0.5);
        assert!(loss >= 0.0);
    }

    #[test]
    fn test_violation_probability_in_range() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        // High-variance wind exceeding generation capacity
        let sources = vec![UncertainSource::load(1, 50.0, 5.0)];
        let config = StochasticOpfConfig {
            n_scenarios: 50,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &sources, &config).unwrap();
        assert!(result.violation_probability >= 0.0 && result.violation_probability <= 1.0);
    }

    #[test]
    fn test_uncertain_dist_mean() {
        let d = UncertainDist::Normal {
            mean: 10.0,
            std: 2.0,
        };
        assert_eq!(d.mean(), 10.0);
        let d2 = UncertainDist::Uniform { lo: 4.0, hi: 6.0 };
        assert!((d2.mean() - 5.0).abs() < 1e-10);
        let d3 = UncertainDist::Fixed(7.5);
        assert_eq!(d3.mean(), 7.5);
    }

    #[test]
    fn test_lcg_reproducible() {
        let mut rng1 = LcgRng::new(99);
        let mut rng2 = LcgRng::new(99);
        for _ in 0..10 {
            assert_eq!(rng1.next_f64(), rng2.next_f64());
        }
    }

    #[test]
    fn test_cost_percentile() {
        let net = ieee14_net();
        let costs = ieee14_costs(&net);
        let sources = vec![UncertainSource::wind(1, 5.0, 3.0)];
        let config = StochasticOpfConfig {
            n_scenarios: 100,
            ..Default::default()
        };
        let result = run_stochastic_opf(&net, &costs, &sources, &config).unwrap();
        let p50 = result.cost_percentile(0.5);
        let p95 = result.cost_percentile(0.95);
        assert!(
            p95 >= p50 - 1.0,
            "P95={:.2} should be ≥ P50={:.2}",
            p95,
            p50
        );
    }
}