寻找slope最大点的函数

function [ port, opt_mu, opt_sigma ] = highest_slope_portfolio( R, RF, mu, sigma )

% This function finds the portfolio with the largest slope

% this function can easily be much more general

% e.g. mu, RF, sigma can be parameters

if nargin < 1

return

elseif nargin == 1

RF=0.02;

mu=[.1 .2]';

sigma=[.1 .2]';

elseif nargin == 2

mu=[.1 .2]';

sigma=[.1 .2]';

end

% Here we use our define correlation coefficient

C=diag(sigma)*R*diag(sigma);

A=2*C;

A(:,end+1)=-(mu-RF);

A(end+1,1:end-1)=(mu-RF)';

Rp=mu(1);

b=zeros(length(mu),1);

b(end+1,1)=Rp-RF;

x=inv(A)*b;

xopt=x(1:length(mu))./sum(x(1:length(mu)))';

% Return value

port = xopt;

opt_mu = xopt' * mu;

opt_sigma = sqrt( xopt' * C * xopt);

end

案例 对比有无 无风险借贷的lending 和 borrowing:

Find the efficient frontier where short sales are allowed with and without risk less lending and borrowing. The following is given and does not change through question one of the assignment.

The risk free rate Rf is 2 %

Asset 1 yearly expected return is 10% and the standard deviation is 10%

Asset 2 yearly expected return is 20% and the standard deviation is 20%

For each of the following correlation coefficient between assets 1 and 2: rho=1; rho=0.5; rho=0; rho=-1

For Pepsi, Coca-Cola and Microsoft, estimate the yearly return, and covariance matrix of assets returns.

% To prevent unnessary loading of data from yahoo finance we add the if

% statement

if ~exist('stocks', 'var')

stocks=hist_stock_data('01011991','01012001','PEP', 'KO','MSFT','frequency','wk');

Pepsi = stocks(1);

CocaCola = stocks(2);

Microsoft = stocks(3);

end

% Caclualte log returns

PEPLR = log(Pepsi.AdjClose(2:end)./Pepsi.AdjClose(1:end-1) );

CCLR = log(CocaCola.AdjClose(2:end)./CocaCola.AdjClose(1:end-1) );

MSLR = log(Microsoft.AdjClose(2:end)./Microsoft.AdjClose(1:end-1) );

LogReturns = [ PEPLR, CCLR, MSLR ];

ymean = 52 * mean(LogReturns)';

ystd = sqrt (52 * var(LogReturns))';

ycorr = corr(LogReturns)'; %mistake was cor before

Calculate the efficient frontier with and without risk less lending and borrowing.

% These is constant throughout the excercise

RF = .02;

xopt = cell(2);

% Calculate the highest slope protfolio with each

[xopt{1}, muopt(1), sigopt(1)] = highest_slope_portfolio( ycorr(1:2, 1:2), RF, ymean(1:2), ystd(1:2) );

[xopt{2}, muopt(2), sigopt(2)] = highest_slope_portfolio( ycorr, RF, ymean, ystd);

% Plotting point by point

hold on;

plot (sigopt(1), muopt(1) , 'x');

hold on;

plot (sigopt(2), muopt(2) , 'go');

% As we know RF = 2% we can already plot the differnet efficient frontiers

% The starting point is always the same. 0 risk 2%

hold on;

plot (0, .02, 'o');

hold on;

RF_p1 = [0 sigopt(1) 2* sigopt(1)];

opt1_p = [.02 muopt(1) (2 * muopt(1) - RF) ];

line(RF_p1, opt1_p );

hold on;

RF_p2 = [0 sigopt(2) 2* sigopt(2)];

opt2_p = [.02 muopt(2) (muopt(2) * 2 - RF)];

line(RF_p2, opt2_p, 'Color',[1 0 0]);

% We can find ANOTHER efficient portfolio on the frontier, by running the

% same optimization with a DIFFERENT interscept

% Calculate the highest slope protfolio with each

xopt2 = cell(2);

[xopt2{1}, muopt2(1), sigopt2(1)] = highest_slope_portfolio( ycorr(1:2, 1:2), .05, ymean(1:2), ystd(1:2) );

[xopt2{2}, muopt2(2), sigopt2(2)] = highest_slope_portfolio( ycorr, .05, ymean, ystd);

%[xopt2(3,:), muopt2(3), sigopt2(3)] = highest_slope_portfolio( R{3}, .05);

%[xopt2(4,:), muopt2(4), sigopt2(4)] = highest_slope_portfolio( R{4}, .05);

% This is what we do, look for optimal point if the RF rate was 5%

% I Plot this too to show the idea

hold on;

plot (0, .05, 'o');

% Plotting the 2nd point on the portfolio

hold on;

plot (sigopt2(1), muopt2(1) , 'go');

hold on;

plot (sigopt2(2), muopt2(2) , 'co');

C = cell(2,1);

% Define the corresponding correlation matrices

C{1}=diag(ystd(1:2))*ycorr(1:2,1:2)*diag(ystd(1:2));

C{2}=diag(ystd)*ycorr*diag(ystd);

% As seen in class we have a general formula for finding the mean variance

% portfolio for two assets -

large_n = 100;

k = 20;

mu_p = zeros(4, 4*k* large_n + 1);

std_p = zeros(4, 4*k* large_n + 1);

%SR

% We will go through different combinations to find the efficient frontier;

for j = 1:2

for i = -2*k* large_n:1:2 * 2*k*large_n

curr_port = i / large_n * xopt2{j} + (1 - i / large_n) * xopt{j};

mu_p (j, i + 2*k * large_n + 1) = curr_port' * ymean(1:j+1);

std_p(j, i + 2*k * large_n + 1) = sqrt(curr_port' * C{j} * curr_port);

end

end

% SR =( mu_p - RF) ./ std_p

%find (max(SR) == SR)

%Plotting the efficient frontiers

hold on;

plot( std_p(1,:), mu_p(1,:));

%pause;

hold on;

plot( std_p(2,:), mu_p(2,:), 'r');

得到的图案:

原文出处:https://www.cnblogs.com/hanani/p/10094544.html

3西格玛计算matlab,使用Matlab计算Efficient frontier相关推荐

  1. matlab科学计算及分析,matlab科学计算

    spContent=当前,科学计算已经成为科学研究.技术创新的重要方法与手段,而作为实现工具的科学计算软件无疑具有至关重要的作用.MATLAB为众多领域的计算问题提供了全面的解决方案,代表了当今国际科 ...

  2. 在matlab中可以计算式子的最大值吗,matlab求最大值

    matlab遗传算法求最大值问题例题完整代码_IT认证_资格考试/认证_教育专区.matlab遗传算法求最大值问题例题完整代码 本文件的目的是减少您打字的烦恼 %主程序:用...... 11MATLA ...

  3. 静电场的有限差分法与matlab 仿真课程设计,计算物理和MATLAB课程设计--自激振动系统的MATLAB仿真.doc...

    东北石油大学课程设计任务书 课程 计算物理和MATLAB课程设计 题目 自激振动系统的MATLAB仿真 专业 姓名 学号 主要内容.基本要求.主要参考资料等 主要内容: 研究范?德?波耳(Van de ...

  4. Matlab 集群计算平台搭建

    家庭云服务器高阶应用:通过Matlab分布式计算来演示所谓的"家庭云计算"(非云储存)(转) Matlab是强大的计算软件,这不需要过多介绍了,大到航天航空领域,小到计算方程式,M ...

  5. matlab圆形器件,计算围绕点+ Matlab的圆形箱

    我的问题与此链接stackoverflow问题有关 本质上,重复在此绘制的图形..我在图像中有一个中心点(x,y),我必须围绕它绘制四个1-4单位半径的圆,它们之间有8个角度. 在此图中,有12个角仓 ...

  6. e-006 matlab,基于MATLAB进行潮流计算

    基于MATLAB 进行潮流计算 学生:王仕龙 2011148213 指导老师:李咸善 摘要:电力系统潮流计算方法有两类,即手算潮流和计算机潮流计算.手算潮流主要借助于形成简化的等值电路来实现,这种方法 ...

  7. qtableview与sqlite使用显示科学计算_使用MATLAB Profiler提升程序运行的效率

    很多同学在研究和学习的过程中都会经常使用 MATLAB.很多时候当编好一个程序之后,我们发现程序运行的效率达不到预想的要求.盲目的去找程序的缺陷往往既花时间也没有什么太大效果.这里我想告诉大家 MAT ...

  8. matlab将常值函数转换为变量,MATLAB与科学计算期末复习题题库15.11.12

    MATLAB 与科学计算期末复习题题库(第一部分) 一.填空 1.MATLAB 的主界面是一个高度集成的工作环境,有四个不同职责分工的窗口,分别 为 . . .和 窗口. 2.MATLAB 的值,应在 ...

  9. matlab 万年历,转 计算万年历的Matlab程序

    this.p={ m:2, b:2, loftPermalink:'', id:'fks_0800670800820800740840800950950850850820710820860820740 ...

最新文章

  1. android webview 更新,Android WebView缓存没更新解决方案
  2. Windows Mobile与OPhone开发对比
  3. iOS多线程开发(三)---Run Loop(一)
  4. 微信小程序设置启动图时出现滚动条
  5. 全程软件测试之测试需求分析与计划(2)
  6. 讨厌麻烦的ora 01722无效数字
  7. 数学基础知识(高精、快速幂、龟速乘……)
  8. Android 编程下 Touch 事件的分发和消费机制
  9. MySQL 5.5 主从复制异步、半同步以及注意事项详解
  10. mysql innodb ibd_MySQL innodb引擎下根据.frm和.ibd文件恢复表结构和数据
  11. linux apktool使用教程,简单介绍ubuntu下apktool的使用与配置
  12. python os读取文件
  13. ERP:“造势”,还是“做事”
  14. nginx跨域配置及压缩配置
  15. 实现div半透明效果
  16. linux 批量ping检测
  17. CDbCriteria CArrayDataProvider zii.widgets.grid (1)
  18. 仅仅有人物没背景的图片怎么弄_只会画人物不会画背景?这3种方法教你快速画背景!...
  19. Eclipse添加Tomcat
  20. re文件管理免root中文版,re管理器免root版本

热门文章

  1. 2019银行校招网站汇总
  2. 外挂辅助技术研究-分析动作数组与攻击捡物功能
  3. 程序员的自我修养系列之一
  4. 【Home键的监听】
  5. CLUE命名实体识别
  6. 在网页上漫步太空,谷歌推出谷歌天空网页版
  7. Hanoi塔问题的递归算法与非递归算法
  8. ubuntu18.04编译GB28181Server
  9. WinMerge——好用的代码对比工具
  10. JavaScript变量提升机制