Skip to content

Commit

Permalink
fix: fix discount factor for Bachelier backend
Browse files Browse the repository at this point in the history
  • Loading branch information
avhz authored and avhz committed Oct 1, 2024
1 parent c6e7d7f commit 1c66875
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 122 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ argmin-math = "0.4.0" # https://docs.rs/argmin-math/latest/argmin_math/
derive_builder = "0.20.0" # https://docs.rs/derive_builder/latest/derive_builder/
errorfunctions = "0.2.0" # https://docs.rs/errorfunctions/latest/errorfunctions/
nalgebra = "0.33.0" # https://docs.rs/nalgebra/latest/nalgebra/
ndarray = "0.16.1" # https://docs.rs/ndarray/latest/ndarray/
ndrustfft = "0.4.0" # https://docs.rs/ndrustfft/latest/ndrustfft/
ndarray-rand = "0.14.0" # https://docs.rs/ndarray-rand/latest/ndarray_rand/
plotly = "0.10.0" # https://docs.rs/plotly/latest/plotly/
plotly = "0.10.0" # https://docs.rs/plotly/latest/plotly/
plotters = "0.3.5" # https://docs.rs/plotters/latest/plotters/
rand = "0.8.5" # https://docs.rs/rand/latest/rand/
rand_distr = "0.4.3" # https://docs.rs/rand_distr/latest/rand_distr/
Expand All @@ -76,6 +75,8 @@ thiserror = "1.0.57" # https://docs.rs/thiserror/latest/thiserror/
yahoo_finance_api = "2.1.0" # https://docs.rs/yahoo-finance-api/latest/yahoo_finance_api/
tokio-test = "0.4.3" # https://docs.rs/tokio-test/latest/tokio_test/

# https://docs.rs/ndarray/latest/ndarray/
ndarray = { version = "0.16.1", features = ["rayon"] }

