File size: 8,070 Bytes
b099c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# %%
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("metrics/best_models.csv")

# global win rate
print(df["best_model"].value_counts(normalize=True).round(2))


# %%
full = pd.read_csv("data/processed/train.csv")

vol = full.groupby("id")["sales"].agg(["mean","std"]).reset_index()
vol["cv"] = vol["std"] / (vol["mean"] + 1e-9)

best = df.merge(vol[["id","cv"]], on="id")

best["cv_bin"] = pd.qcut(best["cv"], 3, labels=["Low","Mid","High"])
print(best.groupby(["cv_bin","best_model"]).size())

# %%
df = pd.read_csv("data/processed/train.csv")

g = df.groupby("id")["sales"]
summary = g.agg(["mean","std","count"])
summary = summary.rename(columns={"count":"T"})

summary["N"] = g.apply(lambda x: (x>0).sum())
summary["ADI"] = summary["T"] / summary["N"].replace(0,1)
summary["CV2"] = (summary["std"]/summary["mean"].replace(0,1))**2

summary.to_csv("metrics/demand_profile.csv")


# %%
summary["ADI_class"] = np.where(summary["ADI"] > 1.32, "High", "Low")
summary["CV2_class"] = np.where(summary["CV2"] > 0.49, "High", "Low")

summary["regime"] = summary["ADI_class"] + "-" + summary["CV2_class"]

# %%
best = pd.read_csv("metrics/best_models.csv")
merged = best.merge(summary[["ADI","CV2","regime"]], on="id", how="left")

# %%
merged.groupby("regime")["best_model"].value_counts(normalize=True).to_csv("metrics/regime_model_performance.csv")

# %%
merged.groupby('best_model').size()

# %%
merged.groupby('regime')['best_model'].value_counts()

# %%
merged.to_csv('metrics/best_by_sku.csv')

# %% [markdown]
# ##  Key Insight


# %%
print(best["best_model"].value_counts(normalize=True).round(2))

# %%
m = pd.read_csv("metrics/combined_metrics.csv")
print(m.groupby("model")["score"].mean().sort_values())

# %%
best[['id','best_model']]

import matplotlib.pyplot as plt
import numpy as np

# model_scores: Series indexed by model name, values = mean score
model_scores = m.groupby("model")["score"].mean().sort_values()

fig, ax = plt.subplots(figsize=(16, 10))

# -------------------------------
# Identify winner + tier bounds
# -------------------------------
best_model = model_scores.index[0]
best_value = model_scores.iloc[0]

# Tier boundaries (keep tunable)
tierB_upper = 70   # top band of "stable enough"
tierC_upper = 80   # start of "avoid if possible"

colors = []
for model, score in model_scores.items():
    if model == best_model:
        colors.append("#0047AB")        # Deep Royal Blue β†’ portfolio winner
    elif score < tierB_upper:
        colors.append("#888888")        # Neutral Grey β†’ stable / acceptable
    else:
        colors.append("#C43131")        # Executive Red β†’ high-noise tier

bars = ax.bar(model_scores.index, model_scores.values, color=colors)

# -------------------------------
# Threshold reference lines
# -------------------------------
ax.axhline(tierB_upper, color="#666666", linestyle="--", linewidth=1)
ax.text(
    len(model_scores) - 0.3,
    tierB_upper + 0.8,
    "Stable Signal Threshold",
    color="#444444",
    fontsize=10,
    ha="right"
)

ax.axhline(tierC_upper, color="#444444", linestyle=":", linewidth=1)
ax.text(
    len(model_scores) - 0.3,
    tierC_upper + 0.8,
    "High-Noise Zone (Avoid for planning)",
    color="#444444",
    fontsize=10,
    ha="right"
)

# -------------------------------
# Value annotations
# -------------------------------
for bar, value in zip(bars, model_scores.values):
    ax.text(
        bar.get_x() + bar.get_width() / 2,
        value + 0.8,
        f"{value:.2f}",
        ha="center",
        fontsize=9,
        fontweight="bold" if value == best_value else "normal"
    )

