discardResiduals
Description
Examples
Discard Residuals for Cox Lifetime PD Model
This example shows how to create a Cox
model and then use discardResiduals
to remove residual information to reduce the model's memory usage.
Load Data
Load the credit portfolio data.
load RetailCreditPanelData.mat
disp(head(data))
ID ScoreGroup YOB Default Year __ __________ ___ _______ ____ 1 Low Risk 1 0 1997 1 Low Risk 2 0 1998 1 Low Risk 3 0 1999 1 Low Risk 4 0 2000 1 Low Risk 5 0 2001 1 Low Risk 6 0 2002 1 Low Risk 7 0 2003 1 Low Risk 8 0 2004
disp(head(dataMacro))
Year GDP Market ____ _____ ______ 1997 2.72 7.61 1998 3.57 26.24 1999 2.86 18.1 2000 2.43 3.19 2001 1.26 -10.51 2002 -0.59 -22.95 2003 0.63 2.78 2004 1.85 9.48
Join the Data
Join the two data components into a single data set.
data = join(data,dataMacro); disp(head(data))
ID ScoreGroup YOB Default Year GDP Market __ __________ ___ _______ ____ _____ ______ 1 Low Risk 1 0 1997 2.72 7.61 1 Low Risk 2 0 1998 3.57 26.24 1 Low Risk 3 0 1999 2.86 18.1 1 Low Risk 4 0 2000 2.43 3.19 1 Low Risk 5 0 2001 1.26 -10.51 1 Low Risk 6 0 2002 -0.59 -22.95 1 Low Risk 7 0 2003 0.63 2.78 1 Low Risk 8 0 2004 1.85 9.48
Partition the Data
Separate the data into training and test partitions.
nIDs = max(data.ID); uniqueIDs = unique(data.ID); rng('default'); % for reproducibility c = cvpartition(nIDs,'HoldOut',0.4); TrainIDInd = training(c); TestIDInd = test(c); TrainDataInd = ismember(data.ID,uniqueIDs(TrainIDInd)); TestDataInd = ismember(data.ID,uniqueIDs(TestIDInd));
Create a Cox Lifetime PD Model
Use fitLifetimePDModel
to create a Cox
model.
pdModel = fitLifetimePDModel(data(TrainDataInd,:),"Cox",... IDVar="ID", AgeVar="YOB", LoanVars="ScoreGroup", ... MacroVars={'GDP','Market'}, ResponseVar="Default"); disp(pdModel)
Cox with properties: ExtrapolationFactor: 1 ModelID: "Cox" Description: "" UnderlyingModel: [1x1 CoxModel] IDVar: "ID" AgeVar: "YOB" LoanVars: "ScoreGroup" MacroVars: ["GDP" "Market"] ResponseVar: "Default" WeightsVar: "" TimeInterval: 1
The Cox pdModel
object uses a noticeable amount of memory.
whos pdModel
Name Size Bytes Class Attributes pdModel 1x1 59001617 risk.credit.pd.Cox
This is because the underlying Cox model stores residual information, and multiple residual types are supported.
head(pdModel.UnderlyingModel.Residuals)
CoxSnell Deviance Martingale Schoenfeld ScaledSchoenfeld Score ScaledScore _________ ________ __________ ________________________ ________________________ ________________ ________________ 0.0092625 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.012537 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.018878 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.026346 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.036303 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.051269 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.038922 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0 0.034104 0 0 NaN NaN NaN NaN NaN NaN NaN NaN 0 0 0 0 0 0 0 0
For additional information on the residuals, see CoxModel
.
Remove Residual Information
For prediction purposes, the residual information can be discarded using discardResiduals
without affecting the prediction or validation functionality of the Cox lifetime PD model.
pdModel = discardResiduals(pdModel)
pdModel = Cox with properties: ExtrapolationFactor: 1 ModelID: "Cox" Description: "" UnderlyingModel: [1x1 CoxModel] IDVar: "ID" AgeVar: "YOB" LoanVars: "ScoreGroup" MacroVars: ["GDP" "Market"] ResponseVar: "Default" WeightsVar: "" TimeInterval: 1
The model storage is minimal once the residuals have been discarded and the Residuals property of the underlying model have been emptied.
whos pdModel
Name Size Bytes Class Attributes pdModel 1x1 9305 risk.credit.pd.Cox
pdModel.UnderlyingModel.Residuals
ans = 0x1 empty table Var1 ____
The prediction and validation functions are not affected after the residuals have been discarded.
pdLifetime = predictLifetime(pdModel,data(1:8,:))
pdLifetime = 8×1
0.0092
0.0143
0.0189
0.0229
0.0265
0.0305
0.0321
0.0330
modelCalibrationPlot(pdModel,data(TrainDataInd,:),'Year')
Copyright 2022 The MathWorks, Inc.
Input Arguments
pdModel
— Probability of default model
Cox
object
Probability of default model, specified as a previously created Cox
object using
fitLifetimePDModel
.
Data Types: object
Output Arguments
pdModel
— Updated Cox PD model
object
Updated Cox PD model, returned as a Cox
model.
Version History
Introduced in R2023a
See Also
modelCalibration
| modelDiscrimination
| modelDiscriminationPlot
| modelCalibrationPlot
| predictLifetime
| fitLifetimePDModel
| Cox
| customLifetimePDModel
Topics
- Basic Lifetime PD Model Validation
- Compare Logistic Model for Lifetime PD to Champion Model
- Compare Lifetime PD Models Using Cross-Validation
- Expected Credit Loss Computation
- Compare Model Discrimination and Model Calibration to Validate of Probability of Default
- Compare Probability of Default Using Through-the-Cycle and Point-in-Time Models
- Overview of Lifetime Probability of Default Models
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)