PD03 commited on
Commit
baaf3da
Β·
verified Β·
1 Parent(s): 7e0ae49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +353 -596
app.py CHANGED
@@ -1,12 +1,11 @@
1
-
2
  import streamlit as st
3
  import numpy as np
4
  import pandas as pd
5
  import plotly.express as px
6
  import plotly.graph_objects as go
7
- from plotly.subplots import make_subplots
8
  import shap
9
  import matplotlib.pyplot as plt
 
10
  from datetime import datetime, timedelta
11
  from sklearn.model_selection import train_test_split
12
  from sklearn.compose import ColumnTransformer
@@ -16,99 +15,74 @@ from sklearn.ensemble import RandomForestRegressor
16
  from sklearn.linear_model import LinearRegression
17
  from sklearn.metrics import r2_score, mean_absolute_error
18
 
19
- # Enhanced page config
20
- st.set_page_config(
21
- page_title="Profitability Intelligence Suite",
22
- page_icon="πŸ“Š",
23
- layout="wide",
24
- initial_sidebar_state="collapsed"
25
- )
26
 
27
- # Custom CSS for premium look
28
  st.markdown("""
29
  <style>
30
  .main-header {
31
- font-size: 2.8rem;
32
  font-weight: 700;
33
  color: #1f77b4;
34
- text-align: center;
35
  margin-bottom: 0.5rem;
36
- text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
37
  }
38
  .sub-header {
39
- font-size: 1.2rem;
40
  color: #666;
41
- text-align: center;
42
  margin-bottom: 2rem;
43
  }
44
- .metric-container {
45
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
46
  padding: 1.5rem;
47
- border-radius: 15px;
48
- box-shadow: 0 8px 16px rgba(0,0,0,0.1);
49
  color: white;
50
- text-align: center;
51
- }
52
- .insight-box {
53
- background: #f8f9fa;
54
- border-left: 5px solid #1f77b4;
55
- padding: 1.5rem;
56
  margin: 1rem 0;
57
- border-radius: 8px;
58
- box-shadow: 0 4px 8px rgba(0,0,0,0.05);
59
  }
60
- .recommendation-card {
61
  background: white;
62
- border: 2px solid #e9ecef;
63
- border-radius: 12px;
64
  padding: 1.5rem;
65
- margin: 1rem 0;
66
- box-shadow: 0 4px 12px rgba(0,0,0,0.08);
67
- transition: transform 0.2s;
68
- }
69
- .recommendation-card:hover {
70
- transform: translateY(-5px);
71
- box-shadow: 0 8px 20px rgba(0,0,0,0.12);
72
- }
73
- .positive-impact {
74
- color: #28a745;
75
- font-weight: 700;
76
- font-size: 1.5rem;
77
- }
78
- .negative-impact {
79
- color: #dc3545;
80
- font-weight: 700;
81
- font-size: 1.5rem;
82
  }
83
- .stTabs [data-baseweb="tab-list"] {
84
- gap: 2rem;
 
 
 
 
85
  }
86
- .stTabs [data-baseweb="tab"] {
87
- height: 3rem;
88
- font-size: 1.1rem;
89
- font-weight: 600;
 
 
90
  }
91
  </style>
92
  """, unsafe_allow_html=True)
93
 
94
  # -----------------------------
95
- # Data Generation (Same as original)
96
  # -----------------------------
97
  @st.cache_data(show_spinner=False)
98
- def generate_synthetic_data(days=60, seed=42, rows_per_day=600):
99
  rng = np.random.default_rng(seed)
100
  start_date = datetime.today().date() - timedelta(days=days)
101
  dates = pd.date_range(start_date, periods=days, freq="D")
 
102
  products = ["Premium Widget", "Standard Widget", "Economy Widget", "Deluxe Widget"]
103
- regions = ["Americas", "EMEA", "Asia Pacific"]
104
  channels = ["Direct Sales", "Distribution Partners", "E-Commerce"]
105
 
106
  base_price = {"Premium Widget": 120, "Standard Widget": 135, "Economy Widget": 110, "Deluxe Widget": 150}
107
- base_cost = {"Premium Widget": 70, "Standard Widget": 88, "Economy Widget": 60, "Deluxe Widget": 95}
108
- region_price_bump = {"Americas": 1.00, "EMEA": 1.03, "Asia Pacific": 0.97}
109
- region_cost_bump = {"Americas": 1.00, "EMEA": 1.02, "Asia Pacific": 1.01}
 
 
110
  channel_discount_mean = {"Direct Sales": 0.06, "Distribution Partners": 0.12, "E-Commerce": 0.04}
111
- channel_discount_std = {"Direct Sales": 0.02, "Distribution Partners": 0.03, "E-Commerce": 0.02}
112
 
113
  seg_epsilon = {}
114
  for p in products:
@@ -129,17 +103,16 @@ def generate_synthetic_data(days=60, seed=42, rows_per_day=600):
129
 
130
  n = rows_per_day
131
  prod = rng.choice(products, size=n, p=[0.35, 0.3, 0.2, 0.15])
132
- reg = rng.choice(regions, size=n, p=[0.4, 0.35, 0.25])
133
- ch = rng.choice(channels, size=n, p=[0.45, 0.35, 0.20])
134
 
135
  base_p = np.array([base_price[x] for x in prod]) * np.array([region_price_bump[x] for x in reg])
136
- base_c = np.array([base_cost[x] for x in prod]) * np.array([region_cost_bump[x] for x in reg])
137
 
138
  discount = np.clip(
139
  np.array([channel_discount_mean[x] for x in ch]) +
140
  rng.normal(0, [channel_discount_std[x] for x in ch]), 0, 0.45
141
  )
142
-
143
  list_price = rng.normal(base_p, 5)
144
  net_price = np.clip(list_price * (1 - discount), 20, None)
145
  unit_cost = np.clip(rng.normal(base_c, 4), 10, None)
@@ -150,86 +123,79 @@ def generate_synthetic_data(days=60, seed=42, rows_per_day=600):
150
  qty = np.maximum(1, rng.poisson(8 * dow_mult * macro * qty_mu))
151
 
152
  revenue = net_price * qty
153
- cogs = unit_cost * qty
154
- gm_val = revenue - cogs
155
- gm_pct = np.where(revenue > 0, gm_val / revenue, 0.0)
156
 
157
  for i in range(n):
158
  records.append({
159
- "date": d,
160
- "product": prod[i],
161
- "region": reg[i],
162
- "channel": ch[i],
163
- "list_price": float(list_price[i]),
164
- "discount_pct": float(discount[i]),
165
- "net_price": float(net_price[i]),
166
- "unit_cost": float(unit_cost[i]),
167
- "qty": int(qty[i]),
168
- "revenue": float(revenue[i]),
169
- "cogs": float(cogs[i]),
170
- "gm_value": float(gm_val[i]),
171
- "gm_pct": float(gm_pct[i]),
172
- "dow": dow
173
  })
174
-
175
- df = pd.DataFrame(records)
176
- return df
177
 
178
  def build_features(df: pd.DataFrame):
179
  feats_num = ["net_price", "unit_cost", "qty", "discount_pct", "list_price", "dow"]
180
  feats_cat = ["product", "region", "channel"]
 
181
  df = df.sort_values("date").copy()
182
  seg = ["product", "region", "channel"]
183
  df["price_per_unit"] = df["net_price"]
184
- df["cost_per_unit"] = df["unit_cost"]
 
185
  df["roll7_qty"] = df.groupby(seg)["qty"].transform(lambda s: s.rolling(7, min_periods=1).median())
186
  df["roll7_price"] = df.groupby(seg)["price_per_unit"].transform(lambda s: s.rolling(7, min_periods=1).median())
187
- df["roll7_cost"] = df.groupby(seg)["cost_per_unit"].transform(lambda s: s.rolling(7, min_periods=1).median())
 
188
  feats_num += ["price_per_unit", "cost_per_unit", "roll7_qty", "roll7_price", "roll7_cost"]
189
- target = "gm_pct"
190
- return df, feats_num, feats_cat, target
191
 
192
  @st.cache_resource(show_spinner=False)
