LuisLozano commited on
Commit
2d285b3
1 Parent(s): f1cb20e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -1,3 +1,54 @@
1
  import streamlit as st
 
 
 
2
 
3
- st.write("Hello World")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
 
6
+
7
+ # Streamlit app layout
8
+
9
+ st.title("Paying insurance with my savings account 💼")
10
+
11
+ # Create the inputs (A, r, n)
12
+
13
+ A = st.number_input("How much do you want monthly?")
14
+
15
+ r = st.number_input("What is the interest rate?")
16
+
17
+ n = int(st.number_input("For how many months?", step = 1))
18
+
19
+
20
+ # Create the outputs
21
+
22
+ Pn_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 outputs
28
+
29
+ st.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 plot
35
+
36
+ cf_list = [-Pn_c] + [A] * n
37
+ cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(float)
38
+ cf_df = st.dataframe(cf_dict.T)
39
+
40
+ # Prepare data for the plot
41
+ colors = ["green" if cf > 0 else "red" for cf in cf_dict.cashflows]
42
+ plt.scatter(range(n + 1), cf_dict.cashflows, c=colors)
43
+ plt.title("Cashflow Diagram")
44
+ plt.xlabel("Period")
45
+ plt.ylabel("Cashflow")
46
+ for 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
+ )
54
+ st.pyplot(plt)