Calculating the matrix K at test inputs after training a Gaussian Process with fitrgp

If I trained a GP using training data D = {X, y} with fitrgp and I obtained my gprMdl:
gprMdl = fitrgp(data.X, data.Y, 'KernelFunction', 'squaredexponential', ...
'BasisFunction', 'none', 'verbose', 1, 'FitMethod', 'exact')
[ystar, ysd, yint] = predict(gprMdl, Xstar)
How can I obtain the matrix K(Xstar, Xstar)? I can not find the subfunction of the RegressionGP that calculates the matrices K.
Thanks

 Accepted Answer

Hi Umberto,
There is an undocumented way of calculating what you want. Here is an example:
rng(0,'twister');
N = 100;
x = linspace(-10,10,N)';
y = 1 + x*5e-2 + sin(x)./x + 0.2*randn(N,1);
gpr = fitrgp(x,y,'FitMethod','Exact','PredictMethod','Exact');
kfcn = gpr.Impl.Kernel.makeKernelAsFunctionOfXNXM(gpr.Impl.ThetaHat)
K = kfcn(x(1:5,:),x(1:7,:))
K(i,j) kernel function evaluated for x(i,:) and x(j,:). For example,
K(3,6)
kfcn(x(3,:),x(6,:))
I would be interested in knowing why you want to compute K.
Hope that helps,
Gautam

7 Comments

Hi Gautam,
thanks for answering. Also, when I predict at test inputs using predict(), it returns ymean, ystd, yint. ystd is the sqrt of the diagonal of the predictive variance-covariance matrix V(fstar | D) at test locations given data. How can I get the whole matrix and not only the diagonal ystd = sqrt(diag(V(fstar | D)))?
My research involves Gaussian Processes and in order to make 'advances' I need the basic elements that go in the predictive formulas :-)
Of course I can calculate everything by myself, and that is what I did until now, but the MATLAB built in functions are much more efficient (obviously)!
Thank you so much again
Very interesting. I'm new to GP and I got the same confuse as you. One way I came up with is to add the prediction value into the GP and calculate the model again, and then withdraw the K matrix the same way Gautam give.
No this didnt work. Please tell me if you have got the way to get what you want. MY MAIL: qiwu@seu.edu.cn
OK I think I found an answer: https://stats.stackexchange.com/questions/133728/posterior-covariance-from-gpml-toolbox
This example works for me (GP built on different data).
@Gautam Pendse does makeKernelAsFunctionOfXNXM produce the prior or posterior covariance matrix? If it's the prior, is there an undocumented function for producing the posterior covariance matrix (other than predictExactWithCov)? (happy to post as a separate question if this is too involved)
I think I'm realizing that what I'm asking for is just the predictExactWithCov function (see Sampling from Posterior Distribution of GPR Model from fitrgp()). I think I misunderstood the "Exact" part of this function. I take it "exact" refers to not using sparse methods rather than assuming no noise in the input data (originally I was under the impression that it was the latter).

Sign in to comment.

More Answers (1)

I have images of 15 plant leaf diseases i have extructed features using GLCM , how can i make a classifier using Gussian , can anyone help lease

Community Treasure Hunt

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

Start Hunting!