Skip to content

Instantly share code, notes, and snippets.

@ericdevries
Created April 16, 2023 08:59
Show Gist options
  • Save ericdevries/bd5d1d2d12b46e53ca23d69507c80ac2 to your computer and use it in GitHub Desktop.
Save ericdevries/bd5d1d2d12b46e53ca23d69507c80ac2 to your computer and use it in GitHub Desktop.
tax calculator
import numpy as np
from typing import Callable
from dataclasses import dataclass
import random
gen = np.random.default_rng()
@dataclass
class Simulation:
name: str
calculator: Callable
def get_ranges(years=30):
ranges = []
for i in range(len(historical_results) - years + 1):
result = reversed(historical_results[i : i + years])
result = map(lambda s: s / 100, result)
ranges.append(list(result))
return ranges
def get_random_range(years=30):
start = random.randint(0, len(historical_results) - years)
result = reversed(historical_results[start : start + years])
result = map(lambda s: s / 100, result)
return list(result)
def calculate_new_taxes(previous_value, current_value):
return max(0, current_value - previous_value - 800) * 0.34
def calculate_old_taxes(previous_value, current_value):
return current_value * (1.2 / 100)
def calculate_no_taxes(previous_value, current_value):
return 0
def cagr(begin, end, years):
return (end / begin) ** (1 / years) - 1
simulations = [
Simulation("new taxes", calculate_new_taxes),
Simulation("old taxes", calculate_old_taxes),
Simulation("no taxes", calculate_no_taxes),
]
def run():
for simulation in simulations:
results = []
for i in get_ranges(30):
results.append(run_simulation(simulation.calculator, i))
df = np.array(results)
start_amount = 100000
contributions = df[:, 0].mean()
final_amount = df[:, 2].mean()
taxes_paid = df[:, 1].mean()
nett_gains = final_amount - start_amount
return_rate = cagr(start_amount, final_amount, 30)
print("=====", simulation.name, "=====")
print("total investments", start_amount + contributions)
print("final amount", final_amount)
print("total taxes paid", taxes_paid)
print("nett gainz", nett_gains)
print("return rate", return_rate)
def run_simulation(calculator, returns):
money = 100000
# yearly = 10000
yearly = 0
total_taxes = 0
total_contributions = 0
for i in returns:
returns = money * i
taxes = calculator(money, money + returns)
money = money + returns - taxes + yearly
total_taxes += taxes
total_contributions += yearly
return (total_contributions, total_taxes, money)
historical_results = [
7.77,
-19.44,
26.89,
16.26,
28.88,
-6.24,
19.42,
9.54,
-0.73,
11.39,
29.60,
13.41,
0.00,
12.78,
23.45,
-38.49,
3.53,
13.62,
3.00,
8.99,
26.38,
-23.37,
-13.04,
-10.14,
19.53,
26.67,
31.01,
20.26,
34.11,
-1.54,
7.06,
4.46,
26.31,
-6.56,
27.25,
12.40,
2.03,
14.62,
26.33,
1.40,
17.27,
14.76,
-9.73,
25.77,
12.31,
1.06,
-11.50,
19.15,
31.55,
-29.72,
-17.37,
15.63,
10.79,
0.10,
-11.36,
7.66,
20.09,
-13.09,
9.06,
12.97,
18.89,
-11.81,
23.13,
-2.97,
8.48,
38.06,
-14.31,
2.62,
26.40,
45.02,
-6.62,
11.78,
16.46,
21.78,
10.26,
-0.65,
0.00,
-11.87,
30.72,
13.80,
19.45,
12.43,
-17.86,
-15.29,
-5.45,
25.21,
-38.59,
27.92,
41.37,
-5.94,
46.59,
-15.15,
-47.07,
-28.48,
-11.91,
37.88,
]
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment