function Call_Option_Pricing_Plot(varargin)
%copyright by fantuanxiaot
%关注fantuanxiaot的量化分析新浪博客:/u/2029278253
%期权定价作图的帽子图和三角图
%Rate年无风险收益率,K是执行价格,T是到期时间(以年计量)
%Sigma是年波动率,S是基期价格
%N是划分的时间段数
%基于二叉树的期权定价作图
%% Call_Option_Pricing is begin
%参数的输入
S=varargin{1};
K=varargin{2};
T=varargin{3};
Rate=varargin{4};
Sigma=varargin{5};
N=varargin{6};
%单期收益率
r=Rate*T/360/N;
DeltaT=T/N;
%初始化期权价格矩阵
Call_Option_Pricing_Matrix=zeros(N+1);
%在Q测度下的风险中性概率
U=exp(Sigma*sqrt(DeltaT));
D=1/U;
P=(exp(r*DeltaT)-D)/(U-D);
%最后一列的期权价值
for i=1:N+1
Call_Option_Pricing_Matrix(i,N+1)=max(0,S*D^(i-1)*U^(N+1-i)-K);
end
%期权价值向前推移
%Call_Option_Pricing_Matrix形成了期权价格递归矩阵
for j=N:-1:1
for i=1:j
Call_Option_Pricing_Matrix(i,j)=exp(-DeltaT*r)*...
(Call_Option_Pricing_Matrix(i,j+1)*P+Call_Option_Pricing_Matrix(i+1,j+1)*(1-P));
end
end
%% Call_Option_Pricing_Hat_Plot is begin
%期权定价的帽子作图
%形成元胞位置矩阵:position_matrix
figure(1)
set(figure(1),'color','w')
hold on
xlim([-0.1 1.1])
ylim([-0.1 1.1])
for i=1:(N+1)
position_matrix{i,N+1}=[1 (N+1-i)/N];
end
for i=1:N
for j=1:i
position_matrix{j,i}=[(i-1)/N (i+1-j)/(i+1)];
end
end
%形成二叉树基础图
hold on
for i=1:N
for j=1:i
position_1=[position_matrix{j,i};position_matrix{j,i+1}];
[arrowx,arrowy]=arrow_plot(gca,position_1(:,1),position_1(:,2));
annotation('textarrow',arrowx,arrowy,'headwidth',10,'color','m')
position_2=[position_matrix{j,i};position_matrix{j+1,i+1}];
[arrowx,arrowy]=arrow_plot(gca,position_2(:,1),position_2(:,2));
annotation('textarrow',arrowx,arrowy,'headwidth',10,'color','r')
end
end
hold on
%基于Call_Option_Pricing_Matrix来标注期权价值
for i=1:(N+1)
for j=1:i
index=num2str(Call_Option_Pricing_Matrix(j,i));
pos=position_matrix{j,i};
text(pos(1),pos(2)+0.02,index,'fontsize',12,'fontname','Times New Roman');
end
end
title('欧式看涨期权定价的尖尖帽子图','fontsize',16,'fontname','楷体')
ylabel('Call Options Value','fontsize',16,'fontname','Times New Roman')
xlabel('时间分段','fontsize',16,'fontname','楷体')
hold off
%% Call_Option_Pricing_Triangle_Plot is begin
%期权定价的三角形作图
clear position_matrix
figure(2)
set(figure(2),'color','w')
hold on
xlim([-0.1 1.1])
ylim([-0.1 1.1])
%形成元胞位置矩阵:position_matrix
for i=1:(N+1)
position_matrix{i,N+1}=[1 (N+1-i)/N];
end
for i=1:N
high=0.5-(i-1)/N*0.5;
if i==1
delta=0;
else
delta=(1-2*high)/(i-1);
end
for j=1:i
position_matrix{j,i}=[(i-1)/N 1-high-(j-1)*delta];
end
end
%形成二叉树基础图
hold on
for i=1:N
for j=1:i
position_1=[position_matrix{j,i};position_matrix{j,i+1}];
[arrowx,arrowy]=arrow_plot(gca,position_1(:,1),position_1(:,2));
annotation('textarrow',arrowx,arrowy,'headwidth',10,'color','g')
position_2=[position_matrix{j,i};position_matrix{j+1,i+1}];
[arrowx,arrowy]=arrow_plot(gca,position_2(:,1),position_2(:,2));
annotation('textarrow',arrowx,arrowy,'headwidth',10,'color','c')
end
end
hold on
%基于Call_Option_Pricing_Matrix来标注期权价值
for i=1:(N+1)
for j=1:i
index=num2str(Call_Option_Pricing_Matrix(j,i));
pos=position_matrix{j,i};
text(pos(1),pos(2)+0.02,index,'fontsize',12,'fontname','Times New Roman');
end
end
title('欧式看涨期权定价的三角图','fontsize',16,'fontname','楷体')
ylabel('Call Options Value','fontsize',16,'fontname','Times New Roman')
xlabel('时间分段','fontsize',16,'fontname','楷体')
hold off
end
%标注箭头函数
function varargout=arrow_plot(varargin)
if length(varargin{1})== 1 && ishandle(varargin{1}) && ...
strcmp(get(varargin{1},'type'),'axes')
hAx=varargin{1};
varargin=varargin(2:end);
else
hAx = gca;
end;
if length(varargin)==1
pos = varargin{1};
else
[x,y] = deal(varargin{:});
end
%%Get limits
axun = get(hAx,'Units');
set(hAx,'Units','normalized');
axpos = get(hAx,'Position');
axlim = axis(hAx);
axwidth = diff(axlim(1:2));
axheight = diff(axlim(3:4));
if exist('x','var')
varargout{1} = (x-axlim(1))*axpos(3)/axwidth + axpos(1);
varargout{2} = (y-axlim(3))*axpos(4)/axheight + axpos(2);
else
pos(1) = (pos(1)-axlim(1))/axwidth*axpos(3) + axpos(1);
pos(2) = (pos(2)-axlim(3))/axheight*axpos(4) + axpos(2);
pos(3) = pos(3)*axpos(3)/axwidth;
pos(4) = pos(4)*axpos(4)/axheight;
varargout{1} = pos;
end
set(hAx,'Units',axun)
end
matlab 期权图 欧式看涨期权定价作图|MATLAB 程序化交易(量化投资)|MATLAB技术论坛 - Powered by Discuz!...