SECTION 1: LEARNING OBJECTIVES
By the end of this lesson, you will be able to:
-
Define predictive analytics and its role in banking.
-
Identify key use cases for predictive analytics in banking.
-
Apply classification models for customer churn prediction.
-
Apply regression models for customer lifetime value (CLV) prediction.
-
Apply time series forecasting for demand and revenue prediction.
-
Implement propensity modelling for cross-sell and upsell.
-
Measure predictive model performance using appropriate metrics.
-
Develop a predictive analytics strategy for a bank.
SECTION 2: WHAT IS PREDICTIVE ANALYTICS?
2.1 Definition
Predictive analytics is the use of data, statistical algorithms, and machine learning techniques to identify the likelihood of future outcomes based on historical data. In banking, it helps answer questions like:
-
Which customers are likely to churn?
-
What is the lifetime value of a customer?
-
Which customers are likely to respond to an offer?
-
What will be the demand for loans next quarter?
-
Which customers are at risk of default?
2.2 Predictive Analytics Use Cases in Banking
| Use Case | Description | Business Impact |
|---|---|---|
| Customer Churn Prediction | Identify customers likely to leave. | Retention, reduced churn. |
| Customer Lifetime Value | Predict future customer value. | Segmentation, resource allocation. |
| Propensity Modelling | Predict likelihood to buy. | Targeted marketing, cross-sell. |
| Credit Risk Assessment | Predict likelihood of default. | Better risk management. |
| Fraud Detection | Predict fraudulent transactions. | Reduced losses. |
| Demand Forecasting | Predict loan/mortgage demand. | Capacity planning, resource allocation. |
| Next-Best-Action | Predict the best offer for each customer. | Personalisation, revenue growth. |
| Collections Scoring | Predict likelihood of payment. | Optimised collections. |
SECTION 3: KEY PREDICTIVE MODELS
3.1 Classification Models
| Model | Description | Banking Use Case |
|---|---|---|
| Logistic Regression | Binary classification. | Churn prediction, default prediction. |
| Decision Trees | Rule-based classification. | Customer segmentation. |
| Random Forest | Ensemble of decision trees. | Fraud detection, credit scoring. |
| XGBoost | Gradient boosting. | High-performance classification. |
| Neural Networks | Deep learning classification. | Complex pattern recognition. |
3.2 Regression Models
| Model | Description | Banking Use Case |
|---|---|---|
| Linear Regression | Linear relationship. | CLV prediction, revenue forecasting. |
| Ridge/Lasso | Regularised regression. | Feature selection. |
| Random Forest Regression | Ensemble regression. | Demand forecasting. |
| XGBoost Regression | Gradient boosting regression. | High-performance regression. |
3.3 Time Series Models
| Model | Description | Banking Use Case |
|---|---|---|
| ARIMA/SARIMA | Classic time series. | Demand forecasting. |
| Prophet | Facebook’s forecasting tool. | Daily/monthly forecasting. |
| LSTM | Deep learning for sequences. | Complex time series. |
SECTION 4: MODEL EVALUATION METRICS
4.1 Classification Metrics
| Metric | Description | Target |
|---|---|---|
| Accuracy | Overall correctness. | > 80% |
| Precision | Positive predictive value. | > 0.80 |
| Recall | Sensitivity/True Positive Rate. | > 0.80 |
| F1-Score | Harmonic mean of precision and recall. | > 0.80 |
| AUC-ROC | Area under the ROC curve. | > 0.80 |
| KS Statistic | Separation of good and bad. | > 0.30 |
4.2 Regression Metrics
| Metric | Description | Target |
|---|---|---|
| MAE | Mean Absolute Error. | Lower is better. |
| RMSE | Root Mean Squared Error. | Lower is better. |
| MAPE | Mean Absolute Percentage Error. | < 10% |
| R² | Coefficient of determination. | > 0.70 |
SECTION 5: IMPLEMENTATION IN PYTHON – PREDICTIVE ANALYTICS
# =================================================================== # MODULE 4, LESSON 3: PREDICTIVE ANALYTICS IN BANKING # =================================================================== import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor from xgboost import XGBClassifier from sklearn.metrics import (roc_auc_score, classification_report, confusion_matrix, mean_absolute_error, mean_squared_error, r2_score) from datetime import datetime, timedelta import warnings warnings.filterwarnings('ignore') print("="*70) print("PREDICTIVE ANALYTICS IN BANKING") print("="*70) # ---------------------------------------------------------------- # PART A: GENERATE SYNTHETIC CUSTOMER DATA # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART A: Generating Synthetic Customer Data") print("-"*60) np.random.seed(42) n_customers = 5000 # Generate customer features customer_data = pd.DataFrame({ 'customer_id': range(1, n_customers + 1), 'age': np.random.normal(45, 15, n_customers).clip(18, 80).astype(int), 'income': np.random.gamma(5, 20, n_customers) + 20, 'account_balance': np.random.gamma(3, 100, n_customers).clip(0, 50000), 'tenure_months': np.random.gamma(2, 30, n_customers).clip(1, 240).astype(int), 'num_products': np.random.choice([1, 2, 3, 4, 5], n_customers, p=[0.2, 0.3, 0.25, 0.15, 0.1]), 'tx_count': np.random.poisson(15, n_customers).clip(0, 50), 'avg_tx_amount': np.random.lognormal(3, 0.5, n_customers).clip(10, 500), 'app_visits': np.random.poisson(8, n_customers).clip(0, 30), 'complaints': np.random.choice([0, 1, 2], n_customers, p=[0.7, 0.2, 0.1]), 'active': np.random.choice([0, 1], n_customers, p=[0.15, 0.85]) }) # Generate churn based on features churn_prob = 1 / (1 + np.exp(-( -3.0 + 0.01 * customer_data['age'] - 0.005 * customer_data['income'] - 0.0001 * customer_data['account_balance'] - 0.01 * customer_data['tenure_months'] - 0.15 * customer_data['num_products'] - 0.02 * customer_data['tx_count'] + 0.05 * customer_data['avg_tx_amount'] - 0.01 * customer_data['app_visits'] + 0.5 * customer_data['complaints'] - 0.5 * customer_data['active'] ))) customer_data['churn'] = np.random.binomial(1, churn_prob) # Generate CLV based on features customer_data['clv'] = ( 1000 + 50 * customer_data['income']/10 + 10 * customer_data['account_balance']/1000 + 200 * customer_data['num_products'] + 20 * customer_data['tx_count'] + 50 * customer_data['app_visits'] - 100 * customer_data['complaints'] + np.random.normal(0, 200, n_customers) ).clip(100, 10000) print(f"Generated {len(customer_data)} customers") print(f"Churn rate: {customer_data['churn'].mean():.2%}") print(f"Average CLV: ${customer_data['clv'].mean():.2f}") # ---------------------------------------------------------------- # PART B: CHURN PREDICTION (CLASSIFICATION) # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART B: Churn Prediction") print("-"*60) # Features for churn prediction features = ['age', 'income', 'account_balance', 'tenure_months', 'num_products', 'tx_count', 'avg_tx_amount', 'app_visits', 'complaints', 'active'] X = customer_data[features] y = customer_data['churn'] # Train-test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Scale features scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Train XGBoost model xgb_model = XGBClassifier(n_estimators=100, max_depth=6, learning_rate=0.1, random_state=42, use_label_encoder=False, eval_metric='logloss') xgb_model.fit(X_train_scaled, y_train) # Predictions y_pred_proba = xgb_model.predict_proba(X_test_scaled)[:, 1] y_pred = (y_pred_proba >= 0.5).astype(int) # Evaluate auc = roc_auc_score(y_test, y_pred_proba) print(f"Churn Model AUC: {auc:.4f}") print("\nClassification Report:") print(classification_report(y_test, y_pred, target_names=['No Churn', 'Churn'])) # Feature importance importance_df = pd.DataFrame({ 'Feature': features, 'Importance': xgb_model.feature_importances_ }).sort_values('Importance', ascending=False) print("\nTop 5 Churn Predictors:") print(importance_df.head(5).to_string(index=False)) # ---------------------------------------------------------------- # PART C: CUSTOMER LIFETIME VALUE (CLV) PREDICTION (REGRESSION) # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART C: Customer Lifetime Value Prediction") print("-"*60) # Train Random Forest Regressor rf_reg = RandomForestRegressor(n_estimators=100, max_depth=10, random_state=42) rf_reg.fit(X_train_scaled, customer_data.loc[X_train.index, 'clv']) # Predictions y_pred_clv = rf_reg.predict(X_test_scaled) y_actual_clv = customer_data.loc[X_test.index, 'clv'] # Evaluate mae_clv = mean_absolute_error(y_actual_clv, y_pred_clv) rmse_clv = np.sqrt(mean_squared_error(y_actual_clv, y_pred_clv)) r2_clv = r2_score(y_actual_clv, y_pred_clv) print(f"CLV Model Performance:") print(f" MAE: ${mae_clv:.2f}") print(f" RMSE: ${rmse_clv:.2f}") print(f" R²: {r2_clv:.4f}") # Feature importance importance_reg = pd.DataFrame({ 'Feature': features, 'Importance': rf_reg.feature_importances_ }).sort_values('Importance', ascending=False) print("\nTop 5 CLV Predictors:") print(importance_reg.head(5).to_string(index=False)) # Visualise predictions fig, axes = plt.subplots(2, 2, figsize=(14, 10)) # Churn ROC Curve ax = axes[0, 0] from sklearn.metrics import roc_curve fpr, tpr, _ = roc_curve(y_test, y_pred_proba) ax.plot(fpr, tpr, label=f'AUC = {auc:.3f}') ax.plot([0, 1], [0, 1], 'r--') ax.set_xlabel('False Positive Rate') ax.set_ylabel('True Positive Rate') ax.set_title('Churn Model ROC Curve') ax.legend() ax.grid(True, alpha=0.3) # Churn Feature Importance ax = axes[0, 1] importance_df_sorted = importance_df.sort_values('Importance', ascending=True) ax.barh(importance_df_sorted['Feature'].head(8), importance_df_sorted['Importance'].head(8)) ax.set_xlabel('Importance') ax.set_title('Top Churn Predictors') ax.grid(True, alpha=0.3) # CLV Actual vs Predicted ax = axes[1, 0] ax.scatter(y_actual_clv, y_pred_clv, alpha=0.3, s=10) ax.plot([y_actual_clv.min(), y_actual_clv.max()], [y_actual_clv.min(), y_actual_clv.max()], 'r--') ax.set_xlabel('Actual CLV ($)') ax.set_ylabel('Predicted CLV ($)') ax.set_title('CLV: Actual vs Predicted') ax.grid(True, alpha=0.3) # CLV Feature Importance ax = axes[1, 1] importance_reg_sorted = importance_reg.sort_values('Importance', ascending=True) ax.barh(importance_reg_sorted['Feature'].head(8), importance_reg_sorted['Importance'].head(8)) ax.set_xlabel('Importance') ax.set_title('Top CLV Predictors') ax.grid(True, alpha=0.3) plt.tight_layout() plt.savefig('predictive_analytics.png', dpi=300, bbox_inches='tight') plt.show() print("Predictive analytics visualisation saved as 'predictive_analytics.png'") # ---------------------------------------------------------------- # PART D: PROPENSITY MODELLING (CROSS-SELL) # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART D: Propensity Modelling - Cross-Sell Prediction") print("-"*60) # Generate synthetic cross-sell data np.random.seed(42) # Customers with high income and high transaction volume are more likely to accept an offer propensity_score = 1 / (1 + np.exp(-( -2.0 + 0.01 * customer_data['income'] + 0.005 * customer_data['account_balance'] + 0.05 * customer_data['num_products'] + 0.02 * customer_data['tx_count'] + 0.01 * customer_data['app_visits'] ))) customer_data['cross_sell'] = np.random.binomial(1, propensity_score) print(f"Cross-sell rate: {customer_data['cross_sell'].mean():.2%}") # Train propensity model X = customer_data[features] y = customer_data['cross_sell'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Train Random Forest rf_propensity = RandomForestClassifier(n_estimators=100, max_depth=8, random_state=42) rf_propensity.fit(X_train_scaled, y_train) y_pred_propensity = rf_propensity.predict_proba(X_test_scaled)[:, 1] auc_propensity = roc_auc_score(y_test, y_pred_propensity) print(f"Propensity Model AUC: {auc_propensity:.4f}") # Segment customers by propensity score customer_data['propensity_score'] = rf_propensity.predict_proba(scaler.transform(X))[:, 1] customer_data['propensity_segment'] = pd.qcut(customer_data['propensity_score'], q=5, labels=['Very Low', 'Low', 'Medium', 'High', 'Very High']) propensity_summary = customer_data.groupby('propensity_segment').agg({ 'customer_id': 'count', 'cross_sell': 'mean', 'income': 'mean', 'account_balance': 'mean' }).round(2) print("\nPropensity Segments:") print(propensity_summary) # ---------------------------------------------------------------- # PART E: PREDICTIVE ANALYTICS ROADMAP # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART E: Predictive Analytics Roadmap") print("-"*60) roadmap = { "Phase 1 (0-6 months) – Foundation": { "Focus": "Build data foundation and basic models.", "Activities": [ "Establish data pipeline for analytics.", "Build churn prediction model.", "Build CLV prediction model.", "Create basic dashboards." ], "Success Metrics": ["Churn model AUC > 0.75", "CLV model R² > 0.60"] }, "Phase 2 (6-12 months) – Scaling": { "Focus": "Scale predictive models across the organisation.", "Activities": [ "Implement propensity modelling.", "Build credit risk models.", "Deploy models in production.", "Enable real-time predictions." ], "Success Metrics": ["5+ models in production", "Predictive accuracy > 80%"] }, "Phase 3 (12-24 months) – Advanced": { "Focus": "Advanced analytics and integration.", "Activities": [ "Implement next-best-action models.", "Build time series forecasting.", "Integrate with marketing automation.", "Enable personalisation." ], "Success Metrics": ["Personalisation rate > 60%", "Revenue uplift > 10%"] }, "Phase 4 (24+ months) – Innovation": { "Focus": "Innovate with generative AI and advanced analytics.", "Activities": [ "Implement generative AI for insights.", "Build autonomous decisioning.", "Explore reinforcement learning.", "Develop industry-leading capabilities." ], "Success Metrics": ["Industry-leading analytics", "Continuous innovation"] } } for phase, details in roadmap.items(): print(f"\n{phase}:") print(f" Focus: {details['Focus']}") print(" Activities:") for activity in details['Activities']: print(f" • {activity}") print(" Success Metrics:") for metric in details['Success Metrics']: print(f" • {metric}") # ---------------------------------------------------------------- # PART F: SUMMARY AND RECOMMENDATIONS # ---------------------------------------------------------------- print("\n" + "="*70) print("PART F: Summary and Recommendations") print("="*70) print(""" Predictive Analytics in Banking – Key Takeaways: 1. Predictive analytics uses historical data to forecast future outcomes. 2. Key use cases: churn prediction, CLV prediction, propensity modelling, credit risk. 3. Classification models (logistic regression, XGBoost) predict binary outcomes. 4. Regression models (Random Forest, XGBoost) predict continuous values. 5. Key metrics: AUC, precision, recall, RMSE, R². 6. Propensity modelling identifies customers likely to respond to offers. 7. Predictive analytics drives personalisation, retention, and revenue growth. Recommendations: - Start with high-impact use cases (churn, CLV). - Build a data pipeline for predictive analytics. - Deploy models in production for real-time insights. - Integrate predictive models with marketing and sales systems. - Measure and track model performance. - Continuously retrain and improve models. """) print("="*70) print("END OF LESSON 3 – MODULE 4") print("="*70)
SECTION 6: SUMMARY FOR THE DATA PRACTITIONER
-
Predictive analytics uses historical data to forecast future outcomes.
-
Key use cases include customer churn prediction, CLV prediction, propensity modelling, and credit risk assessment.
-
Classification models (logistic regression, XGBoost, Random Forest) predict binary outcomes.
-
Regression models (Random Forest, XGBoost) predict continuous values.
-
Key metrics include AUC, precision, recall, RMSE, and R².
-
Propensity modelling identifies customers likely to respond to offers.
-
Predictive analytics drives personalisation, retention, and revenue growth.
SECTION 7: RECOMMENDED NEXT STEPS
-
Start with high-impact use cases (churn, CLV).
-
Build a data pipeline for predictive analytics.
-
Deploy models in production for real-time insights.
-
Integrate predictive models with marketing and sales systems.
-
Measure and track model performance.
-
Continuously retrain and improve models.
-
Prepare for Lesson 4: AI-Powered Personalisation.
[END OF LESSON 3 – MODULE 4