Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +58 -39
src/streamlit_app.py
CHANGED
@@ -1,40 +1,59 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
|
5 |
+
# Title
|
6 |
+
st.title("Financial Modeling App")
|
7 |
+
|
8 |
+
# Tab setup
|
9 |
+
tabs = st.tabs(["Time Value of Money", "Consistent Cash Flow Investment", "Monte Carlo Simulation"])
|
10 |
+
|
11 |
+
# --- Time Value of Money ---
|
12 |
+
with tabs[0]:
|
13 |
+
st.header("Time Value of Money")
|
14 |
+
a = st.number_input("Base Amount (a)", min_value=0.0, value=1000.0)
|
15 |
+
r = st.number_input("Annual Return Rate (r)", min_value=0.0, value=0.05)
|
16 |
+
T = st.number_input("Number of Years (T)", min_value=0, value=10)
|
17 |
+
|
18 |
+
future_value = a * ((1 + r) ** T)
|
19 |
+
st.write(f"Future Value = {a} * (1 + {r})^{T} = **{future_value:,.2f}**")
|
20 |
+
|
21 |
+
# --- Consistent Cash Flow Investment ---
|
22 |
+
with tabs[1]:
|
23 |
+
st.header("Consistent Cash Flow Investment")
|
24 |
+
a_cf = st.number_input("Annual Cash Flow (a)", min_value=0.0, value=1000.0)
|
25 |
+
r_cf = st.number_input("Annual Return Rate (r)", min_value=0.0, value=0.05)
|
26 |
+
T_cf = st.number_input("Number of Years (T)", min_value=0, value=10)
|
27 |
+
|
28 |
+
if r_cf > 0:
|
29 |
+
fv_cf = a_cf * (((1 + r_cf) ** T_cf - 1) / r_cf)
|
30 |
+
else:
|
31 |
+
fv_cf = a_cf * T_cf
|
32 |
+
|
33 |
+
st.write(f"Future Value of Cash Flows = **{fv_cf:,.2f}**")
|
34 |
+
|
35 |
+
# --- Monte Carlo Simulation ---
|
36 |
+
with tabs[2]:
|
37 |
+
st.header("Monte Carlo Simulation")
|
38 |
+
mean_return = st.number_input("Mean Annual Return", value=0.07)
|
39 |
+
std_dev = st.number_input("Standard Deviation", value=0.15)
|
40 |
+
years = st.slider("Number of Years", 1, 100, 30)
|
41 |
+
simulations = 50
|
42 |
+
|
43 |
+
# Simulate paths
|
44 |
+
np.random.seed(42)
|
45 |
+
results = np.zeros((simulations, years))
|
46 |
+
for i in range(simulations):
|
47 |
+
returns = np.random.normal(loc=mean_return, scale=std_dev, size=years)
|
48 |
+
results[i] = np.cumprod(1 + returns)
|
49 |
+
|
50 |
+
# Plot
|
51 |
+
fig = go.Figure()
|
52 |
+
for i in range(simulations):
|
53 |
+
fig.add_trace(go.Scatter(y=results[i], mode='lines', name=f'Sim {i+1}', line=dict(width=1)))
|
54 |
+
fig.update_layout(title="Monte Carlo Simulations of Portfolio Growth",
|
55 |
+
xaxis_title="Years",
|
56 |
+
yaxis_title="Portfolio Value (normalized)",
|
57 |
+
showlegend=False)
|
58 |
+
|
59 |
+
st.plotly_chart(fig)
|