Sampling from Posterior Distribution of GPR Model from fitrgp()

37 ビュー (過去 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 件のコメント
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by