加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

投资组合最优化计算

(2009-10-23 10:17:42)
标签:

杂谈

分类: 最优化计算
   计算投资组合的有效边界,资本市场线,证券市场线,alpha,beta,基本上是投资分析学的基本要求,大部分教科书都是用EXCEL来做,但EXCEL总的来所处理大数据还是欠缺了点。对于教科书上几支股票当然好处理,一当处理几百上千股票,要从数据库直接调用数据来分析,还得借助第三方软件来处理。然而这点对于SAS来说,当然不在话下。
    SAS里的最优化计算源于其中的OR产品,在IML亦有一些子程序可以调用。两者的区别:

The advantages of the IML procedure are as follows:

  • You can use matrix algebra to specify the objective function, nonlinear constraints, and their derivatives in IML modules.
  • The IML procedure offers several subroutines that can be used to specify the objective function or nonlinear constraints, many of which would be very difficult to write for the NLP procedure.
  • You can formulate your own termination criteria by using the "ptit" module argument.
The advantages of the NLP procedure are as follows:
  • Although identical optimization algorithms are used, the NLP procedure can be much faster because of the interactive and more general nature of the IML product.
  • Analytic first- and second-order derivatives can be computed with a special compiler.
  • Additional optimization methods are available in the NLP procedure that do not fit into the framework of this package.
  • Data set processing is much easier than in the IML procedure. You can save results in output data sets and use them in following runs.
  • The printed output contains more information.

    这里介绍了求有效边界和资本市场线的一个小例子。
    有效边界的计算来自于最优化里面的二次规划:有如下形式:
    http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq1.gif
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq2.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq3.gif is the quadratic (also known as Hessian) matrix
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq4.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq5.gif is the constraints matrix
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq6.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq7.gif is the vector of decision variables
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq8.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq7.gif is the vector of linear objective function coefficients
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq9.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq10.gif is the vector of constraints right-hand sides (RHS)
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq11.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq7.gif is the vector of lower bounds on the decision variables
  http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq12.gif   http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq7.gif is the vector of upper bounds on the decision variables

    Nlpqua是IML里用于二次规划的子程序,语法为
    CALL NLPQUA( rc, xr, quad, x0 <,opt, blc, tc, par, "ptit", lin>);

其中:(大部分参数是NLP所有子程序所相同的,不同的子程序还有个别的不同参数,像这里的quad)
  
Optimization Subroutines
Conjugate Gradient Optimization Method
CALL NLPCG( rc, xr, "fun", x0 <, opt, blc, tc, par, "ptit", "grd">);
Double Dogleg Optimization Method
CALL NLPDD( rc, xr, "fun", x0 <,opt, blc, tc, par, "ptit", "grd">);
Nelder-Mead Simplex Optimization Method
CALL NLPNMS( rc, xr, "fun", x0 <,opt, blc, tc, par, "ptit", "nlc">);
Newton-Raphson Optimization Method
CALL NLPNRA( rc, xr, "fun", x0 <,opt, blc, tc, par, "ptit", "grd", "hes">);
Newton-Raphson Ridge Optimization Method
CALL NLPNRR( rc, xr, "fun", x0 <,opt, blc, tc, par, "ptit", "grd", "hes">);
(Dual) Quasi-Newton Optimization Method
CALL NLPQN( rc, xr, "fun", x0 <,opt, blc, tc, par, "ptit", "grd", "nlc", "jacnlc">);
Quadratic Optimization Method
CALL NLPQUA( rc, xr, quad, x0 <,opt, blc, tc, par, "ptit", lin>);
Trust-Region Optimization Method
CALL NLPTR( rc, xr, "fun", x0 <,opt, blc, tc, par, "ptit", "grd", "hes">);


Least-Squares Subroutines
Hybrid Quasi-Newton Least-Squares Methods
CALL NLPHQN( rc, xr, "fun", x0, opt <,blc, tc, par, "ptit", "jac">);
Levenberg-Marquardt Least-Squares Method
CALL NLPLM( rc, xr, "fun", x0, opt <,blc, tc, par, "ptit", "jac">);


