Main Content

discardResiduals

Discard residual information of underlying Cox model

Since R2023a

Description

pdModel = discardResiduals(pdModel) discards residual information of underlying Cox model to reduce memory footprint of the Cox lifetime PD model.

example

Examples

collapse all

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')

Figure contains an axes object. The axes object with title Scatter Grouped by Year Cox, RMSE = 0.0001911, xlabel Year, ylabel PD contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Observed, Cox.

Copyright 2022 The MathWorks, Inc.

Input Arguments

collapse all

Probability of default model, specified as a previously created Cox object using fitLifetimePDModel.

Data Types: object

Output Arguments

collapse all

Updated Cox PD model, returned as a Cox model.

Version History

Introduced in R2023a