相似文件
换一批
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
随心做了一个马克维茨的Gui,分享一下
更多量化分析请见:离歌量化
发现随机生成的收益率数据感觉不对啊,这个图老是画的不正确咋办
输入1或者2即可
代码如下:
本帖隐藏的内容- % copyright by fantuanxiaot
- % 构造马克维茨最小方差边界或者有效边界的简单Gui作图
- % Learing Markowitz_Gui
- % 马克维茨的有效边界为3个股价
- % 可以自己改编
- clc
- clear all
- close all
- % 先构造股价的100个收益率
- return_random1=normrnd(0.1,0.04,100,1);
- return_random2=normrnd(-0.1,0.05,100,1);
- % 收益率的组合形成具有相关性质的收益
- return_1=0.45*return_random1+0.55*return_random2;
- return_2=0.35*return_random1+0.65*return_random2;
- return_3=0.5*return_random1+0.5*return_random2;
- return_matrix=[return_1 return_2 return_3];
- % 马克维茨矩阵的各类参数的构造
- expected_return=mean(return_matrix);
- sigma_return=std(expected_return);
- cov_var_matrix=cov(return_matrix);
- frontile_return=linspace(min(expected_return),max(expected_return),50);
- % 有三只股票
- N=3;
- frontile_std=[];
- for i=1:length(frontile_return)
- [~,fval]=quadprog(cov_var_matrix,zeros(N,1),[],[],[ones(1,3);expected_return],[1;frontile_return(i)]);
- frontile_std=[frontile_std sqrt(2*fval)];
- end
- % 计算其他组合
- another_ratio=unifrnd(-1,1,300,2);
- another_ratio=[another_ratio ones(300,1)-sum(another_ratio,2)];
- another_return=another_ratio*expected_return';
- another_std=zeros(300,1);
- for j=1:length(another_std)
- another_std(j)=sqrt(another_ratio(j,:)*cov_var_matrix*another_ratio(j,:)');
- end
- % 形成有效边界组合
- [~,index]=min(frontile_std);
- efficient_return=frontile_return(index+1:end);
- efficient_std=frontile_std(index+1:end);
- index_another=find(another_return>=frontile_return(index));
- efficient_another_return=another_return(index_another);
- efficient_another_std=another_std(index_another);
- % frontile_return,frontile_std形成了最小方差边界的的组合
- % another_return,another_std形成了其他组合
- % efficient_return,efficient_std为有效边界的组合
- % efficient_another_return,efficient_another_std为中间线位于投资收益的上方
- figure(1)
- set(figure(1),'color',[0.5 1 1])
- set(figure(1),'unit','normalized','position',[0.22,0.14,0.55,0.7])
- H=axes('position',[0,0,1,1],'visible','off');
- % 则在版面上作出了一个Gui之图,而不是在方框里面
- set(gcf,'currentaxes',H)
- my_str='\fontname{Times New Roman}\fontsize{10}Markowitz Gui Simple Example';
- text(0.37,0.97,my_str,'fontsize',12)
- % 标注一个图
- axes_1=axes('unit','normalized','position',[0.3,0.1,0.65,0.85],'fontsize',8,...
- 'fontname','Times New Roman');
- h_text=uicontrol(figure(1),'style','text','unit','normalized','position',[0.03,0.65,0.23,0.3],...
- 'horizontal','left','string',{'选择有效边界:1或者最小方差边界:2','make_your_choice='},'fontsize',12,...
- 'fontname','楷体');
- h_edit=uicontrol(figure(1),'style','edit','unit','normalized','position',[0.03,0.44,0.23,0.2],...
- 'horizontal','left',...
- 'callback',[...
- 'choice=str2num(get(gcbo,''string''));',...
- 'switch choice;',...
- 'case 1;',...
- 'plot(efficient_std,efficient_return,''r<-'',''markerfacecolor'',''r'',''markersize'',5);',...
- 'hold on;',...
- 'plot(efficient_another_std,efficient_another_return,''g*'',''markerfacecolor'',''g'',''markersize'',5);',...
- 'hold off;',...
- 'case 2;',...
- 'plot(frontile_std,frontile_return,''r<-'',''markerfacecolor'',''r'',''markersize'',5);',...
- 'hold on;',...
- 'plot(another_std,another_return,''g*'',''markerfacecolor'',''g'',''markersize'',5);',...
- 'hold off;',...
- 'end;']);
- h_push_1=uicontrol(figure(1),'style','push','unit','normalized','position',[0.03,0.3,0.23,0.13],'string','Yellow Color',...
- 'callback','set(gca,''color'',''y'')');
- h_push_2=uicontrol(figure(1),'style','push','unit','normalized','position',[0.03,0.1,0.23,0.19],'string','Grid on',...
- 'callback','grid on');
复制代码
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|