您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > 数值计算与金融仿真matlab程序总结(双面打印)
变量合法命名:只含字母、数字、下划线,并以字母开头who显示定义变量whos显示定义变量及其信息lookfor“?”查询与关键词有关的所有函数Integer整数real实数complex复数inf无限大NaN非数字formatlong显示多位小数short4位bank2位round()四舍五入fix()去小数部分prod()连乘asin()transpose()或矩阵矩阵转置eye(4)4行4列单位矩阵q13(2,4)矩阵第二行第四列数据q13(2:3,2:3)矩阵q132到3行,2到3列的数据.*对应元素相乘同理./.^a:b:c以a开始,以b为间隔,最大数小于等于c的数列sum(A)对列求和(得行)sum(A,2)对行sum(sum(A))对列求和再对行求prod(A)对列连乘min(A)输出每一列的最小值[m,j]=max(B),m=最大值取值,j=最大值位置判断:a==ba等于ba~=ba不等于b正确输出1.plot(x,y,'(颜色)(形状)')linspace(a,b,n)把起点为a终点为b的直线等分为n份plot(x,y,'.',x,cos(3*pi*x),'g*')两条线一图legend('Sincurve','Coscurve')加图标grid加格子holdon保留原曲线,可用于画多线在一图subplot(121),plot(x,y)将作图区域分为1行2列,作第1个区域的图N=100;h=1/N;x=0:h:1;y=cos(3*pi*x);plot(x,y)x=linspace(0,1,101);y=sin(3*pi*x);plot(x,y)rand(3)3行方阵随机矩阵randnr6=random('Poisson',6,1,6)文档读写IBM=xlsread('IBM.xls');IBM=textread('IBM.txt');IBM=dlmread('IBM.txt','',1,1);IBM=importdata('IBM.xls');m文件whichmfile(文件名)查看路径editmfile编辑脚本mfile与函数mfile的区别,前者是全局变量,后者是局部变量function[var1var2…]=functionname(arg1,arg2,…)多个图形x=-1:.05:1;forn=1:8subplot(4,2,n);plot(x,sin(n*pi*x));end求和s=0fori=1:100s=s+i;endsum能以1^2+2^2+…+n^2表示并小于100的?S=1;n=1;whileS+(n+1)^2100n=n+1;S=S+n^2;end[n,S]If...ThenLoopS=1;n=1;fori=1:100;ifS+(n+1)^2100S=S+(n+1)^2;n=n+1;endend[n,S]Chapter2现金流分析pvvar求变动现金流量的现值pvfixfvvarfvfixPV=pvvar(CashFlow,Rate,CFDates)CashFlow=[100300450]CFDates=['01/12/1987';'02/14/1988';'03/03/1988']PV=pvvar(CashFlow,0.09,CFDates)日期列数据pvvar([-150003000450050006800],0.08)ans=603.1667没有现金流日期时,默认相隔一年Irr固定周期的内部收益率Xirr变动周期的内部收益率Return=irr(CashFlow)Return=xirr(CashFlow,Dates)CF=[-10000300045005000];Return=irr(CF)Return=0.1105Dates=['01/12/00';'02/14/01';'09/03/01';'12/31/02'];Return=xirr(CF,Dates)Return=0.1177cfdurandcfconv久期和凸性CashFlow=[55555105];[Dur,ModDur]=cfdur(CashFlow,0.05)收益Dur=5.3295ModDur=5.0757Conv=cfconv(CF,0.05)Conv=95.5410债券价格和收益率[P,I应付利息]=bndprice(Yield,CouponRate票面利率,‘Settle’交收日,‘Maturity’到期)p=price+accruedintYield=bndyield(Price,CouponRate,Settle,Maturity)Duration=bnddurp(Price,CouponRate,Settle,Maturity)Duration=bnddury(Yield,CouponRate,Settle,Maturity)Convexity=bndconvp(Price,CouponRate,Settle,Maturity)p-yPrice-yieldcurveyields=0.01:0.01:0.20;[PI]=bndprice(yields,0.1,'08/10/07','12/31/20');plot(yields,P+I);gridon;xlabel('Yield');ylabel('Price');Title('Price-YieldCurve');利率免疫duration=holdingperiodsettle='28-Aug-2007';maturity=['15-Jun-2012';'31-Oct-2017';'01-Mar-2027'];couponRate=[0.07;0.06;0.08];yield=[0.06;0.07;0.075];duration=bnddury(yield,couponRate,settle,maturity);convexity=bndconvy(yield,couponRate,settle,maturity);%COMPUTEPORTFOLIOWEIGHTSA=[duration';convexity';111];b=[10;160;1];weights=A\b有效前沿[PortRisk,PortReturn,PortWts]=frontcon(ExpReturn历史收益率,ExpCovariance历史协方差,NumPorts返回结果的个数,PortReturn目标收益,AssetBounds资产界限,Groups,GroupBounds组合界限)r=[0.20.1];两种资产历史收益率s=[0.2-0.1;-0.1,0.4];历史协方差[Risk,Return,Wts]=frontcon(r,s,5);[Risk,Return,Wts]ans=0.29580.16250.62500.37500.30750.17190.71870.28130.34000.18120.81250.18750.38830.19060.90620.09380.44720.20001.00000最后两列为权重含约束条件的有效前沿[PortRisk,PortReturn,PortWts]=portopt(Return,Cov,[],PortReturn,ConSet)约束条件ConSet=portcons('Default',Num,'AssetLims',Min,Max,'GroupLims',Group,GroupMin,GroupMax);r=[0.10.20.15];s=[0.0100,-0.0061,0.0042;-0.0061,0.0400,-0.0252;0.0042,-0.0252,0.0225];ObjReturn=0.17;[Risk,Return,Wts]=portopt(r,s,[],ObjReturn)Return=0.1700Wts=0.02780.42780.5445Return=[0.10.20.15];Cov=0.01*[0.5,-1,0.4;-1,4,-0.2;0.4,-0.2,2.3];PortReturn=[0.150.16];目标收益率在0.15~0.16之间Num=3;Min=[0.20NaNNaN];投资组合中每种资产的下限Max=[0.50.50.5];……上限Group=[110;011];分为两组,第一组由第1、2种资产构成;第二组……GroupMin=[0.2,0.2];第一组的投资比例下限为0.2;二……GroupMax=[0.8,0.8];第一组的投资比例上限为0.8;二……ConSet=portcons('Default',Num,'AssetLims',Min,Max,'GroupLims',Group,GroupMin,GroupMax);[PortRisk,PortReturn,PortWts]=portopt(Return,Cov,[],PortReturn,ConSet)PortRisk=0.0724;0.0910PortReturn=0.1500;0.1600PortWts=0.40000.40000.20000.26060.46060.2789可借贷无风险资产时的有效前沿[RiskyRisk,RiskyReturn,RiskyWts,RiskyFraction所有风险资产占总资产的比例,OverallRisk,OverallReturn]=portalloc(PortRisk,PortReturn,PortWts,RisklessRate,BorrowRate,RiskAversion风险厌恶程度)Ret=[0.10.20.15];Cov=[0.005-0.0100.004;-0.0100.040-0.002;0.004-0.0020.023];[Risk,Return,Wts]=frontcon(Ret,Cov,20);RiskfreeR=0.08;BorrowR=0.12;RiskAversion=3;portalloc(Risk,Return,Wts,RiskfreeR,BorrowR,RiskAversion);(returnthegraphic)[RiskyRisk,RiskyReturn,RiskyWts,RiskyFraction,OverallRisk,OverallReturn]=portalloc(Risk,Return,Wts,RiskfreeR,BorrowR,RiskAversion)最优化问题[x,fval]=fmincon(fun,目标函数,x0,从x0开始试值,A,b,Aeq,beq,lb,ub,nonlcon非线性约束,options)options=optimset('LargeScale','off')注意:x1要加括号x(1);目标函数要建立m文件;非线性约束要建立m文件程序1:function[f]=Myobj(x)f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+x(2)+1);2:写约束条件function[c,ceq]=Mycon(x)%Nonlinearinequalityconstraintsc=[1.5+x(1)*x(2)-x(1)-x(2);-x(1)*x(2)-10];%Nonlinearequalityconstraintsceq=[];Step3:调用最优化路径:A=[11;2-1];b=[20;10];Aeq=[];beq=[];lb=[-10-10];ub=[10,10];x0=[-1,1];%Makeastartingguessatthesolutionoptions=optimset('LargeScale','off');[x,fval]=fmincon(@Myobj,x0,A,b,Aeq,
本文标题:数值计算与金融仿真matlab程序总结(双面打印)
链接地址:https://www.777doc.com/doc-4460471 .html