搜索
人大经济论坛 附件下载

附件下载

所在主题:
文件名:  20 matlab正则基础.doc
资料下载链接地址: https://bbs.pinggu.org/a-1703101.html
附件大小:
fantuanxiaot的新年大礼:一些MATLAB代码、各类学习资料、书籍等。 祝大家新年快乐!

其余请见藤椅、地板、板凳、报纸、8楼、10楼等本页的楼层

我原来的一些资源帖子:
[原创]MATLAB提取整数多项式
[原创]MATLAB网页爬取我偶像的若干图片
[原创]将离歌版主的风险收益做了个gui
[原创]太阳出来爬山gif图
[原创]期权定价的二叉树和三叉树图
[原创]欧式期权三角和核弹头作图
[原创]马克维茨边界作图
[原创]遗传算法模拟退火

更多精彩请见:量化投资离歌量化各类软件实战

一,资料与文献分享、神经网络、配对交易、HMM等

[hide]




















[/hide]
二,MATLAB的代码分享

一个图形的复制技术:by fantuanxiaot

[hide]
  1. %copyright by fantuanxiaot
  2. %基于matlab的图片复制
  3. %一个图片复制动态展示
  4. %看看图片一是如何复制到图片二的
  5. clc
  6. clear all
  7. close all
  8. %设置一个图1
  9. %图1的句柄
  10. h1=figure(1);
  11. t=0:pi/10:2*pi;
  12. r=2;
  13. x=r*exp(i*t);
  14. s='g';
  15. plot(x,s,'linewidth',2)
  16. hold on
  17. plot(x,'rp','markersize',10)
  18. xlabel('Real')
  19. ylabel('Image')
  20. positions=get(h1,'position');
  21. %图2的句柄
  22. h2=figure(2);
  23. set(h2,'position',[positions(1)-300,positions(2),positions(3),positions(4)])
  24. set(h1,'position',[positions(1)+300,positions(2),positions(3),positions(4)])
  25. pause(1)
  26. %图1的轴句柄
  27. h1_axes=findobj(h1,'type','axes');
  28. h2_axes=copyobj(h1_axes,h2);
  29. %这样图1就复制到了图2
复制代码
[/hide]
一个图形让X轴标注并且使得文字倾斜:by faruto


[hide]

  1. %copyright by faruto and fantuanxiaot
  2. %一个让x轴和y轴标注倾斜的窍门
  3. clc
  4. close all
  5. clear all
  6. figure('color',[0.5 1 1],'units','normal','position',[0 0 0.4 0.5])
  7. movegui(gcf,'center')
  8. plot(magic(16))
  9. %获得句柄
  10. current_axes=gca;
  11. strx=get(current_axes,'XTickLabel');
  12. stry=get(current_axes,'YTickLabel');
  13. x=get(current_axes,'XTick');
  14. y=get(current_axes,'YTick');
  15. yl=ylim(current_axes);
  16. xl=xlim(current_axes);
  17. %句柄设置
  18. set(current_axes,'XTickLabel',[]);
  19. set(current_axes,'YTickLabel',[]);
  20. %使之倾斜
  21. xtoy=zeros(1,length(x))+yl(1)-(max(yl)-min(yl))/20;
  22. ytox=zeros(1,length(y))+xl(1)-(max(xl)-min(xl))/20;
  23. text(x,xtoy,strx,'rotation',40,'HorizontalAlignment'...
  24. ,'center','FontName','times','FontSize',16,'backgr','g');
  25. text(ytox,y,stry,'rotation',-40,'HorizontalAlignment'...
  26. ,'center','FontName','times','FontSize',16,'backgr','m');
复制代码

[/hide]

一些图形大家自己看着吧:by fantuanxiaot


