RustyQLib 0.0.3

RustyQLib is a lightweight yet robust quantitative finance library designed to price derivatives and perform risk analysis
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
//! Cliquet (ratchet) options: a strip of forward-start performance
//! periods with local and global caps/floors.
//!
//! The payoff observes period returns `R_i = S_{t_i}/S_{t_{i-1}} - 1`
//! over an equally-spaced reset schedule and pays at maturity
//!
//! ```text
//! N * clamp( sum_i clamp(R_i, local_floor, local_cap),
//!            global_floor, global_cap )
//! ```
//!
//! A **ratchet** is the `local_floor = 0` special case: each period
//! locks in its gain and losses are forgiven. Because the payoff is
//! built from returns it is spot-homogeneous — the classic product
//! whose value is all **forward smile**: under Black-Scholes each
//! period is an independent lognormal and the price collapses to a
//! closed form (a strip of forward-start call spreads); under Heston
//! the forward smile is model-generated and the price genuinely
//! differs, which is the reason desks price cliquets on stochastic-vol
//! models.
//!
//! Engines: `Analytical` (Black-Scholes closed form; requires no
//! global cap/floor, which break the per-period independence) and
//! `MonteCarlo` (GBM per-period sampling, or full Heston paths when
//! parameters are supplied). Under homogeneous dynamics the pure
//! cliquet has zero spot delta; the output reports Monte Carlo
//! standard errors instead of spot Greeks.

use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

use crate::core::montecarlo::{mean_std_err, path_rng};
use crate::core::traits::Instrument;
use crate::core::utils::norm_cdf;
use crate::equity::heston::HestonParams;
use rand::Rng;
use rand_distr::StandardNormal;
use crate::core::errors::RustyQLibError;

/// Pricing engine choice for a cliquet.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CliquetPricer {
    Analytical,
    MonteCarlo,
}

/// Payoff family on the reset schedule.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CliquetStyle {
    /// Sum of locally clamped returns (the classic cliquet; local
    /// floor 0 = ratchet).
    Standard,
    /// Reverse cliquet: a headline `coupon` eroded by the negative
    /// period returns, `coupon + sum_i min(R_i, 0)` — conventionally
    /// sold with `global_floor = 0`. Ignores the local clamp fields.
    Reverse { coupon: f64 },
    /// Napoleon: a `coupon` plus the **worst** period return,
    /// `coupon + min_i R_i` — conventionally `global_floor = 0`.
    /// Ignores the local clamp fields.
    Napoleon { coupon: f64 },
}

/// JSON contract data (`"product_type": "cliquet_option"`).
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CliquetOptionData {
    pub symbol: String,
    /// Number of equally-spaced reset periods.
    pub resets: usize,
    /// Maturity date, `YYYY-MM-DD`.
    pub maturity: String,
    /// Per-period floor on the return (e.g. `0.0` for a ratchet).
    pub local_floor: f64,
    /// Per-period cap on the return.
    pub local_cap: Option<f64>,
    pub global_floor: Option<f64>,
    pub global_cap: Option<f64>,
    pub notional: Option<f64>,
    pub risk_free_rate: f64,
    pub dividend: Option<f64>,
    /// Flat Black-Scholes volatility (GBM engine and the closed form).
    pub volatility: f64,
    /// Optional Heston parameters: when present the Monte Carlo engine
    /// simulates Heston dynamics instead of GBM.
    pub heston: Option<HestonParams>,
    pub pricer: Option<String>,
    pub simulation: Option<u64>,
    pub mc_seed: Option<u64>,
    /// "standard" (default) | "reverse" | "napoleon".
    pub style: Option<String>,
    /// Headline coupon for reverse / napoleon styles.
    pub coupon: Option<f64>,
    /// Pricing as-of date (`YYYY-MM-DD`); defaults to today.
    pub valuation_date: Option<String>,
}

