Skip to content

Commit

Permalink
Add parsing of real distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorLop committed Oct 18, 2023
1 parent 6ba62e9 commit e2aaeec
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.4.6", features = ["derive", "cargo"] }
config = { version = "0.13.3", features = ["json"] }
csv = "1.3.0"
fake = { version = "2.8.0", features = ["dummy", "derive"] }
serde = "1.0.188"
serde_json = "1.0.107"
Expand Down
45 changes: 45 additions & 0 deletions real_distributions/msci_world_dist.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Year,Return
1979,6.908980833589644
1980,38.92918575149808
1981,13.624800989464056
1982,24.57454243718439
1983,41.45140680114741
1984,19.742431857683872
1985,15.803692420531426
1986,18.62865310811976
1987,-4.133160220808307
1988,31.71839306890252
1989,18.347075144314278
1990,-29.293024853587458
1991,24.559545672010483
1992,-0.36961383231766554
1993,34.27841465059389
1994,-2.1942071138661903
1995,14.767124934264613
1996,16.492766455253214
1997,29.360554551930118
1998,17.60075985317194
1999,46.20091722938297
2000,-6.26434398536187
2001,-12.18047971562547
2002,-32.67437382554782
2003,10.521691593588182
2004,6.374332418725557
2005,26.413772128195333
2006,7.548898600013495
2007,-2.4525157717235317
2008,-37.287507810164115
2009,25.575937449016973
2010,20.495592534694175
2011,-2.452266077128064
2012,13.587391807936502
2013,21.192840417371574
2014,19.198069429703562
2015,10.546743100412897
2016,11.039444380970483
2017,7.5812464768771335
2018,-4.382268824231092
2019,30.124827572422962
2020,6.107780474209095
2021,31.981215544038832
2022,-13.076273337277028
31 changes: 31 additions & 0 deletions real_distributions/sp500_dist.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Year,Return
1993,20.660370292792408
1994,-5.689900203172931
1995,30.793243963273053
1996,26.226017971721394
1997,49.02768015658609
1998,21.611566408810827
1999,41.64486939800583
2000,-1.8659416855132511
2001,-6.966709116449332
2002,-34.535329815506714
2003,6.849972946362581
2004,2.81479079869347
2005,21.132978567911394
2006,3.7224518904028967
2007,-5.620848612087319
2008,-33.358136554859186
2009,22.1718425507653
2010,24.0535368956743
2011,5.4500455539601615
2012,13.761233348757433
2013,26.657499787013013
2014,29.138968478655936
2015,13.061470488818411
2016,15.634907769748697
2017,7.0813734035868245
2018,0.15019989075513643
2019,34.01450487798241
2020,8.393156334654396
2021,39.44407338606754
2022,-13.043681942849359
54 changes: 54 additions & 0 deletions src/distributions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use csv::ReaderBuilder;
use std::collections::HashMap;

pub fn get_distributions() -> HashMap<&'static str, Vec<f64>> {
let mut distributions = HashMap::new();

let mut sp500_reader = ReaderBuilder::new()
.from_path("real_distributions/sp500_dist.csv")
.expect("Error loading SP&500 distribution");
let mut msci_world_reader = ReaderBuilder::new()
.from_path("real_distributions/msci_world_dist.csv")
.expect("Error loading MSCI World distribution");

let mut sp500_dist: Vec<f64> = Vec::new();
let mut msci_world_dist: Vec<f64> = Vec::new();

for record in sp500_reader.records().flatten() {
let rate: f64 = record[1]
.parse()
.expect("Failure parsing a return interest into a f64");
sp500_dist.push(rate);
}

if let Some(old_value) = distributions.insert("sp500", sp500_dist) {
panic!("Found an existing value for sp500: {:?}", old_value);
}

for record in msci_world_reader.records().flatten() {
let rate: f64 = record[1]
.parse()
.expect("Failure parsing a return interest into a f64");
msci_world_dist.push(rate);
}

if let Some(old_value) = distributions.insert("msci_world", msci_world_dist) {
panic!("Found an existing value for msci_world: {:?}", old_value);
};

distributions
}

#[cfg(test)]
mod test {
use super::get_distributions;
use pretty_assertions::assert_eq;

#[test]
fn test_get_distributions() {
let distributions = get_distributions();

assert_eq!(distributions.get("sp500").unwrap().len(), 30);
assert_eq!(distributions.get("msci_world").unwrap().len(), 44);
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use clap::{arg, command, Parser};
use config::{Config, File, FileFormat};
use serde::Deserialize;

mod distributions;
mod investment;
mod types;

use investment::Investment;
use types::PositiveFloat;

Expand Down

0 comments on commit e2aaeec

Please sign in to comment.