193
  def train_model(df: pd.DataFrame, feats_num, feats_cat, target):
194
  X = df[feats_num + feats_cat]
195
  y = df[target]
 
196
  pre = ColumnTransformer(
197
  transformers=[
198
  ("cat", OneHotEncoder(handle_unknown="ignore"), feats_cat),
199
  ("num", "passthrough", feats_num),
200
  ]
201
  )
202
- model = RandomForestRegressor(n_estimators=250, max_depth=None, random_state=42, n_jobs=-1, min_samples_leaf=3)
203
  pipe = Pipeline([("pre", pre), ("rf", model)])
 
204
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, shuffle=False)
205
  pipe.fit(X_train, y_train)
206
  pred = pipe.predict(X_test)
207
- r2 = r2_score(y_test, pred)
208
- mae = mean_absolute_error(y_test, pred)
209
- return pipe, {"r2": r2, "mae": mae}, X_test
210
 
211
  @st.cache_resource(show_spinner=False)
212
- def compute_shap(pipe, X_sample, feats_num, feats_cat, shap_sample=800):
213
- np.random.seed(42)
214
- preprocessor = pipe.named_steps["pre"]
215
  rf = pipe.named_steps["rf"]
216
- feature_names = list(preprocessor.named_transformers_["cat"].get_feature_names_out(feats_cat)) + feats_num
217
 
218
  if len(X_sample) > shap_sample:
219
  sample_idx = np.random.choice(len(X_sample), size=shap_sample, replace=False)
220
  X_sample = X_sample.iloc[sample_idx]
221
 
222
- X_t = preprocessor.transform(X_sample)
223
  try:
224
  X_t = X_t.toarray()
225
- except Exception:
226
  pass
227
 
228
  explainer = shap.TreeExplainer(rf)
229
  shap_values = explainer.shap_values(X_t)
230
  shap_df = pd.DataFrame(shap_values, columns=feature_names)
231
-
232
- return shap_df, X_sample.reset_index(drop=True), feature_names
 
233
 
234
  def estimate_segment_elasticity(df: pd.DataFrame, product, region, channel):
235
  seg_df = df[(df["product"]==product)&(df["region"]==region)&(df["channel"]==channel)]
@@ -240,559 +206,350 @@ def estimate_segment_elasticity(df: pd.DataFrame, product, region, channel):
240
  lin = LinearRegression().fit(x, y)
241
  return float(lin.coef_[0]), True
242
 
243
- def simulate_pricing_action(segment_df: pd.DataFrame, elasticity, discount_reduction_pct):
244
  if segment_df.empty:
245
  return None
246
  base = segment_df.iloc[-1]
247
- p0 = base["net_price"]
248
- c0 = base["unit_cost"]
249
- q0 = base["qty"]
250
- d0 = base["discount_pct"]
251
 
252
- new_discount = np.clip(d0 - (discount_reduction_pct/100), 0.0, 0.45)
253
  p1 = max(0.01, base["list_price"] * (1 - new_discount))
254
- c1 = c0
255
-
256
- if p0 <= 0:
257
- q1 = q0
258
- else:
259
- q1 = max(0.0, q0 * (p1 / p0) ** elasticity)
260
-
261
- rev0 = p0 * q0
262
- cogs0 = c0 * q0
263
- rev1 = p1 * q1
264
- cogs1 = c1 * q1
265
 
266
- gm_delta_value = (rev1 - cogs1) - (rev0 - cogs0)
267
- gm0_pct = (rev0 - cogs0)/rev0 if rev0>0 else 0.0
268
- gm1_pct = (rev1 - cogs1)/rev1 if rev1>0 else 0.0
269
 
270
  return {
271
- "baseline_price": p0, "new_price": p1,
272
- "baseline_cost": c0, "new_cost": c1,
273
- "baseline_qty": q0, "new_qty": q1,
274
- "baseline_discount": d0*100, "new_discount": new_discount*100,
275
- "gm_delta_value": gm_delta_value,
276
- "gm0_pct": gm0_pct, "gm1_pct": gm1_pct,
277
- "revenue_delta": rev1 - rev0
278
  }
279
 
280
  # -----------------------------
281
- # Main App
282
  # -----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
 
284
- # Header
285
- st.markdown('<h1 class="main-header">🎯 Profitability Intelligence Suite</h1>', unsafe_allow_html=True)
286
- st.markdown('<p class="sub-header">AI-Powered Margin Analysis & Strategic Recommendations</p>', unsafe_allow_html=True)
287
-
288
- # Generate data
289
- with st.spinner("πŸ”„ Loading business data..."):
290
- df = generate_synthetic_data(days=60, seed=42, rows_per_day=600)
291
- df_feat, feats_num, feats_cat, target = build_features(df)
292
-
293
- # Calculate KPIs
294
- daily = df.groupby("date").agg(
295
- revenue=("revenue","sum"),
296
- cogs=("cogs","sum"),
297
- gm_value=("gm_value","sum")
298
- ).reset_index()
299
- daily["gm_pct"] = np.where(daily["revenue"]>0, daily["gm_value"]/daily["revenue"], 0.0)
300
 
 
 
301
  today_row = daily.iloc[-1]
302
  yesterday_row = daily.iloc[-2] if len(daily) > 1 else today_row
303
- week_ago_row = daily.iloc[-8] if len(daily) > 7 else today_row
304
  roll7 = daily["gm_pct"].tail(7).mean()
305
-
306
- # Executive Dashboard Section
307
- st.markdown("### πŸ“Š Executive Performance Dashboard")
308
 
309
  col1, col2, col3, col4 = st.columns(4)
310
 
311
  with col1:
312
- delta_gm = (today_row["gm_pct"] - yesterday_row["gm_pct"]) * 100
313
- st.metric(
314
- label="Gross Margin %",
315
- value=f"{today_row['gm_pct']*100:.1f}%",
316
- delta=f"{delta_gm:+.2f}pp vs yesterday",
317
- delta_color="normal"
318
- )
319
 
320
  with col2:
321
- delta_rev = ((today_row["revenue"] - yesterday_row["revenue"]) / yesterday_row["revenue"] * 100) if yesterday_row["revenue"] > 0 else 0
322
- st.metric(
323
- label="Revenue (Today)",
324
- value=f"${today_row['revenue']/1e6:.2f}M",
325
- delta=f"{delta_rev:+.1f}% DoD",
326
- delta_color="normal"
327
- )
328
 
329
  with col3:
330
- st.metric(
331
- label="Gross Margin $ (Today)",
332
- value=f"${today_row['gm_value']/1e6:.2f}M",
333
- delta=f"${(today_row['gm_value'] - yesterday_row['gm_value'])/1e6:+.2f}M",
334
- delta_color="normal"
335
- )
336
 
337
  with col4:
338
- avg_gm_vs_week = (today_row["gm_pct"] - week_ago_row["gm_pct"]) * 100
339
- st.metric(
340
- label="7-Day Avg GM%",
341
- value=f"{roll7*100:.1f}%",
342
- delta=f"{avg_gm_vs_week:+.2f}pp WoW",
343
- delta_color="normal"
344
- )
345
-
346
- # Trend visualization
347
- st.markdown("#### πŸ“ˆ Performance Trend Analysis")
348
-
349
- fig_trends = make_subplots(
350
- rows=1, cols=2,
351
- subplot_titles=("Gross Margin % Trend", "Revenue & Margin $ Trend"),
352
- specs=[[{"secondary_y": False}, {"secondary_y": True}]]
353
- )
354
-
355
- # GM% trend
356
- fig_trends.add_trace(
357
- go.Scatter(
358
- x=daily["date"],
359
- y=daily["gm_pct"]*100,
360
- name="GM%",
361
- line=dict(color="#1f77b4", width=3),
362
- fill='tozeroy',
363
- fillcolor="rgba(31, 119, 180, 0.1)"
364
- ),
365
- row=1, col=1
366
- )
367
-
368
- # Revenue and GM$ trend
369
- fig_trends.add_trace(
370
- go.Scatter(
371
- x=daily["date"],
372
- y=daily["revenue"]/1e6,
373
- name="Revenue",
374
- line=dict(color="#2ca02c", width=2)
375
- ),
376
- row=1, col=2
377
- )
378
-
379
- fig_trends.add_trace(
380
- go.Scatter(
381
- x=daily["date"],
382
- y=daily["gm_value"]/1e6,
383
- name="GM Value",
384
- line=dict(color="#ff7f0e", width=2, dash="dash")
385
- ),
386
- row=1, col=2, secondary_y=True
387
- )
388
-
389
- fig_trends.update_xaxes(title_text="Date", row=1, col=1)
390
- fig_trends.update_xaxes(title_text="Date", row=1, col=2)
391
- fig_trends.update_yaxes(title_text="Gross Margin %", row=1, col=1)
392
- fig_trends.update_yaxes(title_text="Revenue ($M)", row=1, col=2)
393
- fig_trends.update_yaxes(title_text="GM Value ($M)", row=1, col=2, secondary_y=True)
394
-
395
- fig_trends.update_layout(height=400, showlegend=True, hovermode="x unified")
396
- st.plotly_chart(fig_trends, use_container_width=True)
397
-
398
- st.markdown("---")
399
-
400
- # Train model
401
- with st.spinner("πŸ€– Training AI model..."):
402
- pipe, metrics, X_test = train_model(df_feat, feats_num, feats_cat, target)
403
-
404
- # Tabs for different sections
405
- tab1, tab2, tab3 = st.tabs(["πŸ” Key Drivers Analysis", "🎯 Strategic Recommendations", "πŸ§ͺ What-If Simulator"])
406
-
407
- with tab1:
408
- st.markdown("### Understanding What Drives Your Profitability")
409
- st.markdown("""
410
  <div class="insight-box">
411
- <b>πŸŽ“ Business Insight:</b> This analysis reveals which business factors have the strongest impact on gross margin.
412
- Understanding these drivers helps prioritize strategic initiatives and operational improvements.
 
413
  </div>
414
  """, unsafe_allow_html=True)
415
 
416
- # Compute SHAP
417
- with st.spinner("πŸ”¬ Analyzing profitability drivers..."):
418
- shap_df, X_test_sample, feature_names = compute_shap(pipe, X_test, feats_num, feats_cat, shap_sample=800)
419
-
420
- # Calculate mean absolute SHAP
421
- mean_abs = shap_df.abs().mean().sort_values(ascending=False)
422
-
423
- # Map technical names to business names
424
- business_name_map = {
425
- "discount_pct": "Discount Level",
426
- "unit_cost": "Unit Cost",
427
- "net_price": "Net Selling Price",
428
- "list_price": "List Price",
429
- "qty": "Order Quantity",
430
- "price_per_unit": "Price per Unit",
431
- "cost_per_unit": "Cost per Unit",
432
- "roll7_qty": "7-Day Avg Quantity",
433
- "roll7_price": "7-Day Avg Price",
434
- "roll7_cost": "7-Day Avg Cost",
435
- "dow": "Day of Week"
436
- }
437
-
438
- # Get top drivers with business names
439
- top_drivers = []
440
- for feat, val in mean_abs.head(10).items():
441
- bus_name = feat
442
- for key, name in business_name_map.items():
443
- if key in feat.lower():
444
- bus_name = name
445
- break
446
- # Check for product/region/channel encoding
447
- if feat.startswith("cat__"):
448
- parts = feat.replace("cat__", "").split("_")
449
- if "product" in feat.lower():
450
- bus_name = f"Product: {parts[-1] if parts else feat}"
451
- elif "region" in feat.lower():
452
- bus_name = f"Region: {parts[-1] if parts else feat}"
453
- elif "channel" in feat.lower():
454
- bus_name = f"Channel: {parts[-1] if parts else feat}"
455
- top_drivers.append({"Driver": bus_name, "Impact Score": val})
456
-
457
- drivers_df = pd.DataFrame(top_drivers)
458
-
459
- col_a, col_b = st.columns([1, 1])
460
-
461
- with col_a:
462
- st.markdown("#### Top 10 Profitability Drivers")
463
-
464
- # Create horizontal bar chart
465
- fig_drivers = go.Figure()
466
- fig_drivers.add_trace(go.Bar(
467
- y=drivers_df["Driver"][::-1],
468
- x=drivers_df["Impact Score"][::-1],
469
- orientation='h',
470
- marker=dict(
471
- color=drivers_df["Impact Score"][::-1],
472
- colorscale='Blues',
473
- line=dict(color='rgb(8,48,107)', width=1.5)
474
- ),
475
- text=drivers_df["Impact Score"][::-1].round(4),
476
- textposition='outside',
477
- ))
478
-
479
- fig_drivers.update_layout(
480
- title="Ranked by Average Impact on Gross Margin",
481
- xaxis_title="Impact Score (higher = stronger influence)",
482
- yaxis_title="",
483
- height=500,
484
- showlegend=False
485
- )
486
- st.plotly_chart(fig_drivers, use_container_width=True)
487
 
488
- with col_b:
489
- st.markdown("#### Key Insights")
 
 
490
 