/// A cliquet/ratchet option on equally-spaced resets.
#[derive(Debug, Clone)]
pub struct Cliquet {
    pub resets: usize,
    /// Year fraction to maturity.
    pub t: f64,
    pub r: f64,
    pub q: f64,
    pub sigma: f64,
    pub local_floor: f64,
    pub local_cap: Option<f64>,
    pub global_floor: Option<f64>,
    pub global_cap: Option<f64>,
    pub notional: f64,
    pub heston: Option<HestonParams>,
    pub style: CliquetStyle,
    pub pricer: CliquetPricer,
    pub paths: usize,
    pub seed: u64,
}

/// Euler substeps per reset period for the Heston path engine.
const HESTON_SUBSTEPS: usize = 32;

impl Cliquet {
    fn clamp_local(&self, ret: f64) -> f64 {
        let mut x = ret.max(self.local_floor);
        if let Some(cap) = self.local_cap {
            x = x.min(cap);
        }
        x
    }

    fn accumulate(&self, ret: f64, total: &mut f64, worst: &mut f64) {
        match self.style {
            CliquetStyle::Standard => *total += self.clamp_local(ret),
            CliquetStyle::Reverse { .. } => *total += ret.min(0.0),
            CliquetStyle::Napoleon { .. } => *worst = worst.min(ret),
        }
    }

    fn clamp_global(&self, sum: f64) -> f64 {
        let mut x = sum;
        if let Some(floor) = self.global_floor {
            x = x.max(floor);
        }
        if let Some(cap) = self.global_cap {
            x = x.min(cap);
        }
        x
    }

    /// Black-Scholes closed form: each period's clamped return is a
    /// forward-start call spread on the lognormal period ratio, and the
    /// periods are independent, so the sum prices term by term. Errs
    /// when a global cap/floor is present (it couples the periods) or
    /// Heston dynamics are requested.
    pub fn analytic_npv(&self) -> Result<f64, RustyQLibError> {
        if self.global_floor.is_some() || self.global_cap.is_some() {
            return Err(RustyQLibError::UnsupportedEngine("global cap/floor couples the periods: use Monte Carlo".to_string()));
        }
        if self.heston.is_some() {
            return Err(RustyQLibError::UnsupportedEngine("Heston cliquets price by Monte Carlo".to_string()));
        }
        let dt = self.t / self.resets as f64;
        let fwd = ((self.r - self.q) * dt).exp();
        let sd = self.sigma * dt.sqrt();
        // undiscounted E[(X - k)+] for the lognormal period ratio X
        let ratio_call = |k: f64| -> f64 {
            let d1 = ((fwd / k).ln() + 0.5 * sd * sd) / sd;
            fwd * norm_cdf(d1) - k * norm_cdf(d1 - sd)
        };
        let df_n = self.notional * (-self.r * self.t).exp();
        match self.style {
            CliquetStyle::Standard => {
                // E[clamp(R, lf, lc)] = lf + call(1 + lf) - call(1 + lc)
                let mut period = self.local_floor + ratio_call(1.0 + self.local_floor);
                if let Some(cap) = self.local_cap {
                    period -= ratio_call(1.0 + cap);
                }
                Ok(df_n * self.resets as f64 * period)
            }
            CliquetStyle::Reverse { coupon } => {
                // E[min(R, 0)] = -E[(1 - X)+], a put on the period ratio
                // struck at 1, via parity: put(1) = call(1) - (F - 1)
                let ratio_put_at_one = ratio_call(1.0) - (fwd - 1.0);
                Ok(df_n * (coupon - self.resets as f64 * ratio_put_at_one))
            }
            CliquetStyle::Napoleon { .. } => {
                Err(RustyQLibError::UnsupportedEngine("the Napoleon's worst-of statistic prices by Monte Carlo".to_string()))
            }
        }
    }

