File size: 1,279 Bytes
0520c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992545a
0520c2e
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
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Function to compute present value
def calculate_pv(cashflows, rate):
    pv = sum(cf / (1 + rate) ** i for i, cf in enumerate(cashflows))
    return pv

# Streamlit app layout
st.title('Cashflow Present Value Calculator')

# Input for the discount rate
rate = st.number_input('Discount Rate (%)', min_value=0.0, max_value=100.0, value=15.0, step=0.1)/100

# Inputs for the number of years
n = st.number_input("How many years?", min_value = 0, step=1)

# Inputs for the cashflows
cf_list = [0] * (n+1)
cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(int)
cf_df = st.data_editor(cf_dict.T)

cashflows = np.array(cf_df.values[0])

# Button to calculate PV
if st.button('Calculate Present Value'):
    pv = calculate_pv(cashflows, rate)
    st.write(f'The Present Value is: ${pv:,.2f}')
    
    # Prepare data for the plot
    colors = ['green' if cf > 0 else 'red' for cf in cashflows]
    plt.scatter(range(n+1), cashflows, c=colors)
    plt.title('Cashflow Diagram')
    plt.xlabel('Period')
    plt.ylabel('Cashflow')
    for i, cf in enumerate(cashflows):
        plt.annotate(f'{cf:,.2f}', (i, cf), textcoords="offset points", xytext=(0,n), ha='center')
    st.pyplot(plt)