Skip to content

Index out of bounds in Bachelier put_iv #312

@dbrun3

Description

@dbrun3

Describe the bug
panic: index out of bounds: the len is 9 but the index is 9

To Reproduce
Steps to reproduce the behavior:
let iv = Bachelier::new(f, r, 0.0).iv(p, k, t, TypeFlag::Put);

Expected behavior
no panic

Below are the call/put iv fns for bachelier. Notice the put_iv for B iterates from 1 to 9

#[inline]
    pub(crate) fn call_iv(price: f64, f: f64, k: f64, t: f64) -> f64 {
        let v = (f - k).abs() / (2. * price - (f - k));
        let eta = v / v.atanh();

        let mut sum1 = 0.0;
        let mut sum2 = 0.0;

        for k in 0..A.len() {
            sum1 += A[k] * eta.powi(k as i32);
        }

        for k in 0..B.len() {
            sum2 += B[k] * eta.powi(k as i32);
        }

        let hn = eta.sqrt() * sum1 / (1. + sum2);

        (FRAC_PI_2 / t).sqrt() * (2. * price - (f - k)) * hn
    }

    #[inline]
    pub(crate) fn put_iv(price: f64, f: f64, k: f64, t: f64) -> f64 {
        let v = (f - k).abs() / (2. * price + (f - k));
        let eta = v / v.atanh();

        let mut sum1 = 0.0;
        let mut sum2 = 0.0;

        for k in 0..A.len() {
            sum1 += A[k] * eta.powi(k as i32);
        }

        for k in 1..=B.len() {                        // <-- is this a typo?
            sum2 += B[k] * eta.powi(k as i32);
        }

        let hn = eta.sqrt() * sum1 / (1. + sum2);

        (FRAC_PI_2 / t).sqrt() * (2. * price + (f - k)) * hn
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions