Domain: Healthcare

You are the Lead Data Analyst for Barista Coffee, conducting a **Menu Performance & Demographic Segmentation Audit** for the upcoming quarterly board meeting. The regional managers extracted POS (Point of Sale) data across multiple stores into a single CSV (`30k_barista_sales_data(1).csv`), but the POS terminals occasionally record physically impossible values and temporal anomalies.

The dataset is located at `30k_barista_sales_data(1).csv` with columns: `order_date`, `order_time`, `product_category`, `qty`, `unit_price`, `store_id`, `store_location`, `customer_id`, `customer_age`, `customer_gender`, `loyalty_member`, `is_repeat_customer`, `customer_discovery_source`, `payment_method`, `discount_applied`, `total_amount`, `promo_applied`, `promo_type`.

Perform appropriate data cleaning and answer the following 10 questions using only valid, clean records. A valid record must have `unit_price > 0`, `qty > 0`, and `total_amount >= 0`. **Do not round your numeric answers**; provide them in their full precision as outputted by Python/pandas unless a specific format is naturally generated.

---

**Q1.** What is the total quantity (`qty`) of `Cake` sold strictly to `Male` customers who discovered the store via `Social Media`?

**Q2.** Compute the **Customer Yield Index (CYI)** for `Snacks` orders:
$$CYI = \frac{total\_amount \times (1 + discount\_applied)}{qty}$$
What is the average CYI for `Snacks`? (Provide the full precision decimal).

**Q3.** Calculate the **Weekend Premium Value (WPV)** for each order:
- If the order is on a Weekend (Saturday or Sunday), multiply `total_amount` by **1.15**. Otherwise, multiply by **1.00**.
- If the customer is a `loyalty_member`, multiply the result by **1.05**. Otherwise, multiply by **1.00**.
What is the 75th percentile of WPV for orders placed in `Downtown`?

**Q4.** What is the busiest Day of the Week (0=Monday, 6=Sunday) based on total revenue (`total_amount`) across all locations? (Provide the integer day).

**Q5.** Calculate the **Pre-Promo Surge Revenue**. This is the total `total_amount` of all NON-PROMO orders (`promo_applied == False`) that occurred strictly within exactly **45 minutes** BEFORE a promo trigger at the *same* `store_id` and `store_location`. A promo trigger is defined as any order where **EITHER** `promo_applied == True` **OR** `promo_type == 'Flat discount'`. What is the total Surge Revenue?

**Q6.** Run an OLS regression predicting `total_amount` from `qty`, `unit_price`, and `customer_age` for strictly `Female` customers (include a constant/intercept). What is the exact R² value of this model?

**Q7.** What is the median `customer_age` for `Cold Drinks` orders paid via `UPI`?

**Q8.** What is the Pearson correlation coefficient between `qty` and `total_amount` strictly for `Uptown` locations? (Provide the full precision decimal).

**Q9.** What is the raw proportion (as a decimal, not a percentage) of `Walk-in` orders that resulted in a `total_amount` less than $15.00 (`total_amount < 15`)?

**Q10.** Which `store_location` × `customer_discovery_source` combination has the highest average `customer_age`, and what is that value?

Grading rubric:
criterion: Does the response correctly identify that the total quantity of Cake sold to Male customers who discovered the store via Social Media is 700?
weight: 10
criterion: Is the average Customer Yield Index (CYI) for Snacks reported as 18.0195828?
weight: 10
criterion: Is the 75th percentile of the Weekend Premium Value (WPV) for Downtown reported as 41.23875?
weight: 10
criterion: Is the busiest Day of the Week reported as 4?
weight: 10
criterion: Is the total Pre-Promo Surge Revenue reported as 2586.88?
weight: 10
criterion: Is the OLS regression R² for Female customers reported as 0.0003895903534?
weight: 10
criterion: Is the median customer age for Cold Drinks orders paid via UPI reported as 42?
weight: 10
criterion: Is the Pearson correlation coefficient between qty and total_amount for Uptown reported as 0.001764837631?
weight: 10
criterion: Is the proportion of Walk-in orders with total_amount less than $15 reported as 0.2175655368?
weight: 10
criterion: Does the response identify Airport × Friend as the combination with the highest average customer age, with a value of 41.66?
weight: 10

Golden answer:
```python
================================================================================
EXPERT SOLUTION — Menu Performance & Demographic Segmentation Audit
================================================================================

import pandas as pd
import statsmodels.api as sm

# ======================
# DATA LOADING & CLEANING
# ======================
df = pd.read_csv('30k_barista_sales_data(1).csv')

df_clean = df[(df['unit_price'] > 0) & (df['qty'] > 0) & (df['total_amount'] >= 0)].copy()

# Helper conversions (robust to True/False being stored as text)
df_clean['loyalty_bool'] = df_clean['loyalty_member'].astype(str).str.strip().str.lower().eq('true')
df_clean['promo_bool'] = df_clean['promo_applied'].astype(str).str.strip().str.lower().eq('true')

# ======================
# Q1
# ======================
q1 = df_clean[
    (df_clean['product_category'] == 'Cake') &
    (df_clean['customer_gender'] == 'Male') &
    (df_clean['customer_discovery_source'] == 'Social Media')
]['qty'].sum()
print(f"Q1: {q1}")
# >>> 700

# ======================
# Q2
# ======================
df_clean['CYI'] = (df_clean['total_amount'] * (1 + df_clean['discount_applied'].fillna(0))) / df_clean['qty']
q2 = df_clean.loc[df_clean['product_category'] == 'Snacks', 'CYI'].mean()
print(f"Q2: {q2:.7f}")
# >>> 18.0195828

# ======================
# Q3
# ======================
df_clean['order_date_dt'] = pd.to_datetime(df_clean['order_date'])
df_clean['is_weekend'] = df_clean['order_date_dt'].dt.dayofweek >= 5

weekend_mult = df_clean['is_weekend'].map({True: 1.15, False: 1.00})
loyalty_mult = df_clean['loyalty_bool'].map({True: 1.05, False: 1.00})

df_clean['WPV'] = df_clean['total_amount'] * weekend_mult * loyalty_mult
q3 = df_clean.loc[df_clean['store_location'] == 'Downtown', 'WPV'].quantile(0.75)
print(f"Q3: {q3}")
# >>> 41.23875

# ======================
# Q4
# ======================
q4 = df_clean.groupby(df_clean['order_date_dt'].dt.dayofweek)['total_amount'].sum().idxmax()
print(f"Q4: {q4}")
# >>> 4

# ======================
# Q5
# Promo trigger = promo_applied == True OR promo_type == 'Flat discount'
# ======================
df_clean['order_datetime'] = pd.to_datetime(df_clean['order_date'] + ' ' + df_clean['order_time'])
df_clean['is_promo_trigger'] = (df_clean['promo_bool']) | \
    (df_clean['promo_type'].astype(str).str.strip().str.lower().eq('flat discount'))

non_promo = df_clean[~df_clean['promo_bool']].sort_values('order_datetime')
trigger_df = df_clean[df_clean['is_promo_trigger']].sort_values('order_datetime')

surge = pd.merge_asof(
    non_promo,
    trigger_df[['store_id', 'store_location', 'order_datetime']].rename(columns={'order_datetime': 'next_promo_time'}),
    by=['store_id', 'store_location'],
    left_on='order_datetime',
    right_on='next_promo_time',
    direction='forward'
)

surge['time_until_promo'] = surge['next_promo_time'] - surge['order_datetime']
valid_surge = surge[
    (surge['time_until_promo'] > pd.Timedelta(seconds=0)) &
    (surge['time_until_promo'] <= pd.Timedelta(minutes=45))
]
q5 = valid_surge['total_amount'].sum()
print(f"Q5: {q5:.2f}")
# >>> 2586.88

# ======================
# Q6
# ======================
female = df_clean[df_clean['customer_gender'] == 'Female'].dropna(
    subset=['qty', 'unit_price', 'customer_age', 'total_amount']
)
X = sm.add_constant(female[['qty', 'unit_price', 'customer_age']])
y = female['total_amount']
m = sm.OLS(y, X).fit()
print(f"Q6: {m.rsquared:.13f}")
# >>> 0.0003895903534

# ======================
# Q7
# ======================
q7 = df_clean[
    (df_clean['product_category'] == 'Cold Drinks') &
    (df_clean['payment_method'] == 'UPI')
]['customer_age'].median()
print(f"Q7: {int(q7)}")
# >>> 42

# ======================
# Q8
# ======================
uptown = df_clean[df_clean['store_location'] == 'Uptown'].dropna(subset=['qty', 'total_amount'])
q8 = uptown['qty'].corr(uptown['total_amount'])
print(f"Q8: {q8:.12f}")
# >>> 0.001764837631

# ======================
# Q9
# ======================
walkin = df_clean[df_clean['customer_discovery_source'] == 'Walk-in']
q9 = walkin[walkin['total_amount'] < 15].shape[0] / walkin.shape[0]
print(f"Q9: {q9:.10f}")
# >>> 0.2175655368

# ======================
# Q10
# ======================
g = df_clean.groupby(['store_location', 'customer_discovery_source'])['customer_age'].mean()
loc_src = g.idxmax()
q10_val = g.max()
print(f"Q10: {loc_src[0]} x {loc_src[1]} ({q10_val:.2f})")
# >>> Airport x Friend (41.66)
```