491
- # Generate business insights
492
- top_3 = drivers_df.head(3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493
 
494
- st.markdown(f"""
495
- <div class="insight-box">
496
- <b>πŸ₯‡ Primary Driver:</b> {top_3.iloc[0]['Driver']}<br>
497
- <small>This factor has the strongest influence on margin performance</small>
498
- </div>
499
- """, unsafe_allow_html=True)
500
 
501
- st.markdown(f"""
502
- <div class="insight-box">
503
- <b>πŸ₯ˆ Secondary Driver:</b> {top_3.iloc[1]['Driver']}<br>
504
- <small>Second most important factor affecting profitability</small>
505
- </div>
506
- """, unsafe_allow_html=True)
507
 
508
- st.markdown(f"""
509
- <div class="insight-box">
510
- <b>πŸ₯‰ Tertiary Driver:</b> {top_3.iloc[2]['Driver']}<br>
511
- <small>Third key factor with significant margin impact</small>
512
- </div>
513
- """, unsafe_allow_html=True)
514
 
515
- # Segment-level insights
516
- st.markdown("#### Segment Performance")
517
 
518
- # Join SHAP with original data
519
- cat_cols = ["product", "region", "channel"]
520
- joined = pd.concat([X_test_sample.reset_index(drop=True), shap_df.reset_index(drop=True)], axis=1)
 
 
521
 
522
- # Find segments with biggest impact
523
- grp = joined.groupby(cat_cols).mean(numeric_only=True)
524
- key_shap_cols = [c for c in grp.columns if c in shap_df.columns]
525
- grp["net_impact"] = grp[key_shap_cols].sum(axis=1)
526
 
527
- top_negative = grp.nsmallest(5, "net_impact")
528
- top_positive = grp.nlargest(5, "net_impact")
 
 
 
 
 
 
 
529
 
530
- st.markdown("**⚠️ Segments Reducing Margin:**")
531
- for idx, row in top_negative.head(3).iterrows():
532
- st.markdown(f"β€’ **{idx[0]}** β€’ {idx[1]} β€’ {idx[2]} *(Impact: {row['net_impact']:.4f})*")
 
 
 
 
 
 
533
 
534
- st.markdown("**βœ… Segments Boosting Margin:**")
535
- for idx, row in top_positive.head(3).iterrows():
536
- st.markdown(f"β€’ **{idx[0]}** β€’ {idx[1]} β€’ {idx[2]} *(Impact: {row['net_impact']:.4f})*")
537
 
538
- with tab2:
539
- st.markdown("### AI-Generated Strategic Recommendations")
540
- st.markdown("""
541
- <div class="insight-box">
542
- <b>πŸ’‘ How This Works:</b> The AI identifies segments with margin pressure and suggests specific pricing actions
543
- to improve profitability. Recommendations are ranked by expected financial impact.
544
- </div>
545
- """, unsafe_allow_html=True)
546
 
547
- # Generate recommendations
548
- with st.spinner("🧠 Generating strategic recommendations..."):
549
- joined = pd.concat([X_test_sample.reset_index(drop=True), shap_df.reset_index(drop=True)], axis=1)
550
- joined["key"] = joined["product"] + "|" + joined["region"] + "|" + joined["channel"]
551
-
552
- cand_cols = [c for c in joined.columns if ("discount" in c.lower() or "cost" in c.lower() or "price" in c.lower()) and c in shap_df.columns]
553
- seg_scores = joined.groupby("key")[cand_cols].mean().sum(axis=1)
554
- worst_keys = seg_scores.sort_values().head(15).index.tolist()
555
-
556
- recs = []
557
- for key in worst_keys:
558
- p, r, c = key.split("|")
559
- hist = df[(df["product"]==p)&(df["region"]==r)&(df["channel"]==c)].sort_values("date")
560
- if hist.empty or len(hist) < 50:
561
- continue
562
-
563
- eps, _ = estimate_segment_elasticity(hist, p, r, c)
564
-
565
- # Suggest discount reduction between 1-3 percentage points
566
- prop_disc_pts = np.clip(abs(seg_scores[key])*10, 1.0, 3.0)
567
- sim = simulate_pricing_action(hist, eps, prop_disc_pts)
568
-
569
- if sim is None or sim["gm_delta_value"] <= 0:
570
- continue
571
-
572
- # Calculate annualized impact (rough estimate)
573
- daily_transactions = len(hist) / ((hist["date"].max() - hist["date"].min()).days + 1)
574
- annual_impact = sim["gm_delta_value"] * daily_transactions * 365
575
-
576
- recs.append({
577
- "Segment": f"{p}",
578
- "Region": r,
579
- "Channel": c,
580
- "Current Discount": f"{sim['baseline_discount']:.1f}%",
581
- "Recommended Discount": f"{sim['new_discount']:.1f}%",
582
- "Expected GM Uplift": sim["gm_delta_value"],
583
- "Annual Impact Estimate": annual_impact,
584
- "Current GM%": sim["gm0_pct"]*100,
585
- "Projected GM%": sim["gm1_pct"]*100,
586
- "Price Elasticity": eps
587
- })
588
 
589
- recs_df = pd.DataFrame(recs).sort_values("Expected GM Uplift", ascending=False)
590
-
591
- if len(recs_df) > 0:
592
- # Show top 3 recommendations in cards
593
- st.markdown("#### πŸ† Top 3 Priority Actions")
594
-
595
- for i, (idx, rec) in enumerate(recs_df.head(3).iterrows()):
596
- with st.container():
597
- st.markdown(f"""
598
- <div class="recommendation-card">
599
- <h4>#{i+1}: {rec['Segment']} β€’ {rec['Region']} β€’ {rec['Channel']}</h4>
600
- <p style="font-size: 1.1rem; margin: 0.5rem 0;">
601
- <b>Recommended Action:</b> Reduce discount from <b>{rec['Current Discount']}</b> to <b>{rec['Recommended Discount']}</b>
602
- </p>
603
- <p style="font-size: 1rem; color: #666; margin: 0.5rem 0;">
604
- Current GM: <b>{rec['Current GM%']:.1f}%</b> β†’ Projected GM: <b style="color: #28a745;">{rec['Projected GM%']:.1f}%</b>
605
- </p>
606
- <p class="positive-impact">
607
- πŸ’° Expected Daily Impact: ${rec['Expected GM Uplift']:.2f}
608
- </p>
609
- <p style="font-size: 0.95rem; color: #666;">
610
- πŸ“Š Estimated Annual Impact: <b>${rec['Annual Impact Estimate']/1e3:.1f}K</b>
611
- </p>
612
- </div>
613
- """, unsafe_allow_html=True)
614
-
615
- st.markdown("---")
616
- st.markdown("#### πŸ“‹ Complete Recommendations List")
617
-
618
- # Format for display
619
- display_df = recs_df.copy()
620
- display_df["Expected GM Uplift"] = display_df["Expected GM Uplift"].apply(lambda x: f"${x:.2f}")
621
- display_df["Annual Impact Estimate"] = display_df["Annual Impact Estimate"].apply(lambda x: f"${x/1e3:.1f}K")
622
- display_df["Current GM%"] = display_df["Current GM%"].apply(lambda x: f"{x:.1f}%")
623
- display_df["Projected GM%"] = display_df["Projected GM%"].apply(lambda x: f"{x:.1f}%")
624
- display_df["Price Elasticity"] = display_df["Price Elasticity"].apply(lambda x: f"{x:.2f}")
625
-
626
- st.dataframe(display_df, use_container_width=True, height=400)
627
-
628
- # Download button
629
- st.download_button(
630
- label="πŸ“₯ Download Full Recommendations (CSV)",
631
- data=recs_df.to_csv(index=False).encode("utf-8"),
632
- file_name=f"profitability_recommendations_{datetime.today().strftime('%Y%m%d')}.csv",
633
- mime="text/csv"
634
- )
635
 
636
- # Aggregate impact
637
- total_daily_impact = recs_df["Expected GM Uplift"].sum()
638
- total_annual_impact = recs_df["Annual Impact Estimate"].sum()
639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
640
  st.markdown(f"""
641
- <div class="insight-box" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none;">
642
- <h3 style="color: white; margin-top: 0;">πŸ’Ž Total Opportunity</h3>
643
- <p style="font-size: 1.3rem; margin: 0.5rem 0;">
644
- <b>Daily GM Impact:</b> ${total_daily_impact:.2f}
645
- </p>
646
- <p style="font-size: 1.6rem; margin: 0.5rem 0;">
647
- <b>Estimated Annual Impact:</b> ${total_annual_impact/1e6:.2f}M
648
- </p>
649
- <small>Based on current transaction volumes and assuming consistent implementation</small>
650
  </div>
651
  """, unsafe_allow_html=True)
652
  else:
653
- st.info("No significant optimization opportunities detected in current data.")
654
-
655
- with tab3:
656
- st.markdown("### Custom What-If Analysis")
657
- st.markdown("""
658
- <div class="insight-box">
659
- <b>πŸ§ͺ Interactive Simulation:</b> Test different pricing strategies for specific segments to understand
660
- the potential impact on revenue, volume, and profitability.
661
- </div>
662
- """, unsafe_allow_html=True)
663
-
664
- # Segment selector
665
- last_day = df["date"].max()
666
- seg_today = df[df["date"]==last_day][["product","region","channel"]].drop_duplicates()
667
-
668
- col_sim1, col_sim2, col_sim3 = st.columns(3)
669
-
670
- with col_sim1:
671
- selected_product = st.selectbox("πŸ“¦ Select Product", sorted(seg_today["product"].unique()))
672
- with col_sim2:
673
- selected_region = st.selectbox("🌍 Select Region", sorted(seg_today["region"].unique()))
674
- with col_sim3:
675
- selected_channel = st.selectbox("πŸ›’ Select Channel", sorted(seg_today["channel"].unique()))
676
-
677
- # Get segment history
678
- seg_hist = df[
679
- (df["product"]==selected_product) &
680
- (df["region"]==selected_region) &
681
- (df["channel"]==selected_channel)
682
- ].sort_values("date")
683
-
684
- if not seg_hist.empty and len(seg_hist) >= 50:
685
- elasticity, _ = estimate_segment_elasticity(seg_hist, selected_product, selected_region, selected_channel)
686
-
687
- # Current state
688
- current = seg_hist.iloc[-1]
689
-
690
  st.markdown(f"""
691
- <div class="insight-box">
692
- <b>πŸ“Š Current State:</b><br>
693
- β€’ Current Discount: <b>{current['discount_pct']*100:.1f}%</b><br>
694
- β€’ Net Price: <b>${current['net_price']:.2f}</b><br>
695
- β€’ Unit Cost: <b>${current['unit_cost']:.2f}</b><br>
696
- β€’ Avg Daily Volume: <b>{seg_hist.tail(7)['qty'].mean():.0f} units</b><br>
697
- β€’ Current GM%: <b>{current['gm_pct']*100:.1f}%</b><br>
698
- β€’ Price Elasticity: <b>{elasticity:.2f}</b> <small>(% change in volume per 1% price change)</small>
699
  </div>
700
  """, unsafe_allow_html=True)
701
 
702
- st.markdown("#### 🎯 Test Pricing Strategy")
703
-
704
- # Pricing strategy slider
705
- discount_change = st.slider(
706
- "Adjust Discount Level (percentage points)",
707
- min_value=-10.0,
708
- max_value=5.0,
709
- value=0.0,
710
- step=0.5,
711
- help="Negative values reduce discount (increase price), positive values increase discount"
712
- )
713
 
714
- if discount_change != 0:
715
- sim = simulate_pricing_action(seg_hist, elasticity, -discount_change)
716
-
717
- if sim:
718
- # Visualization
719
- col_res1, col_res2 = st.columns(2)
720
-
721
- with col_res1:
722
- # Create comparison chart
723
- comparison_data = pd.DataFrame({
724
- 'Metric': ['Price', 'Volume', 'GM%'],
725
- 'Current': [sim['baseline_price'], sim['baseline_qty'], sim['gm0_pct']*100],
726
- 'Projected': [sim['new_price'], sim['new_qty'], sim['gm1_pct']*100]
727
- })
728
-
729
- fig_comp = go.Figure()
730
- fig_comp.add_trace(go.Bar(
731
- name='Current',
732
- x=comparison_data['Metric'],
733
- y=comparison_data['Current'],
734
- marker_color='#94a3b8'
735
- ))
736
- fig_comp.add_trace(go.Bar(
737
- name='Projected',
738
- x=comparison_data['Metric'],
739
- y=comparison_data['Projected'],
740
- marker_color='#3b82f6'
741
- ))
742
-
743
- fig_comp.update_layout(
744
- title="Current vs. Projected Performance",
745
- barmode='group',
746
- height=350
747
- )
748
- st.plotly_chart(fig_comp, use_container_width=True)
749
-
750
- with col_res2:
751
- st.markdown("#### πŸ“ˆ Simulation Results")
752
-
753
- gm_change = sim['gm1_pct'] - sim['gm0_pct']
754
- rev_change_pct = (sim['revenue_delta'] / (sim['baseline_price'] * sim['baseline_qty'])) * 100 if sim['baseline_price'] * sim['baseline_qty'] > 0 else 0
755
- vol_change_pct = ((sim['new_qty'] - sim['baseline_qty']) / sim['baseline_qty']) * 100 if sim['baseline_qty'] > 0 else 0
756
-
757
- st.metric(
758
- "Gross Margin Impact",
759
- f"{sim['gm1_pct']*100:.1f}%",
760
- f"{gm_change*100:+.1f}pp"
761
- )
762
-
763
- st.metric(
764
- "Revenue Impact",
765
- f"${sim['new_price'] * sim['new_qty']:.2f}",
766
- f"{rev_change_pct:+.1f}%"
767
- )
768
-
769
- st.metric(
770
- "Volume Impact",
771
- f"{sim['new_qty']:.0f} units",
772
- f"{vol_change_pct:+.1f}%"
773
- )
774
-
775
- # Daily P&L impact
776
- st.markdown(f"""
777
- <div class="insight-box" style="margin-top: 1rem;">
778
- <b>πŸ’° Daily P&L Impact:</b><br>
779
- <span style="font-size: 1.5rem; {'color: #28a745' if sim['gm_delta_value'] > 0 else 'color: #dc3545'}">
780
- ${sim['gm_delta_value']:+.2f}
781
- </span>
782
- </div>
783
- """, unsafe_allow_html=True)
784
- else:
785
- st.info("πŸ‘† Adjust the discount slider above to simulate different pricing strategies")
786
 
787
- else:
788
- st.warning("⚠️ Insufficient data for selected segment. Please choose a different combination.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
789
 
790
  st.markdown("---")
791
- st.markdown("""
792
- <div style="text-align: center; color: #666; padding: 2rem 0;">
793
- <small>
794
- πŸ”’ Demo Mode: Using synthetic SAP-style data for illustration purposes<br>
795
- For production deployment, connect to live SAP S/4HANA CDS views or data warehouse
796
- </small>
797
- </div>
798
- """, unsafe_allow_html=True)
 
 
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
  import plotly.express as px
5
  import plotly.graph_objects as go
 
6
  import shap
7
  import matplotlib.pyplot as plt
8
+
9
  from datetime import datetime, timedelta
10
  from sklearn.model_selection import train_test_split
11
  from sklearn.compose import ColumnTransformer
 
15
  from sklearn.linear_model import LinearRegression
16
  from sklearn.metrics import r2_score, mean_absolute_error
17
 
18
+ st.set_page_config(page_title="Profitability Intelligence", layout="wide", initial_sidebar_state="collapsed")
 
 
 
 
 
 
19
 
20
+ # Custom CSS for better UI
21
  st.markdown("""
22
  <style>
23
  .main-header {
24
+ font-size: 2.5rem;
25
  font-weight: 700;
26
  color: #1f77b4;
 
27
  margin-bottom: 0.5rem;
 
28
  }
29
  .sub-header {
30
+ font-size: 1.1rem;
31
  color: #666;
 
32
  margin-bottom: 2rem;
33
  }
34
+ .insight-box {
35
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
36
  padding: 1.5rem;
37
+ border-radius: 10px;
 
38
  color: white;
 
 
 
 
 
 
39
  margin: 1rem 0;
 
 
40
  }
41
+ .metric-card {
42
  background: white;
 
 
43
  padding: 1.5rem;
44
+ border-radius: 8px;
45
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
46
+ border-left: 4px solid #1f77b4;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  }
48
+ .recommendation-card {
49
+ background: #f0f9ff;
50
+ padding: 1rem;
51
+ border-radius: 8px;
52
+ border-left: 4px solid #22c55e;
53
+ margin: 0.5rem 0;
54
  }
55
+ .warning-card {
56
+ background: #fef3c7;
57
+ padding: 1rem;
58
+ border-radius: 8px;
59
+ border-left: 4px solid #f59e0b;
60
+ margin: 0.5rem 0;
61
  }
62
  </style>
63
  """, unsafe_allow_html=True)
64
 
65
  # -----------------------------
66
+ # Data Generation (Hidden from UI)
67
  # -----------------------------
68
  @st.cache_data(show_spinner=False)
69
+ def generate_synthetic_data(days=90, seed=42, rows_per_day=800):
70
  rng = np.random.default_rng(seed)
71
  start_date = datetime.today().date() - timedelta(days=days)
72
  dates = pd.date_range(start_date, periods=days, freq="D")
73
+
74
  products = ["Premium Widget", "Standard Widget", "Economy Widget", "Deluxe Widget"]
75
+ regions = ["North America", "Europe", "Asia Pacific"]
76
  channels = ["Direct Sales", "Distribution Partners", "E-Commerce"]
77
 
78
  base_price = {"Premium Widget": 120, "Standard Widget": 135, "Economy Widget": 110, "Deluxe Widget": 150}
79
+ base_cost = {"Premium Widget": 70, "Standard Widget": 88, "Economy Widget": 60, "Deluxe Widget": 95}
80
+
81
+ region_price_bump = {"North America": 1.00, "Europe": 1.03, "Asia Pacific": 0.97}
82
+ region_cost_bump = {"North America": 1.00, "Europe": 1.02, "Asia Pacific": 1.01}
83
+
84
  channel_discount_mean = {"Direct Sales": 0.06, "Distribution Partners": 0.12, "E-Commerce": 0.04}
85
+ channel_discount_std = {"Direct Sales": 0.02, "Distribution Partners": 0.03, "E-Commerce": 0.02}
86
 
87
  seg_epsilon = {}
88
  for p in products:
 
103
 
104
  n = rows_per_day
105
  prod = rng.choice(products, size=n, p=[0.35, 0.3, 0.2, 0.15])
106
+ reg = rng.choice(regions, size=n, p=[0.4, 0.35, 0.25])
107
+ ch = rng.choice(channels, size=n, p=[0.45, 0.35, 0.20])
108
 
109
  base_p = np.array([base_price[x] for x in prod]) * np.array([region_price_bump[x] for x in reg])
110
+ base_c = np.array([base_cost[x] for x in prod]) * np.array([region_cost_bump[x] for x in reg])
111
 
112
  discount = np.clip(
113
  np.array([channel_discount_mean[x] for x in ch]) +
114
  rng.normal(0, [channel_discount_std[x] for x in ch]), 0, 0.45
115
  )
 
116
  list_price = rng.normal(base_p, 5)
117
  net_price = np.clip(list_price * (1 - discount), 20, None)
118
  unit_cost = np.clip(rng.normal(base_c, 4), 10, None)
 
123
  qty = np.maximum(1, rng.poisson(8 * dow_mult * macro * qty_mu))
124
 
125
  revenue = net_price * qty
126
+ cogs = unit_cost * qty
127
+ gm_val = revenue - cogs
128
+ gm_pct = np.where(revenue > 0, gm_val / revenue, 0.0)
129
 
130
  for i in range(n):
131
  records.append({
132
+ "date": d, "product": prod[i], "region": reg[i], "channel": ch[i],
133
+ "list_price": float(list_price[i]), "discount_pct": float(discount[i]),
134
+ "net_price": float(net_price[i]), "unit_cost": float(unit_cost[i]),
135
+ "qty": int(qty[i]), "revenue": float(revenue[i]), "cogs": float(cogs[i]),
136
+ "gm_value": float(gm_val[i]), "gm_pct": float(gm_pct[i]), "dow": dow
 
 
 
 
 
 
 
 
 
137
  })
138
+ return pd.DataFrame(records)
 
 
139
 
140
  def build_features(df: pd.DataFrame):
141
  feats_num = ["net_price", "unit_cost", "qty", "discount_pct", "list_price", "dow"]
142
  feats_cat = ["product", "region", "channel"]
143
+
144
  df = df.sort_values("date").copy()
145
  seg = ["product", "region", "channel"]
146
  df["price_per_unit"] = df["net_price"]
147
+ df["cost_per_unit"] = df["unit_cost"]
148
+
149
  df["roll7_qty"] = df.groupby(seg)["qty"].transform(lambda s: s.rolling(7, min_periods=1).median())
150
  df["roll7_price"] = df.groupby(seg)["price_per_unit"].transform(lambda s: s.rolling(7, min_periods=1).median())
151
+ df["roll7_cost"] = df.groupby(seg)["cost_per_unit"].transform(lambda s: s.rolling(7, min_periods=1).median())
152
+
153
  feats_num += ["price_per_unit", "cost_per_unit", "roll7_qty", "roll7_price", "roll7_cost"]
154
+ return df, feats_num, feats_cat, "gm_pct"
 
155
 
156
  @st.cache_resource(show_spinner=False)
157
  def train_model(df: pd.DataFrame, feats_num, feats_cat, target):
158
  X = df[feats_num + feats_cat]
159
  y = df[target]
160
+
161
  pre = ColumnTransformer(
162
  transformers=[
163
  ("cat", OneHotEncoder(handle_unknown="ignore"), feats_cat),
164
  ("num", "passthrough", feats_num),
165
  ]
166
  )
167
+ model = RandomForestRegressor(n_estimators=300, max_depth=None, random_state=42, n_jobs=-1, min_samples_leaf=3)
168
  pipe = Pipeline([("pre", pre), ("rf", model)])
169
+
170
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, shuffle=False)
171
  pipe.fit(X_train, y_train)
172
  pred = pipe.predict(X_test)
173
+
174
+ return pipe, {"r2": r2_score(y_test, pred), "mae": mean_absolute_error(y_test, pred)}, X_test
 
175
 
176
  @st.cache_resource(show_spinner=False)
177
+ def compute_shap(pipe, X_sample, feats_num, feats_cat, shap_sample=1000, seed=42):
178
+ np.random.seed(seed)
179
+ preproc = pipe.named_steps["pre"]
180
  rf = pipe.named_steps["rf"]
181
+ feature_names = list(preproc.named_transformers_["cat"].get_feature_names_out(feats_cat)) + feats_num
182
 
183
  if len(X_sample) > shap_sample:
184
  sample_idx = np.random.choice(len(X_sample), size=shap_sample, replace=False)
185
  X_sample = X_sample.iloc[sample_idx]
186
 
187
+ X_t = preproc.transform(X_sample)
188
  try:
189
  X_t = X_t.toarray()
190
+ except:
191
  pass
192
 
193
  explainer = shap.TreeExplainer(rf)
194
  shap_values = explainer.shap_values(X_t)
195
  shap_df = pd.DataFrame(shap_values, columns=feature_names)
196
+
197
+ joined = pd.concat([X_sample.reset_index(drop=True), shap_df.reset_index(drop=True)], axis=1)
198
+ return shap_df, X_sample.reset_index(drop=True), feature_names, joined
199
 
200
  def estimate_segment_elasticity(df: pd.DataFrame, product, region, channel):
201
  seg_df = df[(df["product"]==product)&(df["region"]==region)&(df["channel"]==channel)]
 
206
  lin = LinearRegression().fit(x, y)
207
  return float(lin.coef_[0]), True
208
 
209
+ def simulate_action(segment_df: pd.DataFrame, elasticity, delta_discount=0.0, delta_unit_cost=0.0):
210
  if segment_df.empty:
211
  return None
212
  base = segment_df.iloc[-1]
213
+ p0, c0, q0, d0 = base["net_price"], base["unit_cost"], base["qty"], base["discount_pct"]
 
 
 
214
 
215
+ new_discount = np.clip(d0 + delta_discount, 0.0, 0.45)
216
  p1 = max(0.01, base["list_price"] * (1 - new_discount))
217
+ c1 = max(0.01, c0 + delta_unit_cost)
218
+ q1 = max(0.0, q0 * (p1 / p0) ** elasticity) if p0 > 0 else q0
 
 
 
 
 
 
 
 
 
219
 
220
+ rev0, cogs0 = p0 * q0, c0 * q0
221
+ rev1, cogs1 = p1 * q1, c1 * q1
 
222
 
223
  return {
224
+ "baseline_price": p0, "new_price": p1, "baseline_cost": c0, "new_cost": c1,
225
+ "baseline_qty": q0, "new_qty": q1, "gm_delta_value": (rev1 - cogs1) - (rev0 - cogs0),
226
+ "gm0_pct": (rev0 - cogs0)/rev0 if rev0>0 else 0.0,
227
+ "gm1_pct": (rev1 - cogs1)/rev1 if rev1>0 else 0.0,
228
+ "new_discount": new_discount
 
 
229
  }
230
 
231
  # -----------------------------
232
+ # Initialize Data
233
  # -----------------------------
234
+ if "data_loaded" not in st.session_state:
235
+ with st.spinner("πŸ”„ Loading SAP data and building intelligence models..."):
236
+ df = generate_synthetic_data(days=90, seed=42, rows_per_day=800)
237
+ df_feat, feats_num, feats_cat, target = build_features(df)
238
+ pipe, metrics, X_test = train_model(df_feat, feats_num, feats_cat, target)
239
+ shap_df, X_test_sample, feature_names, joined = compute_shap(pipe, X_test, feats_num, feats_cat)
240
+
241
+ st.session_state["df"] = df
242
+ st.session_state["df_feat"] = df_feat
243
+ st.session_state["pipe"] = pipe
244
+ st.session_state["metrics"] = metrics
245
+ st.session_state["shap_df"] = shap_df
246
+ st.session_state["joined"] = joined
247
+ st.session_state["feats_num"] = feats_num
248
+ st.session_state["feats_cat"] = feats_cat
249
+ st.session_state["data_loaded"] = True
250
+
251
+ df = st.session_state["df"]
252
+ joined = st.session_state["joined"]
253
+ metrics = st.session_state["metrics"]
254
+ shap_df = st.session_state["shap_df"]
255
 
256
+ # -----------------------------
257
+ # HEADER
258
+ # -----------------------------
259
+ st.markdown('<p class="main-header">πŸ’° Profitability Intelligence Dashboard</p>', unsafe_allow_html=True)
260
+ st.markdown('<p class="sub-header">AI-powered insights to understand and optimize your gross margin drivers</p>', unsafe_allow_html=True)
261
+
262
+ # -----------------------------
263
+ # EXECUTIVE SUMMARY
264
+ # -----------------------------
265
+ st.markdown("## πŸ“Š Executive Summary")
 
 
 
 
 
 
266
 
267
+ daily = df.groupby("date").agg(revenue=("revenue","sum"), cogs=("cogs","sum"), gm_value=("gm_value","sum")).reset_index()
268
+ daily["gm_pct"] = np.where(daily["revenue"]>0, daily["gm_value"]/daily["revenue"], 0.0)
269
  today_row = daily.iloc[-1]
270
  yesterday_row = daily.iloc[-2] if len(daily) > 1 else today_row
 
271
  roll7 = daily["gm_pct"].tail(7).mean()
272
+ roll30 = daily["gm_pct"].tail(30).mean()
 
 
273
 
274
  col1, col2, col3, col4 = st.columns(4)
275
 
276
  with col1:
277
+ delta = today_row["gm_pct"] - yesterday_row["gm_pct"]
278
+ st.metric("Today's Gross Margin %", f"{today_row['gm_pct']*100:.1f}%",
279
+ f"{delta*100:+.1f}% vs yesterday")
 
 
 
 
280
 
281
  with col2:
282
+ st.metric("Revenue (Today)", f"${today_row['revenue']/1e6:.2f}M")
 
 
 
 
 
 
283
 
284
  with col3:
285
+ trend = "↗️" if roll7 > roll30 else "β†˜οΈ"
286
+ st.metric("7-Day Avg GM%", f"{roll7*100:.1f}%", f"{trend} vs 30-day avg")
 
 
 
 
287
 
288
  with col4:
289
+ st.metric("Gross Profit (Today)", f"${today_row['gm_value']/1e6:.2f}M")
290
+
291
+ # Trend chart
292
+ fig_trend = go.Figure()
293
+ fig_trend.add_trace(go.Scatter(x=daily["date"], y=daily["gm_pct"]*100,
294
+ mode='lines', name='Daily GM%', line=dict(color='#1f77b4', width=2)))
295
+ fig_trend.add_trace(go.Scatter(x=daily["date"], y=daily["gm_pct"].rolling(7).mean()*100,
296
+ mode='lines', name='7-Day Average', line=dict(color='#ff7f0e', width=2, dash='dash')))
297
+ fig_trend.update_layout(title="Gross Margin % Trend", xaxis_title="Date", yaxis_title="GM %",
298
+ height=300, hovermode='x unified')
299
+ st.plotly_chart(fig_trend, use_container_width=True)
300
+
301
+ # Key Insight Box
302
+ gm_change = (today_row["gm_pct"] - roll30) * 100
303
+ if abs(gm_change) > 0.5:
304
+ trend_word = "improved" if gm_change > 0 else "declined"
305
+ st.markdown(f"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  <div class="insight-box">
307
+ <h3>πŸ’‘ Key Insight</h3>
308
+ <p>Your gross margin has <strong>{trend_word} by {abs(gm_change):.1f} percentage points</strong> compared to the 30-day average.
309
+ The analysis below identifies the specific drivers and business segments responsible for this change.</p>
310
  </div>
311
  """, unsafe_allow_html=True)
312
 
313
+ st.markdown("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
+ # -----------------------------
316
+ # DRIVER ANALYSIS
317
+ # -----------------------------
318
+ st.markdown("## πŸ” What's Driving Your Profitability?")
319
 
320
+ st.markdown("""
321
+ Our AI model has analyzed thousands of transactions to identify which factors have the biggest impact on your gross margin.
322
+ Think of this as understanding which levers you can pull to improve profitability.
323
+ """)
324
+
325
+ # Calculate driver importance
326
+ mean_abs = shap_df.abs().mean().sort_values(ascending=False)
327
+
328
+ # Simplify feature names for business users
329
+ def simplify_feature_name(name):
330
+ if "discount" in name.lower():
331
+ return "Discount Level"
332
+ elif "cost_per_unit" in name.lower() or "unit_cost" in name.lower():
333
+ return "Unit Cost"
334
+ elif "price_per_unit" in name.lower() or "net_price" in name.lower():
335
+ return "Selling Price"
336
+ elif "qty" in name.lower():
337
+ return "Volume"
338
+ elif "product_" in name.lower():
339
+ return name.replace("product_", "Product: ")
340
+ elif "channel_" in name.lower():
341
+ return name.replace("channel_", "Channel: ")
342
+ elif "region_" in name.lower():
343
+ return name.replace("region_", "Region: ")
344
+ return name
345
+
346
+ # Top 10 drivers
347
+ top_drivers = mean_abs.head(10)
348
+ driver_names = [simplify_feature_name(f) for f in top_drivers.index]
349
+
350
+ fig_drivers = go.Figure(go.Bar(
351
+ y=driver_names[::-1],
352
+ x=top_drivers.values[::-1],
353
+ orientation='h',
354
+ marker=dict(color=top_drivers.values[::-1], colorscale='Blues', showscale=False)
355
+ ))
356
+ fig_drivers.update_layout(
357
+ title="Top 10 Profit Margin Drivers (Impact Strength)",
358
+ xaxis_title="Impact on Gross Margin",
359
+ yaxis_title="",
360
+ height=400,
361
+ showlegend=False
362
+ )
363
+ st.plotly_chart(fig_drivers, use_container_width=True)
364
 
365
+ # Business interpretation
366
+ st.markdown("""
367
+ **What does this mean?**
368
+ - **Higher bars** = Bigger impact on your gross margin
369
+ - Focus your attention on the top 3-5 drivers for maximum profitability improvement
370
+ """)
371
 
372
+ st.markdown("---")
 
 
 
 
 
373
 
374
+ # -----------------------------
375
+ # SEGMENT PERFORMANCE
376
+ # -----------------------------
377
+ st.markdown("## πŸ“ Performance by Business Segment")
 
 
378
 
379
+ st.markdown("Not all business segments perform equally. Here's where you're winning and where there's opportunity:")
 
380
 
381
+ # Calculate segment performance
382
+ key_feats = [c for c in joined.columns if any(k in c for k in ["discount", "price_per_unit", "cost_per_unit","unit_cost","net_price"])]
383
+ grp = joined.groupby(["product","region","channel"]).mean(numeric_only=True)
384
+ rank_cols = [c for c in grp.columns if c in key_feats]
385
+ segment_impact = grp[rank_cols].sum(axis=1).sort_values()
386
 
387
+ col1, col2 = st.columns(2)
 
 
 
388
 
389
+ with col1:
390
+ st.markdown("### πŸ”΄ Segments Dragging Margin Down")
391
+ worst = segment_impact.head(8)
392
+ worst_df = pd.DataFrame({
393
+ 'Segment': [f"{p} β€’ {r} β€’ {c}" for p, r, c in worst.index],
394
+ 'Margin Impact': worst.values
395
+ })
396
+ worst_df['Impact Score'] = worst_df['Margin Impact'].apply(lambda x: 'πŸ”΄' * min(5, int(abs(x)*10)))
397
+ st.dataframe(worst_df[['Segment', 'Impact Score']], hide_index=True, use_container_width=True)
398
 
399
+ with col2:
400
+ st.markdown("### 🟒 Segments Lifting Margin Up")
401
+ best = segment_impact.tail(8).sort_values(ascending=False)
402
+ best_df = pd.DataFrame({
403
+ 'Segment': [f"{p} β€’ {r} β€’ {c}" for p, r, c in best.index],
404
+ 'Margin Impact': best.values
405
+ })
406
+ best_df['Performance'] = best_df['Margin Impact'].apply(lambda x: '🟒' * min(5, max(1, int(x*10))))
407
+ st.dataframe(best_df[['Segment', 'Performance']], hide_index=True, use_container_width=True)
408
 
409
+ st.markdown("---")
 
 
410
 
411
+ # -----------------------------
412
+ # WHAT-IF SIMULATOR
413
+ # -----------------------------
414
+ st.markdown("## 🎯 What-If Simulator: Test Your Strategies")
 
 
 
 
415
 
416
+ st.markdown("""
417
+ Use this simulator to model the financial impact of potential pricing or cost optimization strategies.
418
+ Select a segment and adjust the levers to see the projected outcome.
419
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
 
421
+ # Segment selector
422
+ last_day = df["date"].max()
423
+ seg_today = df[df["date"]==last_day][["product","region","channel"]].drop_duplicates().sort_values(["product","region","channel"])
424
+ seg_options = seg_today.apply(lambda r: f"{r['product']} β€’ {r['region']} β€’ {r['channel']}", axis=1).tolist()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
 
426
+ col1, col2 = st.columns([2, 1])
 
 
427
 
428
+ with col1:
429
+ selected_segment = st.selectbox("**Select Business Segment:**", seg_options, key="segment_selector")
430
+
431
+ with col2:
432
+ st.markdown("**Scenario Type:**")
433
+ scenario = st.radio("", ["Optimize Discount", "Reduce Costs", "Custom"], horizontal=True, label_visibility="collapsed")
434
+
435
+ prod_sel, reg_sel, ch_sel = [s.strip() for s in selected_segment.split("β€’")]
436
+ seg_hist = df[(df["product"]==prod_sel)&(df["region"]==reg_sel)&(df["channel"]==ch_sel)].sort_values("date")
437
+ elasticity, _ = estimate_segment_elasticity(seg_hist, prod_sel, reg_sel, ch_sel)
438
+
439
+ # Pre-set scenarios
440
+ if scenario == "Optimize Discount":
441
+ delta_disc = -2.0
442
+ delta_cost = 0.0
443
+ st.info("πŸ“‰ Testing a 2 percentage point discount reduction to improve margin")
444
+ elif scenario == "Reduce Costs":
445
+ delta_disc = 0.0
446
+ delta_cost = -3.0
447
+ st.info("πŸ’° Testing a $3 reduction in unit cost through operational efficiency")
448
+ else:
449
+ col1, col2 = st.columns(2)
450
+ with col1:
451
+ delta_disc = st.slider("Adjust Discount (percentage points)", -10.0, 10.0, -2.0, 0.5,
452
+ help="Negative = tighter discount, Positive = deeper discount")
453
+ with col2:
454
+ delta_cost = st.slider("Adjust Unit Cost ($)", -10.0, 10.0, 0.0, 0.5,
455
+ help="Negative = cost reduction, Positive = cost increase")
456
+
457
+ # Run simulation
458
+ sim_res = simulate_action(seg_hist, elasticity, delta_discount=delta_disc/100.0, delta_unit_cost=delta_cost)
459
+
460
+ if sim_res:
461
+ st.markdown("### πŸ“ˆ Projected Impact")
462
+
463
+ # Results in clean columns
464
+ metric_col1, metric_col2, metric_col3, metric_col4 = st.columns(4)
465
+
466
+ with metric_col1:
467
+ price_change = ((sim_res['new_price'] - sim_res['baseline_price']) / sim_res['baseline_price']) * 100
468
+ st.metric("Price per Unit", f"${sim_res['new_price']:.2f}", f"{price_change:+.1f}%")
469
+
470
+ with metric_col2:
471
+ cost_change = ((sim_res['new_cost'] - sim_res['baseline_cost']) / sim_res['baseline_cost']) * 100
472
+ st.metric("Cost per Unit", f"${sim_res['new_cost']:.2f}", f"{cost_change:+.1f}%")
473
+
474
+ with metric_col3:
475
+ qty_change = ((sim_res['new_qty'] - sim_res['baseline_qty']) / sim_res['baseline_qty']) * 100
476
+ st.metric("Volume", f"{sim_res['new_qty']:.0f} units", f"{qty_change:+.1f}%")
477
+
478
+ with metric_col4:
479
+ gm_change = (sim_res['gm1_pct'] - sim_res['gm0_pct']) * 100
480
+ st.metric("Gross Margin %", f"{sim_res['gm1_pct']*100:.1f}%", f"{gm_change:+.1f} pts")
481
+
482
+ # Financial impact
483
+ if sim_res['gm_delta_value'] > 0:
484
  st.markdown(f"""
485
+ <div class="recommendation-card">
486
+ <h4>βœ… Positive Impact: +${sim_res['gm_delta_value']:.2f} in daily gross profit</h4>
487
+ <p>This strategy would <strong>improve profitability</strong> for this segment.
488
+ Expected price elasticity of {elasticity:.2f} means volume {('decreases' if elasticity < 0 and delta_disc < 0 else 'adjusts')}
489
+ as prices change, but margin improvement outweighs volume impact.</p>
 
 
 
 
490
  </div>
491
  """, unsafe_allow_html=True)
492
  else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493
  st.markdown(f"""
494
+ <div class="warning-card">
495
+ <h4>⚠️ Negative Impact: ${sim_res['gm_delta_value']:.2f} in daily gross profit</h4>
496
+ <p>This strategy would <strong>reduce profitability</strong> for this segment.
497
+ Consider alternative approaches or test smaller adjustments.</p>
 
 
 
 
498
  </div>
499
  """, unsafe_allow_html=True)
500
 
501
+ st.markdown("---")
 
 
 
 
 
 
 
 
 
 
502
 
503
+ # -----------------------------
504
+ # AI RECOMMENDATIONS
505
+ # -----------------------------
506
+ st.markdown("## πŸ’‘ AI-Powered Recommendations")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507
 
508
+ st.markdown("""
509
+ Based on the analysis of all segments, here are the top opportunities to improve profitability.
510
+ These recommendations are ranked by expected financial impact.
511
+ """)
512
+
513
+ # Generate recommendations
514
+ worst_keys = segment_impact.head(20).index.tolist()
515
+ recs = []
516
+ for p, r, c in worst_keys:
517
+ hist = df[(df["product"]==p)&(df["region"]==r)&(df["channel"]==c)].sort_values("date")
518
+ if hist.empty:
519
+ continue
520
+ eps, _ = estimate_segment_elasticity(hist, p, r, c)
521
+ prop_disc_pts = -np.clip(abs(segment_impact[(p,r,c)])*10, 0.5, 3.0)
522
+ sim = simulate_action(hist, eps, delta_discount=prop_disc_pts/100.0, delta_unit_cost=0.0)
523
+ if sim and sim["gm_delta_value"] > 0:
524
+ recs.append({
525
+ "Segment": f"{p} β€’ {r} β€’ {c}",
526
+ "Recommended Action": f"Reduce discount by {abs(prop_disc_pts):.1f}%",
527
+ "Expected Daily Uplift": f"${sim['gm_delta_value']:.2f}",
528
+ "New Margin %": f"{sim['gm1_pct']*100:.1f}%",
529
+ "Risk Level": "Low" if abs(eps) < 0.5 else "Medium"
530
+ })
531
+
532
+ rec_df = pd.DataFrame(recs).sort_values("Expected Daily Uplift", ascending=False).head(10)
533
+
534
+ if not rec_df.empty:
535
+ st.dataframe(rec_df, hide_index=True, use_container_width=True)
536
+
537
+ total_potential = rec_df["Expected Daily Uplift"].str.replace("$", "").astype(float).sum()
538
+ st.success(f"🎯 **Total Daily Profit Opportunity: ${total_potential:.2f}** | Annualized: ${total_potential * 365:,.0f}")
539
+
540
+ # Download button
541
+ csv = rec_df.to_csv(index=False).encode('utf-8')
542
+ st.download_button(
543
+ label="πŸ“₯ Download Full Recommendations (CSV)",
544
+ data=csv,
545
+ file_name=f"profitability_recommendations_{datetime.now().strftime('%Y%m%d')}.csv",
546
+ mime="text/csv"
547
+ )
548
+ else:
549
+ st.info("No high-confidence recommendations available at this time. Current segment performance is well-optimized.")
550
 
551
  st.markdown("---")
552
+
553
+ # Footer
554
+ st.caption("πŸ”’ **Demo Environment** | Data shown is synthetic for demonstration. Connect to your SAP system for live insights.")
555
+ st.caption(f"Model Performance: RΒ² = {metrics['r2']:.3f} | Analyzing {len(df):,} transactions across {len(df['product'].unique())} products")