Sofi1606 commited on
Commit
65131e2
1 Parent(s): 1a5ebb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -29
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('App de prueba')
7
 
8
- A = st.number_input("Escribe la cantidad que ahorrarás ($): ")
9
- T = st.number_input("Escribe cuantos meses quieres ver: ", step=1)
 
 
 
 
 
 
 
 
10
 
11
- tasas = []
12
- for i in range(int(T)):
13
- tasa = st.number_input(f"Tasa de interés nominal para el mes {i+1} (%): ")
14
- tasas.append(tasa)
15
 
16
- if T > 0:
17
- z = np.arange(T)
18
 
19
- r_d = A * (1 + np.array(tasas) / (12 * 100)) ** z
20
- r_c = A * np.exp(np.array(tasas) * z / (12 * 100))
21
 
22
- new_dict = {"Mes": np.arange(1, T+1),
23
- "Retorno discreto": r_d,
24
- "Retorno continuo": r_c}
25
 
26
- df = pd.DataFrame(new_dict)
27
- st.dataframe(df)
 
 
 
28
 
29
- st.write(f"En {int(T)} meses tendrás: $ {r_c[-1]}")
 
 
30
 
31
- # Creación de la gráfica
32
- plt.figure(figsize=(10, 6))
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
- else:
43
- st.write("Por favor, ingresa un valor de T mayor que 0.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)