# https://docs.rs/num/latest/num/
num = { version = "0.4.1", features = ["rand"] }
Expand Down
29 changes: 0 additions & 29 deletions book/src/Modules/data/curves.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,3 @@ Curves can be fit to market data. Here we include an example of a spot curve bei
{{#include ../../../../examples/curves_spot.rs}}
```


<table>
<tr>
<th> Good </th>
<th> Bad </th>
</tr>
<tr>
<td>

```rust
int foo() {
int result = 4;
return result;
}
```

</td>
<td>

```rust
int foo() {
int x = 4;
return x;
}
```

</td>
</tr>
</table>
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
[build-system]
requires = ["maturin>=1.6,<2.0"]
build-backend = "maturin"

[tool.maturin]
features = ["pyo3/extension-module"]

[project]
name = "RustQuant"
requires-python = ">=3.8"
Expand All @@ -14,3 +7,10 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]

[build-system]
requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"

[tool.maturin]
features = ["pyo3/extension-module"]
7 changes: 3 additions & 4 deletions src/data/yahoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
//! Module to fetch data from Yahoo! Finance,
//! and store it in a Polars DataFrame object.

use crate::error::RustQuantError;
use polars::prelude::*;
use time::OffsetDateTime;
use yahoo::YahooError;
use yahoo_finance_api as yahoo;

use crate::error::RustQuantError;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STRUCTS, TRAITS, AND ENUMS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -209,7 +208,7 @@ impl YahooFinanceReader for YahooFinanceData {
let adjclose = quotes.iter().map(|q| q.adjclose).collect::<Vec<_>>();

let df = df!(
"date" => Series::new("date", date).cast(&DataType::Date)?,
"date" => Series::new("date".into(), date).cast(&DataType::Date)?,
"open" => open,
"high" => high,
"low" => low,
Expand Down Expand Up @@ -266,7 +265,7 @@ impl YahooFinanceReader for YahooFinanceData {
let df = df!(
"contract" => contract,
"strike" => strike,
"last_trade_date" => Series::new("last_trade_date", last_trade_date)
"last_trade_date" => Series::new("last_trade_date".into(), last_trade_date)
.cast(&DataType::Date)
?,
"last_price" => last_price,
Expand Down
5 changes: 3 additions & 2 deletions src/pricer/backends/bachelier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ impl ModifiedBachelier {
);

let d1 = (S - K) / (v * T.sqrt());
let df = (-r * T).exp();

let n = Gaussian::default();

match self.option_type {
TypeFlag::Call => ((S - K) * (-r * T).exp()) * n.cdf(d1) + v * T.sqrt() * n.pdf(d1),
TypeFlag::Put => ((K - S) * (-r * T).exp()) * n.cdf(-d1) + v * T.sqrt() * n.pdf(-d1),
TypeFlag::Call => df * ((S - K) * n.cdf(d1) + v * T.sqrt() * n.pdf(d1)),
TypeFlag::Put => df * ((K - S) * n.cdf(-d1) + v * T.sqrt() * n.pdf(-d1)),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/stochastics/fractional_brownian_motion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,21 @@ impl FractionalBrownianMotion {
}
let r_fft = FftHandler::new(r.len());
let mut sqrt_eigenvalues = Array1::<Complex<f64>>::zeros(r.len());

ndfft_par(&data, &mut sqrt_eigenvalues, &r_fft, 0);

sqrt_eigenvalues.par_mapv_inplace(|x| Complex::new((x.re / (2.0 * n as f64)).sqrt(), x.im));

let rnd = Array1::<Complex<f64>>::random(
2 * n,
ComplexDistribution::new(StandardNormal, StandardNormal),
);
let fgn = &sqrt_eigenvalues * &rnd;
let fft_handler = FftHandler::new(2 * n);
let mut fgn_fft = Array1::<Complex<f64>>::zeros(2 * n);

ndfft_par(&fgn, &mut fgn_fft, &fft_handler, 0);

let fgn = fgn_fft
.slice(s![1..n + 1])
.mapv(|x: Complex<f64>| (x.re * (n as f64).powf(-self.hurst)) * t_n.powf(self.hurst));
Expand Down
51 changes: 0 additions & 51 deletions src/time/countries/custom_calendar.rs

This file was deleted.

51 changes: 24 additions & 27 deletions src/time/countries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,74 @@
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//! This module defines calendars and holidays for different countries.

/// This module defines a custom calendar type.
pub mod custom_calendar;
//! calendars and holidays for different countries.

/// Calendars implemented for African countries.
pub mod africa {
/// This module defines Botswana holidays and calendars.
/// Botswana holidays and calendars.
pub mod botswana;
}

/// Calendars implemented for Asian countries.
pub mod asia {
/// This module defines China holidays and calendars.
/// China holidays and calendars.
pub mod china;
/// This module defines Hong Kong holidays and calendars.
/// Hong Kong holidays and calendars.
pub mod hong_kong;
/// This module defines India holidays and calendars.
/// India holidays and calendars.
pub mod india;
/// This module defines Indonesia holidays and calendars.
/// Indonesia holidays and calendars.
pub mod indonesia;
/// This module defines Singapore holidays and calendars.
/// Singapore holidays and calendars.
pub mod singapore;
}

/// Calendars implemented for European countries.
pub mod europe {
/// This module defines Austria holidays and calendars.
/// Austria holidays and calendars.
pub mod austria;
/// This module defines Czech Republic holidays and calendars.
/// Czech Republic holidays and calendars.
pub mod czech_republic;
/// This module defines Denmark holidays and calendars.
/// Denmark holidays and calendars.
pub mod denmark;
/// This module defines Finland holidays and calendars.
/// Finland holidays and calendars.
pub mod finland;
/// This module defines France holidays and calendars.
/// France holidays and calendars.
pub mod france;
/// This module defines Germany holidays and calendars.
/// Germany holidays and calendars.
pub mod germany;
/// This module defines Hungary holidays and calendars.
/// Hungary holidays and calendars.
pub mod hungary;
/// This module defines Iceland holidays and calendars.
/// Iceland holidays and calendars.
pub mod iceland;
/// This module defines Netherlands holidays and calendars.
/// Netherlands holidays and calendars.
pub mod netherlands;
/// This module defines United Kingdom holidays and calendars.
/// United Kingdom holidays and calendars.
pub mod united_kingdom;
}

/// Calendars implemented for North American countries.
pub mod north_america {
/// This module defines Canada holidays and calendars.
/// Canada holidays and calendars.
pub mod canada;
/// This module defines United States holidays and calendars.
/// United States holidays and calendars.
pub mod united_states;
}

/// Calendars implemented for Oceanian countries.
pub mod oceania {
/// This module defines Australia holidays and calendars.
/// Australia holidays and calendars.
pub mod australia;
/// This module defines New Zealand holidays and calendars.
/// New Zealand holidays and calendars.
pub mod new_zealand;
}

/// Calendars implemented for South American countries.
pub mod south_america {
/// This module defines Argentina holidays and calendars.
/// Argentina holidays and calendars.
pub mod argentina;
/// This module defines Brazil holidays and calendars.
/// Brazil holidays and calendars.
pub mod brazil;
/// This module defines Chile holidays and calendars.
/// Chile holidays and calendars.
pub mod chile;
}

0 comments on commit 1c66875

Please sign in to comment.