# -------------------------------
# Styling
# -------------------------------
ax.set_title(
    "Portfolio-Level Model Performance\n(Lower Score = More Stable, Lower Error Risk)",
    fontsize=18,
    fontweight="bold",
    pad=16,
)

ax.set_ylabel("Mean Score (MAE + |Bias|)", fontsize=12)
ax.set_xlabel("Forecasting Models", fontsize=12)

# Fix tick warning: set ticks explicitly, then labels
x_positions = np.arange(len(model_scores))
ax.set_xticks(x_positions)
ax.set_xticklabels(model_scores.index, rotation=45, ha="right")

# Clean look
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.grid(axis="y", alpha=0.25)

plt.tight_layout()
plt.savefig("docs/model_score_ranking.png", dpi=300, bbox_inches="tight")
plt.show()











# Tier boundaries (tunable later)
tierB_upper = 70
tierC_upper = 80

# -------------------------------------------------
# PREP
# -------------------------------------------------
model_scores = m.groupby("model")["score"].mean().sort_values()

best_model = model_scores.index[0]
best_value = model_scores.iloc[0]

# Executive tiers


# Executive-friendly group labels
stable_models = model_scores[model_scores < tierB_upper].index
secondary_models = model_scores[(model_scores >= tierB_upper) & (model_scores < tierC_upper)].index
high_noise_models = model_scores[model_scores >= tierC_upper].index

def group_color(model):
    if model == best_model:
        return "#0047AB"             # Royal blue highlight
    elif model in stable_models:
        return "#4C8BBF"             # Soft blue-grey (Stable)
    elif model in secondary_models:
        return "#A5A5A5"             # Neutral grey (Secondary)
    else:
        return "#C43131"             # Executive red (High noise)

colors = [group_color(m) for m in model_scores.index]

# -------------------------------------------------
# PLOT
# -------------------------------------------------
fig, ax = plt.subplots(figsize=(14, 8))

bars = ax.bar(model_scores.index, model_scores.values, color=colors)

# -------------------------------------------------
# Threshold lines with simpler executive labels
# -------------------------------------------------
ax.axhline(tierB_upper, color="#666666", linestyle="--", linewidth=1)
ax.text(
    -0.3, tierB_upper + 0.5,
    "Stable Signal Threshold",
    fontsize=10, color="#444444", ha="left"
)

ax.axhline(tierC_upper, color="#888888", linestyle=":", linewidth=1)
ax.text(
    -0.3, tierC_upper + 0.5,
    "High-Noise Zone",
    fontsize=10, color="#444444", ha="left"
)

# -------------------------------------------------
# SIMPLIFIED VALUE LABELS
# -------------------------------------------------
for bar, value in zip(bars, model_scores.values):
    if value < tierB_upper:
        label_color = "#002147"
    elif value < tierC_upper:
        label_color = "#444444"
    else:
        label_color = "#7A0000"

    ax.text(
        bar.get_x() + bar.get_width()/2,
        value + 0.5,
        f"{value:.2f}",
        ha="center",
        fontsize=9,
        color=label_color
    )

# -------------------------------------------------
# EXECUTIVE STYLING
# -------------------------------------------------
ax.set_title(
    "Forecast Model Stability Comparison\n(lower = more stable, lower risk)",
    fontsize=18,
    fontweight="bold",
    pad=20
)

ax.set_ylabel("Stability Score (MAE + |Bias|)", fontsize=12)

# Remove clutter by hiding long model names
ax.set_xticks([])
ax.set_xlabel("Model Families (ranked left β†’ right)", fontsize=12)

# Add grouped legend explanation
group_handles = [
    plt.Rectangle((0,0),1,1, color="#4C8BBF"),
    plt.Rectangle((0,0),1,1, color="#A5A5A5"),
    plt.Rectangle((0,0),1,1, color="#C43131"),
    plt.Rectangle((0,0),1,1, color="#0047AB"),
]

ax.legend(
    group_handles,
    ["Stable Models", "Secondary Models", "High-Noise Models", "Best Performer"],
    frameon=False,
    loc="upper left"
)

# Clean look
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.grid(axis="y", alpha=0.25)

plt.tight_layout()
plt.savefig("docs/model_score_ranking_exec.png", dpi=300, bbox_inches="tight")
plt.show()