Imagine that I have a weight-of-evidence scorecard that has the following properties. I have an intercept value of -3.15 and three coefficients: -1.01, -1.00, -0.93. I would like to convert my model predictions in logit form to scorecard points (ideally maximum of 100 points).
Unlike typical scorecards where more points is better, I want to do the opposite. In case a customer received more points, I would be able to say that 30 points because feature A is bad, another 40 points is because feature B is bad, and remaining 30 are due to feature 3. This would be similar to Shapley values where total prediction is decomposed into elements.
How can I achieve this preferably in Python or even SQL?
I use formula ratio goods to ratio bads in my WOE formula such that a WOE of -3 would correspond to higher risk than on average.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Converting logit model predictions to a custom scorecard with inverted scoring (more points indicating higher risk) and decomposing the score into contributions from each feature, similar to Shapley values, involves several steps. I'll outline the process and provide examples in Python, as it's more versatile for such calculations than SQL. If you must integrate this with SQL, you can compute the logit scores in Python and then store the results in your database.
Step 1: Understanding the Logit Model
Your logit model predicts probabilities (P) of an event (e.g., default, non-payment) using the logistic function:
[ P = \frac{1}{1 + e^{-z}} ]
where ( z ) is the linear combination of your features (( X )) with coefficients (( \beta )), plus an intercept (( \beta_0 )):
[ z = \beta_0 + \beta_1X_1 + \beta_2X_2 + \beta_3X_3 ]
Given:
( \beta_0 = -3.15 )
( \beta_1 = -1.01 )
( \beta_2 = -1.00 )
( \beta_3 = -0.93 )
Step 2: Converting Logit to Custom Scorecard Points
To convert logit scores to a 0-100 point system where higher points indicate higher risk, you'll first need to decide on a mapping strategy. A common approach is to map the entire range of possible logit scores to your 0-100 scale. Since logit scores range from negative infinity to positive infinity, you'll need to define a practical range based on your data or assumptions.
Assumption for Mapping:
For simplicity, let's assume the 1st percentile of logit scores maps to 0 points (very low risk) and the 99th percentile maps to 100 points (very high risk). You would typically calculate these percentiles from your dataset's predicted logit scores.
Calculating Mapping Bounds:
To determine min_logit and max_logit, follow these steps:
Predict Logit Scores: Use your model to predict logit scores for your dataset.
Calculate Percentiles: Compute the 1st and 99th percentiles of these logit scores. These will serve as min_logit and max_logit, respectively.
Example:
python
import numpy as np
# Assuming 'logit_scores' is an array of predicted logit scores for your dataset
min_logit = np.percentile(logit_scores, 1) # 1st percentile
max_logit = np.percentile(logit_scores, 99) # 99th percentile
Example Python Code for Mapping and Decomposition:
python
import numpy as np
from scipy.special import expit
def logit_to_score(logit_score, min_logit=-6, max_logit=-2):
"""
Maps a logit score to a 0-100 point system.
Parameters:
- logit_score: The logit score to be mapped.
- min_logit: The minimum logit score (maps to 0 points).
- max_logit: The maximum logit score (maps to 100 points).
Returns:
- score: The mapped score between 0 and 100.
"""
# Map logit score range to 0-100
score = ((logit_score - min_logit) / (max_logit - min_logit)) * 100
# Ensure score is within bounds
return np.clip(score, 0, 100)
def predict_logit(intercept, coefficients, features):
"""
Predicts the logit score for given features.
Parameters:
- intercept: The model intercept.
- coefficients: Array of coefficients for each feature.
- features: Array of feature values.
Returns:
- logit_score: The predicted logit score.
"""
return intercept + np.sum(coefficients * features)
def decompose_score(intercept, coefficients, features, min_logit, max_logit):
"""
Decomposes the total score into feature contributions.
Parameters:
- intercept: The model intercept.
- coefficients: Array of coefficients for each feature.
- features: Array of feature values.
- min_logit: Minimum logit score for mapping.
- max_logit: Maximum logit score for mapping.
Returns:
- total_score: The total risk score.
- feature_scores: List of contributions from each feature.
"""
# Predict logit score
logit_score = predict_logit(intercept, coefficients, features)
# Calculate total score
total_score = logit_to_score(logit_score, min_logit, max_logit)
# Initialize feature contributions
feature_scores = []
# Calculate contribution of each feature
for i, (coef, feat) in enumerate(zip(coefficients, features)):
# Predict logit score with only this feature
feat_logit_score = predict_logit(intercept, coefficients, np.zeros_like(features))
feat_logit_score += coef * feat
# Map to score and calculate contribution
feat_score = logit_to_score(feat_logit_score, min_logit, max_logit)
contribution = feat_score - logit_to_score(intercept, min_logit, max_logit) # Adjust for intercept
feature_scores.append(contribution)
# Adjust contributions to sum up to the total score
residual = total_score - sum(feature_scores)
feature_scores = [score + residual / len(features) for score in feature_scores]
return total_score, feature_scores
# Example usage
intercept = -3.15
coefficients = np.array([-1.01, -1.00, -0.93])
features = np.array([1, 1, 1]) # Example feature values
min_logit, max_logit = -6, -2 # Example bounds
total_score, feature_scores = decompose_score(intercept, coefficients, features, min_logit, max_logit)
print(f"Total Score: {total_score:.2f}")
for i, score in enumerate(feature_scores):
print(f"Feature {i+1} Contribution: {score:.2f}")
Notes:
Mapping Bounds (min_logit and max_logit): These bounds are critical as they define the range of logit scores that will be mapped to the 0-100 scale. Calculating them from the 1st and 99th percentiles of your dataset's logit scores ensures that the scorecard reflects the data's distribution.
Feature Contributions: This method approximates feature contributions similarly to Shapley values but with a simplification for direct calculation from logit scores. For true Shapley values, consider using libraries like shap.
SQL Integration: If you need to integrate this with SQL, compute the scores and feature contributions in Python, then insert the results into your database. This ensures that the complex calculations are handled efficiently in Python while leveraging SQL for data storage and querying.