Skip to content

Commit

Permalink
chore: Add benchmark test for implied normal vol
Browse files Browse the repository at this point in the history
  • Loading branch information
nakashima-hikaru committed Jan 19, 2024
1 parent e742c16 commit c08a089
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
16 changes: 8 additions & 8 deletions benches/bench.rs → benches/implied_black_vol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use rand::Rng;

extern crate test;

use test::Bencher;
use implied_vol::implied_black_volatility;
use test::Bencher;

#[bench]
fn bench_call_atm(b: &mut Bencher) {
fn call_atm(b: &mut Bencher) {
let price = 0.01;
let f = 100.0;
let k = f;
Expand All @@ -18,7 +18,7 @@ fn bench_call_atm(b: &mut Bencher) {
}

#[bench]
fn bench_call_itm(b: &mut Bencher) {
fn call_itm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
Expand All @@ -31,7 +31,7 @@ fn bench_call_itm(b: &mut Bencher) {
}

#[bench]
fn bench_call_otm(b: &mut Bencher) {
fn call_otm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
Expand All @@ -44,7 +44,7 @@ fn bench_call_otm(b: &mut Bencher) {
}

#[bench]
fn bench_put_atm(b: &mut Bencher) {
fn put_atm(b: &mut Bencher) {
let price = 0.01;
let f = 100.0;
let k = f;
Expand All @@ -53,7 +53,7 @@ fn bench_put_atm(b: &mut Bencher) {
b.iter(|| implied_black_volatility(price, f, k, t, q));
}
#[bench]
fn bench_put_itm(b: &mut Bencher) {
fn put_itm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
Expand All @@ -66,7 +66,7 @@ fn bench_put_itm(b: &mut Bencher) {
}

#[bench]
fn bench_put_otm(b: &mut Bencher) {
fn put_otm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
Expand All @@ -76,4 +76,4 @@ fn bench_put_otm(b: &mut Bencher) {
let t = 1e5 * r3;
let q = false;
b.iter(|| implied_black_volatility(price, f, k, t, q));
}
}
79 changes: 79 additions & 0 deletions benches/implied_normal_vol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#![feature(test)]

use rand::Rng;

extern crate test;

use implied_vol::implied_normal_volatility;
use test::Bencher;

#[bench]
fn call_atm(b: &mut Bencher) {
let price = 0.01;
let f = 100.0;
let k = f;
let t = 1.0;
let q = true;
b.iter(|| implied_normal_volatility(price, f, k, t, q));
}

#[bench]
fn call_itm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
let price = 1.0 * (1.0 - r) + 1.0 * r * r2;
let f = 1.0;
let k = 1.0 * r;
let t = 1e5 * r3;
let q = true;
b.iter(|| implied_normal_volatility(price, f, k, t, q));
}

#[bench]
fn call_otm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
let price = 1.0 * r * r2;
let f = 1.0 * r;
let k = 1.0;
let t = 1e5 * r3;
let q = true;
b.iter(|| implied_normal_volatility(price, f, k, t, q));
}

#[bench]
fn put_atm(b: &mut Bencher) {
let price = 0.01;
let f = 100.0;
let k = f;
let t = 1.0;
let q = false;
b.iter(|| implied_normal_volatility(price, f, k, t, q));
}
#[bench]
fn put_itm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
let price = 1.0 * r * r2;
let f = 1.0;
let k = 1.0 * r;
let t = 1e5 * r3;
let q = false;
b.iter(|| implied_normal_volatility(price, f, k, t, q));
}

#[bench]
fn put_otm(b: &mut Bencher) {
let seed: [u8; 32] = [13; 32];
let mut rng: rand::rngs::StdRng = rand::SeedableRng::from_seed(seed);
let (r, r2, r3): (f64, f64, f64) = rng.gen();
let price = 1.0 * (1.0 - r) + 1.0 * r * r2;
let f = 1.0 * r;
let k = 1.0;
let t = 1e5 * r3;
let q = false;
b.iter(|| implied_normal_volatility(price, f, k, t, q));
}

0 comments on commit c08a089

Please sign in to comment.