Skip to content

Commit 0bd7d9b

Browse files
committed
refactor: use simulation types throughout
1 parent 0de7d10 commit 0bd7d9b

3 files changed

Lines changed: 68 additions & 65 deletions

File tree

src/lib/ForecastGraph.svelte

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
<script lang="ts">
22
import * as d3 from 'd3';
33
import { afterUpdate } from 'svelte';
4+
import type { YearState } from '$lib/simulation';
45
5-
interface DataPoint {
6-
year: number;
7-
savings: number;
8-
cashFlow: number;
9-
}
10-
export let forecast: Array<DataPoint>;
6+
export let simulation: Array<YearState>;
117
12-
$: data = forecast.map(({ savings, year, cashFlow }) => ({
8+
$: data = simulation.map(({ iskSavings, year, cashFlow }) => ({
139
year: new Date(year, 1),
14-
savings,
10+
savings: iskSavings,
1511
cashFlow
1612
}));
1713

src/lib/simulation.ts

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,61 @@ export interface TimeBoundCashFlow {
3232
//servicePensionContribution: number;
3333
}
3434

35+
export interface Scenario {
36+
parameters: Parameters;
37+
startState: YearState;
38+
endYear: number;
39+
}
40+
41+
export function defaultScenario(): Scenario {
42+
const currentYear = new Date().getFullYear();
43+
return {
44+
parameters: {
45+
inflationRate: 0.025,
46+
iskInterestRate: 0.08,
47+
iskTax: 0.01086,
48+
cashFlows: [
49+
{
50+
startYear: currentYear,
51+
endYear: currentYear + 8,
52+
yearlyNetCashFlow: 38500 * 12
53+
// publicPensionContributing: true,
54+
// servicePensionContribution: 4.5
55+
},
56+
{
57+
startYear: currentYear,
58+
endYear: currentYear + 8,
59+
yearlyNetCashFlow: 2755 * 12
60+
// publicPensionContributing: false,
61+
// servicePensionContribution: 100
62+
},
63+
{
64+
startYear: currentYear,
65+
endYear: currentYear + 8,
66+
yearlyNetCashFlow: -2755 * 12
67+
// publicPensionContributing: false,
68+
// servicePensionContribution: 0
69+
},
70+
{
71+
startYear: currentYear,
72+
endYear: currentYear + 50,
73+
yearlyNetCashFlow: -15000 * 12
74+
// pensionContributing: false,
75+
// publicPensionContributing: false,
76+
// servicePensionContribution: 0
77+
}
78+
]
79+
},
80+
startState: {
81+
year: currentYear,
82+
iskSavings: 1000000,
83+
compoundedInflation: 1,
84+
cashFlow: 0
85+
},
86+
endYear: 2100
87+
};
88+
}
89+
3590
function simulateYear(state: YearState, params: Parameters): YearState {
3691
const newYear = state.year + 1;
3792

@@ -66,10 +121,10 @@ function simulateYear(state: YearState, params: Parameters): YearState {
66121
};
67122
}
68123

69-
export function simulateYears(years: number, state: YearState, params: Parameters): YearState[] {
70-
const results = [state];
71-
for (let i = 0; i < years; i++) {
72-
const newState = simulateYear(results[i], params);
124+
export function simulate({ endYear, startState, parameters }: Scenario): YearState[] {
125+
const results = [startState];
126+
for (let i = 0; i + startState.year < endYear; i++) {
127+
const newState = simulateYear(results[i], parameters);
73128
results.push(newState);
74129
}
75130
return results;

src/routes/+page.svelte

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,20 @@
11
<script lang="ts">
22
import ForecastGraph from '$lib/ForecastGraph.svelte';
3-
import { type Parameters, simulateYears, type YearState } from '$lib/simulation';
3+
import { defaultScenario, simulate } from '$lib/simulation';
44
5-
const parameters: Parameters = {
6-
inflationRate: 0.025,
7-
iskInterestRate: 0.08,
8-
iskTax: 0.01086,
9-
cashFlows: [
10-
{
11-
startYear: 2024 + 0,
12-
endYear: 2024 + 8,
13-
yearlyNetCashFlow: 38500 * 12,
14-
// publicPensionContributing: true,
15-
// servicePensionContribution: 4.5
16-
},
17-
{
18-
startYear: 2024 + 0,
19-
endYear: 2024 + 8,
20-
yearlyNetCashFlow: 2755 * 12,
21-
// publicPensionContributing: false,
22-
// servicePensionContribution: 100
23-
},
24-
{
25-
startYear: 2024 + 0,
26-
endYear: 2024 + 8,
27-
yearlyNetCashFlow: -2755 * 12,
28-
// publicPensionContributing: false,
29-
// servicePensionContribution: 0
30-
},
31-
{
32-
startYear: 2024 + 0,
33-
endYear: 2024 + 50,
34-
yearlyNetCashFlow: -15000 * 12,
35-
// pensionContributing: false,
36-
// publicPensionContributing: false,
37-
// servicePensionContribution: 0
38-
}
39-
]
40-
};
41-
const startState: YearState = {
42-
year: 2024,
43-
iskSavings: 1000000,
44-
compoundedInflation: 1,
45-
cashFlow: 0
46-
};
5+
let scenario = defaultScenario();
476
48-
$: results = simulateYears(74, startState, parameters);
49-
$: data = results.map((res) => {
50-
return {
51-
year: res.year,
52-
savings: res.iskSavings,
53-
cashFlow: res.cashFlow
54-
};
55-
});
7+
$: simulation = simulate(scenario);
568
</script>
579

5810
<section>TODO scenario drawer</section>
5911
<section>TODO scenario editor</section>
6012
<section>
61-
<ForecastGraph forecast={data} />
13+
<ForecastGraph {simulation} />
6214
</section>
6315
<section>
6416
<h2>Results</h2>
65-
<pre>{JSON.stringify(results, null, 2)}</pre>
17+
<pre>{JSON.stringify(simulation, null, 2)}</pre>
6618
</section>
6719

6820
<style lang="scss">

0 commit comments

Comments
 (0)