Example code for trainedmodel.predictfcn

50 views (last 30 days)
Jeremy Grata
Jeremy Grata on 2 Dec 2024
Answered: Abhas on 30 Dec 2024 at 9:12
Is there a way to have matlab generate a script showing the steps the predictfcn of a trained regression model uses to generate predictions? The steps executed when yfit=trainedmodel.predictfcn(x)is called? PCA steps, if included in the model, should be shown also.
For example, a GPR Rational Quadratic model:
trainedModelGPR_RQ.RegressionGP=
RegressionGP
PredictorNames: {1×58 cell}
ResponseName: 'Y'
CategoricalPredictors: []
ResponseTransform: 'none'
NumObservations: 1487
KernelFunction: 'RationalQuadratic'
KernelInformation: [1×1 struct]
BasisFunction: 'Constant'
Beta: 74.872929476342
Sigma: 0.276734525120447
PredictorLocation: [58×1 double]
PredictorScale: [58×1 double]
Alpha: [1487×1 double]
ActiveSetVectors: [1487×58 double]
PredictMethod: 'Exact'
ActiveSetSize: 1487
FitMethod: 'Exact'
ActiveSetMethod: 'Random'
IsActiveSetVector: [1487×1 logical]
LogLikelihood: -6383.13344737248
ActiveSetHistory: []
BCDInformation: []
with
trainedModelGPR_RQ.RegressionGP.KernelInformation.KernelParameters =
1.83516539103131
0.288843383795902
32.3071857452573
where
trainedModelGPR_RQ.RegressionGP.KernelInformation.KernelParameterNames =
3×1 cell array
{'SigmaL' }
{'AlphaRQ'}
{'SigmaF' }
For a new set of predictors, Xnew having a size of m x 58, how are the predictions for Xnew computed (manually, ie not using predict) given informaiton in the model the above? Looking for something in the form of:
ypred=A*f(Xnew)-B, where A and B are some combination of values stored in the trained model and f may be a PCA step on Xnew, if part of the model.
For GPR Rational Quadratic, ignoring any PCA, what are
A and B?

Answers (1)

Abhas
Abhas on 30 Dec 2024 at 9:12
MATLAB doesn't provide a built-in feature to directly generate a script outlining the steps "predictFcn" uses. However, you can manually script the steps using the properties stored in the "RegressionGP" model. This involves reconstructing the prediction workflow based on the parameters in "trainedModel.RegressionGP".
If PCA preprocessing is part of the model, the steps can include:
  1. Standardizing predictors ("PredictorLocation" and "PredictorScale").
  2. Applying PCA transformation (using the PCA coefficients stored in the model).
  3. Computing predictions using the GPR workflow.
Gaussian Process Regression (GPR) predicts a response for a new set of predictors "Xnew"​ using a combination of the kernel function, hyperparameters, and observed data stored in the trained model. The core formula for GPR prediction is:
y_pred = k_new' * inv(K + sigma^2 * I) * y
% y_pred: Predicted response vector (m x 1).
% k_new: Kernel vector between training data and new predictors (n x m).
% K: Kernel matrix of training data (n x n).
% sigma^2: Noise variance.
% I: Identity matrix of size n x n.
% y: Training response vector (n x 1).
You may refer to the below documentation link to know more about the same: https://www.mathworks.com/help/stats/gaussian-process-regression-models.html
For the Rational Quadratic kernel, the kernel function is defined as:
k(x_i, x_j) = sigma_f^2 * (1 + ||x_i - x_j||^2 / (2 * alpha * l^2))^(-alpha)
% sigma_f^2: Signal variance (SigmaF).
% alpha: Shape parameter (AlphaRQ).
% l: Characteristic length scale (SigmaL).
% ||x_i - x_j||^2: Squared Euclidean distance between points x_i and x_j.
From your model:
  • SigmaL=1.83516539103131
  • AlphaRQ=0.288843383795902
  • SigmaF=32.3071857452573
  • Noise variance (σ2) = (0.276734525120447)^2
For GPR Rational Quadratic, there are no explicit coefficients A and B. Instead:
  • A: Corresponds to the kernel vector "k'new∗inv(K+sigma2I)".
  • B: No direct scalar subtraction exists, but the model accounts for shifts in data through standardization.
y_pred = A * f(Xnew)
% f(Xnew) is the transformation after standardization, and A is implicitly defined through the kernel computation.
You may refer the below documentation links to have a better idea on Rational Quadratic Kernel: https://www.mathworks.com/help/stats/kernel-covariance-function-options.html#:~:text=.-,Rational%20Quadratic%20Kernel,-You%20can%20specify
I hope this helps!

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!