import streamlit as st import numpy as np import pandas as pd import matplotlib.pyplot as plt # Streamlit app layout st.title("Paying insurance with my savings account 💼") # Create the inputs (A, r, n) A = st.number_input("How much do you want monthly?") r = st.number_input("What is the interest rate?") n = int(st.number_input("For how many months?", step = 1)) # Create the outputs Pn_d = (A * 12 / r) * (1 - (1 / (1 + r/12)**n)) # Pn_c = (A / (np.exp(r/12) - 1)) * (1 - np.exp(- r * n / 12)) # Print the outputs st.write(f"The value of P is {Pn_d:,.2f} with discrete model") # st.write(f"The value of P is {Pn_c:,.2f} with continuous model") # Draw the plot cf_list = [-Pn_d] + [A] * n cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(float) cf_df = st.dataframe(cf_dict.T) # Prepare data for the plot colors = ["green" if cf > 0 else "red" for cf in cf_dict.cashflows] plt.scatter(range(n + 1), cf_dict.cashflows, c=colors) plt.title("Cashflow Diagram") plt.xlabel("Period") plt.ylabel("Cashflow") for i, cf in enumerate(cf_dict.cashflows): plt.annotate( f"{cf:,.2f}", (i, cf), textcoords="offset points", xytext=(0, n), ha="center", ) st.pyplot(plt)