File size: 1,230 Bytes
f1cb20e
2d285b3
 
 
f1cb20e
2d285b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d651670
2d285b3
 
 
 
 
 
d651670
2d285b3
 
 
 
d651670
2d285b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)