File size: 1,158 Bytes
fa63f53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Streamlit app layout
st.title("Insurance Payment with Savings 💼")

# Create the inputs (A, r, n)
monthly_payment = st.number_input("Desired monthly payment:")
interest_rate = st.number_input("Interest rate (%):")
months = int(st.number_input("Number of months:", step=1))

# Calculate the present value
present_value = (monthly_payment * 12 / interest_rate) * (1 - (1 / (1 + interest_rate/12)**months))

# Display the present value
st.write(f"The present value is {present_value:,.2f} using the discrete model")

# Cashflow DataFrame
cashflows = [-present_value] + [monthly_payment] * months
cf_df = pd.DataFrame({"cashflows": cashflows}).astype(float)
st.dataframe(cf_df.T)

# Plot the Cashflow Diagram
colors = ["green" if cf > 0 else "red" for cf in cf_df.cashflows]
plt.scatter(range(months + 1), cf_df.cashflows, c=colors)
plt.title("Cashflow Diagram")
plt.xlabel("Period")
plt.ylabel("Cashflow")
for i, cf in enumerate(cf_df.cashflows):
    plt.annotate(f"{cf:,.2f}", (i, cf), textcoords="offset points", xytext=(0, months), ha="center")
st.pyplot(plt)