[hide]


  1. %各类养眼的作图
  2. %大家拿去自己练习练习
  3. %copyright by fantuanxiaot
  4. clc
  5. clear all
  6. close all
  7. format compact
  8. figure('color',[0.5 1 1],'position',[100 80 1200 580])
  9. %%右嵌入的作图
  10. subplot(2,3,1)
  11. patch('xdata',[ones(1,11)*5 ones(1,11)*10],'ydata',[0:10 10:-1:0],...
  12. 'facecolor',[1 0.9 0.9],'edgecolor','g')
  13. hold on
  14. plot(1:10,'color',[0.7 0 1])
  15. %上左边
  16. legend('patch plot','straight line',2)
  17. set(gca,'fontname','Times New Roman')
  18. %%左嵌入的作图
  19. subplot(2,3,2)
  20. patch('xdata',[ones(1,11)*0 ones(1,11)*5],'ydata',[10:-1:0 0:10],...
  21. 'facecolor',[1 0.9 0.9],'edgecolor','g')
  22. hold on
  23. plot(1:10,'color',[0.7 0 1])
  24. %上右边
  25. legend('patch plot','straight line',1)
  26. set(gca,'fontname','Times New Roman')
  27. %%两边嵌入的作图
  28. subplot(2,3,3)
  29. patch('xdata',[ones(1,11)*0 ones(1,11)*4.99],'ydata',[10:-1:0 0:10],...
  30. 'facecolor',[1 0.9 0.9],'edgecolor','g')
  31. hold on
  32. patch('xdata',[ones(1,11)*5.01 ones(1,11)*10],'ydata',[0:10 10:-1:0],...
  33. 'facecolor',[1 1 0],'edgecolor','g')
  34. plot(1:10,'color',[0.7 0 1],'linewidth',1)
  35. %上右边
  36. legend('patch plot 1','patch plot 2','straight line',1)
  37. set(gca,'fontname','Times New Roman')
  38. %%做一个多维色彩的bar图形
  39. subplot(2,3,4)
  40. y=randn(1,10);
  41. h_bar=bar(y);
  42. h_bar_children=get(h_bar,'Children');
  43. %每一行代表四个点的坐标
  44. h_matrix=get(h_bar_children,'Faces');
  45. %每一个坐标代表
  46. color_matrix=get(h_bar_children,'FaceVertexCData');
  47. %对每一行h_matrix,也就是一个方块,通过color_matrix取出4个点的坐标,其对应的颜色在color_matrix中,将4个点的颜色
  48. %信息均修改为more_color
  49. for more_color=1:length(y)
  50. color_matrix(h_matrix(more_color,:))=more_color;
  51. end
  52. set(h_bar_children,'FaceVertexCData',color_matrix)
  53. legend('bar plot')
  54. set(gca,'fontname','Times New Roman')
  55. colorbar
  56. %%做一个bar图形,上面与下面的颜色不同
  57. clear all
  58. subplot(2,3,5)
  59. y=randn(1,100);
  60. y=sort(y);
  61. h_bar=bar(y);
  62. h_bar_children=get(h_bar,'Children');
  63. %每一行代表四个点的坐标
  64. h_matrix=get(h_bar_children,'Faces');
  65. %每一个坐标代表
  66. color_matrix=get(h_bar_children,'FaceVertexCData');
  67. %上下颜色不同
  68. big_index=find(y>=0);
  69. small_index=find(y<0);
  70. color_matrix(h_matrix(small_index,:))=8;
  71. color_matrix(h_matrix(big_index,:))=4;
  72. set(h_bar_children,'FaceVertexCData',color_matrix,'edgecolor','none')
  73. legend('bar plot')
  74. set(gca,'fontname','Times New Roman')
  75. %%另一类bar图形,区分收益率的正负
  76. subplot(2,3,6)
  77. %模拟的收益率
  78. N=100;
  79. c=normrnd(0,1,1,100);
  80. index_big=find(c>=0);
  81. index_small=find(c<0);
  82. index_date_num=1:N;
  83. bar(index_date_num(index_big),c(index_big),'facecolor','g','edgecolor','g')
  84. hold on
  85. bar(index_date_num(index_small),c(index_small),'facecolor','r','edgecolor','r')
  86. set(gca,'fontname','Times New Roman')
  87. legend('bar plot')
  88. clear all
