Download visualization/plots.py from MinsaitEU/VaR: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/spaces/MinsaitEU/VaR/resolve/main/visualization/plots.py
- Command line
-
hf download hf://spaces/MinsaitEU/VaR/visualization/plots.py
-
curl -L -o plots.py https://huggingface.co/spaces/MinsaitEU/VaR/resolve/main/visualization/plots.py
2.58 kB
| import plotly.graph_objects as go | |
| def plot_prices(prices): | |
| """ | |
| Plot historical asset prices. | |
| """ | |
| fig = go.Figure() | |
| for column in prices.columns: | |
| fig.add_trace( | |
| go.Scatter( | |
| x=prices.index, | |
| y=prices[column], | |
| mode="lines", | |
| name=column, | |
| ) | |
| ) | |
| fig.update_layout( | |
| title="Historical Prices", | |
| xaxis_title="Date", | |
| yaxis_title="Price", | |
| hovermode="x unified", | |
| template="plotly_white", | |
| ) | |
| return fig | |
| def plot_portfolio_returns(returns): | |
| """ | |
| Plot portfolio daily returns. | |
| """ | |
| fig = go.Figure() | |
| fig.add_trace( | |
| go.Scatter( | |
| x=returns.index, | |
| y=returns, | |
| mode="lines", | |
| name="Portfolio", | |
| ) | |
| ) | |
| fig.add_hline( | |
| y=0, | |
| line_dash="dot", | |
| ) | |
| fig.update_layout( | |
| title="Portfolio Returns", | |
| xaxis_title="Date", | |
| yaxis_title="Return", | |
| hovermode="x unified", | |
| template="plotly_white", | |
| ) | |
| return fig | |
| def plot_return_distribution( | |
| returns, | |
| var=None, | |
| es=None, | |
| ): | |
| """ | |
| Plot portfolio return distribution | |
| with VaR and ES thresholds. | |
| """ | |
| fig = go.Figure() | |
| fig.add_trace( | |
| go.Histogram( | |
| x=returns, | |
| nbinsx=80, | |
| name="Returns", | |
| ) | |
| ) | |
| if var is not None: | |
| fig.add_vline( | |
| x=-var, | |
| line_dash="dash", | |
| annotation_text=( | |
| f"VaR: {var:.2%}" | |
| ), | |
| ) | |
| if es is not None: | |
| fig.add_vline( | |
| x=-es, | |
| line_dash="dot", | |
| annotation_text=( | |
| f"ES: {es:.2%}" | |
| ), | |
| ) | |
| fig.update_layout( | |
| title="Portfolio Return Distribution", | |
| xaxis_title="Return", | |
| yaxis_title="Frequency", | |
| template="plotly_white", | |
| ) | |
| return fig | |
| def plot_var_comparison(results): | |
| """ | |
| Compare VaR estimates from different methods. | |
| """ | |
| fig = go.Figure() | |
| methods = list(results.keys()) | |
| values = list(results.values()) | |
| fig.add_trace( | |
| go.Bar( | |
| x=methods, | |
| y=values, | |
| text=[ | |
| f"{value:.2%}" | |
| for value in values | |
| ], | |
| textposition="auto", | |
| ) | |
| ) | |
| fig.update_layout( | |
| title="VaR Comparison", | |
| xaxis_title="Method", | |
| yaxis_title="VaR", | |
| template="plotly_white", | |
| ) | |
| return fig |