Update app.py
Browse files
app.py
CHANGED
@@ -3,41 +3,67 @@ import numpy as np
|
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
5 |
|
6 |
-
st.title(
|
7 |
|
8 |
-
A = st.number_input(
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
tasa = st.number_input(f"Tasa de interés nominal para el mes {i+1} (%): ")
|
14 |
-
tasas.append(tasa)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
"Retorno continuo": r_c}
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
plt.plot(np.arange(1, T+1), r_c, label='Retorno continuo', marker='o', linestyle='-', color='blue', alpha=0.8)
|
34 |
-
plt.xlabel('Mes')
|
35 |
-
plt.ylabel('Inversión ($)')
|
36 |
-
plt.title('Crecimiento de la inversión a lo largo de los meses', fontsize=16, fontweight='bold')
|
37 |
-
plt.grid(True, linestyle='--', alpha=0.5)
|
38 |
-
plt.legend()
|
39 |
-
plt.tight_layout()
|
40 |
-
st.pyplot(plt)
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
5 |
|
6 |
+
st.title("Investment app 💼")
|
7 |
|
8 |
+
A = st.number_input(
|
9 |
+
"Insert the initial investment (in $): "
|
10 |
+
)
|
11 |
+
r = st.number_input(
|
12 |
+
"Insert the interest rate (nominal in %): "
|
13 |
+
)
|
14 |
+
T = st.number_input(
|
15 |
+
"Insert the number of months of your investment: ",
|
16 |
+
step=1
|
17 |
+
)
|
18 |
|
19 |
+
# Numpy array with the months
|
20 |
+
t = np.arange(T + 1)
|
|
|
|
|
21 |
|
22 |
+
# Numpy array for the interest rate
|
23 |
+
r_list = np.array([r] * len(t))
|
24 |
|
25 |
+
# Numpy array with the returns for each month in t
|
26 |
+
y = A * (1 + (r_list / (12*100)))**t
|
27 |
|
28 |
+
# Numpy array with the continuously compunded returns
|
29 |
+
y_c = A * np.exp(r_list * t / (12*100))
|
|
|
30 |
|
31 |
+
# Create the investment dictionary
|
32 |
+
inv_dict = {"Month": t,
|
33 |
+
"Rate": r_list,
|
34 |
+
"Returns": y,
|
35 |
+
"Continuous returns": y_c}
|
36 |
|
37 |
+
# Create the investment dataframe
|
38 |
+
inv_df = pd.DataFrame(inv_dict)
|
39 |
+
inv_df.columns = inv_dict.keys()
|
40 |
|
41 |
+
st.write(f"Table with returns at an interest rate of {r}")
|
42 |
+
new_df = st.data_editor(inv_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
new_y = [A]
|
45 |
+
new_y_c = [A]
|
46 |
+
|
47 |
+
for i in range(1, len(t)):
|
48 |
+
r_temp = new_df["Rate"].values[i]
|
49 |
+
# New numpy array with the returns for each month in t
|
50 |
+
val = new_y[i-1] * (1 + r_temp / (12*100))
|
51 |
+
new_y.append(val)
|
52 |
+
|
53 |
+
# New numpy array with the continuously compunded returns
|
54 |
+
val_c = new_y_c[i-1] * np.exp(r_temp / (12*100))
|
55 |
+
new_y_c.append(val_c)
|
56 |
+
|
57 |
+
new_df["Returns"] = new_y
|
58 |
+
new_df["Continuous returns"] = new_y_c
|
59 |
+
new_df["Actual returns"] = 100 * (new_df["Continuous returns"] - new_df["Continuous returns"].shift(1)) / new_df["Continuous returns"].shift(1)
|
60 |
+
|
61 |
+
st.dataframe(new_df)
|
62 |
+
|
63 |
+
fig, ax = plt.subplots()
|
64 |
+
ax.set_title("Investment returns plot")
|
65 |
+
ax.set_xlabel("Months")
|
66 |
+
ax.set_ylabel("Actual returns")
|
67 |
+
ax.plot(new_df["Month"], new_df["Actual returns"])
|
68 |
+
|
69 |
+
st.pyplot(fig)
|