复制代码

[/hide]
如何用MATLAB作图跟踪自己的动态权益收益:by fantuanxiaot

[hide]


  1. %用matlab如何记录自己的收益与回报
  2. %正的收益红色,负的收益绿色
  3. %copyright by fantuanxiaot
  4. %准备工作
  5. clc
  6. clear all
  7. close all
  8. format compact
  9. figure(1)
  10. set(figure(1),'color',[0.5 1 1])
  11. set(figure(1),'position',[1 1 800 530])
  12. movegui(figure(1),'center')
  13. set(gca,'units','normalized','position',[0.15,0.2,0.7,0.7])
  14. %取最近100天的dynamic return
  15. N=100;
  16. %记录收益标准的天数
  17. M=20;
  18. %收益率的准备
  19. single_return=normrnd(0,0.07,1,N);
  20. date_num=today-N+1:today;
  21. date_str=datestr(date_num,29);
  22. date_cellstr=cellstr(date_str);
  23. %做柱状图
  24. x_index=1:N;
  25. one_index=find(single_return>=0);
  26. two_index=find(single_return<0);
  27. bar(x_index(one_index),single_return(one_index),...
  28. 'facecolor','r','edgecolor','r')
  29. hold on
  30. bar(x_index(two_index),single_return(two_index),...
  31. 'facecolor','g','edgecolor','g')
  32. title('Follow Your Return Day by Day','fontname',...
  33. 'Times','fontsize',14)
  34. set(gca,'xtick',[])
  35. set(gca,'xticklabel',[])
  36. y_range=ylim(gca);
  37. %重构坐标
  38. y_high=zeros(1,M)+min(y_range)-(range(y_range))/30;
  39. x_index=ceil((1:M)./M*N);
  40. str=date_cellstr(x_index);
  41. text(x_index,y_high,str,'rotation',-40,'HorizontalAlignment'...
  42. ,'left','FontName','times','FontSize',10,'vertical','bottom');
  43. set(gca,'fontname','Times','fontsize',14)
  44. y_index=linspace(min(y_range),0,10)';
  45. hold on
  46. plot(repmat(x_index,length(y_index),1),...
  47. repmat(y_index,1,length(x_index)),'b--','linewidth',1)
  48. clear all
复制代码

[/hide]

基于常方差弹性期权定价模型的比较静态分析(常方差弹性期权定价模型见Hull的《期权期货》):by fantuanxiaot



