Spaces:
Sleeping
Sleeping
File size: 3,651 Bytes
459d15e cbdae8d 459d15e 7706f4d 459d15e cbdae8d 459d15e cbdae8d 459d15e cbdae8d 459d15e 7706f4d 459d15e cbdae8d 459d15e cbdae8d 459d15e cbdae8d 459d15e 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e cbdae8d 07b710e 459d15e | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from data.schema import Out
def metrics_dataframe(metrics):
df = pd.DataFrame(metrics)
return df
def plot_loss_distribution(
metrics,
):
df = pd.DataFrame(metrics)
df[Out.CONFIDENCE] = (
df[Out.CONFIDENCE] * 100
)
fig = px.bar(
df,
x=Out.CONFIDENCE,
y=Out.VAR,
title="VaR by Confidence Level",
text=Out.VAR,
)
fig.update_layout(
template="plotly_white",
xaxis_title="Confidence Level (%)",
yaxis_title="VaR",
)
return fig
def plot_var_es(
metrics,
):
df = pd.DataFrame(metrics)
df[Out.CONFIDENCE] = (
df[Out.CONFIDENCE] * 100
)
fig = go.Figure()
fig.add_trace(
go.Bar(
x=df[Out.CONFIDENCE],
y=df[Out.VAR],
name=Out.VAR,
)
)
fig.add_trace(
go.Bar(
x=df[Out.CONFIDENCE],
y=df[Out.ES],
name=Out.ES,
)
)
fig.update_layout(
title="VaR vs Expected Shortfall",
xaxis_title="Confidence Level (%)",
yaxis_title="Loss",
barmode="group",
template="plotly_white",
)
return fig
def plot_component_var(component_metrics, level="Item"):
"""
Bar chart of Component VaR by item (plant, factor, ...).
"""
df = pd.DataFrame(component_metrics)
if df.empty:
raise ValueError(
"No Component VaR results to plot."
)
confidence = df[Out.CONFIDENCE].iloc[0] * 100
df = df.sort_values(
Out.COMPONENT_VAR,
ascending=True,
)
fig = px.bar(
df,
x=Out.COMPONENT_VAR,
y=Out.ITEM,
orientation="h",
text=Out.COMPONENT_VAR,
title=(
f"Component VaR by {level} "
f"({confidence:.1f}% confidence)"
),
hover_data=[
Out.STANDALONE_VAR,
Out.CONTRIBUTION,
Out.DIVERSIFICATION,
Out.COMPONENT_ES,
],
)
fig.add_vline(
x=0,
line_color="black",
line_width=1,
)
fig.update_layout(
template="plotly_white",
xaxis_title="Component VaR",
yaxis_title=level,
)
return fig
def plot_component_var_contribution(
component_metrics,
level="Item",
):
"""
Bar chart of percentage contribution to portfolio VaR.
Negative contributions can indicate hedging effects.
"""
df = pd.DataFrame(component_metrics)
if df.empty:
raise ValueError(
"No Component VaR results to plot."
)
confidence = df[Out.CONFIDENCE].iloc[0] * 100
df = df.sort_values(
Out.CONTRIBUTION,
ascending=True,
)
fig = px.bar(
df,
x=Out.CONTRIBUTION,
y=Out.ITEM,
orientation="h",
text=Out.CONTRIBUTION,
title=(
f"VaR Contribution by {level} "
f"({confidence:.1f}% confidence)"
),
hover_data=[
Out.COMPONENT_VAR,
Out.PORTFOLIO_VAR,
Out.STANDALONE_VAR,
],
)
fig.add_vline(
x=0,
line_color="black",
line_width=1,
)
fig.update_layout(
template="plotly_white",
xaxis_title="Contribution to Portfolio VaR (%)",
yaxis_title=level,
)
return fig |