Sampling from Posterior Distribution of GPR Model from fitrgp()

조회 수: 26 (최근 30일)
Sterling Baird
Sterling Baird 2021년 1월 6일
댓글: Jitin Malhotra 2021년 6월 25일
I can get a gprMdl via:
rng(0,'twister');
N = 100;
x = linspace(-10,10,N)';
y = 1 + x*5e-2 + sin(x)./x + 0.2*randn(N,1);
gprMdl = fitrgp(x,y,'FitMethod','Exact','PredictMethod','Exact');
and the posterior covariance matrix via:
kfcn = gprMdl.Impl.Kernel.makeKernelAsFunctionOfXNXM(gprMdl.Impl.ThetaHat)
K = kfcn(x(1:5,:),x(1:7,:))
How can I go about sampling a model from the posterior distribution of gprMdl?
To clarify, I mean like the curves from the 2nd tile of:
Being able to do this is a matter of being able to finish up a graduate program, so any help is much appreciated!
  댓글 수: 1
Sterling Baird
Sterling Baird 2021년 1월 6일
편집: Sterling Baird 2021년 1월 6일
Perhaps all I need is to use the mean from predict and the covariance matrix from the undocumented function along with mvnrnd, similar to what's described in: https://www.mathworks.com/matlabcentral/answers/442492-sampling-based-on-a-correlation-matrix-for-multiple-parameters
Though I'm not sure if this is sampling from the posterior..
Thoughts?

댓글을 달려면 로그인하십시오.

채택된 답변

Gautam Pendse
Gautam Pendse 2021년 1월 7일
Hi Sterling,
Here's an example illustrating how to sample from the posterior distribution of a GPR model. The code uses an undocumented function predictExactWithCov. If you have categorical predictors, you would need to convert them to dummy variables before using that function.
Hope this helps,
Gautam
function testPosteriorCovarianceGP()
% 1. Some dummy data.
rng(0,'twister');
X = [-10,-8,-5,-1,1,3,7,10]';
N = numel(X);
y = 1 - X*5e-2 + sin(X)./X + 1e-4*randn(N,1);
M = 1000;
Xnew = linspace(-10,10,M)';
% 2. Fit a GPR model.
gpr = fitrgp(X,y,'Sigma',0.01,'SigmaLowerBound',1e-6);
% 3. Access the posterior covariance matrix by calling the undocumented
% predictExactWithCov method on the gpr.Impl object. Vectors ynew can be
% simulated from a Normal distribution with mean pred and covariance covmat
% using cholcov or svd and randn.
alpha = 0.05;
[pred,covmat,ci] = predictExactWithCov(gpr.Impl,Xnew,alpha);
figure(1);
plot(Xnew,pred,'b');
hold on;
plot(Xnew,ci(:,1),'m');
plot(Xnew,ci(:,2),'g');
plot(X,y,'ko','MarkerFaceColor','k');
xlabel('x');
ylabel('y');
figure(2);
T = cholcov(covmat);
numSamples = 50;
ynew = pred + T'*randn(M,numSamples);
plot(Xnew,ynew);
hold on;
plot(X,y,'ko','MarkerFaceColor','k');
xlabel('x');
ylabel('y');
end
  댓글 수: 2
Sterling Baird
Sterling Baird 2021년 1월 14일
편집: Sterling Baird 2021년 1월 14일
What is alpha? (Edit: I just realized it controls the confidence intervals)
Jitin Malhotra
Jitin Malhotra 2021년 6월 25일
@Gautam Pendse, I din't understand what exactly you did and how i should use it on my data. If possible can you please provide more details about this undocumented function? predictExactWithCov(gpr.Impl,Xnew,alpha);
My main motive is to calculate CI_width and CI_var, which is basically the 95% confidence interval width and variance acc. to given formulae
well I got my model as output from regression learner app, How should I move forward on this?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Gaussian Process Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by