[hide]



  1. function Gev_Option_Pricing_Plot()
  2. %copyright by fantuanxiaot
  3. %基于常方差弹性欧式看涨期权定价模型的二维和三维比较静态分析
  4. %S是期权的基期价格,K为行权价格,T是到期时间,O是波动参数
  5. %R是年连续无风险利率,Q是年连续红利率,A是常方差弹性模型的alpha参数
  6. %参数的设置
  7. S=10;
  8. K=12;
  9. T=1;
  10. O=0.16;
  11. R=0.04;
  12. Q=0.01;
  13. A=0.95;
  14. %% 基于常方差弹性欧式看涨期权定价模型的二维比较分析图
  15. %subplot(2,2,1)
  16. figure(1)
  17. set(figure(1),'color','w')
  18. subplot(2,2,1)
  19. hold on
  20. k=11:0.2:13;
  21. s=9:0.5:10;
  22. for i=1:length(k)
  23. c1(i)=Cev_Call_Option_Pricing(s(1),k(i),T,O,R,Q,A);
  24. c2(i)=Cev_Call_Option_Pricing(s(2),k(i),T,O,R,Q,A);
  25. c3(i)=Cev_Call_Option_Pricing(s(3),k(i),T,O,R,Q,A);
  26. end
  27. c=[c1;c2;c3];
  28. minc=min(min(c));
  29. maxc=max(max(c));
  30. xlim([k(1)-0.1 k(end)+0.1])
  31. ylim([minc-0.1 maxc+0.1])
  32. plot(k,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  33. hold on
  34. plot(k,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  35. plot(k,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  36. xlabel('执行价格','FontName','楷体','FontSize',16);
  37. ylabel('期权价格','FontName','楷体','FontSize',16);
  38. title('常方差弹性欧式看涨期权定价和执行价格的关系','FontName','楷体','FontSize',16);
  39. legend('基期价格=9','基期价格=9.5','基期价格=10',1)
  40. set(gca,'fontname','Times New Roman','FontSize',12)
  41. %subplot(2,2,2)
  42. clear c1 c2 c3
  43. subplot(2,2,2)
  44. hold on
  45. alpha=0.8:0.02:0.96;
  46. s=9:0.5:10;
  47. for i=1:length(alpha)
  48. c1(i)=Cev_Call_Option_Pricing(s(1),K,T,O,R,Q,alpha(i));
  49. c2(i)=Cev_Call_Option_Pricing(s(2),K,T,O,R,Q,alpha(i));
  50. c3(i)=Cev_Call_Option_Pricing(s(3),K,T,O,R,Q,alpha(i));
  51. end
  52. c=[c1;c2;c3];
  53. minc=min(min(c));
  54. maxc=max(max(c));
  55. xlim([alpha(1)-0.05 alpha(end)+0.05])
  56. ylim([minc-0.1 maxc+0.1])
  57. plot(alpha,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  58. hold on
  59. plot(alpha,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  60. plot(alpha,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  61. xlabel('常方差弹性模型参数阿尔法','FontName','楷体','FontSize',16);
  62. ylabel('期权价格','FontName','楷体','FontSize',16);
  63. title('常方差弹性欧式看涨期权定价和阿尔法的关系','FontName','楷体','FontSize',16);
  64. legend('基期价格=9','基期价格=9.5','基期价格=10',2)
  65. set(gca,'fontname','Times New Roman','FontSize',12)
  66. %subplot(2,2,3)
  67. clear c1 c2 c3
  68. subplot(2,2,3)
  69. hold on
  70. t=0.5:0.1:1.5;
  71. for i=1:length(t)
  72. c1(i)=Cev_Call_Option_Pricing(s(1),K,t(i),O,R,Q,A);
  73. c2(i)=Cev_Call_Option_Pricing(s(2),K,t(i),O,R,Q,A);
  74. c3(i)=Cev_Call_Option_Pricing(s(3),K,t(i),O,R,Q,A);
  75. end
  76. c=[c1;c2;c3];
  77. minc=min(min(c));
  78. maxc=max(max(c));
  79. xlim([t(1)-0.05 t(end)+0.05])
  80. ylim([minc-0.1 maxc+0.1])
  81. plot(t,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  82. hold on
  83. plot(t,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  84. plot(t,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  85. xlabel('到期时间','FontName','楷体','FontSize',16);
  86. ylabel('期权价格','FontName','楷体','FontSize',16);
  87. title('常方差弹性欧式看涨期权定价和到期时间的关系','FontName','楷体','FontSize',16);
  88. legend('基期价格=9','基期价格=9.5','基期价格=10',2)
  89. set(gca,'fontname','Times New Roman','FontSize',12)
  90. %subplot(2,2,4)
  91. subplot(2,2,4)
  92. hold on
  93. clear c1 c2 c3
  94. r=0.03:0.005:0.06;
  95. for i=1:length(r)
  96. c1(i)=Cev_Call_Option_Pricing(s(1),K,T,O,r(i),Q,A);
  97. c2(i)=Cev_Call_Option_Pricing(s(2),K,T,O,r(i),Q,A);
  98. c3(i)=Cev_Call_Option_Pricing(s(3),K,T,O,r(i),Q,A);
  99. end
  100. c=[c1;c2;c3];
  101. minc=min(min(c));
  102. maxc=max(max(c));
  103. xlim([r(1)-0.005 r(end)+0.005])
  104. ylim([minc-0.1 maxc+0.1])
  105. plot(r,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  106. hold on
  107. plot(r,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  108. plot(r,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  109. xlabel('无风险利率','FontName','楷体','FontSize',16);
  110. ylabel('期权价格','FontName','楷体','FontSize',16);
  111. title('常方差弹性欧式看涨期权定价和无风险利率的关系','FontName','楷体','FontSize',16);
  112. legend('基期价格=9','基期价格=9.5','基期价格=10',2)
  113. set(gca,'fontname','Times New Roman','FontSize',12)
  114. hold off
  115. %% 基于常方差弹性欧式看涨期权定价模型的三维比较分析图
  116. clear c
  117. %c是length(k)*length(alpha)阶的矩阵
  118. %mesh_k和mesh_alpha是length(alpha)*length(k)阶的矩阵
  119. for i=1:length(k)
  120. for j=1:length(alpha)
  121. c(i,j)=Cev_Call_Option_Pricing(S,k(i),T,O,R,Q,alpha(j));
  122. end
  123. end
  124. [mesh_k,mesh_alpha]=meshgrid(k,alpha);
  125. figure(2)
  126. set(figure(2),'color','w')
  127. subplot(1,2,1)
  128. h=meshz(mesh_alpha,mesh_k,c');
  129. hold on
  130. colormap('HSV')
  131. set(h,'linewidth',3,'edgelighting','phong')
  132. xlabel('阿尔法','FontName','楷体','FontSize',12);
  133. ylabel('执行价格','FontName','楷体','FontSize',12);
  134. zlabel('期权价值','FontName','楷体','FontSize',12);
  135. title('常方差弹性欧式看涨期权定价和执行价格与阿尔法的关系','FontName','楷体','FontSize',16);
  136. clear c
  137. subplot(1,2,2)
  138. for i=1:length(t)
  139. for j=1:length(r)
  140. c(i,j)=Cev_Call_Option_Pricing(S,K,t(i),O,r(j),Q,A);
  141. end
  142. end
  143. [mesh_t,mesh_r]=meshgrid(t,r);
  144. h=meshz(mesh_t,mesh_r,c');
  145. hold on
  146. set(h,'linewidth',3,'edgelighting','phong')
  147. xlabel('到期时间','FontName','楷体','FontSize',12);
  148. ylabel('无风险利率','FontName','楷体','FontSize',12);
  149. zlabel('期权价值','FontName','楷体','FontSize',12);
  150. title('常方差弹性欧式看涨期权定价和无风险利率与到期时间的关系','FontName','楷体','FontSize',16);
  151. hold off
  152. end
  153. function C=Cev_Call_Option_Pricing(S,K,T,O,R,Q,A)
  154. %copyright by fantuanxiaot
  155. %模型具体请见赫尔的《期权期货及其他衍生品》
  156. %常方差弹性欧式看涨期权定价模型
  157. %而在这里0<alpha<1
  158. v=O^2/(2*(R-Q)*(A-1))*(exp(2*(R-Q)*(A-1)*T)-1);
  159. a=(K*exp((Q-R)*T))^(2*(1-A))/v/(1-A)^2;
  160. b=1/(1-A);
  161. c=S^(2*(1-A))/v/(1-A)^2;
  162. %ncx2cdf代表非中心化的卡方分布cdf
  163. %ncx2cdf第一参数代表自变量,第二个参数是自由度,第三个参数是非中心化参数
  164. d1=1-ncx2cdf(a,c,b+2);
  165. d2=ncx2cdf(c,a,b);
  166. %这样就得到了基于常方差弹性模型的欧式看涨期权定价
  167. C=S*exp(-Q*T)*d1-K*exp(-R*T)*d2;
  168. end
复制代码


[/hide]









    熟悉论坛请点击新手指南
下载说明
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。
2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。
3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。
(如有侵权,欢迎举报)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

GMT+8, 2025-12-9 05:10