金融MATLAB实验报告三解析.doc
安徽财经大学金融证券实验室实 验 报 告实验课程名称 金融MATLAB 开 课 系 部 金融学院 班 级 学 号 姓 名 指 导 教 师 2015年*月*日实验名称MATLAB金融数量分析学院 学号姓名实验准备实 验 目 的Ø 学会使用MATLAB金融工具箱进行金融数量分析,如:期权定价分析、投资组合绩效分析、收益和风险的计算、有效前沿的计算、固定收益证券的久期和凸度计算、利率的期限结构、技术指标计算等。 实 验 设 计 方 案使用MATLAB金融工具箱对下述6个主题进行数量分析,数据来源自行在网上搜寻,要求是2014年之后的数据。(可参照各章的例题)1.期权定价分析 (第10章)2.收益、风险和有效前沿的计算 (第12章)3.投资组合绩效分析 (第13章)4.固定收益证券的久期和凸度计算 (第17章)5.利率的期限结构 (第18章)6.技术指标分析 (第22章)本实验报告不指定具体的题目,请大家自行设定,同学相互之间不要出现雷同。实验分析过程一、收益、风险和有效前沿的计算 从Wind咨询金融终端分别下载三只股票(美好集团、石油化服和首开股份)从2013年年初至今的日收盘价价格,经过相关处理得出三只股票的收益率均值、标准差以及协方差矩阵等数据,如下表。现根据表格数据进行关于收益、风险和有效前沿计算。1. 收益率和风险计算函数例1.假设等权重配置美好集团、石油化服、首开股份,计算资产组合的风险和收益。解:> >> ExpReturn=0.0018,0.0016,0.0006;ExpCovariiance=0.0010,0.0004,0.0005;0.0004,0.0017,0.0003;0.0005,0.0003,0.0013;PortWts=1/3*ones(1,3);PortRisk, PortReturn = portstats(ExpReturn, ExpCovariance,PortWts)PortRisk = 0.0170PortReturn = 0.00132.有效前沿计算函数例2.(1)怎样配置美好集团、石油化服和首开股份,则资产组合为最优组合?解:>> ExpReturn=0.0018,0.0016,0.0006;>> ExpCovariiance=0.0010,0.0004,0.0005;0.0004,0.0017,0.0003;0.0005,0.0003,0.0013;>> NumPorts=10;>> PortRisk, PortReturn, PortWts = frontcon(ExpReturn,ExpCovariance, NumPorts)PortRisk = 0.0137 0.0139 0.0144 0.0151 0.0158 0.0166 0.0175 0.0184 0.0194 0.0229PortReturn = 0.0006 0.0008 0.0009 0.0010 0.0012 0.0013 0.0014 0.0015 0.0017 0.0018PortWts = 0 0.0439 0.9561 0 0.1724 0.8276 0.0275 0.2678 0.7047 0.1032 0.3054 0.5914 0.1789 0.3430 0.4781 0.2546 0.3806 0.3647 0.3303 0.4183 0.2514 0.4060 0.4559 0.1381 0.4817 0.4935 0.02481.0000 0 0画图:frontcon(ExpReturn,ExpCovariance, NumPorts)(2)例2中如果各个资产投资上限为50%,求解有效前沿?>> ExpReturn=0.0018,0.0016,0.0006;ExpCovariiance=0.0010,0.0004,0.0005; 0.0004,0.0017,0.0003; 0.0005,0.0003,0.0013;NumPorts =10;AssetBounds=0,0,0;0.5,0.5,0.5;PortRisk, PortReturn, PortWts = frontcon(ExpReturn,ExpCovariance, NumPorts,AssetBounds)PortRisk = 0.0156 0.0160 0.0164 0.0168 0.0173 0.0177 0.0182 0.0187 0.0192 0.0197PortReturn = 0.0011 0.0012 0.0013 0.0013 0.0014 0.0014 0.0015 0.0016 0.0016 0.0017PortWts = 0.1196 0.3804 0.5000 0.1967 0.3519 0.4514 0.2344 0.3706 0.3949 0.2722 0.3894 0.3385 0.3099 0.4081 0.2820 0.3476 0.4268 0.2256 0.3853 0.4456 0.1691 0.4230 0.4643 0.1126 0.4608 0.4831 0.0562 0.5000 0.5000 0frontcon(ExpReturn,ExpCovariance, NumPorts,AssetBounds)3.约束条件下有效前沿例3. 例配置美好集团、石油化服、首开股份三个资产,美好集团最大配置60%,石油化服最大配置70%,首开股份最大配置50%,美好集团为资产集合A,石油化服、首开股份组成资产计划B,集合A的最大配置为70%,集合B的最大配置为50%,集合A的配置不能超过集合B的3倍,则如何配置?解:>> NumAssets = 3; ExpReturn=0.0018,0.0016,0.0006;ExpCovariiance=0.0010,0.0004,0.0005; 0.0004,0.0017,0.0003; 0.0005,0.0003,0.0013;NumPorts =5;PVal = 1;AssetMin = 0;AssetMax=0.6,0.7,0.5;GroupA = 1 0 0;GroupB = 0 1 1;GroupMax =0.7,0.5;AtoBmax = 3;ConSet = portcons('PortValue', PVal, NumAssets,'AssetLims',.AssetMin, AssetMax, NumAssets, 'GroupComparison',GroupA, NaN,.AtoBmax, GroupB,GroupMax );PortRisk, PortReturn, PortWts = portopt(ExpReturn, ExpCovariance, NumPorts, , ConSet)PortRisk = 0.0156 0.0165 0.0176 0.0187 0.0199PortReturn = 0.0011 0.0013 0.0014 0.0016 0.0017PortWts = 0.1196 0.3804 0.5000 0.2468 0.3768 0.3764 0.3346 0.4204 0.2450 0.4225 0.4640 0.11350.6000 0.4000 0画图>> portopt(ExpReturn, ExpCovariance,NumPorts, , ConSet)二、投资组合绩效分析从Wind咨询金融终端分别下载三只股票(华丽家族、华远地产、合肥城建)和一只指数(中证800)从2014年年初至今的日收盘价数据,加以处理整理。之后根据2013-2014年的数据,对华丽家族、华远地产、合肥城建、中证800进行投资组合绩效分析。解:1. 读取数据>> typ, desc, fmt = xlsfinfo('HHHZ.xlsX');>> data,textdate= xlsread('HHHZ.xlsX');>> HHHZ=data;>> save HHHZ HHHZ2. 三只股票和一只指数的精致曲线>> load HHHZ>> figure;>> hold on>> plot(HHHZ(:,1)/HHHZ(1,1),'k')>> plot(HHHZ(:,2)/HHHZ(1,2),'r-.')>> plot(HHHZ(:,3)/HHHZ(1,3),'bo')>> plot(HHHZ(:,4)/HHHZ(1,4),'g-')>> xlabel('time')>> ylabel('price')>> legend('中证800,华丽家族','华远地产','合肥城建')3. Beta与Alpha计算以中证800作为市场收益,使用portbeta函数分别计算华丽家族、华远地产、合肥城建的beta值。过程如下:(1) betaj计算load HHHZRate=price1ret(HHHZ);BSbeta=portbeta(Rate(:,4),Rate(:,1);(2)Alpha计算以中证800作为市场收益,使用portalpha函数分别计算华丽家族、华远地产、合肥城建的beta值。过程如下:>> load HHHZRate=price2ret(HHHZ);zz800=Rate(:,1);hljz=Rate(:,2);hydc=Rate(:,3);hfcj=Rate(:,4);daynum=fix(length(Rate)/2);cash=(1+0.03)(1/daynum)-1;cash=cash*ones(daynum,1);RatioHL2013=daynum*portalpha(hljz(1:daynum),zz800(1:daynum),cash,'capm')RatioHL2014=daynum*portalpha(hljz(daynum+1:2*daynum),zz800(daynum+1:2*daynum),cash,'capm')RatioHY2013=daynum*portalpha(hydc(1:daynum),zz800(1:daynum),cash,'capm')RatioHY2014=daynum*portalpha(hydc(daynum+1:2*daynum),zz800(daynum+1:2*daynum),cash,'capm')RatioHF2014=daynum*portalpha(hfcj(1:daynum),zz800(1:daynum),cash,'capm')RatioHF2013=daynum*portalpha(hfcj(daynum+1:2*daynum),zz800(daynum+1:2*daynum),cash,'capm')RatioHL2013 = 0.3119RatioHL2014 = 0.2903RatioHY2013 = 0.1122RatioHY2014 = 0.1566RatioHF2013 = 0.1046RatioHF2014 =0.03154. 夏普比率计算华丽家族、华远地产、合肥城建的夏普比率>> load HHHZRate=price2ret(HHHZ);hljz=Rate(:,2);hydc=Rate(:,3);hfcj=Rate(:,4);daynum=fix(length(Rate)/2);Cash=(1+0.03)(1/daynum)-1;RatioHL2013=sharpe(hljz(1:daynum),Cash)RatioHL2014=sharpe(hljz(daynum+1:2*daynum),Cash)RatioHYDC2013=sharpe(hydc(1:daynum),Cash)RatioHYDC2014=sharpe(hydc(daynum+1:2*daynum),Cash)RatioHF2013=sharpe(hfcj(1:daynum),Cash)RatioHF2014=sharpe(hfcj(daynum+1:2*daynum),Cash)结果:RatioHL2013 = 0.0768RatioHL2014 = 0.0549RatioHYDC2013 = 0.0493RatioHYDC2014 = 0.0460RatioHF2013 = 0.0876RatioHF2014 =0.03775. 信息比率以中证800指数作为业绩比较基准,计算华丽家族、华远地产、合肥城建成长的信息比率编码:>> load HHHZRate=price2ret(HHHZ);zz800=Rate(:,1);hljz=Rate(:,2);hydc=Rate(:,3);hfcj=Rate(:,4);daynum=fix(length(Rate)/2);RatioHL2013=inforatio(hljz(1:daynum),zz800(1:daynum)RatioHL2014=inforatio(hljz(daynum+1:2*daynum),zz800(daynum+1:2*daynum)RatioHYDC2013=inforatio(hydc(1:daynum),zz800(1:daynum)RatioHYDC2014=inforatio(hydc(daynum+1:2*daynum),zz800(daynum+1:2*daynum)RatioHF2013=inforatio(hfcj(1:daynum),zz800(1:daynum)RatioHF2014=inforatio(hfcj(daynum+1:2*daynum),zz800(daynum+1:2*daynum)计算结果:RatioHL2013 = 0.0115RatioHL2014 = -0.0107RatioHYDC2013 = -0.0279RatioHYDC2014 = -0.0171RatioHF2013 = -0.0455RatioHF2014 = -0.04866. 最大回撤根据华丽家族的数据计算最大回撤>> load HHHZTRate=HHHZ(:,2)/HHHZ(1,2)-1;MaxDD,MaxDDIndex=maxdrawdown(TRate,'arithmetic')plot(TRate)hold onplot(MaxDDIndex,TRate(MaxDDIndex),'r-o','MarkerSize',10)计算结果MaxDD = 2.3110MaxDDIndex = 359 4177. 市场指数变化的最大回撤图根据中证800指数数据画出最大收益回撤>> load HHHZZZ800price=HHHZ(:,1);N=length(ZZ800price);RetraceRatio=zeros(N,1);for i=2:N C=max(ZZ800price(1:i); if C=ZZ800price(i) RetraceRatio(i)=0; else RetraceRatio(i)=(ZZ800price(i)-C)/C; endendTRate=ZZ800price/ZZ800price(1)-1;f=figure;fill(1:N,N,RetraceRatio;0,'r')hold onplot(TRate);xlabel('time')ylabel('Rate/RetraceRatio')三、固定收益证券的久期和凸度计算从wind资讯金融终端下载三只国债的相关信息如下,进行国定收益证券的久期和凸度的计算。证券编号固定收益证券到期收益率票面利率结算日到期日计息方式15000715附息国债073.0450 3.5400 16-Apr-1516-Apr-22每年付息一次15002615附息国债263.0600 3.0500 22-Oct-1522-Oct-22每年付息一次15001915附息国债192.9050 3.1400 8-Sep158-Sep-20每年付息一次1. 三种债券的价格及计算日的利息计算(1)15附息国债07的价格和结算日利息计算>> Yield=0.03045;CouponRate=0.0354;Settle='16-Apr-2015'Maturity='16-Apr-2022'Period=1;Basis=0;Price,AccruedInt=bndprice(Yield,CouponRate,Settle,Maturity,Period,Basis)Price = 102.9320AccruedInt = 0(2)15附息国债26的价格和结算日利息计算>> Yield=0.036;CouponRate=0.0305;Settle='22-Oct-2015'Maturity='22-Oct-22'Period=1;Basis=0;Price,AccruedInt=bndprice(Yield,CouponRate,Settle,Maturity,Period,Basis)Price = 96.4564AccruedInt = 0(2)15附息国债19的价格和结算日利息计算>> Yield=0.02905;CouponRate=0.0314;Settle='8-Sep-2015'Maturity='8-Sep-2020'Period=1;Basis=0;Price,AccruedInt=bndprice(Yield,CouponRate,Settle,Maturity,Period,Basis)Price = 100.9817AccruedInt = 02. 根据债券价格计算久期(1)15附息国债07的久期计算PRICE=102.9320;CouponRate=0.0354;Settle='16-Apr-2015'Maturity='16-Apr-2022'Period=1;Basis=0;ModDuration, YearDuration, PerDuration=bnddurp(Price,CouponRate, Settle, Maturity, Period, Basis)ModDuration = 6.2212YearDuration = 6.3255PerDuration = 12.6509(2)15附息国债26的久期计算>> PRICE=96.4564;CouponRate=0.0305;Settle='22-Oct-2015'Maturity='22-Oct-22'Period=1;Basis=0;ModDuration, YearDuration, PerDuration=bnddurp(Price,CouponRate, Settle, Maturity, Period, Basis)ModDuration = 6.3209YearDuration = 6.4116PerDuration = 12.8233(3)15附息国债19的久期计算>> Price=100.9817;CouponRate=0.0314;Settle='8-Sep-2015'Maturity='8-Sep-2020'Period=1;Basis=0;ModDuration, YearDuration, PerDuration=bnddurp(Price,CouponRate, Settle, Maturity, Period, Basis)ModDuration = 4.6390YearDuration = 4.7064PerDuration =9.41273.根据债券收益率计算久期(1)15附息国债07的久期计算>> Yield=0.03045;CouponRate=0.0354;Settle='16-Apr-2015'Maturity='16-Apr-2022'Period=1;Basis=0;ModDuration, YearDuration, PerDuration=bnddurp(Yield,CouponRate, Settle, Maturity, Period, Basis)ModDuration = 0.0931YearDuration = 1.0086PerDuration = 2.0172(2)15附息国债26的久期计算>> Yield=0.036;CouponRate=0.0305;Settle='22-Oct-2015'Maturity='22-Oct-22'Period=1;Basis=0;ModDuration, YearDuration, PerDuration=bnddurp(Yield,CouponRate, Settle, Maturity, Period, Basis)ModDuration = 0.1093YearDuration = 1.0118PerDuration =2.0236(3)15附息国债19的久期计算>> Yield=0.02905;CouponRate=0.0314;Settle='8-Sep-2015'Maturity='8-Sep-2020'Period=1;Basis=0;ModDuration, YearDuration, PerDuration=bnddurp(Yield,CouponRate, Settle, Maturity, Period, Basis)ModDuration = 0.0966YearDuration = 1.0093PerDuration =2.01853. 根据价格计算凸度(1)15附息国债07的凸度计算>> Price=102.9320;CouponRate=0.0354;Settle='16-Apr-2015'Maturity='16-Apr-2022'Period=1;Basis=0;YearConvexity,PerConvexity=bndconvp(Price,CouponRate, Settle, Maturity, Period, Basis)YearConvexity = 44.4010PerConvexity = 177.6039(2)15附息国债26的凸度计算>> Price=96.4564;CouponRate=0.0305;Settle='22-Oct-2015'Maturity='22-Oct-22'Period=1;Basis=0;YearConvexity,PerConvexity=bndconvp(Price,CouponRate, Settle, Maturity, Period, Basis)YearConvexity = 44.7740PerConvexity = 179.0958(3)15附息国债19的凸度计算>> Price=100.9817;CouponRate=0.0314;Settle='8-Sep-2015'Maturity='8-Sep-2020'Period=1;Basis=0;YearConvexity,PerConvexity=bndconvp(Price,CouponRate, Settle, Maturity, Period, Basis)YearConvexity = 24.5867PerConvexity = 98.34674.根据收益率计算凸度(1)15附息国债07的凸度计算>> Yield=0.03045;CouponRate=0.0354;Settle='16-Apr-2015'Maturity='16-Apr-2022'Period=1;Basis=0;YearConvexity,PerConvexity=bndconvp(Price,CouponRate, Settle, Maturity, Period, Basis)YearConvexity = 44.1973PerConvexity = 176.7893(2)15附息国债26的凸度计算>> Yield=0.036;CouponRate=0.0305;Settle='22-Oct-2015'Maturity='22-Oct-22'Period=1;Basis=0;YearConvexity,PerConvexity=bndconvy(Price,CouponRate, Settle, Maturity, Period, Basis)YearConvexity = 5.6626e-04PerConvexity = 0.0023(3)15附息国债19的凸度计算>> Yield=0.02905;CouponRate=0.0314;Settle='8-Sep-2015'Maturity='8-Sep-2020'Period=1;Basis=0;YearConvexity,PerConvexity=bndconvy(Price,CouponRate, Settle, Maturity, Period, Basis)YearConvexity = 5.6626e-04PerConvexity = 0.0023实验结论 实验结果评价成绩: 指导教师签字: