-
Notifications
You must be signed in to change notification settings - Fork 3
/
allocation.py
98 lines (69 loc) · 3.2 KB
/
allocation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import numpy as np
balance = {"Liquid": 0, "ST Fixed Income": 0, "LT Fixed Income": 0, "ETF": 0, "Tech": 0, "CurrRetirement": 0}
user = {"Salary": None, "Age": None, "Plan": 'Default'}
deposit = 0
young_default = np.array([0, 10, 20, 70])
mid_age_default = np.array([0, 25, 35, 40])
old_default = np.array([80, 0, 20, 0])
to_interpolate = [young_default, mid_age_default, old_default]
ages = [18,42,65]
high_risk_LT = np.array([0, 10, 20, 70])/100
high_risk_ST = np.array([50, 0, 0, 50])/100
low_risk_LT = np.array([0, 70, 25, 5])/100
low_risk_ST = np.array([80, 0, 20, 0])/100
def interpolate_plan(vectors, positions, target_position):
# Find the two nearest positions to the target position
idx = np.searchsorted(positions, target_position, side="right")
left_position = positions[idx - 1]
right_position = positions[idx]
# Calculate the interpolation weight
weight = (target_position - left_position) / (right_position - left_position)
# Interpolate between the vectors
interpolated_vector = (
(1 - weight) * vectors[idx - 1] + weight * vectors[idx]
)
return interpolated_vector
def allocate(balance, deposit, user):
if balance["Liquid"] < user["Salary"] / 2:
needed = user["Salary"] / 2 - balance["Liquid"]
if deposit < needed:
balance["Liquid"] += deposit
return balance
else:
balance["Liquid"] += needed
deposit -= needed
if balance["CurrRetirement"] < 21500:
if deposit < 21500 - balance["CurrRetirement"]:
balance["CurrRetirement"] += deposit
return balance
else:
deposit -= (21500 - balance["CurrRetirement"])
balance["CurrRetirement"] = 21500
if(user["Plan"] == "Default"):
interpolated_plan = interpolate_plan(to_interpolate, ages, user["Age"]) / 100
balance["ST Fixed Income"] += interpolated_plan[0]*deposit
balance["LT Fixed Income"] += interpolated_plan[1]*deposit
balance["ETF"] += interpolated_plan[2]*deposit
balance["Tech"] += interpolated_plan[3]*deposit
return balance
if(user["Plan"] == "High Risk Long Term"):
balance["ST Fixed Income"] += high_risk_LT[0]*deposit
balance["LT Fixed Income"] += high_risk_LT[1]*deposit
balance["ETF"] += high_risk_LT[2]*deposit
balance["Tech"] += high_risk_LT[3]*deposit
elif(user["Plan"] == "Low Risk Long Term"):
balance["ST Fixed Income"] += low_risk_LT[0]*deposit
balance["LT Fixed Income"] += low_risk_LT[1]*deposit
balance["ETF"] += low_risk_LT[2]*deposit
balance["Tech"] += low_risk_LT[3]*deposit
elif(user["Plan"] == "High Risk Short Term"):
balance["ST Fixed Income"] += high_risk_ST[0]*deposit
balance["LT Fixed Income"] += high_risk_ST[1]*deposit
balance["ETF"] += high_risk_ST[2]*deposit
balance["Tech"] += high_risk_ST[3]*deposit
elif(user["Plan"] == "Low Risk Short Term"):
balance["ST Fixed Income"] += low_risk_ST[0]*deposit
balance["LT Fixed Income"] += low_risk_ST[1]*deposit
balance["ETF"] += low_risk_ST[2]*deposit
balance["Tech"] += low_risk_ST[3]*deposit
return balance