Supplementary Subroutines
Approximate Derivatives by Finite Differences
CALL NLPFDD( f, g, h, "fun", x0 <,par, "grd">);
Feasible Point Subject to Constraints
CALL NLPFEA( xr, x0, blc <,par>);

    rc 返回关于最优化计算终止的原因,取值为正(整数1到10,分别说明不同的终止原因)说明成功地
       进行了最优化计算,取值为负(-1到-10),说明最优化计算不成功。
    xr 返回x的最优化取值。
    quad: 输入变量,为 http://support.sas.com/onlinedoc/913/getDoc/en/ormpug.hlp/images/qpsolver_qpsolvereq2.gif 
    x0:   输入变量,提供x迭代的初值。
    opt:  可选输入向量,向量最大长度11,可指定一些计算过程的选项,如pot[1]取值0表示最小化
           目标函数,1表示最大化。
    blc:  可选输入向量,确定x的边界和线性约束条件。        
  • The matrix input argument "blc" enables you to specify boundary and general linear constraints.
  • The IML module input argument "nlc" enables you to specify general constraints, particularly nonlinear constraints.
数据集return代码如下的链接。

http://blog.sina.com.cn/s/blog_62615f170100fooa.html


*------------------求预期收益下的风险最小投资组合-------------------*;
proc corr data=return cov outp=Cov_out ;
var x1-x8;
quit;
proc iml;
 use Cov_out;
 read ALL var _NUM_ where(_type_="COV") into cov;
 read all var _num_ where(_type_='MEAN') into mean;
close Cov_out;

 quad=2#cov;                         *二次规划里的G;
 x=j(1,8,1/8);                       *X初值;
 optn={0 2};
 blc={0. 0. 0. 0. 0. 0. 0. 0.  . ,
      1. 1. 1. 1. 1. 1. 1. 1.  . ,
      1. 1. 1. 1. 1. 1. 1. 1.  0. 1.};
 conexp_re=mean||{1 0.014};
 blc=blc//conexp_re;
 call nlpqua(rc,xest,quad,x,optn,blc);

quit;
最优结果为:                                 XEST

   0.2879656     0    0.2592307     0     0.3274852   0.0674204     0.0578981
方差为:
                        Value of Objective Function = 0.0092786879

blc ,通常有N+2列,N表示结构参数的个数,n+1列表示约束的方向(0表示=,1表示>=,-1表示=<;),n+2列表示约束条件的右边项(right hand side),low1,2表示x1-x8的上下界,low3表示组合的权重约束,low4表示组合的预期收益约束条件。
quad, 注意这里是2倍于协方差矩阵。


*---------------求过无风险利率与有效边界相切时对应的有效组合------------*;
*---------------即最大化定义的函数object--------------------------------------*;
proc iml;
 use Cov_out;
 read ALL var _NUM_ where(_type_="COV") into COV;
read all var _num_ where(_type_='MEAN') into mean;
close Cov_out;

start object(x) global(mean, COV);
    riskfree=0.0042;
    fobj=(x*t(mean)-riskfree)/sqrt(x*COV*t(x));     *斜率最大化;
    return(fobj);
 finish;

 x=j(1,8,0);
optn={1 2};
 con={0. 0. 0. 0. 0. 0. 0. 0.  . ,
      1. 1. 1. 1. 1. 1. 1. 1.  . ,
      1. 1. 1. 1. 1. 1. 1. 1.  0. 1.};
 call nlpnra(rc,xest,"object",x,optn,con);
 quit;
结果为:
                                                                  Gradient    Active
                                                                 Objective    Bound
                              N Parameter         Estimate        Function    Constraint

                              1 X1                0.603799        0.034043
                              2 X2            6.600081E-18       -0.043763     Lower BC
                              3 X3            -5.59042E-19       -0.038213     Lower BC
                              4 X4            -8.19928E-19       -0.024777     Lower BC
                              5 X5                0.033167        0.034044
                              6 X6            -1.84992E-18       -0.020329     Lower BC
                              7 X7                0.363033        0.034043
                              8 X8             -4.8823E-18        0.008635     Lower BC

                                      Value of Objective Function = 0.1700929602




0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有