Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,56 @@ It simulates various market scenarios to analyze the dependencies between differ
|
|
| 18 |
Users can assess potential outcomes in terms of best, worst, and mean case scenarios.
|
| 19 |
""")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
st.sidebar.title("Input Parameters")
|
| 22 |
|
| 23 |
# Sidebar: How to Use (closed by default)
|
|
@@ -62,58 +112,7 @@ run_button = st.sidebar.button("Run Analysis")
|
|
| 62 |
if run_button and len(portfolio_tickers) == len(portfolio_weights):
|
| 63 |
|
| 64 |
# Explanation of the Simulation and Copula Models
|
| 65 |
-
st.markdown("## Simulating Market Drop Scenarios: Best, Worst, and Mean Cases")
|
| 66 |
-
st.markdown("This section explores how the portfolio performs under simulated market drop scenarios using copula models.")
|
| 67 |
-
|
| 68 |
-
with st.expander("Methodology", expanded=False):
|
| 69 |
-
st.markdown("## Simulating Market Drop Scenarios: Best, Worst, and Mean Cases")
|
| 70 |
-
#st.markdown("## Transforming Returns")
|
| 71 |
-
st.markdown("""
|
| 72 |
-
To prepare the returns for copula modeling, we transform them to a uniform distribution using quantile transformation.
|
| 73 |
-
This is to ensure that the data fits within the range required by the copula model.
|
| 74 |
-
""")
|
| 75 |
-
st.latex(r"""
|
| 76 |
-
u_i = \frac{\text{rank}(R_i)}{n + 1}
|
| 77 |
-
""")
|
| 78 |
-
|
| 79 |
-
#st.markdown("## Fitting the Copula Model")
|
| 80 |
-
st.markdown("""
|
| 81 |
-
Next, we fit a multivariate Student-t copula to the transformed data. The copula model captures the dependencies between the stock returns to understand the joint behavior of the assets.
|
| 82 |
-
""")
|
| 83 |
-
st.latex(r"""
|
| 84 |
-
C_{\nu}(u_1, u_2, \ldots, u_d) = t_{\nu, \Sigma}(t_{\nu}^{-1}(u_1), t_{\nu}^{-1}(u_2), \ldots, t_{\nu}^{-1}(u_d))
|
| 85 |
-
""")
|
| 86 |
-
|
| 87 |
-
#st.markdown("## Simulating Scenarios")
|
| 88 |
-
st.markdown("""
|
| 89 |
-
Using the fitted copula model, we simulate a large number of return scenarios. This simulation helps us explore potential future outcomes
|
| 90 |
-
and assess the portfolio's risk under various market conditions.
|
| 91 |
-
""")
|
| 92 |
-
|
| 93 |
-
#st.markdown("## Market Drop Scenarios")
|
| 94 |
-
st.markdown("""
|
| 95 |
-
We identify scenarios where the specified ticker drops by the given percentage. By analyzing these scenarios, we can evaluate how the portfolio performs under stress.
|
| 96 |
-
""")
|
| 97 |
-
|
| 98 |
-
#st.markdown("## Portfolio Returns")
|
| 99 |
-
st.markdown("""
|
| 100 |
-
For each identified scenario, we calculate the portfolio returns. The portfolio return is a weighted sum of the individual stock returns.
|
| 101 |
-
""")
|
| 102 |
-
st.latex(r"""
|
| 103 |
-
R_p = \sum_{i=1}^{n} w_i R_i
|
| 104 |
-
""")
|
| 105 |
-
|
| 106 |
-
#st.markdown("## Visualizing Results")
|
| 107 |
-
st.markdown("""
|
| 108 |
-
We visualize the simulated portfolio returns using histograms, cumulative distribution functions, and kernel density estimates.
|
| 109 |
-
These visualizations help us understand the distribution and characteristics of potential returns.
|
| 110 |
-
""")
|
| 111 |
-
|
| 112 |
-
#st.markdown("## Portfolio Price Trajectory")
|
| 113 |
-
st.markdown("""
|
| 114 |
-
Finally, we visualize the portfolio's price trajectory under the worst-case, best-case, and mean scenarios.
|
| 115 |
-
This helps in understanding the potential impact of market drops on the portfolio value.
|
| 116 |
-
""")
|
| 117 |
|
| 118 |
# Define the portfolio and allocation
|
| 119 |
portfolio_allocation = dict(zip(portfolio_tickers, portfolio_weights))
|
|
|
|
| 18 |
Users can assess potential outcomes in terms of best, worst, and mean case scenarios.
|
| 19 |
""")
|
| 20 |
|
| 21 |
+
with st.expander("Methodology", expanded=False):
|
| 22 |
+
st.markdown("## Simulating Market Drop Scenarios: Best, Worst, and Mean Cases")
|
| 23 |
+
#st.markdown("## Transforming Returns")
|
| 24 |
+
st.markdown("""
|
| 25 |
+
To prepare the returns for copula modeling, we transform them to a uniform distribution using quantile transformation.
|
| 26 |
+
This is to ensure that the data fits within the range required by the copula model.
|
| 27 |
+
""")
|
| 28 |
+
st.latex(r"""
|
| 29 |
+
u_i = \frac{\text{rank}(R_i)}{n + 1}
|
| 30 |
+
""")
|
| 31 |
+
|
| 32 |
+
#st.markdown("## Fitting the Copula Model")
|
| 33 |
+
st.markdown("""
|
| 34 |
+
Next, we fit a multivariate Student-t copula to the transformed data. The copula model captures the dependencies between the stock returns to understand the joint behavior of the assets.
|
| 35 |
+
""")
|
| 36 |
+
st.latex(r"""
|
| 37 |
+
C_{\nu}(u_1, u_2, \ldots, u_d) = t_{\nu, \Sigma}(t_{\nu}^{-1}(u_1), t_{\nu}^{-1}(u_2), \ldots, t_{\nu}^{-1}(u_d))
|
| 38 |
+
""")
|
| 39 |
+
|
| 40 |
+
#st.markdown("## Simulating Scenarios")
|
| 41 |
+
st.markdown("""
|
| 42 |
+
Using the fitted copula model, we simulate a large number of return scenarios. This simulation helps us explore potential future outcomes
|
| 43 |
+
and assess the portfolio's risk under various market conditions.
|
| 44 |
+
""")
|
| 45 |
+
|
| 46 |
+
#st.markdown("## Market Drop Scenarios")
|
| 47 |
+
st.markdown("""
|
| 48 |
+
We identify scenarios where the specified ticker drops by the given percentage. By analyzing these scenarios, we can evaluate how the portfolio performs under stress.
|
| 49 |
+
""")
|
| 50 |
+
|
| 51 |
+
#st.markdown("## Portfolio Returns")
|
| 52 |
+
st.markdown("""
|
| 53 |
+
For each identified scenario, we calculate the portfolio returns. The portfolio return is a weighted sum of the individual stock returns.
|
| 54 |
+
""")
|
| 55 |
+
st.latex(r"""
|
| 56 |
+
R_p = \sum_{i=1}^{n} w_i R_i
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
#st.markdown("## Visualizing Results")
|
| 60 |
+
st.markdown("""
|
| 61 |
+
We visualize the simulated portfolio returns using histograms, cumulative distribution functions, and kernel density estimates.
|
| 62 |
+
These visualizations help us understand the distribution and characteristics of potential returns.
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
#st.markdown("## Portfolio Price Trajectory")
|
| 66 |
+
st.markdown("""
|
| 67 |
+
Finally, we visualize the portfolio's price trajectory under the worst-case, best-case, and mean scenarios.
|
| 68 |
+
This helps in understanding the potential impact of market drops on the portfolio value.
|
| 69 |
+
""")
|
| 70 |
+
|
| 71 |
st.sidebar.title("Input Parameters")
|
| 72 |
|
| 73 |
# Sidebar: How to Use (closed by default)
|
|
|
|
| 112 |
if run_button and len(portfolio_tickers) == len(portfolio_weights):
|
| 113 |
|
| 114 |
# Explanation of the Simulation and Copula Models
|
| 115 |
+
#st.markdown("## Simulating Market Drop Scenarios: Best, Worst, and Mean Cases")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Define the portfolio and allocation
|
| 118 |
portfolio_allocation = dict(zip(portfolio_tickers, portfolio_weights))
|