LuisLozano commited on
Commit
0520c2e
1 Parent(s): 2011f4e

Upload app2.py

Browse files
Files changed (1) hide show
  1. app2.py +40 -0
app2.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ # Function to compute present value
7
+ def calculate_pv(cashflows, rate):
8
+ pv = sum(cf / (1 + rate) ** i for i, cf in enumerate(cashflows))
9
+ return pv
10
+
11
+ # Streamlit app layout
12
+ st.title('Cashflow Present Value Calculator')
13
+
14
+ # Input for the discount rate
15
+ rate = st.number_input('Discount Rate (%)', min_value=0.0, max_value=100.0, value=15.0, step=0.1)/100
16
+
17
+ # Inputs for the number of years
18
+ n = st.number_input("How many years?", min_value = 0, step=1)
19
+
20
+ # Inputs for the cashflows
21
+ cf_list = [0] * (n+1)
22
+ cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(int)
23
+ cf_df = st.data_editor(cf_dict.T)
24
+
25
+ cashflows = np.array(cf_df.values[0])
26
+
27
+ # Button to calculate PV
28
+ if st.button('Calculate Present Value'):
29
+ pv = calculate_pv(cashflows, rate)
30
+ st.write(f'The Present Value is: ${pv:,.2f}')
31
+
32
+ # Prepare data for the plot
33
+ colors = ['green' if cf > 0 else 'red' for cf in cashflows]
34
+ plt.scatter(range(n+1), cashflows, c=colors)
35
+ plt.title('Cashflow Diagram')
36
+ plt.xlabel('Period')
37
+ plt.ylabel('Cashflow')
38
+ for i, cf in enumerate(cashflows):
39
+ plt.annotate(f'{cf:,.2f}', (i+1, cf), textcoords="offset points", xytext=(0,n), ha='center')
40
+ st.pyplot(plt)