Skip to content

Commit

Permalink
Update stochastic processes example (#52 closed).
Browse files Browse the repository at this point in the history
  • Loading branch information
avhz committed Jun 18, 2023
1 parent 00f7981 commit 308a04e
Show file tree
Hide file tree
Showing 21 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions examples/option_pricing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use RustQuant::options::*;

fn main() {
let VanillaOption = EuropeanOption {
let vanilla_option = EuropeanOption {
initial_price: 100.0,
strike_price: 110.0,
risk_free_rate: 0.05,
Expand All @@ -10,7 +10,7 @@ fn main() {
time_to_maturity: 0.5,
};

let prices = VanillaOption.price();
let prices = vanilla_option.price();

println!("Call price = \t {}", prices.0);
println!("Put price = \t {}", prices.1);
Expand Down
44 changes: 24 additions & 20 deletions examples/stochastic_processes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// Run this example using:
// cargo run --example stochastic_processes
//
// This example generates paths for each stochastic process
// and plots them using the `plotters` crate.
//
// See the ./images/ directory for the output.

use RustQuant::{
stochastics::{
black_derman_toy::{BlackDermanToy, Sigma},
Expand All @@ -16,40 +24,36 @@ fn main() {
let ev = ExtendedVasicek::new(alpha_t, 2.0, theta_t);
let gbm = GeometricBrownianMotion::new(0.05, 0.9);
let hl = HoLee::new(0.2, theta_t);
let hw = HullWhite::new(2.0, 0.2, theta_t);
let hw = HullWhite::new(0.1, 0.2, theta_t);
let ou = OrnsteinUhlenbeck::new(0.05, 0.9, 0.1);

// Generate path using Euler-Maruyama scheme.
// Parameters: x_0, t_0, t_n, n, sims, parallel.
let abm_out = (&abm).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let bdt_out = (&bdt).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let bm_out = (&bm).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let bm_out = (&bm).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let cir_out = (&cir).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let ev_out = (&ev).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let ev_out = (&ev).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let gbm_out = (&gbm).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let hl_out = (&hl).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let hw_out = (&hw).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let ou_out = (&ou).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let hl_out = (&hl).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let hw_out = (&hw).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);
let ou_out = (&ou).euler_maruyama(10.0, 0.0, 0.5, 100, 1, false);

// Plot the paths.
//
// CURRENTLY PANICS.
// Waiting on updated plotters crate (v0.0.34 -> v0.0.35).
// See here: https://github.com/plotters-rs/plotters/issues/453
plot_vector((&abm_out.paths[0]).clone(),"./images/arithmetic_brownian_motion.png").unwrap();
plot_vector((&abm_out.paths[0]).clone(), "./images/arithmetic_brownian_motion.png").unwrap();
plot_vector((&bdt_out.paths[0]).clone(), "./images/black_derman_toy.png").unwrap();
plot_vector((&bm_out.paths[0]).clone(), "./images/brownian_motion.png").unwrap();
plot_vector((&cir_out.paths[0]).clone(),"./images/cox_ingersoll_ross.png").unwrap();
plot_vector((&ev_out.paths[0]).clone(), "./images/extended_vasicek.png").unwrap();
plot_vector((&gbm_out.paths[0]).clone(),"./images/geometric_brownian_motion.png").unwrap();
plot_vector((&hl_out.paths[0]).clone(), "./images/ho_lee.png").unwrap();
plot_vector((&hw_out.paths[0]).clone(), "./images/hull_white.png").unwrap();
plot_vector((&ou_out.paths[0]).clone(),"./images/ornstein_uhlenbeck.png").unwrap();
plot_vector((&bm_out.paths[0]).clone(), "./images/brownian_motion.png").unwrap();
plot_vector((&cir_out.paths[0]).clone(), "./images/cox_ingersoll_ross.png").unwrap();
plot_vector((&ev_out.paths[0]).clone(), "./images/extended_vasicek.png").unwrap();
plot_vector((&gbm_out.paths[0]).clone(), "./images/geometric_brownian_motion.png").unwrap();
plot_vector((&hl_out.paths[0]).clone(), "./images/ho_lee.png").unwrap();
plot_vector((&hw_out.paths[0]).clone(), "./images/hull_white.png").unwrap();
plot_vector((&ou_out.paths[0]).clone(), "./images/ornstein_uhlenbeck.png").unwrap();
}

fn theta_t(_t: f64) -> f64 {
1.5
0.1
}
fn alpha_t(_t: f64) -> f64 {
2.0
0.05
}
Binary file removed images/BM1.png
Binary file not shown.
Binary file removed images/BM2.png
Binary file not shown.
Binary file removed images/BM3_parallel.png
Binary file not shown.
Binary file removed images/CIR1.png
Binary file not shown.
Binary file removed images/CIR2.png
Binary file not shown.
Binary file removed images/GBM1.png
Binary file not shown.
Binary file removed images/GBM2.png
Binary file not shown.
Binary file removed images/OU1.png
Binary file not shown.
Binary file removed images/OU2.png
Binary file not shown.
Binary file added images/arithmetic_brownian_motion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/black_derman_toy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/brownian_motion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cox_ingersoll_ross.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/extended_vasicek.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/geometric_brownian_motion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/ho_lee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/hull_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/ornstein_uhlenbeck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/autodiff/tape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// IMPORTS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use std::{rc::Rc, sync::Arc};

use super::OperationArity;

use {super::variable::Variable, super::vertex::Vertex, std::cell::RefCell};
Expand All @@ -27,13 +29,15 @@ use {super::variable::Variable, super::vertex::Vertex, std::cell::RefCell};
pub struct Tape {
/// Vector containing the vertices in the Wengert List.
pub vertices: RefCell<Vec<Vertex>>,
// pub vertices: RefCell<Rc<[Vertex]>>,
}

impl Default for Tape {
#[inline]
fn default() -> Self {
Tape {
vertices: RefCell::new(Vec::new()),
// vertices: RefCell::new(Rc::new([])),
}
}
}
Expand All @@ -45,6 +49,7 @@ impl Tape {
pub fn new() -> Self {
Tape {
vertices: RefCell::new(Vec::new()),
// vertices: RefCell::new(Rc::new([])),
}
}

Expand Down

0 comments on commit 308a04e

Please sign in to comment.