forked from lawrennd/gp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpPosteriorSample.m
115 lines (96 loc) · 2.57 KB
/
gpPosteriorSample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function f = gpPosteriorSample(kernType, numSamps, params, lims, seed, bw)
% GPPOSTERIORSAMPLE Create a plot of samples from a posterior covariance.
% FORMAT
% DESC creates a plot of samples from a kernel with the given
% parameters and variance.
% ARG kernType : the type of kernel to sample from.
% ARG numSamps : the number of samples to take.
% ARG params : parameter vector for the kernel.
% ARG lims : limits of the x axis.
%
% COPYRIGHT : Neil D. Lawrence, 2008
% GP
global printDiagram
if nargin < 6
bw = false;
if nargin < 5
seed = [];
if nargin < 4
lims = [-3 3];
if nargin < 3
params = [];
if nargin < 2
numSamps = 10;
end
end
end
end
end
if ~isempty(seed)
randn('seed', seed);
rand('seed', seed);
end
t_star = linspace(lims(1), lims(2), 200)';
kern = kernCreate(t_star, kernType);
if isfield(kern, 'comp')
for i=1:length(kern.comp)
kern.comp{i}.transforms = [];
end
end
if ~isempty(params)
feval = str2func([kern.type 'KernExpandParam']);
kern = feval(kern, params);
end
feval = str2func([kern.type 'KernExtractParam']);
[params, names] = feval(kern);
paramStr = [];
for i = 1:length(names)
Name = names{i};
Name(1) = upper(Name(1));
ind = find(Name==' ');
Name(ind+1) = upper(Name(ind+1));
Name(ind) = '';
paramStr = [paramStr Name num2str(params(i))];
end
paramStr(find(paramStr==46)) = 'p';
infoStr = ['Samples' num2str(numSamps) 'Seed' num2str(randn('seed'))];
% Covariance of the prior.
K_starStar = kernCompute(kern, t_star, t_star);
% Generate "training data" from a sine wave.
t = rand(5, 1)*(lims(2)-lims(1))*0.6+lims(1)*0.6;
f = sin(t);
% Compute kernel for training data.
K_starf = kernCompute(kern, t_star, t);
K_ff = kernCompute(kern, t);
% Mean and covariance of posterior.
fbar = K_starf*pdinv(K_ff)*f;
Sigma = K_starStar - K_starf*pdinv(K_ff)*K_starf';
% Sample from the posterior.
fsamp = real(gsamp(fbar, Sigma, numSamps));
% Plot and save
figure
linHand = plot(t_star, fsamp);
hold on
linHandPlot = plot(t, f, 'r.')
set(linHandPlot, 'markersize', 30)
zeroAxes(gca, 0.025, 18, 'times');
set(linHand, 'linewidth', 1)
app = '';
if bw
set(linHand, 'color', [0 0 0])
set(linHandPlot, 'color', [0 0 0])
app = 'bw';
end
if iscell(kernType)
KernType = [];
for i = length(kernType):-1:1
KernType = [kernType{i} KernType];
KernType(1) = upper(KernType(1));
end
else
KernType(1) = upper(kernType(1));
end
if exist('printDiagram', 'var') & printDiagram
printPlot(['gpPosteriorSample' KernType infoStr paramStr app], ...
'../tex/diagrams', '../html')
end