fantuanxiaot的新年大礼:一些MATLAB代码、各类学习资料、书籍等。 祝大家新年快乐!
其余请见藤椅、地板、板凳、报纸、8楼、10楼等本页的楼层
我原来的一些资源帖子:[原创]MATLAB提取整数多项式
[原创]MATLAB网页爬取我偶像的若干图片
[原创]将离歌版主的风险收益做了个gui
[原创]太阳出来爬山gif图
[原创]期权定价的二叉树和三叉树图
[原创]欧式期权三角和核弹头作图
[原创]马克维茨边界作图
[原创]遗传算法模拟退火
更多精彩请见:量化投资,离歌量化,各类软件实战
一,资料与文献分享、神经网络、配对交易、HMM等
[hide]
[/hide]
二,MATLAB的代码分享
一个图形的复制技术:by fantuanxiaot
[hide]
- %copyright by fantuanxiaot
- %基于matlab的图片复制
- %一个图片复制动态展示
- %看看图片一是如何复制到图片二的
- clc
- clear all
- close all
- %设置一个图1
- %图1的句柄
- h1=figure(1);
- t=0:pi/10:2*pi;
- r=2;
- x=r*exp(i*t);
- s='g';
- plot(x,s,'linewidth',2)
- hold on
- plot(x,'rp','markersize',10)
- xlabel('Real')
- ylabel('Image')
- positions=get(h1,'position');
- %图2的句柄
- h2=figure(2);
- set(h2,'position',[positions(1)-300,positions(2),positions(3),positions(4)])
- set(h1,'position',[positions(1)+300,positions(2),positions(3),positions(4)])
- pause(1)
- %图1的轴句柄
- h1_axes=findobj(h1,'type','axes');
- h2_axes=copyobj(h1_axes,h2);
- %这样图1就复制到了图2
复制代码[/hide]
一个图形让X轴标注并且使得文字倾斜:by faruto
[hide] - %copyright by faruto and fantuanxiaot
- %一个让x轴和y轴标注倾斜的窍门
- clc
- close all
- clear all
- figure('color',[0.5 1 1],'units','normal','position',[0 0 0.4 0.5])
- movegui(gcf,'center')
- plot(magic(16))
- %获得句柄
- current_axes=gca;
- strx=get(current_axes,'XTickLabel');
- stry=get(current_axes,'YTickLabel');
- x=get(current_axes,'XTick');
- y=get(current_axes,'YTick');
- yl=ylim(current_axes);
- xl=xlim(current_axes);
- %句柄设置
- set(current_axes,'XTickLabel',[]);
- set(current_axes,'YTickLabel',[]);
- %使之倾斜
- xtoy=zeros(1,length(x))+yl(1)-(max(yl)-min(yl))/20;
- ytox=zeros(1,length(y))+xl(1)-(max(xl)-min(xl))/20;
- text(x,xtoy,strx,'rotation',40,'HorizontalAlignment'...
- ,'center','FontName','times','FontSize',16,'backgr','g');
- text(ytox,y,stry,'rotation',-40,'HorizontalAlignment'...
- ,'center','FontName','times','FontSize',16,'backgr','m');
复制代码[/hide] 一些图形大家自己看着吧:by fantuanxiaot
[hide]
- %各类养眼的作图
- %大家拿去自己练习练习
- %copyright by fantuanxiaot
- clc
- clear all
- close all
- format compact
- figure('color',[0.5 1 1],'position',[100 80 1200 580])
- %%右嵌入的作图
- subplot(2,3,1)
- patch('xdata',[ones(1,11)*5 ones(1,11)*10],'ydata',[0:10 10:-1:0],...
- 'facecolor',[1 0.9 0.9],'edgecolor','g')
- hold on
- plot(1:10,'color',[0.7 0 1])
- %上左边
- legend('patch plot','straight line',2)
- set(gca,'fontname','Times New Roman')
- %%左嵌入的作图
- subplot(2,3,2)
- patch('xdata',[ones(1,11)*0 ones(1,11)*5],'ydata',[10:-1:0 0:10],...
- 'facecolor',[1 0.9 0.9],'edgecolor','g')
- hold on
- plot(1:10,'color',[0.7 0 1])
- %上右边
- legend('patch plot','straight line',1)
- set(gca,'fontname','Times New Roman')
- %%两边嵌入的作图
- subplot(2,3,3)
- patch('xdata',[ones(1,11)*0 ones(1,11)*4.99],'ydata',[10:-1:0 0:10],...
- 'facecolor',[1 0.9 0.9],'edgecolor','g')
- hold on
- patch('xdata',[ones(1,11)*5.01 ones(1,11)*10],'ydata',[0:10 10:-1:0],...
- 'facecolor',[1 1 0],'edgecolor','g')
- plot(1:10,'color',[0.7 0 1],'linewidth',1)
- %上右边
- legend('patch plot 1','patch plot 2','straight line',1)
- set(gca,'fontname','Times New Roman')
- %%做一个多维色彩的bar图形
- subplot(2,3,4)
- y=randn(1,10);
- h_bar=bar(y);
- h_bar_children=get(h_bar,'Children');
- %每一行代表四个点的坐标
- h_matrix=get(h_bar_children,'Faces');
- %每一个坐标代表
- color_matrix=get(h_bar_children,'FaceVertexCData');
- %对每一行h_matrix,也就是一个方块,通过color_matrix取出4个点的坐标,其对应的颜色在color_matrix中,将4个点的颜色
- %信息均修改为more_color
- for more_color=1:length(y)
- color_matrix(h_matrix(more_color,:))=more_color;
- end
- set(h_bar_children,'FaceVertexCData',color_matrix)
- legend('bar plot')
- set(gca,'fontname','Times New Roman')
- colorbar
- %%做一个bar图形,上面与下面的颜色不同
- clear all
- subplot(2,3,5)
- y=randn(1,100);
- y=sort(y);
- h_bar=bar(y);
- h_bar_children=get(h_bar,'Children');
- %每一行代表四个点的坐标
- h_matrix=get(h_bar_children,'Faces');
- %每一个坐标代表
- color_matrix=get(h_bar_children,'FaceVertexCData');
- %上下颜色不同
- big_index=find(y>=0);
- small_index=find(y<0);
- color_matrix(h_matrix(small_index,:))=8;
- color_matrix(h_matrix(big_index,:))=4;
- set(h_bar_children,'FaceVertexCData',color_matrix,'edgecolor','none')
- legend('bar plot')
- set(gca,'fontname','Times New Roman')
- %%另一类bar图形,区分收益率的正负
- subplot(2,3,6)
- %模拟的收益率
- N=100;
- c=normrnd(0,1,1,100);
- index_big=find(c>=0);
- index_small=find(c<0);
- index_date_num=1:N;
- bar(index_date_num(index_big),c(index_big),'facecolor','g','edgecolor','g')
- hold on
- bar(index_date_num(index_small),c(index_small),'facecolor','r','edgecolor','r')
- set(gca,'fontname','Times New Roman')
- legend('bar plot')
- clear all
复制代码[/hide]
如何用MATLAB作图跟踪自己的动态权益收益:by fantuanxiaot [hide]
- %用matlab如何记录自己的收益与回报
- %正的收益红色,负的收益绿色
- %copyright by fantuanxiaot
- %准备工作
- clc
- clear all
- close all
- format compact
- figure(1)
- set(figure(1),'color',[0.5 1 1])
- set(figure(1),'position',[1 1 800 530])
- movegui(figure(1),'center')
- set(gca,'units','normalized','position',[0.15,0.2,0.7,0.7])
- %取最近100天的dynamic return
- N=100;
- %记录收益标准的天数
- M=20;
- %收益率的准备
- single_return=normrnd(0,0.07,1,N);
- date_num=today-N+1:today;
- date_str=datestr(date_num,29);
- date_cellstr=cellstr(date_str);
- %做柱状图
- x_index=1:N;
- one_index=find(single_return>=0);
- two_index=find(single_return<0);
- bar(x_index(one_index),single_return(one_index),...
- 'facecolor','r','edgecolor','r')
- hold on
- bar(x_index(two_index),single_return(two_index),...
- 'facecolor','g','edgecolor','g')
- title('Follow Your Return Day by Day','fontname',...
- 'Times','fontsize',14)
- set(gca,'xtick',[])
- set(gca,'xticklabel',[])
- y_range=ylim(gca);
- %重构坐标
- y_high=zeros(1,M)+min(y_range)-(range(y_range))/30;
- x_index=ceil((1:M)./M*N);
- str=date_cellstr(x_index);
- text(x_index,y_high,str,'rotation',-40,'HorizontalAlignment'...
- ,'left','FontName','times','FontSize',10,'vertical','bottom');
- set(gca,'fontname','Times','fontsize',14)
- y_index=linspace(min(y_range),0,10)';
- hold on
- plot(repmat(x_index,length(y_index),1),...
- repmat(y_index,1,length(x_index)),'b--','linewidth',1)
- clear all
复制代码
[/hide] 基于常方差弹性期权定价模型的比较静态分析(常方差弹性期权定价模型见Hull的《期权期货》):by fantuanxiaot
[hide]
- function Gev_Option_Pricing_Plot()
- %copyright by fantuanxiaot
- %基于常方差弹性欧式看涨期权定价模型的二维和三维比较静态分析
- %S是期权的基期价格,K为行权价格,T是到期时间,O是波动参数
- %R是年连续无风险利率,Q是年连续红利率,A是常方差弹性模型的alpha参数
- %参数的设置
- S=10;
- K=12;
- T=1;
- O=0.16;
- R=0.04;
- Q=0.01;
- A=0.95;
- %% 基于常方差弹性欧式看涨期权定价模型的二维比较分析图
- %subplot(2,2,1)
- figure(1)
- set(figure(1),'color','w')
- subplot(2,2,1)
- hold on
- k=11:0.2:13;
- s=9:0.5:10;
- for i=1:length(k)
- c1(i)=Cev_Call_Option_Pricing(s(1),k(i),T,O,R,Q,A);
- c2(i)=Cev_Call_Option_Pricing(s(2),k(i),T,O,R,Q,A);
- c3(i)=Cev_Call_Option_Pricing(s(3),k(i),T,O,R,Q,A);
- end
- c=[c1;c2;c3];
- minc=min(min(c));
- maxc=max(max(c));
- xlim([k(1)-0.1 k(end)+0.1])
- ylim([minc-0.1 maxc+0.1])
- plot(k,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
- hold on
- plot(k,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
- plot(k,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
- xlabel('执行价格','FontName','楷体','FontSize',16);
- ylabel('期权价格','FontName','楷体','FontSize',16);
- title('常方差弹性欧式看涨期权定价和执行价格的关系','FontName','楷体','FontSize',16);
- legend('基期价格=9','基期价格=9.5','基期价格=10',1)
- set(gca,'fontname','Times New Roman','FontSize',12)
- %subplot(2,2,2)
- clear c1 c2 c3
- subplot(2,2,2)
- hold on
- alpha=0.8:0.02:0.96;
- s=9:0.5:10;
- for i=1:length(alpha)
- c1(i)=Cev_Call_Option_Pricing(s(1),K,T,O,R,Q,alpha(i));
- c2(i)=Cev_Call_Option_Pricing(s(2),K,T,O,R,Q,alpha(i));
- c3(i)=Cev_Call_Option_Pricing(s(3),K,T,O,R,Q,alpha(i));
- end
- c=[c1;c2;c3];
- minc=min(min(c));
- maxc=max(max(c));
- xlim([alpha(1)-0.05 alpha(end)+0.05])
- ylim([minc-0.1 maxc+0.1])
- plot(alpha,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
- hold on
- plot(alpha,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
- plot(alpha,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
- xlabel('常方差弹性模型参数阿尔法','FontName','楷体','FontSize',16);
- ylabel('期权价格','FontName','楷体','FontSize',16);
- title('常方差弹性欧式看涨期权定价和阿尔法的关系','FontName','楷体','FontSize',16);
- legend('基期价格=9','基期价格=9.5','基期价格=10',2)
- set(gca,'fontname','Times New Roman','FontSize',12)
- %subplot(2,2,3)
- clear c1 c2 c3
- subplot(2,2,3)
- hold on
- t=0.5:0.1:1.5;
- for i=1:length(t)
- c1(i)=Cev_Call_Option_Pricing(s(1),K,t(i),O,R,Q,A);
- c2(i)=Cev_Call_Option_Pricing(s(2),K,t(i),O,R,Q,A);
- c3(i)=Cev_Call_Option_Pricing(s(3),K,t(i),O,R,Q,A);
- end
- c=[c1;c2;c3];
- minc=min(min(c));
- maxc=max(max(c));
- xlim([t(1)-0.05 t(end)+0.05])
- ylim([minc-0.1 maxc+0.1])
- plot(t,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
- hold on
- plot(t,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
- plot(t,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
- xlabel('到期时间','FontName','楷体','FontSize',16);
- ylabel('期权价格','FontName','楷体','FontSize',16);
- title('常方差弹性欧式看涨期权定价和到期时间的关系','FontName','楷体','FontSize',16);
- legend('基期价格=9','基期价格=9.5','基期价格=10',2)
- set(gca,'fontname','Times New Roman','FontSize',12)
- %subplot(2,2,4)
- subplot(2,2,4)
- hold on
- clear c1 c2 c3
- r=0.03:0.005:0.06;
- for i=1:length(r)
- c1(i)=Cev_Call_Option_Pricing(s(1),K,T,O,r(i),Q,A);
- c2(i)=Cev_Call_Option_Pricing(s(2),K,T,O,r(i),Q,A);
- c3(i)=Cev_Call_Option_Pricing(s(3),K,T,O,r(i),Q,A);
- end
- c=[c1;c2;c3];
- minc=min(min(c));
- maxc=max(max(c));
- xlim([r(1)-0.005 r(end)+0.005])
- ylim([minc-0.1 maxc+0.1])
- plot(r,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
- hold on
- plot(r,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
- plot(r,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
- xlabel('无风险利率','FontName','楷体','FontSize',16);
- ylabel('期权价格','FontName','楷体','FontSize',16);
- title('常方差弹性欧式看涨期权定价和无风险利率的关系','FontName','楷体','FontSize',16);
- legend('基期价格=9','基期价格=9.5','基期价格=10',2)
- set(gca,'fontname','Times New Roman','FontSize',12)
- hold off
- %% 基于常方差弹性欧式看涨期权定价模型的三维比较分析图
- clear c
- %c是length(k)*length(alpha)阶的矩阵
- %mesh_k和mesh_alpha是length(alpha)*length(k)阶的矩阵
- for i=1:length(k)
- for j=1:length(alpha)
- c(i,j)=Cev_Call_Option_Pricing(S,k(i),T,O,R,Q,alpha(j));
- end
- end
- [mesh_k,mesh_alpha]=meshgrid(k,alpha);
- figure(2)
- set(figure(2),'color','w')
- subplot(1,2,1)
- h=meshz(mesh_alpha,mesh_k,c');
- hold on
- colormap('HSV')
- set(h,'linewidth',3,'edgelighting','phong')
- xlabel('阿尔法','FontName','楷体','FontSize',12);
- ylabel('执行价格','FontName','楷体','FontSize',12);
- zlabel('期权价值','FontName','楷体','FontSize',12);
- title('常方差弹性欧式看涨期权定价和执行价格与阿尔法的关系','FontName','楷体','FontSize',16);
- clear c
- subplot(1,2,2)
- for i=1:length(t)
- for j=1:length(r)
- c(i,j)=Cev_Call_Option_Pricing(S,K,t(i),O,r(j),Q,A);
- end
- end
- [mesh_t,mesh_r]=meshgrid(t,r);
- h=meshz(mesh_t,mesh_r,c');
- hold on
- set(h,'linewidth',3,'edgelighting','phong')
- xlabel('到期时间','FontName','楷体','FontSize',12);
- ylabel('无风险利率','FontName','楷体','FontSize',12);
- zlabel('期权价值','FontName','楷体','FontSize',12);
- title('常方差弹性欧式看涨期权定价和无风险利率与到期时间的关系','FontName','楷体','FontSize',16);
- hold off
- end
- function C=Cev_Call_Option_Pricing(S,K,T,O,R,Q,A)
- %copyright by fantuanxiaot
- %模型具体请见赫尔的《期权期货及其他衍生品》
- %常方差弹性欧式看涨期权定价模型
- %而在这里0<alpha<1
- v=O^2/(2*(R-Q)*(A-1))*(exp(2*(R-Q)*(A-1)*T)-1);
- a=(K*exp((Q-R)*T))^(2*(1-A))/v/(1-A)^2;
- b=1/(1-A);
- c=S^(2*(1-A))/v/(1-A)^2;
- %ncx2cdf代表非中心化的卡方分布cdf
- %ncx2cdf第一参数代表自变量,第二个参数是自由度,第三个参数是非中心化参数
- d1=1-ncx2cdf(a,c,b+2);
- d2=ncx2cdf(c,a,b);
- %这样就得到了基于常方差弹性模型的欧式看涨期权定价
- C=S*exp(-Q*T)*d1-K*exp(-R*T)*d2;
- end
复制代码
[/hide]
|