寻找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');

得到的图案:

matlab frontier,使用Matlab计算Efficient frontier相关推荐

  1. 3西格玛计算matlab,使用Matlab计算Efficient frontier

    寻找slope最大点的函数 function [ port, opt_mu, opt_sigma ] = highest_slope_portfolio( R, RF, mu, sigma ) % T ...

  2. 基于Python实现有效前沿(Efficient Frontier)

    马科维兹有效前沿 有效前沿亦称"有效边界".理性的投资者一般是厌恶风险而偏好收益的,对于相同的风险水平,会选择能提供最大收益率的组合:对于相同的预期收益率,会选择风险最小的组合.能 ...

  3. 美赛整理之Matlab的工程数学计算学习笔记(高等数学)

    美赛整理之Matlab的工程数学计算学习笔记(高等数学) 1.极限的定义和判别: 2.绘制特殊曲面 3.求两个空间曲面的交线 4.定积分的计算 5.多重积分的计算 1.截面法: 2.定义法 (1)先画 ...

  4. matlab求刚度,matlab直接刚度法计算结构频率

    matlab直接刚度法计算结构频率 syms E I K r l x cja1=-E*I*K*r^3*(cos(x)*sinh(x)+sin(x)*cosh(x))jc1=-E*I*K*r^2*sin ...

  5. matlab一元二次回归,MATLAB一元线性回归方程的计算及检验.doc

    MATLAB一元线性回归方程的计算及检验 1. 从input语句键盘输入一组数据(xi,yi),i=1,2,-n. 2. 计算一元线性回归方程y=ax+b的系数a和b,用两种方法计算: 一是公式:: ...

  6. matlab多项式计算题目,MATLAB数据分析与多项式计算-习题答案

    <MATLAB数据分析与多项式计算-习题答案>由会员分享,可在线阅读,更多相关<MATLAB数据分析与多项式计算-习题答案(4页珍藏版)>请在人人文库网上搜索. 1.第6章 M ...

  7. MATLAB与高等数学--极限计算

    limit函数用法 在MATLAB中使用limit计算函数在某点的极限: limit(f,a) 例1: >> syms x; >> limit((x^3+1)/(x^4+2)) ...

  8. matlab中pwelch函数计算功率谱密度

    出处:Matlab用pwelch函数计算功率谱 - 知乎 (zhihu.com) 1:函数形式 [pxx,f] = pwelch(x,window,noverlap,NFFT,fs) 该函数可以自适应 ...

  9. 位移传递率matlab编程,机械振动设计计算与VB编程实例

    1 编写机械振动计算程序的一般步骤1 1.1 明确编写程序的目的和了解清楚相关的计算公式.物理量纲1 1.2 在简单的界面上用BASIC编写计算程序并完成调试2 1.3 设计一个较为完备的用户界面4 ...

  10. 计算久期matlab,[MATLAB代码模板]固定收益证券计算

    固定收益证券计算 1固定收益债券定价 (1)bndprice函数 目的: 给固定收益债券定价 格式: [Price,AccruedInt]=bndprice(Yield,CouponRate,Sett ...

最新文章

  1. 想学单片机怎么入手?学单片机前先学什么?
  2. 100连接蓝牙_车机蓝牙连接常见问题说明
  3. php地图,地图php接口
  4. java map与set的区别_java 集合(list,set,map)三者之间的关系和区别
  5. golang反编译_【Golang】脱胎换骨的defer(一)
  6. ajax发送私信,$.ajax()方法详解
  7. python getopt使用_如何使用getopt.getoptpython中的方法?
  8. jquery ready() 与window onload的区别
  9. 高温持续,三峡水库向长江中下游补水5亿立方米
  10. 网线传输速度测试_千兆网络的速度测试
  11. 在ubuntu中添加widows启动项的简单方法
  12. 【论文阅读】Extract Free Dense Labels from CLIP
  13. latex 字母上面加符号
  14. 张先轶博士(OpenBLAS开源项目发起人,PerfXLab创始人)的采访录,中关村管委会千帆计划之“创见新面孔”专题采访活动
  15. 计算机的码片是指什么作用,计算机通信作业共有四个站进行码分多址的CDMA通信4个站的码片 爱问知识人...
  16. [附源码]java毕业设计点餐系统论文
  17. 聊一聊什么是SaaS,以及遇到的问题......
  18. 一维区间上高斯数值积分的MATLAB实现
  19. 国际大会演讲ppt_大会演讲,透明公正
  20. SpringBoot b2b2c 多用户商城系统

热门文章

  1. 傻瓜式操作的三个网络赚零花钱的小项目
  2. 云存储市场上演“新三国演义”
  3. unique函数_包含虚函数的类应该有虚析构函数或保护析构函数?
  4. linux分区用来支持虚拟内存,Linux分区方案
  5. WebSocket心跳检测和重连机制
  6. PHP获取指定月份第一天、最后一天
  7. java spring 连接池配置_Spring中常用的连接池配置
  8. 全网首发:JDK绘制文字:七、使用字体图像进行绘制
  9. 七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
  10. 分析华为毕昇JDK8:类数据共享CDS有效果,幅度很小