    /// Monte Carlo price with standard error: per-period lognormal
    /// sampling under GBM, full Euler paths under Heston. Deterministic
    /// per seed.
    pub fn mc_npv(&self) -> (f64, f64) {
        let dt = self.t / self.resets as f64;
        let mut sum = 0.0;
        let mut sum_sq = 0.0;
        for i in 0..self.paths {
            let mut rng = path_rng(self.seed, i as u64);
            let mut total = 0.0;
            let mut worst = f64::INFINITY;
            match &self.heston {
                None => {
                    let drift = (self.r - self.q - 0.5 * self.sigma * self.sigma) * dt;
                    let sd = self.sigma * dt.sqrt();
                    for _ in 0..self.resets {
                        let z: f64 = rng.sample(StandardNormal);
                        let ret = (drift + sd * z).exp() - 1.0;
                        self.accumulate(ret, &mut total, &mut worst);
                    }
                }
                Some(hp) => {
                    let sub = dt / HESTON_SUBSTEPS as f64;
                    let rho_bar = (1.0 - hp.rho * hp.rho).sqrt();
                    let mut v: f64 = hp.v0;
                    for _ in 0..self.resets {
                        let mut log_ret = 0.0;
                        for _ in 0..HESTON_SUBSTEPS {
                            let z1: f64 = rng.sample(StandardNormal);
                            let z2: f64 = rng.sample(StandardNormal);
                            let zv = hp.rho * z1 + rho_bar * z2;
                            let vp = v.max(0.0);
                            log_ret += (self.r - self.q - 0.5 * vp) * sub
                                + (vp * sub).sqrt() * z1;
                            v += hp.kappa * (hp.theta - vp) * sub
                                + hp.vol_of_vol * (vp * sub).sqrt() * zv;
                        }
                        self.accumulate(log_ret.exp() - 1.0, &mut total, &mut worst);
                    }
                }
            }
            let units = match self.style {
                CliquetStyle::Standard => self.clamp_global(total),
                CliquetStyle::Reverse { coupon } => self.clamp_global(coupon + total),
                CliquetStyle::Napoleon { coupon } => self.clamp_global(coupon + worst),
            };
            let payoff = self.notional * (-self.r * self.t).exp() * units;
            sum += payoff;
            sum_sq += payoff * payoff;
        }
        mean_std_err(sum, sum_sq, self.paths)
    }

    /// Price with the configured engine (analytic falls back to Monte
    /// Carlo when global constraints or Heston dynamics require it).
    /// Build from contract data, panicking on any invalid field. Fallible
    /// callers should use [`Cliquet::try_from_json`].
    pub fn from_json(data: &CliquetOptionData) -> Box<Cliquet> {
        Self::try_from_json(data).unwrap_or_else(|e| panic!("{e}"))
    }

    pub fn try_from_json(data: &CliquetOptionData) -> Result<Box<Cliquet>, RustyQLibError> {
        let today =
            crate::core::data_models::parse_valuation_date(data.valuation_date.as_deref())?;
        let maturity = NaiveDate::parse_from_str(&data.maturity, "%Y-%m-%d")
            .map_err(|_| RustyQLibError::invalid_input(
                "maturity",
                format!("invalid date '{}' (expected YYYY-MM-DD)", data.maturity),
            ))?;
        let t = (maturity - today).num_days() as f64 / 365.0;
        if t <= 0.0 {
            return Err(RustyQLibError::invalid_input("maturity", "cliquet is expired"));
        }
        if data.resets < 1 {
            return Err(RustyQLibError::invalid_input("resets", "need at least one reset period"));
        }
        if let Some(hp) = &data.heston {
            hp.validate()?;
        }
        let pricer = match data.pricer.as_deref().map(str::trim) {
            None | Some("Analytical") | Some("analytical") | Some("bs") => {
                CliquetPricer::Analytical
            }
            Some("MonteCarlo") | Some("montecarlo") | Some("MC") | Some("mc") => {
                CliquetPricer::MonteCarlo
            }
            Some(other) => return Err(RustyQLibError::invalid_input(
                "pricer",
                format!("invalid cliquet pricer '{other}' (use Analytical or MonteCarlo)"),
            )),
        };
        let need_coupon = |style: &str| {
            data.coupon.ok_or_else(|| RustyQLibError::invalid_input(
                "coupon",
                format!("{style} cliquet needs a coupon"),
            ))
        };
        let style = match data.style.as_deref().map(str::trim) {
            None | Some("standard") | Some("Standard") => CliquetStyle::Standard,
            Some("reverse") | Some("Reverse") => CliquetStyle::Reverse {
                coupon: need_coupon("reverse")?,
            },
            Some("napoleon") | Some("Napoleon") => CliquetStyle::Napoleon {
                coupon: need_coupon("napoleon")?,
            },
            Some(other) => return Err(RustyQLibError::invalid_input(
                "style",
                format!("invalid cliquet style '{other}' (use standard, reverse or napoleon)"),
            )),
        };
        Ok(Box::new(Cliquet {
            resets: data.resets,
            t,
            r: data.risk_free_rate,
            q: data.dividend.unwrap_or(0.0),
            sigma: data.volatility,
            local_floor: data.local_floor,
            local_cap: data.local_cap,
            global_floor: data.global_floor,
            global_cap: data.global_cap,
            notional: data.notional.unwrap_or(1.0),
            heston: data.heston,
            style,
            pricer,
            paths: data.simulation.unwrap_or(100_000) as usize,
            seed: data.mc_seed.unwrap_or(42),
        }))
    }
}

impl Instrument for Cliquet {
    fn try_npv(&self) -> Result<f64, RustyQLibError> {
        Ok(self.price()?.pv)
    }

    /// Analytic where the payoff permits (falling back to Monte Carlo
    /// otherwise, mirroring the documented pricing policy); the standard
    /// error is reported whenever a simulation produced the value.
    fn price(&self) -> Result<crate::core::results::PricingResult, RustyQLibError> {
        let (pv, std_err) = match self.pricer {
            CliquetPricer::Analytical => match self.analytic_npv() {
                Ok(v) => (v, None),
                Err(_) => {
                    let (pv, se) = self.mc_npv();
                    (pv, Some(se))
                }
            },
            CliquetPricer::MonteCarlo => {
                let (pv, se) = self.mc_npv();
                (pv, Some(se))
            }
        };
        // the return-based payoff is spot-homogeneous: no spot Greeks
        Ok(crate::core::results::PricingResult { pv, greeks: Default::default(), std_err })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::equity::blackscholes::bs_price;
    use crate::core::trade::PutOrCall;

    fn base() -> Cliquet {
        Cliquet {
            resets: 12,
            t: 1.0,
            r: 0.03,
            q: 0.01,
            sigma: 0.2,
            local_floor: 0.0,
            local_cap: Some(0.03),
            global_floor: None,
            global_cap: None,
            notional: 1.0,
            heston: None,
            style: CliquetStyle::Standard,
            pricer: CliquetPricer::Analytical,
            paths: 60_000,
            seed: 42,
        }
    }

    #[test]
    fn single_period_ratchet_is_a_scaled_atm_call() {
        // one reset, floor 0, no cap: pays (S_T/S_0 - 1)+ = ATM call / S0
        let mut c = base();
        c.resets = 1;
        c.local_cap = None;
        let analytic = c.analytic_npv().unwrap();
        let atm = bs_price(100.0, 100.0, c.r, c.q, c.sigma, c.t, PutOrCall::Call) / 100.0;
        assert!((analytic - atm).abs() < 1e-12, "{analytic} vs {atm}");
    }

    #[test]
    fn monte_carlo_agrees_with_the_closed_form() {
        let c = base();
        let analytic = c.analytic_npv().unwrap();
        let (mc, se) = c.mc_npv();
        assert!(
            (mc - analytic).abs() < 3.0 * se + 1e-4,
            "mc {mc} +/- {se} vs analytic {analytic}"
        );
        // deterministic per seed
        assert_eq!(c.mc_npv().0, mc);
    }

    #[test]
    fn caps_and_floors_move_the_price_the_right_way() {
        let c = base();
        let baseline = c.analytic_npv().unwrap();
        // a tighter local cap must cheapen the strip
        let mut tight = base();
        tight.local_cap = Some(0.01);
        assert!(tight.analytic_npv().unwrap() < baseline);
        // a global floor adds value, a global cap removes it (MC only)
        let mut floored = base();
        floored.global_floor = Some(0.06);
        assert!(floored.analytic_npv().is_err());
        assert!(floored.mc_npv().0 > c.mc_npv().0 - 1e-12);
        let mut capped = base();
        capped.global_cap = Some(0.10);
        assert!(capped.mc_npv().0 < c.mc_npv().0);
        // local floor above the cap is degenerate but bounded
        let mut sunk = base();
        sunk.local_floor = -0.02;
        assert!(sunk.analytic_npv().unwrap() < baseline);
    }

    #[test]
    fn heston_with_tiny_vol_of_vol_matches_the_gbm_closed_form() {
        let mut c = base();
        c.heston = Some(HestonParams {
            v0: c.sigma * c.sigma,
            kappa: 1.0,
            theta: c.sigma * c.sigma,
            vol_of_vol: 1e-4,
            rho: 0.0,
        });
        c.paths = 40_000;
        let (mc, se) = c.mc_npv();
        let analytic = base().analytic_npv().unwrap();
        assert!(
            (mc - analytic).abs() < 4.0 * se + 5e-4,
            "heston-degenerate {mc} +/- {se} vs analytic {analytic}"
        );
    }

    #[test]
    fn heston_forward_smile_moves_the_cliquet_off_black_scholes() {
        // same total variance, real vol-of-vol and negative rho: the
        // capped/floored strip prices differently from flat-vol GBM —
        // the whole reason cliquets are priced on stochastic vol
        let mut c = base();
        c.heston = Some(HestonParams {
            v0: 0.04,
            kappa: 1.5,
            theta: 0.04,
            vol_of_vol: 0.7,
            rho: -0.7,
        });
        c.paths = 60_000;
        let (mc, se) = c.mc_npv();
        let flat = base().analytic_npv().unwrap();
        assert!(
            (mc - flat).abs() > 3.0 * se,
            "expected a forward-smile effect: heston {mc} +/- {se} vs bs {flat}"
        );
    }

    #[test]
    fn reverse_cliquet_closed_form_matches_monte_carlo() {
        // unfloored reverse: coupon minus a strip of forward-start puts
        let mut c = base();
        c.style = CliquetStyle::Reverse { coupon: 0.20 };
        c.local_cap = None;
        let analytic = c.analytic_npv().unwrap();
        let (mc, se) = c.mc_npv();
        assert!(
            (mc - analytic).abs() < 3.0 * se + 1e-4,
            "mc {mc} +/- {se} vs analytic {analytic}"
        );
        // the conventional 0% floor only adds value
        let mut floored = c.clone();
        floored.global_floor = Some(0.0);
        assert!(floored.analytic_npv().is_err());
        assert!(floored.mc_npv().0 >= mc - 1e-12);
    }

    #[test]
    fn single_period_napoleon_is_a_forward_start_call() {
        // one reset, floor 0: max(0, C + R) = (X - (1 - C))+ on the ratio
        let coupon = 0.10;
        let mut c = base();
        c.resets = 1;
        c.local_cap = None;
        c.style = CliquetStyle::Napoleon { coupon };
        c.global_floor = Some(0.0);
        c.paths = 200_000;
        let (mc, se) = c.mc_npv();
        // a call on the unit-spot period ratio struck at 1 - C, already
        // discounted by bs_price
        let analytic = bs_price(1.0, 1.0 - coupon, c.r, c.q, c.sigma, c.t, PutOrCall::Call);
        assert!(
            (mc - analytic).abs() < 3.0 * se + 1e-4,
            "mc {mc} +/- {se} vs analytic {analytic}"
        );
    }

    #[test]
    fn napoleon_worsens_with_more_resets_and_higher_vol() {
        let napoleon = |resets: usize, sigma: f64| -> f64 {
            let mut c = base();
            c.resets = resets;
            c.t = 1.0;
            c.sigma = sigma;
            c.local_cap = None;
            c.style = CliquetStyle::Napoleon { coupon: 0.10 };
            c.global_floor = Some(0.0);
            c.paths = 30_000;
            c.mc_npv().0
        };
        // the minimum of more period returns is worse
        assert!(napoleon(1, 0.2) > napoleon(4, 0.2));
        assert!(napoleon(4, 0.2) > napoleon(12, 0.2));
        // and the structure is short volatility
        assert!(napoleon(12, 0.15) > napoleon(12, 0.30));
    }

    #[test]
    fn napoleon_vol_of_vol_exposure_is_large_and_directionally_convex() {
        // Same total variance, real vol-of-vol. Note the direction: the
        // FLOORED Napoleon is convex in the worst-month distribution (the
        // 0% floor truncates the fat left tail, while calm-vol regimes
        // improve the worst month), so vol-of-vol RAISES the buyer's
        // value here — the famous Napoleon blowups were the sellers'
        // short position in exactly this convexity. The unfloored
        // structure, by contrast, is hurt by vol clustering.
        let mut gbm = base();
        gbm.style = CliquetStyle::Napoleon { coupon: 0.10 };
        gbm.local_cap = None;
        gbm.global_floor = Some(0.0);
        gbm.paths = 60_000;
        let (flat, flat_se) = gbm.mc_npv();
        let hp = HestonParams { v0: 0.04, kappa: 1.5, theta: 0.04, vol_of_vol: 0.7, rho: -0.7 };
        let mut heston = gbm.clone();
        heston.heston = Some(hp);
        let (stoch, stoch_se) = heston.mc_npv();
        let noise = (flat_se * flat_se + stoch_se * stoch_se).sqrt();
        assert!(stoch > flat + 5.0 * noise, "floored: heston {stoch} vs gbm {flat}");

        // without the floor two effects compete (worst-month concavity
        // vs the right-skewed CIR variance making the typical month
        // calmer), so no sign is asserted — only that the model choice
        // moves the price by far more than the Monte Carlo noise
        let mut gbm_unfloored = gbm.clone();
        gbm_unfloored.global_floor = None;
        let (flat_u, se_u) = gbm_unfloored.mc_npv();
        let mut heston_unfloored = gbm_unfloored.clone();
        heston_unfloored.heston = Some(hp);
        let (stoch_u, se_u2) = heston_unfloored.mc_npv();
        let noise_u = (se_u * se_u + se_u2 * se_u2).sqrt();
        assert!(
            (stoch_u - flat_u).abs() > 5.0 * noise_u,
            "unfloored: heston {stoch_u} vs gbm {flat_u} (noise {noise_u})"
        );
    }

    #[test]
    fn styled_json_contracts_parse() {
        let json = r#"{
            "symbol": "NAP", "resets": 12, "maturity": "2030-01-01",
            "local_floor": 0.0, "global_floor": 0.0,
            "risk_free_rate": 0.03, "volatility": 0.2,
            "style": "napoleon", "coupon": 0.08,
            "pricer": "MC", "simulation": 5000
        }"#;
        let data: CliquetOptionData = serde_json::from_str(json).unwrap();
        let napoleon = Cliquet::from_json(&data);
        assert_eq!(napoleon.style, CliquetStyle::Napoleon { coupon: 0.08 });
        let pv = napoleon.npv();
        assert!(pv > 0.0 && pv < 0.08, "{pv}"); // bounded by the coupon
    }

    #[test]
    fn json_contract_round_trip() {
        let json = r#"{
            "symbol": "CLIQ", "resets": 4, "maturity": "2030-01-01",
            "local_floor": 0.0, "local_cap": 0.05, "global_floor": 0.02,
            "notional": 1000000.0, "risk_free_rate": 0.03, "dividend": 0.01,
            "volatility": 0.25, "pricer": "MC", "simulation": 20000, "mc_seed": 7
        }"#;
        let data: CliquetOptionData = serde_json::from_str(json).unwrap();
        let cliquet = Cliquet::from_json(&data);
        assert_eq!(cliquet.resets, 4);
        assert_eq!(cliquet.pricer, CliquetPricer::MonteCarlo);
        let pv = cliquet.npv();
        // bounded by the discounted global-capped maximum
        assert!(pv > 0.0 && pv < 1_000_000.0 * 4.0 * 0.05, "{pv}");
    }
}