Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 4 out of 34 pages
Exam (elaborations)

Stanford CS109 Python Data Analytics Predictive Modeling Master Guide | 2026 update

Document preview thumbnail
Preview 4 out of 34 pages

Stanford CS109 Python Data Analytics Predictive Modeling Master Guide | 2026 update

Content preview

---

# Master Study Guide: Python Data Analytics & Predictive Modeling 2026
### Harvard University — STAT 195 | Stanford University — CS 109
**15-Page Edition | Graduate-Level Reference | Data Science & AI Track**

---

## Preface

This master study guide represents a rigorous, graduate-level synthesis of the most
consequential methodologies in applied data analytics and predictive modeling as practiced
in production-scale commercial environments in 2026. Designed for students enrolled in
Harvard's **STAT 195: Data Science for Business** and Stanford's **CS 109: Probability for
Computer Scientists and Data Scientists**, this guide bridges the intellectual rigor of
Ivy-League statistical theory with the engineering precision demanded by real-world
deployment at scale.

The modern data scientist operating in 2026 must command a unified arsenal: the
mathematical foundations of probabilistic inference and statistical learning theory; the
engineering discipline of production-grade Python implementation; the architectural
understanding of distributed data systems; and the business acumen to translate model
outputs into measurable organizational value. This guide synthesizes all four dimensions
across five substantive domains — customer churn prediction, real-time inventory
optimization, neural network sales forecasting, MLOps and API deployment, and causal
inference — culminating in ten advanced examination questions that demand genuine
analytical synthesis rather than mere recall.

---

# PART I: CUSTOMER CHURN PREDICTION — ADVANCED METHODOLOGY

---

## Chapter 1: Theoretical Foundations and Production Architecture

### 1.1 The Business Mathematics of Customer Churn

**Customer churn** — the cessation of a customer's commercial relationship with a firm —
represents one of the highest-leverage predictive targets in applied data science. The
economic rationale is well-established: customer acquisition costs (CAC) in mature
e-commerce markets typically exceed customer lifetime value (CLV) for the first 6–18
months of the relationship, meaning that each churned customer represents not merely lost
future revenue but a sunk acquisition investment that generates negative return.

The fundamental modeling objective is not churn prediction per se but **expected value
maximization**: given a probabilistic churn score p̂(churn | x) for each customer, and given

,the cost c_intervention of a retention intervention and the expected value v_retained of a
successfully retained customer, the optimal intervention policy is:

Intervene if: p̂(churn | x) × v_retained > c_intervention / p_success(intervention)

This formulation reveals that churn modeling is inherently a **decision-theoretic problem**,
not merely a classification problem. A model with 99% accuracy on a balanced dataset may
generate negative expected value if deployed without this economic framework.

### 1.2 Feature Engineering — The Critical Differentiator

Feature engineering — the transformation of raw transactional data into model-ready
representations — accounts for the majority of predictive performance gains in production
churn models. The **RFM framework** (Recency, Frequency, Monetary) provides a
theoretically grounded foundation:

```python
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

def compute_rfm_features(
transactions: pd.DataFrame,
reference_date: datetime,
customer_id_col: str = "customer_id",
date_col: str = "transaction_date",
value_col: str = "transaction_value"
) -> pd.DataFrame:
"""
Compute RFM (Recency, Frequency, Monetary) features for churn modeling.

Parameters
----------
transactions : pd.DataFrame
Raw transaction log with customer_id, date, and value columns.
reference_date : datetime
Snapshot date for recency calculation (typically today or
end of observation window).

Returns
-------
pd.DataFrame
Customer-level RFM feature matrix.

Notes
-----

, Recency is log-transformed to reduce right-skew typical of
transactional data. Monetary value uses median (robust to outliers)
rather than mean.
"""
rfm = transactions.groupby(customer_id_col).agg(
recency=(date_col, lambda x: (reference_date - x.max()).days),
frequency=(date_col, "count"),
monetary=(value_col, "median")
).reset_index()

# Log-transform recency and monetary to normalize distributions
rfm["log_recency"] = np.log1p(rfm["recency"])
rfm["log_monetary"] = np.log1p(rfm["monetary"])

# Behavioral decay score: high recency + low frequency → high churn risk
rfm["decay_score"] = rfm["log_recency"] / (np.log1p(rfm["frequency"]) + 1e-8)

return rfm


def engineer_behavioral_features(
sessions: pd.DataFrame,
customer_id_col: str = "customer_id"
) -> pd.DataFrame:
"""
Engineer behavioral features from clickstream/session data.

Features include session time, cart abandonment rate,
discount dependency, and engagement trend (slope of
weekly session count over last 90 days).
"""
behavioral = sessions.groupby(customer_id_col).agg(
avg_session_time=("session_duration", "mean"),
cart_abandon_rate=("cart_abandoned", "mean"),
discount_usage_rate=("used_discount", "mean"),
total_sessions=("session_id", "count"),
pages_per_session=("pages_viewed", "mean"),
).reset_index()

# Engagement trend: linear regression slope of weekly sessions
# Negative slope indicates declining engagement → churn signal
def engagement_slope(group):
if len(group) < 3:
return 0.0
weeks = np.arange(len(group))
slope, _ = np.polyfit(weeks, group["session_count"].values, 1)
return slope

, # (Assumes weekly_sessions DataFrame is pre-aggregated)
return behavioral
```

### 1.3 Model Architecture — From Baseline to State-of-the-Art

A rigorous model development process progresses through three tiers:

**Tier 1 — Interpretable Baselines:**

```python
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.metrics import roc_auc_score, classification_report
import xgboost as xgb
import warnings
warnings.filterwarnings("ignore")

def train_baseline_models(
X_train: pd.DataFrame,
y_train: pd.Series,
cv_folds: int = 5
) -> dict:
"""
Train and cross-validate a suite of baseline churn classifiers.

Returns a dictionary of fitted models with their CV AUC scores.
Stratified K-Fold preserves class imbalance ratio across folds.
"""
skf = StratifiedKFold(n_splits=cv_folds, shuffle=True, random_state=42)

models = {
"logistic_regression": LogisticRegression(
C=1.0,
class_weight="balanced", # Handles class imbalance
max_iter=1000,
random_state=42
),
"random_forest": RandomForestClassifier(
n_estimators=300,
max_depth=8,
min_samples_leaf=20,
class_weight="balanced",
random_state=42,
n_jobs=-1
),
"xgboost": xgb.XGBClassifier(

Document information

Uploaded on
May 12, 2026
Number of pages
34
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$17.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
NaireExams
4.1
(50)
Sold
184
Followers
170
Items
2955
Last sold
2 weeks ago


Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions