Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,6 @@ import pandas as pd
|
|
| 4 |
import plotly.express as px
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
-
import shap
|
| 8 |
-
import matplotlib.pyplot as plt
|
| 9 |
from datetime import datetime, timedelta
|
| 10 |
from sklearn.model_selection import train_test_split
|
| 11 |
from sklearn.compose import ColumnTransformer
|
|
@@ -17,7 +15,6 @@ from sklearn.metrics import r2_score, mean_absolute_error
|
|
| 17 |
import warnings
|
| 18 |
warnings.filterwarnings('ignore')
|
| 19 |
|
| 20 |
-
# Enhanced page config
|
| 21 |
st.set_page_config(
|
| 22 |
page_title="Profitability Intelligence Suite",
|
| 23 |
page_icon="π",
|
|
@@ -25,7 +22,7 @@ st.set_page_config(
|
|
| 25 |
initial_sidebar_state="collapsed"
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# Custom CSS
|
| 29 |
st.markdown("""
|
| 30 |
<style>
|
| 31 |
.main-header {
|
|
@@ -34,7 +31,6 @@ st.markdown("""
|
|
| 34 |
color: #1f77b4;
|
| 35 |
text-align: center;
|
| 36 |
margin-bottom: 0.5rem;
|
| 37 |
-
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 38 |
}
|
| 39 |
.sub-header {
|
| 40 |
font-size: 1.2rem;
|
|
@@ -56,7 +52,6 @@ st.markdown("""
|
|
| 56 |
padding: 1.5rem;
|
| 57 |
margin: 1rem 0;
|
| 58 |
border-radius: 8px;
|
| 59 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.05);
|
| 60 |
}
|
| 61 |
.recommendation-card {
|
| 62 |
background: white;
|
|
@@ -65,31 +60,20 @@ st.markdown("""
|
|
| 65 |
padding: 1.5rem;
|
| 66 |
margin: 1rem 0;
|
| 67 |
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
|
| 68 |
-
transition: transform 0.2s;
|
| 69 |
-
}
|
| 70 |
-
.recommendation-card:hover {
|
| 71 |
-
transform: translateY(-5px);
|
| 72 |
-
box-shadow: 0 8px 20px rgba(0,0,0,0.12);
|
| 73 |
}
|
| 74 |
.positive-impact {
|
| 75 |
color: #28a745;
|
| 76 |
font-weight: 700;
|
| 77 |
font-size: 1.5rem;
|
| 78 |
}
|
| 79 |
-
.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
height: 3rem;
|
| 84 |
-
font-size: 1.1rem;
|
| 85 |
-
font-weight: 600;
|
| 86 |
}
|
| 87 |
</style>
|
| 88 |
""", unsafe_allow_html=True)
|
| 89 |
|
| 90 |
-
# -----------------------------
|
| 91 |
-
# Data Generation
|
| 92 |
-
# -----------------------------
|
| 93 |
@st.cache_data(show_spinner=False)
|
| 94 |
def generate_synthetic_data(days=60, seed=42, rows_per_day=600):
|
| 95 |
rng = np.random.default_rng(seed)
|
|
@@ -171,67 +155,82 @@ def generate_synthetic_data(days=60, seed=42, rows_per_day=600):
|
|
| 171 |
df = pd.DataFrame(records)
|
| 172 |
return df
|
| 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 |
def estimate_segment_elasticity(df, product, region, channel):
|
| 237 |
seg_df = df[(df["product"]==product)&(df["region"]==region)&(df["channel"]==channel)]
|
|
@@ -285,516 +284,365 @@ def simulate_pricing_action(segment_df, elasticity, discount_reduction_pct):
|
|
| 285 |
except:
|
| 286 |
return None
|
| 287 |
|
| 288 |
-
# -----------------------------
|
| 289 |
# Main App
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
st.markdown('<h1 class="main-header">π― Profitability Intelligence Suite</h1>', unsafe_allow_html=True)
|
| 293 |
-
st.markdown('<p class="sub-header">AI-Powered Margin Analysis & Strategic Recommendations</p>', unsafe_allow_html=True)
|
| 294 |
|
| 295 |
# Generate data
|
| 296 |
with st.spinner("π Loading business data..."):
|
| 297 |
df = generate_synthetic_data(days=60, seed=42, rows_per_day=600)
|
| 298 |
-
df_feat, feats_num, feats_cat, target = build_features(df)
|
| 299 |
|
| 300 |
-
# Calculate
|
| 301 |
daily = df.groupby("date").agg(
|
| 302 |
revenue=("revenue","sum"),
|
| 303 |
cogs=("cogs","sum"),
|
| 304 |
-
gm_value=("gm_value","sum")
|
|
|
|
| 305 |
).reset_index()
|
| 306 |
daily["gm_pct"] = np.where(daily["revenue"]>0, daily["gm_value"]/daily["revenue"], 0.0)
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
| 311 |
roll7 = daily["gm_pct"].tail(7).mean()
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
col1, col2, col3, col4 = st.columns(4)
|
| 317 |
|
| 318 |
with col1:
|
| 319 |
-
delta_gm = (today_row["gm_pct"] - yesterday_row["gm_pct"]) * 100
|
| 320 |
st.metric(
|
| 321 |
label="Gross Margin %",
|
| 322 |
-
value=f"{
|
| 323 |
-
delta=f"{
|
| 324 |
delta_color="normal"
|
| 325 |
)
|
| 326 |
|
| 327 |
with col2:
|
| 328 |
-
delta_rev = ((today_row["revenue"] - yesterday_row["revenue"]) / yesterday_row["revenue"] * 100) if yesterday_row["revenue"] > 0 else 0
|
| 329 |
st.metric(
|
| 330 |
-
label="
|
| 331 |
-
value=f"${
|
| 332 |
-
delta=f"{
|
| 333 |
delta_color="normal"
|
| 334 |
)
|
| 335 |
|
| 336 |
with col3:
|
|
|
|
| 337 |
st.metric(
|
| 338 |
-
label="
|
| 339 |
-
value=f"${
|
| 340 |
-
delta=f"
|
| 341 |
delta_color="normal"
|
| 342 |
)
|
| 343 |
|
| 344 |
with col4:
|
| 345 |
-
|
| 346 |
st.metric(
|
| 347 |
-
label="
|
| 348 |
-
value=f"{
|
| 349 |
-
delta=f"{
|
| 350 |
delta_color="normal"
|
| 351 |
)
|
| 352 |
|
| 353 |
-
# Trend
|
| 354 |
-
st.markdown("#### π
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
fig_trends.add_trace(
|
| 375 |
-
go.Scatter(
|
| 376 |
-
x=daily["date"],
|
| 377 |
-
y=daily["revenue"]/1e6,
|
| 378 |
-
name="Revenue",
|
| 379 |
-
line=dict(color="#2ca02c", width=2)
|
| 380 |
-
),
|
| 381 |
-
row=1, col=2
|
| 382 |
)
|
| 383 |
-
|
| 384 |
-
fig_trends.add_trace(
|
| 385 |
-
go.Scatter(
|
| 386 |
-
x=daily["date"],
|
| 387 |
-
y=daily["gm_value"]/1e6,
|
| 388 |
-
name="GM Value",
|
| 389 |
-
line=dict(color="#ff7f0e", width=2, dash="dash")
|
| 390 |
-
),
|
| 391 |
-
row=1, col=2, secondary_y=True
|
| 392 |
-
)
|
| 393 |
-
|
| 394 |
-
fig_trends.update_xaxes(title_text="Date", row=1, col=1)
|
| 395 |
-
fig_trends.update_xaxes(title_text="Date", row=1, col=2)
|
| 396 |
-
fig_trends.update_yaxes(title_text="Gross Margin %", row=1, col=1)
|
| 397 |
-
fig_trends.update_yaxes(title_text="Revenue ($M)", row=1, col=2)
|
| 398 |
-
fig_trends.update_yaxes(title_text="GM Value ($M)", row=1, col=2, secondary_y=True)
|
| 399 |
-
|
| 400 |
-
fig_trends.update_layout(height=400, showlegend=True, hovermode="x unified")
|
| 401 |
-
st.plotly_chart(fig_trends, use_container_width=True)
|
| 402 |
|
| 403 |
st.markdown("---")
|
| 404 |
|
| 405 |
-
#
|
| 406 |
-
with st.spinner("
|
| 407 |
-
|
| 408 |
-
y = df_feat[target].copy()
|
| 409 |
-
pipe, metrics, X_test = train_model(feats_num, feats_cat, target, X, y)
|
| 410 |
-
st.success(f"β
Model trained: RΒ² = {metrics['r2']:.3f}, MAE = {metrics['mae']:.4f}")
|
| 411 |
-
|
| 412 |
-
# Compute SHAP once for all tabs
|
| 413 |
-
with st.spinner("π¬ Analyzing profitability drivers..."):
|
| 414 |
-
shap_df, X_test_sample, feature_names = compute_shap_values(pipe, X_test, feats_num, feats_cat, shap_sample=400)
|
| 415 |
|
| 416 |
-
#
|
| 417 |
-
tab1, tab2, tab3 = st.tabs(["
|
| 418 |
|
| 419 |
with tab1:
|
| 420 |
-
st.markdown("###
|
| 421 |
-
|
|
|
|
| 422 |
<div class="insight-box">
|
| 423 |
-
<b
|
| 424 |
-
|
|
|
|
| 425 |
</div>
|
| 426 |
""", unsafe_allow_html=True)
|
| 427 |
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
"
|
| 435 |
-
"
|
| 436 |
-
"
|
| 437 |
-
"
|
| 438 |
-
"
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
"
|
| 442 |
-
"
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
for feat, val in mean_abs.head(10).items():
|
| 448 |
-
bus_name = feat
|
| 449 |
-
for key, name in business_name_map.items():
|
| 450 |
-
if key == feat:
|
| 451 |
-
bus_name = name
|
| 452 |
-
break
|
| 453 |
-
if feat.startswith("cat__"):
|
| 454 |
-
parts = feat.replace("cat__", "").replace("product_", "").replace("region_", "").replace("channel_", "")
|
| 455 |
-
if "product" in feat.lower():
|
| 456 |
-
bus_name = f"Product: {parts}"
|
| 457 |
-
elif "region" in feat.lower():
|
| 458 |
-
bus_name = f"Region: {parts}"
|
| 459 |
-
elif "channel" in feat.lower():
|
| 460 |
-
bus_name = f"Channel: {parts}"
|
| 461 |
-
top_drivers.append({"Driver": bus_name, "Impact Score": val})
|
| 462 |
-
|
| 463 |
-
drivers_df = pd.DataFrame(top_drivers)
|
| 464 |
-
|
| 465 |
-
col_a, col_b = st.columns([1, 1])
|
| 466 |
-
|
| 467 |
-
with col_a:
|
| 468 |
-
st.markdown("#### Top 10 Profitability Drivers")
|
| 469 |
-
|
| 470 |
-
fig_drivers = go.Figure()
|
| 471 |
-
fig_drivers.add_trace(go.Bar(
|
| 472 |
-
y=drivers_df["Driver"][::-1],
|
| 473 |
-
x=drivers_df["Impact Score"][::-1],
|
| 474 |
-
orientation='h',
|
| 475 |
-
marker=dict(
|
| 476 |
-
color=drivers_df["Impact Score"][::-1],
|
| 477 |
-
colorscale='Blues',
|
| 478 |
-
line=dict(color='rgb(8,48,107)', width=1.5)
|
| 479 |
-
),
|
| 480 |
-
text=[f"{v:.4f}" for v in drivers_df["Impact Score"][::-1]],
|
| 481 |
-
textposition='outside',
|
| 482 |
-
))
|
| 483 |
-
|
| 484 |
-
fig_drivers.update_layout(
|
| 485 |
-
title="Ranked by Average Impact on Gross Margin",
|
| 486 |
-
xaxis_title="Impact Score",
|
| 487 |
-
yaxis_title="",
|
| 488 |
-
height=500,
|
| 489 |
-
showlegend=False
|
| 490 |
-
)
|
| 491 |
-
st.plotly_chart(fig_drivers, use_container_width=True)
|
| 492 |
-
|
| 493 |
-
with col_b:
|
| 494 |
-
st.markdown("#### Key Insights")
|
| 495 |
-
|
| 496 |
-
top_3 = drivers_df.head(3)
|
| 497 |
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
|
|
|
|
|
|
| 504 |
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
|
|
|
| 511 |
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
|
|
|
|
|
|
| 518 |
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
st.markdown("**β
Product-Region-Channel Combinations Boosting Margin:**")
|
| 543 |
-
for _, row in top_positive.head(5).iterrows():
|
| 544 |
-
st.markdown(f"""
|
| 545 |
-
<div class="recommendation-card" style="border-left: 4px solid #28a745; padding: 0.8rem; margin: 0.5rem 0;">
|
| 546 |
-
<b>{row['product']}</b> β’ {row['region']} β’ {row['channel']}<br>
|
| 547 |
-
<small style="color: #28a745;">Cumulative Impact: {row['net_impact']:.4f}</small>
|
| 548 |
-
</div>
|
| 549 |
-
""", unsafe_allow_html=True)
|
| 550 |
-
|
| 551 |
-
# Visualization
|
| 552 |
-
st.markdown("---")
|
| 553 |
-
st.markdown("#### Segment Impact Visualization")
|
| 554 |
-
|
| 555 |
-
fig_segments = px.treemap(
|
| 556 |
-
grp,
|
| 557 |
-
path=['product', 'region', 'channel'],
|
| 558 |
-
values=grp['net_impact'].abs(),
|
| 559 |
-
color='net_impact',
|
| 560 |
-
color_continuous_scale='RdYlGn',
|
| 561 |
-
title="Product-Region-Channel Combinations Impact on Margin"
|
| 562 |
-
)
|
| 563 |
-
fig_segments.update_layout(height=500)
|
| 564 |
-
st.plotly_chart(fig_segments, use_container_width=True)
|
| 565 |
-
|
| 566 |
-
except Exception as e:
|
| 567 |
-
st.warning(f"Unable to compute detailed segment analysis: {str(e)}")
|
| 568 |
-
else:
|
| 569 |
-
st.error("Unable to compute driver analysis. Please check your data.")
|
| 570 |
|
| 571 |
with tab2:
|
| 572 |
-
st.markdown("###
|
| 573 |
st.markdown("""
|
| 574 |
<div class="insight-box">
|
| 575 |
-
<b
|
| 576 |
-
to improve profitability. Recommendations are ranked by expected financial impact.
|
| 577 |
</div>
|
| 578 |
""", unsafe_allow_html=True)
|
| 579 |
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
total_daily_impact = recs_df["Expected GM Uplift"].sum()
|
| 648 |
-
total_annual_impact = recs_df["Annual Impact Estimate"].sum()
|
| 649 |
-
|
| 650 |
-
st.markdown(f"""
|
| 651 |
-
<div class="insight-box" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none;">
|
| 652 |
-
<h3 style="color: white; margin-top: 0;">π Total Opportunity</h3>
|
| 653 |
-
<p style="font-size: 1.3rem; margin: 0.5rem 0;">
|
| 654 |
-
<b>Daily GM Impact:</b> ${total_daily_impact:.2f}
|
| 655 |
-
</p>
|
| 656 |
-
<p style="font-size: 1.6rem; margin: 0.5rem 0;">
|
| 657 |
-
<b>Estimated Annual Impact:</b> ${total_annual_impact/1e6:.2f}M
|
| 658 |
-
</p>
|
| 659 |
-
</div>
|
| 660 |
-
""", unsafe_allow_html=True)
|
| 661 |
-
else:
|
| 662 |
-
st.info("No significant optimization opportunities detected in current data.")
|
| 663 |
-
except Exception as e:
|
| 664 |
-
st.error(f"Error generating recommendations: {str(e)}")
|
| 665 |
-
else:
|
| 666 |
-
st.error("Unable to generate recommendations. Please check your data.")
|
| 667 |
|
| 668 |
with tab3:
|
| 669 |
-
st.markdown("###
|
| 670 |
st.markdown("""
|
| 671 |
<div class="insight-box">
|
| 672 |
-
<b
|
| 673 |
-
the potential impact on revenue, volume, and profitability.
|
| 674 |
</div>
|
| 675 |
""", unsafe_allow_html=True)
|
| 676 |
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 688 |
|
| 689 |
-
|
| 690 |
-
(df["product"]==selected_product) &
|
| 691 |
-
(df["region"]==selected_region) &
|
| 692 |
-
(df["channel"]==selected_channel)
|
| 693 |
-
].sort_values("date")
|
| 694 |
|
| 695 |
-
if
|
| 696 |
-
|
| 697 |
-
current = seg_hist.iloc[-1]
|
| 698 |
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
|
| 711 |
-
st.markdown("
|
|
|
|
|
|
|
| 712 |
|
| 713 |
-
|
| 714 |
-
"
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
step=0.5,
|
| 719 |
-
help="Negative values reduce discount (increase price)"
|
| 720 |
)
|
| 721 |
-
|
| 722 |
-
if discount_change != 0:
|
| 723 |
-
sim = simulate_pricing_action(seg_hist, elasticity, -discount_change)
|
| 724 |
-
|
| 725 |
-
if sim:
|
| 726 |
-
col_res1, col_res2 = st.columns(2)
|
| 727 |
-
|
| 728 |
-
with col_res1:
|
| 729 |
-
comparison_data = pd.DataFrame({
|
| 730 |
-
'Metric': ['Price', 'Volume', 'GM%'],
|
| 731 |
-
'Current': [sim['baseline_price'], sim['baseline_qty'], sim['gm0_pct']*100],
|
| 732 |
-
'Projected': [sim['new_price'], sim['new_qty'], sim['gm1_pct']*100]
|
| 733 |
-
})
|
| 734 |
-
|
| 735 |
-
fig_comp = go.Figure()
|
| 736 |
-
fig_comp.add_trace(go.Bar(
|
| 737 |
-
name='Current',
|
| 738 |
-
x=comparison_data['Metric'],
|
| 739 |
-
y=comparison_data['Current'],
|
| 740 |
-
marker_color='#94a3b8'
|
| 741 |
-
))
|
| 742 |
-
fig_comp.add_trace(go.Bar(
|
| 743 |
-
name='Projected',
|
| 744 |
-
x=comparison_data['Metric'],
|
| 745 |
-
y=comparison_data['Projected'],
|
| 746 |
-
marker_color='#3b82f6'
|
| 747 |
-
))
|
| 748 |
-
|
| 749 |
-
fig_comp.update_layout(
|
| 750 |
-
title="Current vs. Projected Performance",
|
| 751 |
-
barmode='group',
|
| 752 |
-
height=350
|
| 753 |
-
)
|
| 754 |
-
st.plotly_chart(fig_comp, use_container_width=True)
|
| 755 |
-
|
| 756 |
-
with col_res2:
|
| 757 |
-
st.markdown("#### π Simulation Results")
|
| 758 |
-
|
| 759 |
-
gm_change = sim['gm1_pct'] - sim['gm0_pct']
|
| 760 |
-
|
| 761 |
-
st.metric(
|
| 762 |
-
"Gross Margin Impact",
|
| 763 |
-
f"{sim['gm1_pct']*100:.1f}%",
|
| 764 |
-
f"{gm_change*100:+.1f}pp"
|
| 765 |
-
)
|
| 766 |
-
|
| 767 |
-
st.metric(
|
| 768 |
-
"Revenue Impact",
|
| 769 |
-
f"${sim['new_price'] * sim['new_qty']:.2f}",
|
| 770 |
-
f"${sim['revenue_delta']:+.2f}"
|
| 771 |
-
)
|
| 772 |
-
|
| 773 |
-
vol_change = sim['new_qty'] - sim['baseline_qty']
|
| 774 |
-
st.metric(
|
| 775 |
-
"Volume Impact",
|
| 776 |
-
f"{sim['new_qty']:.0f} units",
|
| 777 |
-
f"{vol_change:+.0f} units"
|
| 778 |
-
)
|
| 779 |
-
|
| 780 |
-
st.markdown(f"""
|
| 781 |
-
<div class="insight-box" style="margin-top: 1rem;">
|
| 782 |
-
<b>π° Daily P&L Impact:</b><br>
|
| 783 |
-
<span style="font-size: 1.5rem; {'color: #28a745' if sim['gm_delta_value'] > 0 else 'color: #dc3545'}">
|
| 784 |
-
${sim['gm_delta_value']:+.2f}
|
| 785 |
-
</span>
|
| 786 |
-
</div>
|
| 787 |
-
""", unsafe_allow_html=True)
|
| 788 |
-
else:
|
| 789 |
-
st.info("π Adjust the discount slider above to simulate different pricing strategies")
|
| 790 |
else:
|
| 791 |
-
st.
|
| 792 |
|
| 793 |
st.markdown("---")
|
| 794 |
st.markdown("""
|
| 795 |
-
<div style="text-align: center; color: #666; padding:
|
| 796 |
-
<small>
|
| 797 |
-
π Demo Mode: Using synthetic SAP-style data for illustration purposes
|
| 798 |
-
</small>
|
| 799 |
</div>
|
| 800 |
""", unsafe_allow_html=True)
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
|
|
|
|
|
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from sklearn.model_selection import train_test_split
|
| 9 |
from sklearn.compose import ColumnTransformer
|
|
|
|
| 15 |
import warnings
|
| 16 |
warnings.filterwarnings('ignore')
|
| 17 |
|
|
|
|
| 18 |
st.set_page_config(
|
| 19 |
page_title="Profitability Intelligence Suite",
|
| 20 |
page_icon="π",
|
|
|
|
| 22 |
initial_sidebar_state="collapsed"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Custom CSS
|
| 26 |
st.markdown("""
|
| 27 |
<style>
|
| 28 |
.main-header {
|
|
|
|
| 31 |
color: #1f77b4;
|
| 32 |
text-align: center;
|
| 33 |
margin-bottom: 0.5rem;
|
|
|
|
| 34 |
}
|
| 35 |
.sub-header {
|
| 36 |
font-size: 1.2rem;
|
|
|
|
| 52 |
padding: 1.5rem;
|
| 53 |
margin: 1rem 0;
|
| 54 |
border-radius: 8px;
|
|
|
|
| 55 |
}
|
| 56 |
.recommendation-card {
|
| 57 |
background: white;
|
|
|
|
| 60 |
padding: 1.5rem;
|
| 61 |
margin: 1rem 0;
|
| 62 |
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
.positive-impact {
|
| 65 |
color: #28a745;
|
| 66 |
font-weight: 700;
|
| 67 |
font-size: 1.5rem;
|
| 68 |
}
|
| 69 |
+
.negative-impact {
|
| 70 |
+
color: #dc3545;
|
| 71 |
+
font-weight: 700;
|
| 72 |
+
font-size: 1.5rem;
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
</style>
|
| 75 |
""", unsafe_allow_html=True)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
@st.cache_data(show_spinner=False)
|
| 78 |
def generate_synthetic_data(days=60, seed=42, rows_per_day=600):
|
| 79 |
rng = np.random.default_rng(seed)
|
|
|
|
| 155 |
df = pd.DataFrame(records)
|
| 156 |
return df
|
| 157 |
|
| 158 |
+
def analyze_margin_bridge(df, current_date, prior_date):
|
| 159 |
+
"""
|
| 160 |
+
Professional Price-Volume-Mix (PVM) analysis following FP&A best practices
|
| 161 |
+
Breaks down GM variance into: Price Effect, Volume Effect, Mix Effect, Cost Effect
|
| 162 |
+
"""
|
| 163 |
+
current_data = df[df["date"] == current_date].copy()
|
| 164 |
+
prior_data = df[df["date"] == prior_date].copy()
|
| 165 |
+
|
| 166 |
+
# Calculate totals for both periods
|
| 167 |
+
current_total_revenue = current_data["revenue"].sum()
|
| 168 |
+
current_total_cogs = current_data["cogs"].sum()
|
| 169 |
+
current_total_gm = current_total_revenue - current_total_cogs
|
| 170 |
+
current_gm_pct = current_total_gm / current_total_revenue if current_total_revenue > 0 else 0
|
| 171 |
+
|
| 172 |
+
prior_total_revenue = prior_data["revenue"].sum()
|
| 173 |
+
prior_total_cogs = prior_data["cogs"].sum()
|
| 174 |
+
prior_total_gm = prior_total_revenue - prior_total_cogs
|
| 175 |
+
prior_gm_pct = prior_total_gm / prior_total_revenue if prior_total_revenue > 0 else 0
|
| 176 |
+
|
| 177 |
+
total_gm_variance = current_total_gm - prior_total_gm
|
| 178 |
+
|
| 179 |
+
# Aggregate by segment
|
| 180 |
+
current_seg = current_data.groupby(["product", "region", "channel"]).agg({
|
| 181 |
+
"revenue": "sum",
|
| 182 |
+
"cogs": "sum",
|
| 183 |
+
"qty": "sum",
|
| 184 |
+
"net_price": "mean",
|
| 185 |
+
"unit_cost": "mean"
|
| 186 |
+
}).reset_index()
|
| 187 |
+
current_seg["gm"] = current_seg["revenue"] - current_seg["cogs"]
|
| 188 |
+
current_seg["gm_pct"] = current_seg["gm"] / current_seg["revenue"]
|
| 189 |
+
|
| 190 |
+
prior_seg = prior_data.groupby(["product", "region", "channel"]).agg({
|
| 191 |
+
"revenue": "sum",
|
| 192 |
+
"cogs": "sum",
|
| 193 |
+
"qty": "sum",
|
| 194 |
+
"net_price": "mean",
|
| 195 |
+
"unit_cost": "mean"
|
| 196 |
+
}).reset_index()
|
| 197 |
+
prior_seg["gm"] = prior_seg["revenue"] - prior_seg["cogs"]
|
| 198 |
+
prior_seg["gm_pct"] = prior_seg["gm"] / prior_seg["revenue"]
|
| 199 |
+
|
| 200 |
+
# Merge segments
|
| 201 |
+
merged = pd.merge(
|
| 202 |
+
current_seg,
|
| 203 |
+
prior_seg,
|
| 204 |
+
on=["product", "region", "channel"],
|
| 205 |
+
suffixes=("_curr", "_prior"),
|
| 206 |
+
how="outer"
|
| 207 |
+
).fillna(0)
|
| 208 |
+
|
| 209 |
+
# Price-Volume-Mix Decomposition (industry standard method)
|
| 210 |
+
# Price Effect: (Current Price - Prior Price) Γ Current Volume
|
| 211 |
+
merged["price_effect"] = (merged["net_price_curr"] - merged["net_price_prior"]) * merged["qty_curr"]
|
| 212 |
+
|
| 213 |
+
# Volume Effect: (Current Volume - Prior Volume) Γ Prior Price Γ Prior GM%
|
| 214 |
+
merged["volume_effect"] = (merged["qty_curr"] - merged["qty_prior"]) * merged["net_price_prior"] * merged["gm_pct_prior"]
|
| 215 |
+
|
| 216 |
+
# Cost Effect: -(Current Cost - Prior Cost) Γ Current Volume
|
| 217 |
+
merged["cost_effect"] = -(merged["unit_cost_curr"] - merged["unit_cost_prior"]) * merged["qty_curr"]
|
| 218 |
+
|
| 219 |
+
# Mix Effect: Residual (actual GM change minus price/volume/cost effects)
|
| 220 |
+
merged["gm_variance"] = merged["gm_curr"] - merged["gm_prior"]
|
| 221 |
+
merged["mix_effect"] = merged["gm_variance"] - (merged["price_effect"] + merged["volume_effect"] + merged["cost_effect"])
|
| 222 |
+
|
| 223 |
+
return merged, {
|
| 224 |
+
"total_gm_variance": total_gm_variance,
|
| 225 |
+
"price_effect_total": merged["price_effect"].sum(),
|
| 226 |
+
"volume_effect_total": merged["volume_effect"].sum(),
|
| 227 |
+
"cost_effect_total": merged["cost_effect"].sum(),
|
| 228 |
+
"mix_effect_total": merged["mix_effect"].sum(),
|
| 229 |
+
"current_gm": current_total_gm,
|
| 230 |
+
"prior_gm": prior_total_gm,
|
| 231 |
+
"current_gm_pct": current_gm_pct,
|
| 232 |
+
"prior_gm_pct": prior_gm_pct
|
| 233 |
+
}
|
| 234 |
|
| 235 |
def estimate_segment_elasticity(df, product, region, channel):
|
| 236 |
seg_df = df[(df["product"]==product)&(df["region"]==region)&(df["channel"]==channel)]
|
|
|
|
| 284 |
except:
|
| 285 |
return None
|
| 286 |
|
|
|
|
| 287 |
# Main App
|
| 288 |
+
st.markdown('<h1 class="main-header">π― Daily Profitability Variance Analysis</h1>', unsafe_allow_html=True)
|
| 289 |
+
st.markdown('<p class="sub-header">Understanding What Drives Daily Margin Changes</p>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 290 |
|
| 291 |
# Generate data
|
| 292 |
with st.spinner("π Loading business data..."):
|
| 293 |
df = generate_synthetic_data(days=60, seed=42, rows_per_day=600)
|
|
|
|
| 294 |
|
| 295 |
+
# Calculate daily aggregates
|
| 296 |
daily = df.groupby("date").agg(
|
| 297 |
revenue=("revenue","sum"),
|
| 298 |
cogs=("cogs","sum"),
|
| 299 |
+
gm_value=("gm_value","sum"),
|
| 300 |
+
qty=("qty","sum")
|
| 301 |
).reset_index()
|
| 302 |
daily["gm_pct"] = np.where(daily["revenue"]>0, daily["gm_value"]/daily["revenue"], 0.0)
|
| 303 |
|
| 304 |
+
current_date = daily["date"].max()
|
| 305 |
+
prior_date = current_date - timedelta(days=1)
|
| 306 |
+
current_row = daily[daily["date"]==current_date].iloc[0]
|
| 307 |
+
prior_row = daily[daily["date"]==prior_date].iloc[0]
|
| 308 |
+
week_ago_row = daily.iloc[-8] if len(daily) > 7 else current_row
|
| 309 |
roll7 = daily["gm_pct"].tail(7).mean()
|
| 310 |
|
| 311 |
+
gm_variance_pp = (current_row["gm_pct"] - prior_row["gm_pct"]) * 100
|
| 312 |
+
gm_variance_dollar = current_row["gm_value"] - prior_row["gm_value"]
|
| 313 |
+
|
| 314 |
+
# Executive Dashboard
|
| 315 |
+
st.markdown("### π Executive Summary")
|
| 316 |
|
| 317 |
col1, col2, col3, col4 = st.columns(4)
|
| 318 |
|
| 319 |
with col1:
|
|
|
|
| 320 |
st.metric(
|
| 321 |
label="Gross Margin %",
|
| 322 |
+
value=f"{current_row['gm_pct']*100:.2f}%",
|
| 323 |
+
delta=f"{gm_variance_pp:+.2f}pp",
|
| 324 |
delta_color="normal"
|
| 325 |
)
|
| 326 |
|
| 327 |
with col2:
|
|
|
|
| 328 |
st.metric(
|
| 329 |
+
label="Gross Margin $",
|
| 330 |
+
value=f"${current_row['gm_value']/1e6:.2f}M",
|
| 331 |
+
delta=f"${gm_variance_dollar/1e6:+.2f}M",
|
| 332 |
delta_color="normal"
|
| 333 |
)
|
| 334 |
|
| 335 |
with col3:
|
| 336 |
+
revenue_var_pct = ((current_row["revenue"] - prior_row["revenue"]) / prior_row["revenue"] * 100) if prior_row["revenue"] > 0 else 0
|
| 337 |
st.metric(
|
| 338 |
+
label="Revenue",
|
| 339 |
+
value=f"${current_row['revenue']/1e6:.2f}M",
|
| 340 |
+
delta=f"{revenue_var_pct:+.1f}%",
|
| 341 |
delta_color="normal"
|
| 342 |
)
|
| 343 |
|
| 344 |
with col4:
|
| 345 |
+
volume_var_pct = ((current_row["qty"] - prior_row["qty"]) / prior_row["qty"] * 100) if prior_row["qty"] > 0 else 0
|
| 346 |
st.metric(
|
| 347 |
+
label="Volume (Units)",
|
| 348 |
+
value=f"{current_row['qty']:,.0f}",
|
| 349 |
+
delta=f"{volume_var_pct:+.1f}%",
|
| 350 |
delta_color="normal"
|
| 351 |
)
|
| 352 |
|
| 353 |
+
# Trend chart
|
| 354 |
+
st.markdown("#### π Gross Margin Trend (Last 30 Days)")
|
| 355 |
+
recent_daily = daily.tail(30)
|
| 356 |
+
|
| 357 |
+
fig_trend = go.Figure()
|
| 358 |
+
fig_trend.add_trace(go.Scatter(
|
| 359 |
+
x=recent_daily["date"],
|
| 360 |
+
y=recent_daily["gm_pct"]*100,
|
| 361 |
+
mode='lines+markers',
|
| 362 |
+
name="GM%",
|
| 363 |
+
line=dict(color="#1f77b4", width=3),
|
| 364 |
+
fill='tozeroy',
|
| 365 |
+
fillcolor="rgba(31, 119, 180, 0.1)"
|
| 366 |
+
))
|
| 367 |
+
fig_trend.add_hline(y=roll7*100, line_dash="dash", line_color="red",
|
| 368 |
+
annotation_text="7-Day Average", annotation_position="right")
|
| 369 |
+
fig_trend.update_layout(
|
| 370 |
+
xaxis_title="Date",
|
| 371 |
+
yaxis_title="Gross Margin %",
|
| 372 |
+
height=350,
|
| 373 |
+
hovermode="x unified"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
)
|
| 375 |
+
st.plotly_chart(fig_trend, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
st.markdown("---")
|
| 378 |
|
| 379 |
+
# Perform margin bridge analysis
|
| 380 |
+
with st.spinner("π¬ Performing Price-Volume-Mix analysis..."):
|
| 381 |
+
variance_detail, summary = analyze_margin_bridge(df, current_date, prior_date)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
+
# Main Analysis Tabs
|
| 384 |
+
tab1, tab2, tab3 = st.tabs(["π Margin Bridge (PVM)", "π Segment Deep Dive", "π‘ Pricing Opportunities"])
|
| 385 |
|
| 386 |
with tab1:
|
| 387 |
+
st.markdown(f"### Gross Margin Bridge: {prior_date.strftime('%b %d')} β {current_date.strftime('%b %d')}")
|
| 388 |
+
|
| 389 |
+
st.markdown(f"""
|
| 390 |
<div class="insight-box">
|
| 391 |
+
<b>π Variance Summary:</b><br>
|
| 392 |
+
Gross margin changed by <b>${gm_variance_dollar/1000:+.1f}K</b> ({gm_variance_pp:+.2f} percentage points)<br>
|
| 393 |
+
from {prior_row['gm_pct']*100:.2f}% to {current_row['gm_pct']*100:.2f}%
|
| 394 |
</div>
|
| 395 |
""", unsafe_allow_html=True)
|
| 396 |
|
| 397 |
+
# Waterfall Chart - Professional PVM Analysis
|
| 398 |
+
st.markdown("#### Price-Volume-Mix (PVM) Waterfall Analysis")
|
| 399 |
+
|
| 400 |
+
waterfall_data = pd.DataFrame({
|
| 401 |
+
"Category": [
|
| 402 |
+
f"{prior_date.strftime('%b %d')}<br>Gross Margin",
|
| 403 |
+
"Price<br>Effect",
|
| 404 |
+
"Volume<br>Effect",
|
| 405 |
+
"Cost<br>Effect",
|
| 406 |
+
"Mix<br>Effect",
|
| 407 |
+
f"{current_date.strftime('%b %d')}<br>Gross Margin"
|
| 408 |
+
],
|
| 409 |
+
"Value": [
|
| 410 |
+
summary["prior_gm"],
|
| 411 |
+
summary["price_effect_total"],
|
| 412 |
+
summary["volume_effect_total"],
|
| 413 |
+
summary["cost_effect_total"],
|
| 414 |
+
summary["mix_effect_total"],
|
| 415 |
+
summary["current_gm"]
|
| 416 |
+
],
|
| 417 |
+
"Type": ["absolute", "relative", "relative", "relative", "relative", "total"]
|
| 418 |
+
})
|
| 419 |
+
|
| 420 |
+
fig_waterfall = go.Figure(go.Waterfall(
|
| 421 |
+
orientation="v",
|
| 422 |
+
measure=waterfall_data["Type"],
|
| 423 |
+
x=waterfall_data["Category"],
|
| 424 |
+
y=waterfall_data["Value"],
|
| 425 |
+
text=[f"${v/1000:.1f}K" if abs(v) > 100 else f"${v:.0f}" for v in waterfall_data["Value"]],
|
| 426 |
+
textposition="outside",
|
| 427 |
+
connector={"line": {"color": "rgb(63, 63, 63)"}},
|
| 428 |
+
increasing={"marker": {"color": "#28a745"}},
|
| 429 |
+
decreasing={"marker": {"color": "#dc3545"}},
|
| 430 |
+
totals={"marker": {"color": "#1f77b4"}}
|
| 431 |
+
))
|
| 432 |
+
|
| 433 |
+
fig_waterfall.update_layout(
|
| 434 |
+
title="Gross Margin Variance Breakdown",
|
| 435 |
+
showlegend=False,
|
| 436 |
+
height=450,
|
| 437 |
+
yaxis_title="Gross Margin ($)"
|
| 438 |
+
)
|
| 439 |
+
st.plotly_chart(fig_waterfall, use_container_width=True)
|
| 440 |
|
| 441 |
+
# Explanation of each component
|
| 442 |
+
col_exp1, col_exp2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
+
with col_exp1:
|
| 445 |
+
st.markdown(f"""
|
| 446 |
+
<div class="insight-box">
|
| 447 |
+
<b>π° Price Effect:</b> ${summary['price_effect_total']/1000:+.1f}K<br>
|
| 448 |
+
<small>Impact of changes in average selling prices across all transactions.
|
| 449 |
+
Positive = higher prices captured, Negative = price erosion or higher discounts.</small>
|
| 450 |
+
</div>
|
| 451 |
+
""", unsafe_allow_html=True)
|
| 452 |
|
| 453 |
+
st.markdown(f"""
|
| 454 |
+
<div class="insight-box">
|
| 455 |
+
<b>π¦ Volume Effect:</b> ${summary['volume_effect_total']/1000:+.1f}K<br>
|
| 456 |
+
<small>Impact of selling more or fewer units at prior period margins.
|
| 457 |
+
Positive = higher volumes, Negative = volume decline.</small>
|
| 458 |
+
</div>
|
| 459 |
+
""", unsafe_allow_html=True)
|
| 460 |
|
| 461 |
+
with col_exp2:
|
| 462 |
+
st.markdown(f"""
|
| 463 |
+
<div class="insight-box">
|
| 464 |
+
<b>π Cost Effect:</b> ${summary['cost_effect_total']/1000:+.1f}K<br>
|
| 465 |
+
<small>Impact of changes in unit costs (COGS).
|
| 466 |
+
Positive = cost reduction, Negative = cost inflation.</small>
|
| 467 |
+
</div>
|
| 468 |
+
""", unsafe_allow_html=True)
|
| 469 |
|
| 470 |
+
st.markdown(f"""
|
| 471 |
+
<div class="insight-box">
|
| 472 |
+
<b>π Mix Effect:</b> ${summary['mix_effect_total']/1000:+.1f}K<br>
|
| 473 |
+
<small>Impact of shifts in product, channel, or customer mix.
|
| 474 |
+
Reflects selling relatively more/less of high-margin items.</small>
|
| 475 |
+
</div>
|
| 476 |
+
""", unsafe_allow_html=True)
|
| 477 |
+
|
| 478 |
+
# Key Insight
|
| 479 |
+
dominant_effect = max([
|
| 480 |
+
("Price changes", summary['price_effect_total']),
|
| 481 |
+
("Volume changes", summary['volume_effect_total']),
|
| 482 |
+
("Cost changes", summary['cost_effect_total']),
|
| 483 |
+
("Mix shifts", summary['mix_effect_total'])
|
| 484 |
+
], key=lambda x: abs(x[1]))
|
| 485 |
+
|
| 486 |
+
st.markdown(f"""
|
| 487 |
+
<div class="{'insight-box' if gm_variance_dollar > 0 else 'warning-box'}">
|
| 488 |
+
<b>π― Key Takeaway:</b><br>
|
| 489 |
+
The primary driver of today's margin {'improvement' if gm_variance_dollar > 0 else 'decline'} was
|
| 490 |
+
<b>{dominant_effect[0]}</b>, contributing ${dominant_effect[1]/1000:+.1f}K to the overall variance.
|
| 491 |
+
</div>
|
| 492 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
with tab2:
|
| 495 |
+
st.markdown("### Segment-Level Variance Analysis")
|
| 496 |
st.markdown("""
|
| 497 |
<div class="insight-box">
|
| 498 |
+
<b>π Detailed Breakdown:</b> Which specific product-region-channel combinations drove the margin change?
|
|
|
|
| 499 |
</div>
|
| 500 |
""", unsafe_allow_html=True)
|
| 501 |
|
| 502 |
+
# Top positive and negative contributors
|
| 503 |
+
variance_detail_sorted = variance_detail.sort_values("gm_variance", ascending=False)
|
| 504 |
+
|
| 505 |
+
col_seg1, col_seg2 = st.columns(2)
|
| 506 |
+
|
| 507 |
+
with col_seg1:
|
| 508 |
+
st.markdown("#### π Top 5 Margin Gainers")
|
| 509 |
+
top_gainers = variance_detail_sorted.head(5)
|
| 510 |
+
|
| 511 |
+
for idx, row in top_gainers.iterrows():
|
| 512 |
+
if row["gm_variance"] > 0:
|
| 513 |
+
st.markdown(f"""
|
| 514 |
+
<div class="recommendation-card" style="border-left: 4px solid #28a745;">
|
| 515 |
+
<b>{row['product']}</b><br>
|
| 516 |
+
<small>{row['region']} β’ {row['channel']}</small><br>
|
| 517 |
+
<span class="positive-impact">+${row['gm_variance']:.2f}</span><br>
|
| 518 |
+
<small>
|
| 519 |
+
β’ Price Effect: ${row['price_effect']:+.2f}<br>
|
| 520 |
+
β’ Volume Effect: ${row['volume_effect']:+.2f}<br>
|
| 521 |
+
β’ Cost Effect: ${row['cost_effect']:+.2f}<br>
|
| 522 |
+
β’ Mix Effect: ${row['mix_effect']:+.2f}
|
| 523 |
+
</small>
|
| 524 |
+
</div>
|
| 525 |
+
""", unsafe_allow_html=True)
|
| 526 |
+
|
| 527 |
+
with col_seg2:
|
| 528 |
+
st.markdown("#### π Top 5 Margin Losers")
|
| 529 |
+
top_losers = variance_detail_sorted.tail(5)
|
| 530 |
+
|
| 531 |
+
for idx, row in top_losers.iterrows():
|
| 532 |
+
if row["gm_variance"] < 0:
|
| 533 |
+
st.markdown(f"""
|
| 534 |
+
<div class="recommendation-card" style="border-left: 4px solid #dc3545;">
|
| 535 |
+
<b>{row['product']}</b><br>
|
| 536 |
+
<small>{row['region']} β’ {row['channel']}</small><br>
|
| 537 |
+
<span class="negative-impact">${row['gm_variance']:.2f}</span><br>
|
| 538 |
+
<small>
|
| 539 |
+
β’ Price Effect: ${row['price_effect']:+.2f}<br>
|
| 540 |
+
β’ Volume Effect: ${row['volume_effect']:+.2f}<br>
|
| 541 |
+
β’ Cost Effect: ${row['cost_effect']:+.2f}<br>
|
| 542 |
+
β’ Mix Effect: ${row['mix_effect']:+.2f}
|
| 543 |
+
</small>
|
| 544 |
+
</div>
|
| 545 |
+
""", unsafe_allow_html=True)
|
| 546 |
+
|
| 547 |
+
# Detailed table
|
| 548 |
+
st.markdown("---")
|
| 549 |
+
st.markdown("#### Complete Segment Variance Table")
|
| 550 |
+
|
| 551 |
+
display_variance = variance_detail[[
|
| 552 |
+
"product", "region", "channel", "gm_variance",
|
| 553 |
+
"price_effect", "volume_effect", "cost_effect", "mix_effect"
|
| 554 |
+
]].sort_values("gm_variance", ascending=False)
|
| 555 |
+
|
| 556 |
+
display_variance.columns = [
|
| 557 |
+
"Product", "Region", "Channel", "GM Variance",
|
| 558 |
+
"Price Effect", "Volume Effect", "Cost Effect", "Mix Effect"
|
| 559 |
+
]
|
| 560 |
+
|
| 561 |
+
st.dataframe(display_variance.style.format({
|
| 562 |
+
"GM Variance": "${:,.2f}",
|
| 563 |
+
"Price Effect": "${:,.2f}",
|
| 564 |
+
"Volume Effect": "${:,.2f}",
|
| 565 |
+
"Cost Effect": "${:,.2f}",
|
| 566 |
+
"Mix Effect": "${:,.2f}"
|
| 567 |
+
}).background_gradient(subset=["GM Variance"], cmap="RdYlGn", vmin=-1000, vmax=1000),
|
| 568 |
+
use_container_width=True, height=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
|
| 570 |
with tab3:
|
| 571 |
+
st.markdown("### Pricing Optimization Opportunities")
|
| 572 |
st.markdown("""
|
| 573 |
<div class="insight-box">
|
| 574 |
+
<b>π‘ AI Recommendations:</b> Based on segments with declining margins, here are pricing actions to consider.
|
|
|
|
| 575 |
</div>
|
| 576 |
""", unsafe_allow_html=True)
|
| 577 |
|
| 578 |
+
# Focus on segments with negative GM variance and negative price effects
|
| 579 |
+
problem_segments = variance_detail[
|
| 580 |
+
(variance_detail["gm_variance"] < -50) |
|
| 581 |
+
(variance_detail["price_effect"] < -50)
|
| 582 |
+
].copy()
|
| 583 |
+
problem_segments["priority_score"] = problem_segments["gm_variance"]
|
| 584 |
+
problem_segments = problem_segments.sort_values("priority_score")
|
| 585 |
+
|
| 586 |
+
recs = []
|
| 587 |
+
for _, seg in problem_segments.head(15).iterrows():
|
| 588 |
+
p, r, c = seg["product"], seg["region"], seg["channel"]
|
| 589 |
+
hist = df[(df["product"]==p)&(df["region"]==r)&(df["channel"]==c)].sort_values("date")
|
| 590 |
+
|
| 591 |
+
if hist.empty or len(hist) < 50:
|
| 592 |
+
continue
|
| 593 |
+
|
| 594 |
+
eps, _ = estimate_segment_elasticity(hist, p, r, c)
|
| 595 |
+
discount_reduction = 2.0 # Standard 2pp reduction
|
| 596 |
+
sim = simulate_pricing_action(hist, eps, discount_reduction)
|
| 597 |
+
|
| 598 |
+
if sim and sim["gm_delta_value"] > 0:
|
| 599 |
+
daily_txns = len(hist) / ((hist["date"].max() - hist["date"].min()).days + 1)
|
| 600 |
+
annual_impact = sim["gm_delta_value"] * daily_txns * 365
|
| 601 |
+
|
| 602 |
+
recs.append({
|
| 603 |
+
"Segment": p,
|
| 604 |
+
"Region": r,
|
| 605 |
+
"Channel": c,
|
| 606 |
+
"Yesterday GM Loss": seg["gm_variance"],
|
| 607 |
+
"Root Cause": "Price erosion" if seg["price_effect"] < -30 else "Volume decline" if seg["volume_effect"] < -30 else "Cost increase",
|
| 608 |
+
"Recommended Action": f"Reduce discount from {sim['baseline_discount']:.1f}% to {sim['new_discount']:.1f}%",
|
| 609 |
+
"Expected Daily GM Uplift": sim["gm_delta_value"],
|
| 610 |
+
"Estimated Annual Impact": annual_impact
|
| 611 |
+
})
|
| 612 |
|
| 613 |
+
recs_df = pd.DataFrame(recs).sort_values("Expected Daily GM Uplift", ascending=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 614 |
|
| 615 |
+
if len(recs_df) > 0:
|
| 616 |
+
st.markdown("#### π Top 3 Priority Actions")
|
|
|
|
| 617 |
|
| 618 |
+
for i, (_, rec) in enumerate(recs_df.head(3).iterrows()):
|
| 619 |
+
st.markdown(f"""
|
| 620 |
+
<div class="recommendation-card">
|
| 621 |
+
<h4>#{i+1}: {rec['Segment']} β’ {rec['Region']} β’ {rec['Channel']}</h4>
|
| 622 |
+
<p><b>Yesterday's Performance:</b> Lost ${abs(rec['Yesterday GM Loss']):.2f} in gross margin</p>
|
| 623 |
+
<p><b>Root Cause:</b> {rec['Root Cause']}</p>
|
| 624 |
+
<p><b>Recommended Action:</b> {rec['Recommended Action']}</p>
|
| 625 |
+
<p class="positive-impact">π° Expected Daily Recovery: ${rec['Expected Daily GM Uplift']:.2f}</p>
|
| 626 |
+
<p><small>π Annual Impact Estimate: ${rec['Estimated Annual Impact']/1e3:.1f}K</small></p>
|
| 627 |
+
</div>
|
| 628 |
+
""", unsafe_allow_html=True)
|
| 629 |
|
| 630 |
+
st.markdown("---")
|
| 631 |
+
st.markdown("#### Complete Action Plan")
|
| 632 |
+
st.dataframe(recs_df, use_container_width=True)
|
| 633 |
|
| 634 |
+
st.download_button(
|
| 635 |
+
label="π₯ Download Recommendations (CSV)",
|
| 636 |
+
data=recs_df.to_csv(index=False).encode("utf-8"),
|
| 637 |
+
file_name=f"margin_recovery_plan_{current_date.strftime('%Y%m%d')}.csv",
|
| 638 |
+
mime="text/csv"
|
|
|
|
|
|
|
| 639 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
else:
|
| 641 |
+
st.success("β
All segments performing well. No immediate pricing interventions needed.")
|
| 642 |
|
| 643 |
st.markdown("---")
|
| 644 |
st.markdown("""
|
| 645 |
+
<div style="text-align: center; color: #666; padding: 1rem;">
|
| 646 |
+
<small>π Demo Mode: Using synthetic SAP-style transaction data for illustration</small>
|
|
|
|
|
|
|
| 647 |
</div>
|
| 648 |
""", unsafe_allow_html=True)
|