Marlene13/Demo1
0
1import streamlit as st2import numpy as np3import pandas as pd4import matplotlib.pyplot as plt5 6 7# Streamlit app layout8 9st.title("Paying insurance with my savings account 💼")10 11# Create the inputs (A, r, n)12 13A = st.number_input("How much do you want monthly?")14 15r = st.number_input("What is the interest rate?")16 17n = int(st.number_input("For how many months?", step = 1))18 19 20# Create the outputs21 22Pn_d = (A * 12 / r) * (1 - (1 / (1 + r/12)**n))23 24# Pn_c = (A / (np.exp(r/12) - 1)) * (1 - np.exp(- r * n / 12))25 26 27# Print the outputs28 29st.write(f"The value of P is {Pn_d:,.2f} with discrete model")30 31# st.write(f"The value of P is {Pn_c:,.2f} with continuous model")32 33 34# Draw the plot35 36cf_list = [-Pn_d] + [A] * n37cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(float)38cf_df = st.dataframe(cf_dict.T)39 40# Prepare data for the plot41colors = ["green" if cf > 0 else "red" for cf in cf_dict.cashflows]42plt.scatter(range(n + 1), cf_dict.cashflows, c=colors)43plt.title("Cashflow Diagram")44plt.xlabel("Period")45plt.ylabel("Cashflow")46for i, cf in enumerate(cf_dict.cashflows):47 plt.annotate(48 f"{cf:,.2f}",49 (i, cf),50 textcoords="offset points",51 xytext=(0, n),52 ha="center",53 )54st.pyplot(plt)55 56 