在 MatLab 中绘制标准化价格的 3D 图
Plotting a 3D graph of normalized prices in MatLab
我正在做高斯过程,我从给定矩阵计算每年的回归,其中每一行代表一年,所以代码是:
M1 = MainMatrix; %This is the given Matrix
ker =@(x,y) exp(-1013*(x-y)'*(x-y));
[ns, ms] = size(M1);
for N = 1:ns
x = M1(N,:);
C = zeros(ms,ms);
for i = 1:ms
for j = 1:ms
C(i,j)= ker(x(i),x(j));
end
end
u = randn(ms,1);
[A,S, B] = svd(C);
z = A*sqrt(S)*u; % z = A S^.5 u
我想在 Graph 3D 中绘制每个回归,如下所示:
我知道情节是 ribbon
,但我不知道我该怎么做
这是一个 ribbon
图,在 y=0 处有一个额外的表面,可以用 fill3
绘制
无需使用 ribbon
即可生成所需的绘图。只需对所有价格使用 surf
-绘图,对 z=0 处的飞机使用 fill3
-绘图。平面的边界是根据图形的实际限制计算的。因此我们需要在绘制平面之前设置限制。然后只需要一些调整就可以生成几乎相同的外观。
代码如下:
% generate some data
days = (1:100)';
price = days*[0.18,-0.08,0.07,-0.10,0.12,-0.08,0.05];
price = price + 0.5*randn(size(price));
years = 2002+(1:size(price,2));
% prepare plot
width = 0.6;
X = ones(size(price,1),1)*0.5;
X = [-X,X]*width;
figure; hold on;
% plot all 'ribbons'
for i = 1:size(price,2)
h = surf([days,days],X+years(i),[price(:,i),price(:,i)]);
set(h,'MeshStyle','column');
end
% set axis limits
set(gca,'ZLim',[-20,20]);
% plot plane at z=0
limx = get(gca,'XLim');
limy = get(gca,'YLim');
fill3(reshape([limx;limx],1,[]),[flip(limy),limy],zeros(1,4),'g','FaceAlpha',0.2)
% set labels
xlabel('Day of trading')
ylabel('Year')
zlabel('Normalized Price')
% tweak appearance
set(gca,'YTick',years);
set(gca,'YDir','reverse');
view([-38,50])
colormap jet;
grid on;
%box on;
这是结果:
我正在做高斯过程,我从给定矩阵计算每年的回归,其中每一行代表一年,所以代码是:
M1 = MainMatrix; %This is the given Matrix
ker =@(x,y) exp(-1013*(x-y)'*(x-y));
[ns, ms] = size(M1);
for N = 1:ns
x = M1(N,:);
C = zeros(ms,ms);
for i = 1:ms
for j = 1:ms
C(i,j)= ker(x(i),x(j));
end
end
u = randn(ms,1);
[A,S, B] = svd(C);
z = A*sqrt(S)*u; % z = A S^.5 u
我想在 Graph 3D 中绘制每个回归,如下所示:
我知道情节是 ribbon
,但我不知道我该怎么做
这是一个 ribbon
图,在 y=0 处有一个额外的表面,可以用 fill3
无需使用 ribbon
即可生成所需的绘图。只需对所有价格使用 surf
-绘图,对 z=0 处的飞机使用 fill3
-绘图。平面的边界是根据图形的实际限制计算的。因此我们需要在绘制平面之前设置限制。然后只需要一些调整就可以生成几乎相同的外观。
代码如下:
% generate some data
days = (1:100)';
price = days*[0.18,-0.08,0.07,-0.10,0.12,-0.08,0.05];
price = price + 0.5*randn(size(price));
years = 2002+(1:size(price,2));
% prepare plot
width = 0.6;
X = ones(size(price,1),1)*0.5;
X = [-X,X]*width;
figure; hold on;
% plot all 'ribbons'
for i = 1:size(price,2)
h = surf([days,days],X+years(i),[price(:,i),price(:,i)]);
set(h,'MeshStyle','column');
end
% set axis limits
set(gca,'ZLim',[-20,20]);
% plot plane at z=0
limx = get(gca,'XLim');
limy = get(gca,'YLim');
fill3(reshape([limx;limx],1,[]),[flip(limy),limy],zeros(1,4),'g','FaceAlpha',0.2)
% set labels
xlabel('Day of trading')
ylabel('Year')
zlabel('Normalized Price')
% tweak appearance
set(gca,'YTick',years);
set(gca,'YDir','reverse');
view([-38,50])
colormap jet;
grid on;
%box on;
这是结果: