|
import streamlit as st |
|
import numpy as np |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
|
|
st.title("Investment app 💼") |
|
|
|
A = st.number_input( |
|
"Insert the initial investment (in $): " |
|
) |
|
r = st.number_input( |
|
"Insert the interest rate (nominal in %): " |
|
) |
|
T = st.number_input( |
|
"Insert the number of months of your investment: ", |
|
step=1 |
|
) |
|
|
|
|
|
t = np.arange(T + 1) |
|
|
|
|
|
r_list = np.array([r] * len(t)) |
|
|
|
|
|
y = A * (1 + (r_list / (12*100)))**t |
|
|
|
|
|
y_c = A * np.exp(r_list * t / (12*100)) |
|
|
|
|
|
inv_dict = {"Month": t, |
|
"Rate": r_list, |
|
"Returns": y, |
|
"Continuous returns": y_c} |
|
|
|
|
|
inv_df = pd.DataFrame(inv_dict) |
|
inv_df.columns = inv_dict.keys() |
|
|
|
st.write(f"Table with returns at an interest rate of {r}") |
|
new_df = st.data_editor(inv_df) |
|
|
|
new_y = [A] |
|
new_y_c = [A] |
|
|
|
for i in range(1, len(t)): |
|
r_temp = new_df["Rate"].values[i] |
|
|
|
val = new_y[i-1] * (1 + r_temp / (12*100)) |
|
new_y.append(val) |
|
|
|
|
|
val_c = new_y_c[i-1] * np.exp(r_temp / (12*100)) |
|
new_y_c.append(val_c) |
|
|
|
new_df["Returns"] = new_y |
|
new_df["Continuous returns"] = new_y_c |
|
new_df["Actual returns"] = 100 * (new_df["Continuous returns"] - new_df["Continuous returns"].shift(1)) / new_df["Continuous returns"].shift(1) |
|
|
|
st.dataframe(new_df) |
|
|
|
fig, ax = plt.subplots() |
|
ax.set_title("Investment returns plot") |
|
ax.set_xlabel("Months") |
|
ax.set_ylabel("Actual returns") |
|
ax.plot(new_df["Month"], new_df["Actual returns"]) |
|
|
|
st.pyplot(fig) |
|
|