2

This is my first attempt to write the covariance function. I have following values,

x = [-1.50 -1.0 -.75 -.40 -.25 0.00]; 
sf = 1.27;
ell = 1;
sn = 0.3;

The formula for squared exponential covariance function is enter image description here

The matlab code for that I have written as :

K = sf^2*exp(-0.5*(squareform(pdist(x)).^2)/ell^2)+(sn)^2*eye(Ntr,Ntr);

where sf is the signal standard deviation, ell the characteristic length scale, sn the noise standard deviation and Ntr the length of the training input data x. But this giving me no result. Is there is any mistake in my coding ?

And once I calculate, I want to summarize into matrix form as shown below , enter image description here

Any help please ?

if x_ = 0.2 then how we can calculate :

a) K_ =[k(x_,x1) k(x_,x2)..........k(x_,xn)] and

b) K__ = k(x_,x_)

Using matlab ?

4

1 回答 1

1

我收到以下错误消息:

Error using  + 
Matrix dimensions must agree.

Error in untitled3 (line 7)
K = sf^2*exp(-0.5*(squareform(pdist(x)).^2)/ell^2)+(sn)^2*eye(Ntr,Ntr);

表明您的矩阵尺寸不一致。如果您分别评估代码的各个部分,您会注意到它pdist(x)返回一个1x0向量。pdist在预期格式的文档中x解释了:

X 的行对应观察值,列对应变量

因此,pdist(x)您应该为 的转置计算它,而不是计算x,即pdist(x.')

K = sf^2*exp(-0.5*(squareform(pdist(x.')).^2)/ell^2)+(sn)^2*eye(Ntr,Ntr);

作为结论,请务必仔细阅读错误消息和文档,尤其是输入参数的预期格式。

子问题

要计算(在您提到的公式中)K的特定值,您可以将给定的公式几乎从字面上转换为 MATLAB:x_x'

K_ = sf^2*exp(-0.5*(x-x_).^2/ell^2)+(sn)^2*(x == x_);

要计算K__,您可以使用上面的公式和设置x = x_,或者您可以将公式简化为:

K__ = sf^2+sn^2;
于 2017-05-20T12:13:07.027 回答