App2.0
Browse files
App2.0
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.slider('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.text_area("Enter Cashflows (comma-separated)", value=', '.join(map(str, cf_list)))
|
24 |
+
|
25 |
+
cashflows = np.array([float(cf.strip()) for cf in cf_df.split(',')])
|
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 |
+
# Plotting cashflow diagram
|
33 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
34 |
+
colors = ['green' if cf > 0 else 'red' for cf in cashflows]
|
35 |
+
ax.plot(range(n+1), cashflows, marker='o', color='b', linestyle='-')
|
36 |
+
ax.fill_between(range(n+1), cashflows, color=colors, alpha=0.3)
|
37 |
+
ax.set_title('Cashflow Diagram')
|
38 |
+
ax.set_xlabel('Period')
|
39 |
+
ax.set_ylabel('Cashflow')
|
40 |
+
for i, cf in enumerate(cashflows):
|
41 |
+
ax.text(i, cf, f'{cf:,.2f}', ha='center', va='bottom')
|
42 |
+
ax.grid(True)
|
43 |
+
st.pyplot(fig)
|