use super::calendar::SESSIONS_PER_YEAR;
use super::symbol::Right;
use crate::mathx;
pub const MIN_STEPS: usize = 101;
pub const EXERCISE_BY_EXCEPTION: f64 = 0.01;
const TIME_TOLERANCE: f64 = 1e-9;
const EXERCISE_TOLERANCE: f64 = 1e-10;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct OptionSpec {
pub right: Right,
pub spot: f64,
pub strike: f64,
pub rate: f64,
pub vol: f64,
pub expiry: f64,
}
impl OptionSpec {
pub fn new(right: Right, spot: f64, strike: f64, rate: f64, vol: f64, expiry: f64) -> Self {
Self { right, spot, strike, rate, vol, expiry }
}
pub fn intrinsic(&self, s: f64) -> f64 {
intrinsic(self.right, self.strike, s)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct CashDividend {
pub time: f64,
pub amount: f64,
}
impl CashDividend {
pub fn new(time: f64, amount: f64) -> Self {
Self { time, amount }
}
pub fn from_ex_day(day: i64, ex_day: i64, amount: f64) -> Self {
Self { time: years(ex_day - day - 1), amount }
}
}
pub fn years(sessions: i64) -> f64 {
sessions as f64 / SESSIONS_PER_YEAR as f64
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Style {
American,
European,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Lattice {
LeisenReimer,
CoxRossRubinstein,
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct BoundaryPoint {
pub time: f64,
pub exercised: f64,
pub held: Option<f64>,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TreeValue {
pub value: f64,
pub hold: f64,
pub delta: f64,
pub gamma: f64,
pub steps: usize,
pub boundary: Vec<BoundaryPoint>,
}
pub fn steps_for(sessions: i64) -> usize {
let per_session = if sessions > 0 { 2 * sessions as usize } else { 0 };
core::cmp::max(MIN_STEPS, per_session) | 1
}
fn intrinsic(right: Right, strike: f64, s: f64) -> f64 {
match right {
Right::Call => mathx::max(s - strike, 0.0),
Right::Put => mathx::max(strike - s, 0.0),
}
}
fn peizer_pratt(z: f64, n: usize) -> f64 {
let n = n as f64;
let a = z / (n + 1.0 / 3.0 + 0.1 / (n + 1.0));
let root = mathx::sqrt(0.25 - 0.25 * mathx::exp(-a * a * (n + 1.0 / 6.0)));
if z >= 0.0 {
0.5 + root
} else {
0.5 - root
}
}
pub fn price(
spec: &OptionSpec,
dividends: &[CashDividend],
steps: usize,
style: Style,
lattice: Lattice,
) -> Result<TreeValue, String> {
let OptionSpec { right, spot, strike, rate, vol, expiry } = *spec;
if !(spot > 0.0 && spot.is_finite()) {
return Err(format!("spot must be positive and finite, got {spot}"));
}
if !(strike > 0.0 && strike.is_finite()) {
return Err(format!("strike must be positive and finite, got {strike}"));
}
if !(vol > 0.0 && vol.is_finite()) {
return Err(format!("volatility must be positive and finite, got {vol}"));
}
if !rate.is_finite() {
return Err(format!("rate must be finite, got {rate}"));
}
if !(expiry >= 0.0 && expiry.is_finite()) {
return Err(format!("expiry must be non-negative and finite, got {expiry}"));
}
if let Some(d) = dividends.iter().find(|d| !(d.amount >= 0.0 && d.amount.is_finite() && d.time.is_finite())) {
return Err(format!("a dividend must have a finite time and a non-negative amount, got {d:?}"));
}
if expiry == 0.0 {
let value = spec.intrinsic(spot);
let in_money = value > 0.0;
let delta = match (right, in_money) {
(Right::Call, true) => 1.0,
(Right::Put, true) => -1.0,
_ => 0.0,
};
return Ok(TreeValue { value, hold: value, delta, gamma: 0.0, steps: 0, boundary: Vec::new() });
}
if steps < 2 {
return Err(format!("a tree needs at least 2 steps, got {steps}"));
}
if lattice == Lattice::LeisenReimer && steps % 2 == 0 {
return Err(format!("a Leisen-Reimer tree needs an odd number of steps, got {steps}"));
}
let live: Vec<CashDividend> = dividends
.iter()
.copied()
.filter(|d| d.amount > 0.0 && d.time >= -TIME_TOLERANCE && d.time < expiry - TIME_TOLERANCE)
.map(|d| CashDividend { time: mathx::max(d.time, 0.0), amount: d.amount })
.collect();
let n = steps;
let dt = expiry / n as f64;
let held: Vec<f64> = (0..=n)
.map(|i| {
let t = i as f64 * dt;
live.iter()
.filter(|d| d.time >= t - TIME_TOLERANCE)
.map(|d| d.amount * mathx::exp(-rate * (d.time - t)))
.sum()
})
.collect();
let escrowed = spot - held[0];
if !(escrowed > 0.0) {
return Err(format!(
"the dividends' present value {} is not below the spot {spot}",
held[0]
));
}
let growth = mathx::exp(rate * dt);
let disc = mathx::exp(-rate * dt);
let (u, d, p) = match lattice {
Lattice::LeisenReimer => {
let sd = vol * mathx::sqrt(expiry);
let d1 = (mathx::log(escrowed / strike) + (rate + 0.5 * vol * vol) * expiry) / sd;
let d2 = d1 - sd;
let p = peizer_pratt(d2, n);
let p_bar = peizer_pratt(d1, n);
let u = growth * p_bar / p;
let d = (growth - p * u) / (1.0 - p);
(u, d, p)
}
Lattice::CoxRossRubinstein => {
let u = mathx::exp(vol * mathx::sqrt(dt));
let d = 1.0 / u;
(u, d, (growth - d) / (u - d))
}
};
if !(p > 0.0 && p < 1.0 && d > 0.0 && u > d) {
return Err(format!(
"the lattice has no risk-neutral probability (u {u}, d {d}, p {p}); take more steps"
));
}
let q = 1.0 - p;
let ratio = u / d;
let spots = |i: usize, out: &mut Vec<f64>| {
out.clear();
let mut s = escrowed * mathx::pow(d, i as f64);
for _ in 0..=i {
out.push(s + held[i]);
s *= ratio;
}
};
let mut s_row = Vec::with_capacity(n + 1);
spots(n, &mut s_row);
let mut v: Vec<f64> = s_row.iter().map(|&s| spec.intrinsic(s)).collect();
let american = style == Style::American;
let tol = EXERCISE_TOLERANCE * strike;
let mut boundary = Vec::new();
let mut gaps = Vec::with_capacity(n + 1);
let mut step2: Option<(Vec<f64>, Vec<f64>)> = None;
let mut step1: Option<(Vec<f64>, Vec<f64>)> = None;
let mut hold_at_root = 0.0;
for i in (0..n).rev() {
spots(i, &mut s_row);
gaps.clear();
let mut exercised_any = false;
for j in 0..=i {
let hold = disc * (p * v[j + 1] + q * v[j]);
let exercise = spec.intrinsic(s_row[j]);
let gap = exercise - hold;
if american && exercise > 0.0 && gap > tol {
v[j] = exercise;
exercised_any = true;
} else {
v[j] = hold;
}
gaps.push(gap);
if i == 0 {
hold_at_root = hold;
}
}
if exercised_any {
boundary.push(boundary_at(right, i as f64 * dt, &s_row, &gaps, tol));
}
if i == 2 {
step2 = Some((s_row.clone(), v[..3].to_vec()));
} else if i == 1 {
step1 = Some((s_row.clone(), v[..2].to_vec()));
}
}
boundary.reverse();
let (s1, v1) = step1.expect("a tree of two or more steps has a first step");
let delta = (v1[1] - v1[0]) / (s1[1] - s1[0]);
let (s2, v2) = step2.expect("a tree of two or more steps has a second step");
let slope_up = (v2[2] - v2[1]) / (s2[2] - s2[1]);
let slope_down = (v2[1] - v2[0]) / (s2[1] - s2[0]);
let gamma = (slope_up - slope_down) / (0.5 * (s2[2] - s2[0]));
Ok(TreeValue { value: v[0], hold: hold_at_root, delta, gamma, steps: n, boundary })
}
fn boundary_at(right: Right, time: f64, spots: &[f64], gaps: &[f64], tol: f64) -> BoundaryPoint {
let exercised = |j: usize| gaps[j] > tol;
let (edge, next) = match right {
Right::Put => {
let edge = (0..spots.len()).rev().find(|&j| exercised(j)).expect("a step with an exercised node");
(edge, if edge + 1 < spots.len() { Some(edge + 1) } else { None })
}
Right::Call => {
let edge = (0..spots.len()).find(|&j| exercised(j)).expect("a step with an exercised node");
(edge, if edge > 0 { Some(edge - 1) } else { None })
}
};
BoundaryPoint { time, exercised: spots[edge], held: next.map(|k| spots[k]) }
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct ExerciseDecision {
pub exercise: bool,
pub intrinsic: f64,
pub hold: f64,
}
pub fn early_exercise(spec: &OptionSpec, dividends: &[CashDividend], steps: usize) -> Result<ExerciseDecision, String> {
let intrinsic = spec.intrinsic(spec.spot);
if spec.expiry == 0.0 {
price(spec, dividends, steps, Style::American, Lattice::LeisenReimer)?;
return Ok(ExerciseDecision { exercise: false, intrinsic, hold: intrinsic });
}
let tree = price(spec, dividends, steps, Style::American, Lattice::LeisenReimer)?;
let eve = dividends
.iter()
.any(|d| d.amount > 0.0 && d.time.abs() <= TIME_TOLERANCE && d.time < spec.expiry);
let allowed = match spec.right {
Right::Call => eve,
Right::Put => true,
};
let exercise = allowed && intrinsic > 0.0 && intrinsic - tree.hold > EXERCISE_TOLERANCE * spec.strike;
Ok(ExerciseDecision { exercise, intrinsic, hold: tree.hold })
}
pub fn exercised_at_expiry(right: Right, strike: f64, settlement: f64) -> bool {
intrinsic(right, strike, settlement) >= EXERCISE_BY_EXCEPTION - 1e-9
}
#[cfg(test)]
mod tests {
use super::*;
const LR: Lattice = Lattice::LeisenReimer;
const CRR: Lattice = Lattice::CoxRossRubinstein;
fn norm_cdf(x: f64) -> f64 {
0.5 * mathx::erfc(-x / core::f64::consts::SQRT_2)
}
fn black_scholes(spec: &OptionSpec) -> f64 {
let OptionSpec { right, spot, strike, rate, vol, expiry } = *spec;
let sd = vol * mathx::sqrt(expiry);
let d1 = (mathx::log(spot / strike) + (rate + 0.5 * vol * vol) * expiry) / sd;
let d2 = d1 - sd;
let df = mathx::exp(-rate * expiry);
match right {
Right::Call => spot * norm_cdf(d1) - strike * df * norm_cdf(d2),
Right::Put => strike * df * norm_cdf(-d2) - spot * norm_cdf(-d1),
}
}
fn value(spec: &OptionSpec, divs: &[CashDividend], n: usize, style: Style, lattice: Lattice) -> f64 {
price(spec, divs, n, style, lattice).unwrap().value
}
#[test]
fn the_reference_black_scholes_reads_hulls_example() {
let c = black_scholes(&OptionSpec::new(Right::Call, 42.0, 40.0, 0.10, 0.20, 0.5));
let p = black_scholes(&OptionSpec::new(Right::Put, 42.0, 40.0, 0.10, 0.20, 0.5));
assert!((c - 4.76).abs() < 0.005, "{c}");
assert!((p - 0.81).abs() < 0.005, "{p}");
}
#[test]
fn a_european_tree_converges_to_black_scholes_at_second_order() {
for right in [Right::Call, Right::Put] {
for &(s, k, r, vol, t) in &[
(100.0, 100.0, 0.05, 0.20, 1.0),
(42.0, 40.0, 0.10, 0.20, 0.5),
(50.0, 60.0, 0.03, 0.45, 0.25),
(80.0, 70.0, 0.00, 0.30, 2.0),
] {
let spec = OptionSpec::new(right, s, k, r, vol, t);
let bs = black_scholes(&spec);
let errs: Vec<f64> = [51, 101, 201, 401]
.iter()
.map(|&n| (value(&spec, &[], n, Style::European, LR) - bs).abs())
.collect();
assert!(errs[3] < 1e-4, "{right:?} {s} {k}: {errs:?}");
for w in errs.windows(2) {
assert!(w[1] < w[0] / 3.0 || w[1] < 1e-9, "{right:?} {s} {k}: {errs:?}");
}
}
}
}
#[test]
fn an_american_call_without_dividends_is_the_european() {
for &(s, k, r, vol, t) in &[
(100.0, 100.0, 0.05, 0.20, 1.0),
(150.0, 100.0, 0.08, 0.30, 0.5),
(60.0, 100.0, 0.02, 0.50, 3.0),
(100.0, 90.0, 0.00, 0.25, 1.0),
] {
let spec = OptionSpec::new(Right::Call, s, k, r, vol, t);
let a = price(&spec, &[], 201, Style::American, LR).unwrap();
let e = value(&spec, &[], 201, Style::European, LR);
assert!((a.value - e).abs() <= 1e-12 * s, "{s} {k}: {} against {e}", a.value);
assert!(a.boundary.is_empty(), "{s} {k}: exercised at {:?}", a.boundary[0]);
let decision = early_exercise(&spec, &[], 201).unwrap();
assert!(!decision.exercise);
}
}
#[test]
fn an_american_put_is_worth_more_than_the_european() {
for &(s, k, r, vol, t) in &[
(100.0, 100.0, 0.05, 0.20, 1.0),
(50.0, 50.0, 0.10, 0.40, 5.0 / 12.0),
(80.0, 100.0, 0.08, 0.30, 0.5),
(120.0, 100.0, 0.03, 0.25, 2.0),
] {
let spec = OptionSpec::new(Right::Put, s, k, r, vol, t);
let a = value(&spec, &[], 201, Style::American, LR);
let e = value(&spec, &[], 201, Style::European, LR);
assert!(a > e + 1e-4, "{s} {k}: {a} against {e}");
assert!(a >= spec.intrinsic(s));
}
let spec = OptionSpec::new(Right::Put, 90.0, 100.0, 0.0, 0.30, 1.0);
let a = price(&spec, &[], 201, Style::American, LR).unwrap();
assert!((a.value - value(&spec, &[], 201, Style::European, LR)).abs() < 1e-12);
assert!(a.boundary.is_empty());
}
#[test]
fn hulls_five_step_american_put() {
let spec = OptionSpec::new(Right::Put, 50.0, 50.0, 0.10, 0.40, 5.0 / 12.0);
let v = value(&spec, &[], 5, Style::American, CRR);
assert!((v - 4.49).abs() < 0.005, "{v}");
let lr = value(&spec, &[], 101, Style::American, LR);
assert!((lr - 4.28).abs() < 0.005, "{lr}");
let crr = value(&spec, &[], 500, Style::American, CRR);
assert!((crr - 4.283).abs() < 0.0005, "{crr}");
assert!((lr - crr).abs() < 0.002, "{lr} against {crr}");
assert!((black_scholes(&spec) - 4.08).abs() < 0.005);
}
#[test]
fn hulls_five_step_put_with_a_known_dividend() {
let spec = OptionSpec::new(Right::Put, 52.0, 50.0, 0.10, 0.40, 5.0 / 12.0);
let divs = [CashDividend::new(3.5 / 12.0, 2.06)];
let v = value(&spec, &divs, 5, Style::American, CRR);
assert!((v - 4.44).abs() < 0.005, "{v}");
let pv = 2.06 * mathx::exp(-0.10 * 3.5 / 12.0);
assert!((52.0 - pv - 50.0).abs() < 0.005);
}
#[test]
fn hulls_dividend_call_and_blacks_approximation() {
let spec = OptionSpec::new(Right::Call, 40.0, 40.0, 0.09, 0.30, 0.5);
let divs = [CashDividend::new(2.0 / 12.0, 0.5), CashDividend::new(5.0 / 12.0, 0.5)];
let pv: f64 = divs.iter().map(|d| d.amount * mathx::exp(-0.09 * d.time)).sum();
let escrowed = OptionSpec { spot: 40.0 - pv, ..spec };
assert!((black_scholes(&escrowed) - 3.67).abs() < 0.005);
let short = OptionSpec { spot: 40.0 - 0.5 * mathx::exp(-0.09 * 2.0 / 12.0), expiry: 5.0 / 12.0, ..spec };
assert!((black_scholes(&short) - 3.52).abs() < 0.005);
let e = value(&spec, &divs, 401, Style::European, LR);
assert!((e - black_scholes(&escrowed)).abs() < 1e-4, "{e}");
let a = value(&spec, &divs, 401, Style::American, LR);
assert!(a >= 3.67 - 0.005 && a > e, "{a} {e}");
}
#[test]
fn the_put_boundary_rises_to_the_strike() {
for &(r, vol, t) in &[(0.10, 0.40, 5.0 / 12.0), (0.05, 0.20, 1.0), (0.08, 0.30, 2.0)] {
let spec = OptionSpec::new(Right::Put, 100.0, 100.0, r, vol, t);
let tree = price(&spec, &[], 401, Style::American, LR).unwrap();
let b = &tree.boundary;
assert!(b.len() > 300, "{r} {vol} {t}: {} points", b.len());
for w in b.windows(2) {
assert!(w[1].time > w[0].time);
let held = w[1].held.expect("a node above the boundary is held");
assert!(w[1].exercised < held && w[1].exercised < 100.0);
assert!(held > w[0].exercised, "{r} {vol} {t}: {:?} then {:?}", w[0], w[1]);
}
let mid = |p: &BoundaryPoint| 0.5 * (p.exercised + p.held.unwrap());
let stride = b.len() / 10;
for k in (stride..b.len()).step_by(stride) {
assert!(mid(&b[k]) > mid(&b[k - stride]), "{r} {vol} {t}: {:?} then {:?}", b[k - stride], b[k]);
}
let last = b.last().unwrap();
assert!(last.time > 0.99 * t && last.held.unwrap() > 99.0, "{last:?}");
assert!(mid(&b[0]) < mid(last) - 5.0, "{:?} {last:?}", b[0]);
}
}
#[test]
fn a_call_is_exercised_only_on_the_eve_and_deeper_with_more_life_left() {
let mut previous = 0.0;
for t in [0.15, 0.25, 0.4] {
let spec = OptionSpec::new(Right::Call, 100.0, 100.0, 0.05, 0.25, t);
let divs = [CashDividend::new(0.1, 3.0)];
let tree = price(&spec, &divs, 401, Style::American, LR).unwrap();
assert_eq!(tree.boundary.len(), 1, "{t}: {:?}", tree.boundary);
let point = tree.boundary[0];
assert!(point.time <= 0.1 && point.time > 0.1 - t / 401.0, "{point:?}");
let held = point.held.expect("a node below the boundary is held");
assert!(held < point.exercised && held > 100.0, "{t}: {point:?}");
assert!(point.exercised > previous, "{t}: {point:?}");
previous = point.exercised;
}
let long = OptionSpec::new(Right::Call, 100.0, 100.0, 0.05, 0.25, 1.0);
let tree = price(&long, &[CashDividend::new(0.1, 3.0)], 401, Style::American, LR).unwrap();
assert!(tree.boundary.is_empty(), "{:?}", tree.boundary);
}
#[test]
fn dividends_count_only_before_their_ex_dates() {
let spec = OptionSpec::new(Right::Call, 100.0, 100.0, 0.05, 0.25, 0.5);
let none = value(&spec, &[], 201, Style::American, LR);
for time in [0.5, 0.75, -0.01] {
let v = value(&spec, &[CashDividend::new(time, 2.0)], 201, Style::American, LR);
assert_eq!(v, none, "{time}");
}
let divs = [CashDividend::new(0.25, 2.0)];
let with = value(&spec, &divs, 201, Style::American, LR);
let euro = value(&spec, &divs, 201, Style::European, LR);
assert!(with < none && with > euro, "{with} {euro} {none}");
let put = OptionSpec { right: Right::Put, ..spec };
assert!(value(&put, &divs, 201, Style::American, LR) > value(&put, &[], 201, Style::American, LR));
let deep = OptionSpec::new(Right::Call, 150.0, 100.0, 0.05, 0.25, 0.5);
let eve = [CashDividend::from_ex_day(10, 11, 5.0)];
assert_eq!(eve[0].time, 0.0);
let decision = early_exercise(&deep, &eve, 201).unwrap();
assert!(decision.exercise && decision.intrinsic > decision.hold, "{decision:?}");
let tree = price(&deep, &eve, 201, Style::American, LR).unwrap();
assert_eq!(tree.value, 50.0);
let ahead = [CashDividend::from_ex_day(10, 12, 5.0)];
assert!(!early_exercise(&deep, &ahead, 201).unwrap().exercise);
let small = [CashDividend::from_ex_day(10, 11, 0.01)];
let atm = OptionSpec { spot: 101.0, ..deep };
let d = early_exercise(&atm, &small, 201).unwrap();
assert!(!d.exercise && d.hold > d.intrinsic, "{d:?}");
}
#[test]
fn a_european_with_a_dividend_is_black_scholes_on_the_escrowed_spot() {
let spec = OptionSpec::new(Right::Call, 100.0, 95.0, 0.04, 0.30, years(42));
for ex in [1, 2, 11, 30, 42] {
let divs = [CashDividend::from_ex_day(0, ex, 1.5)];
let e = value(&spec, &divs, steps_for(42), Style::European, LR);
let pv = if divs[0].time < spec.expiry { 1.5 * mathx::exp(-0.04 * divs[0].time) } else { 0.0 };
let bs = black_scholes(&OptionSpec { spot: 100.0 - pv, ..spec });
assert!((e - bs).abs() < 2e-4, "{ex}: {e} against {bs}");
}
}
#[test]
fn the_dealer_exercises_a_deep_put_when_rates_are_high() {
let deep = OptionSpec::new(Right::Put, 60.0, 100.0, 0.08, 0.20, 0.5);
let d = early_exercise(&deep, &[], 201).unwrap();
assert!(d.exercise && d.intrinsic == 40.0 && d.hold < 40.0, "{d:?}");
assert!(!early_exercise(&OptionSpec { rate: 0.0, ..deep }, &[], 201).unwrap().exercise);
assert!(!early_exercise(&OptionSpec { spot: 95.0, ..deep }, &[], 201).unwrap().exercise);
let eve = [CashDividend::new(0.0, 3.0)];
let near = OptionSpec { spot: 78.0, ..deep };
assert!(early_exercise(&near, &[], 201).unwrap().exercise);
assert!(!early_exercise(&near, &eve, 201).unwrap().exercise);
}
#[test]
fn the_greeks_match_black_scholes_on_a_european() {
let spec = OptionSpec::new(Right::Call, 100.0, 100.0, 0.05, 0.20, 1.0);
let tree = price(&spec, &[], 401, Style::European, LR).unwrap();
let sd = 0.20;
let d1 = (0.05 + 0.5 * 0.04) / sd;
let bs_delta = norm_cdf(d1);
let bs_gamma = mathx::exp(-0.5 * d1 * d1) / mathx::sqrt(2.0 * core::f64::consts::PI) / (100.0 * sd);
assert!((tree.delta - bs_delta).abs() < 2e-3, "{} {bs_delta}", tree.delta);
assert!((tree.gamma - bs_gamma).abs() < 2e-3, "{} {bs_gamma}", tree.gamma);
let put = price(&OptionSpec { right: Right::Put, ..spec }, &[], 401, Style::European, LR).unwrap();
assert!((put.delta - (bs_delta - 1.0)).abs() < 2e-3);
}
#[test]
fn exercise_by_exception_at_a_cent() {
assert!(exercised_at_expiry(Right::Call, 50.0, 50.01));
assert!(!exercised_at_expiry(Right::Call, 50.0, 50.0));
assert!(!exercised_at_expiry(Right::Call, 50.0, 50.009));
assert!(exercised_at_expiry(Right::Put, 42.5, 42.49));
assert!(!exercised_at_expiry(Right::Put, 42.5, 42.5));
let spec = OptionSpec::new(Right::Put, 40.0, 42.5, 0.05, 0.3, 0.0);
assert_eq!(value(&spec, &[], 101, Style::American, LR), 2.5);
assert!(!early_exercise(&spec, &[], 101).unwrap().exercise);
}
#[test]
fn bad_inputs_are_refused() {
let ok = OptionSpec::new(Right::Put, 100.0, 100.0, 0.05, 0.2, 1.0);
assert!(price(&ok, &[], 100, Style::American, LR).is_err());
assert!(price(&ok, &[], 1, Style::American, CRR).is_err());
assert!(price(&OptionSpec { vol: 0.0, ..ok }, &[], 101, Style::American, LR).is_err());
assert!(price(&OptionSpec { spot: f64::NAN, ..ok }, &[], 101, Style::American, LR).is_err());
assert!(price(&ok, &[CashDividend::new(0.5, 120.0)], 101, Style::American, LR).is_err());
assert!(price(&ok, &[CashDividend::new(0.5, -1.0)], 101, Style::American, LR).is_err());
assert_eq!(steps_for(0), MIN_STEPS);
assert_eq!(steps_for(21), MIN_STEPS);
assert_eq!(steps_for(252), 505);
assert_eq!(steps_for(51) % 2, 1);
}
#[test]
fn the_lookahead_feeds_the_tree() {
use crate::market::dividends::{lookahead, phase_for, DividendState};
use crate::params::ModelParams;
let mut p = ModelParams::pt_v1();
p.dividend_payout_share = 1.0;
let state = DividendState {
payout: 0.5,
target_yield: 0.03,
price_ema: 100.0,
amount: 0.75,
declared: true,
accrual: 0.0,
paid_today: 0.0,
};
let ph = phase_for("ACME");
let day = ph + 63 * 2 - 1;
let expiry = day + 84;
let ahead = lookahead(&p, "ACME", Some(&state), day, 100.0, expiry);
assert_eq!(ahead.len(), 2);
let divs: Vec<CashDividend> =
ahead.iter().map(|u| CashDividend::from_ex_day(day, u.ex_day, u.amount)).collect();
assert_eq!(divs[0].time, 0.0);
assert_eq!(divs[1].time, years(63));
let spec = OptionSpec::new(Right::Call, 100.0, 90.0, 0.04, 0.25, years(expiry - day));
let with = value(&spec, &divs, steps_for(84), Style::American, LR);
let without = value(&spec, &[], steps_for(84), Style::American, LR);
assert!(with < without);
}
}