Skip to content

Commit

Permalink
fixed window size of sigma and made it a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Aug 14, 2023
1 parent a4aba0a commit 7400ca6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
19 changes: 10 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,6 @@ impl<'a> eframe::App for BalanceApp<'a> {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::new([true, true]).show(ui, |ui| {
heading(ui, "Balance");
if let Some(status_msg) = &self.status_msg {
ui.label(status_msg);
} else if self.charts.persisted.is_empty() {
ui.label("add simulated or historical charts to compute balances");
} else {
ui.label("balance computation ready");
}
ui.separator();
heading2(ui, "1. Add Charts");
egui::CollapsingHeader::new("Simulate").show(ui, |ui| {
egui::Grid::new("simulate-inputs")
Expand Down Expand Up @@ -377,6 +369,7 @@ impl<'a> eframe::App for BalanceApp<'a> {
expected_yearly_return,
is_eyr_independent,
noise,
12,
n_months,
) {
Ok(values) => {
Expand Down Expand Up @@ -547,7 +540,6 @@ impl<'a> eframe::App for BalanceApp<'a> {
}
ui.separator();
heading2(ui, "3. Investigate Results");
ui.label(" ");

ui.horizontal(|ui| {
if ui
Expand Down Expand Up @@ -613,6 +605,15 @@ impl<'a> eframe::App for BalanceApp<'a> {
export_csv(&self.charts).unwrap();
}
});
ui.separator();
if let Some(status_msg) = &self.status_msg {
ui.label(status_msg);
} else if self.charts.persisted.is_empty() {
ui.label("add simulated or historical charts to compute balances");
} else {
ui.label("balance computation ready");
}
ui.separator();
if let Some(best_trigger) = &self.best_rebalance_trigger {
egui::Grid::new("best-balance").show(ui, |ui| {
ui.label("(best) balance");
Expand Down
25 changes: 13 additions & 12 deletions src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,34 +185,34 @@ pub fn unix_to_now_nanos() -> BlcResult<u64> {
% (u64::MAX as u128)) as u64)
}

const SIGMA_WINDOW_SIZE: usize = 12;

pub fn random_walk(
expected_yearly_return: f64,
is_return_indpendent: bool,
is_markovian: bool,
sigma_mean: f64,
sigma_window_size: usize,
n_months: usize,
) -> BlcResult<Vec<f64>> {
let mut sigma_rng = StdRng::seed_from_u64(unix_to_now_nanos()?);
let sigma_distribution = Normal::new(sigma_mean, sigma_mean).map_err(to_blc)?;
let mut last_sigmas = [sigma_mean; SIGMA_WINDOW_SIZE];
let mut rv_rng = StdRng::seed_from_u64(unix_to_now_nanos()?);
let mut last_sigmas = vec![sigma_mean; sigma_window_size];
let mut monthly_factor_rng = StdRng::seed_from_u64(unix_to_now_nanos()?);
let start_price = 1.0;
let mut res = vec![start_price; n_months + 1];
let expected_monthly_return = (1.0 + (expected_yearly_return / 100.0)).powf(1.0 / 12.0);
let mut mu = expected_monthly_return;
for (i, sigma) in (1..(n_months + 1)).zip(sigma_distribution.sample_iter(&mut sigma_rng)) {
for i in 0..9 {
for i in 0..(sigma_window_size - 1) {
last_sigmas[i] = last_sigmas[i + 1];
}
last_sigmas[9] = sigma;
last_sigmas[sigma_window_size - 1] = sigma;
last_sigmas.sort_by(|a, b| a.partial_cmp(b).unwrap());
let sigma = last_sigmas[SIGMA_WINDOW_SIZE / 2].abs();
let sigma = last_sigmas[sigma_window_size / 2].abs();
let d = Normal::new(mu, sigma).map_err(to_blc)?;
let rv = d.sample(&mut rv_rng);
res[i] = (res[i - 1] * rv).max(1e-1);
let monthly_factor = d.sample(&mut monthly_factor_rng);
res[i] = (res[i - 1] * monthly_factor).max(1e-1);

if !is_return_indpendent && sigma - sigma_mean > 0.0 {
if !is_markovian && sigma - sigma_mean > 0.0 {
let actual_total_return: f64 = (1..=i)
.map(|j| res[j] / res[j - 1])
.product::<f64>()
Expand Down Expand Up @@ -441,6 +441,7 @@ fn compute_total_balance(
(initial_balances.iter().sum(), initial_balances.iter().sum())
}
}

#[test]
fn test_adapt() {
let price_dev = vec![3.0, 6.0, 12.0, 6.0];
Expand Down Expand Up @@ -558,13 +559,13 @@ fn test_compute_balance() {

#[test]
fn test_compound() {
let compound_interest: Vec<f64> = random_walk(5.0, true, 0.0, 240).unwrap();
let compound_interest: Vec<f64> = random_walk(5.0, true, 0.0, 12, 240).unwrap();
let (b, p) =
compute_total_balance(&[&compound_interest], &[10000.0], None, NONE_REBALANCE_DATA);
assert!((b - 26532.98).abs() < 1e-2);
assert!((p - 10000.0).abs() < 1e-12);

let compound_interest: Vec<f64> = random_walk(5.0, true, 0.0, 360).unwrap();
let compound_interest: Vec<f64> = random_walk(5.0, true, 0.0, 12, 360).unwrap();
let ci_len = compound_interest.len();
let monthly_payments: Vec<f64> = vec![1000.0; ci_len - 1];
let (b, _) = compute_total_balance(
Expand Down

0 comments on commit 7400ca6

Please sign in to comment.