📈
Your Role
Arjun — Marketing Analyst, Fintech

Your team loses money on low-value customer segments. You have customer transaction data and want to predict lifetime value (LTV) before spending acquisition budget. Linear regression is your first tool to find the pattern.

200
Customer records
8
Features per customer
₹80K–₹260K
LTV range
📥
Sample: customer_ltv.csv
200 rows · customer age, tenure, transactions, balance, LTV in ₹

1️⃣ What Regression Does in One Picture

Linear regression is a line through a cloud of points that minimises total squared distance to the data.

Monthly transactions vs LTV: the best-fit line
Scatter plot of monthly transactions vs lifetime value with fitted regression line
What this shows: Each blue dot is a customer. The orange line minimises the sum of squared distances (red dashed lines). This line lets us predict LTV from any customer's transaction count.

2️⃣ The Equation: y = β₀ + β₁ × x + ε

Every regression model follows this form. Here's what each term means:

Anatomy of the regression equation
y = β₀ + β₁ × x + ε y Predicted LTV (₹) β₀ Intercept (base LTV) β₁ × x Slope × feature (effect size) ε Error term (residual) Example: If β₀=₹50K, β₁=₹2500/txn, and a customer has 40 txns: predicted LTV = ₹50K + ₹2500×40 = ₹150K
Key insight: β₁ (slope) tells you the effect size: how much LTV increases per unit of your feature. If β₁=₹2500, each extra transaction adds ₹2500 to predicted LTV. The error ε is what the model can't explain.

3️⃣ Simple Regression in Pandas

Fit a single-variable model: predict LTV from monthly_txn_count alone.

Python · scipy.stats.linregress

Output:

Metric Value Meaning
Intercept (β₀) ₹45,230 Base LTV when transactions = 0
Slope (β₁) ₹2,487/txn Each extra monthly transaction adds ₹2,487 to LTV
R² value 0.687 Transactions explain 68.7% of LTV variation
P-value 2.3e-31 Relationship is statistically significant (p < 0.001)
What this tells the team: A customer with 50 monthly transactions is worth roughly ₹45,230 + (₹2,487 × 50) = ₹169,670 in lifetime value. This relationship is rock-solid (p < 0.001), so focus acquisition on high-transaction-frequency segments.

4️⃣ Multiple Regression with statsmodels

Now add more features: age, tenure, avg_balance, product_count. Statsmodels gives you detailed diagnostics.

Python · OLS (Ordinary Least Squares)

Key output from .summary():

Feature Coefficient Std Err t-stat P > |t| Interpretation
const (β₀) ₹12,850 4,240 3.03 0.003 Base LTV
monthly_txn_count ₹2,420 85 28.5 <0.001 Each txn → ₹2,420 more LTV (strong)
avg_balance_inr 0.0045 0.0012 3.75 <0.001 ₹1000 balance → ₹4.50 more LTV
tenure_months ₹180 45 4.00 <0.001 Each extra month → ₹180 more LTV
product_count ₹8,950 2,120 4.22 <0.001 Each extra product → ₹8,950 more LTV
age ₹180 92 1.96 0.051 Weak effect; older customers slightly higher LTV
R² = 0.825, F-statistic = 198.4 (p < 0.001), overall model is highly significant
Interpretation: All features except age have p < 0.01 (highly significant). Holding other features constant, each extra transaction adds ₹2,420; each extra product adds ₹8,950. The multi-feature model (R²=0.825) explains 82.5% of LTV variation — much better than transactions alone (68.7%).

5️⃣ R² and Residuals: Measuring Model Fit

R² tells you how much variation your model explains. Residual plots reveal if assumptions are violated.

Predicted vs Actual LTV: a strong model clings to the diagonal
Scatter plot of predicted vs actual LTV showing model fit quality
Residuals scatter: no pattern = good (linear assumption holds)
Residuals scatter plot showing random distribution around zero
What to look for: If R²=0.825, your model captures 82.5% of the variation. The remaining 17.5% is unexplained (noise, unobserved features, randomness). Residual plots should show random scatter around zero with no funnel shape or curves. Patterns = missing non-linear relationships or outliers.

6️⃣ Assumptions & Gotchas

Linear regression is simple but brittle. Break these assumptions and your model fails quietly.

1. Linearity

The relationship between x and y must be linear, not curved or stepwise.

Fix: Check residual plot. If U-shaped, add polynomial terms (x², x³) or use decision trees.

2. No Extreme Outliers

One customer with ₹1M LTV (when typical is ₹150K) can pull the line way off.

Fix: Use robust regression or cap outliers at 99th percentile before fitting.

3. No Perfect Multicollinearity

If two features are 100% correlated (e.g. monthly_txn_count and avg_balance), their coefficients blow up.

Fix: Drop one feature or compute VIF (variance inflation factor). VIF > 5 = multicollinearity.

4. Residuals Roughly Normal

Errors should follow a bell curve, not be skewed or have heavy tails.

Fix: Check Q–Q plot. If errors are non-normal, transform y (log, sqrt) or use robust methods.

The big picture: Linear regression is the starting point. When assumptions break (non-linear relationships, extreme outliers, categorical features), upgrade to decision trees and gradient boosting (next track). They handle complexity you'll find in real fintech, telecom, and healthcare data.

🧠
Knowledge Check
Q1. What does R² = 0.3 tell you?
The model explains 70% of the variation in y.
The model explains 30% of the variation in y; 70% is unexplained.
The correlation between x and y is 0.3.
30% of predictions are correct.
Q2. What does a p-value of 0.001 on a coefficient mean?
There is a 0.1% chance the coefficient is positive.
There is a <0.1% chance the coefficient is zero (we reject null hypothesis).
The coefficient explains 0.1% of the variance.
1 out of every 1000 predictions are wrong.
Q3. Why is multicollinearity a problem in linear regression?
It makes the model too slow to train.
It inflates standard errors and makes coefficients unstable; tiny data changes flip signs/magnitudes.
It prevents the model from converging.
It only affects small datasets (n < 100).
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

Ready to Build Predictive Models?

Join the Best AI Program: The AI-Era Analyst — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Analyst →