
http://www.ma-xy.com
第一章 二次规划 1.1 问题的引入与分析
7 r = 0 . 0 0 2 ;%获 利 下 限
8 Aeq = ones ( 1 , nAssets ) ;
9 beq = 1 ;
10 Aineq = −mean_return ’ ;
11 bineq = −r ;
12 lb = z e r os ( nAssets , 1 ) ;
13 ub = ones ( nAssets , 1 ) ;
14 c = ze r o s ( nAssets , 1 ) ;
15 opt io ns = optimoptions ( ’ quadprog ’ , ’ Algorithm ’ , ’ i n t e r i o r −point−convex ’ ) ;
16 opt io ns = optimoptions ( options , ’ Display ’ , ’ i t e r ’ , ’TolFun ’ ,1 e−10) ;
17 [ x1 , f v a l 1 ] = quadprog ( Covariance , c , Aineq , bineq , Aeq , beq , lb , ub , [ ] , op tio ns ) ;
18 plotPortfDemoStandardModel ( x1 )
19 % 分 组 投 资 225−Asset Problem with Group Co ns traints
20 % 新 的 投 资 组 合 要 求 :
21 % sum_1_75 xi >=0.3;sum_76_150 xi >=0.3;sum_151_225 xi >=0.3
22 Groups = blkdiag ( ones ( 1 , nAssets /3) , ones (1 , nAssets /3) , ones (1 , nAssets /3) ) ;
23 Aineq = [ Aineq ; −Groups ] ; % co nve rt to <= c o nst r a i n t
24 bineq = [ bineq ; −0.3* ones (3 ,1) ] ; % by changing s i g n s
25 [ x2 , f v a l 2 ] = quadprog ( Covariance , c , Aineq , bineq , Aeq , beq , lb , ub , [ ] , op tio ns ) ;
26 % Plot r e s u l t s , superimposed to r e s u l t s from previo us problem .
27 plotPortfDemoGroupModel ( x1 , x2 ) ;
28 %% 1000 支 股 票
29 % 为 了 展 示 quadprog 的 内 点 算 法 能 解 决 更 大 的 问 题 , 我 们 使 用 一 个 随 机 生 成 1000 的 数 据 集 。
30 % 我 们 生 成 一 个 随 机 相 关 矩 阵 ( 对 称 半 正 定 , 对 角 线 ) 。
31 % Reset random stream f o r r e p r o d u c i b i l i t y .
32 rng ( 0 , ’ t w i s t e r ’ ) ;
33 nAssets = 1000;
34 % Generate means o f r e turn s between −0.1 and 0 . 4 .
35 a = −0.1;
36 b = 0 . 4 ;
37 mean_return = a + (b−a) . * rand ( nAssets , 1 ) ;
38 % Generate standard d e v i a t i o n s of r e t urns between 0 .0 8 and 0 . 6 .
39 a = 0 . 0 8 ;
40 b = 0 . 6 ;
41 stdDev_return = a + (b−a ) . * rand ( nAssets , 1 ) ;
42 % Corre l a t ion matrix , generated using C orre l a t ion = ga l l e r y ( ’ randcorr ’ , nAssets ) .
43 % ( Generating a c o r r e l a t i o n matrix of t h i s s i z e takes a while , so we load
44 % a pre−generated one i n s tea d . )
45 load ( ’ correlationMatrixDemo . mat ’ , ’ Cor r e lati o n ’ ) ;
46 % C alc ul ate co varia nce matrix from c o r r e l a t i o n matrix .
47 Covariance = C orre l a tion . * ( stdDev_return * stdDev_return ’ ) ;
48 % Define and Solve Randomly Generated 1000−Asset Problem
49 % We now d e f i n e the standard QP problem ( no group co n s t r a i n t s here ) and s o l v e .
50 r = 0 . 1 5 ;
51 Aeq = ones ( 1 , nAssets ) ;
52 beq = 1 ;
53 Aineq = −mean_return ’ ;
54 bineq =
−
r ;
55 lb = z e r os ( nAssets , 1 ) ;
56 ub = ones ( nAssets , 1 ) ;
57 c = ze r o s ( nAssets , 1 ) ;
58 x3 = quadprog ( Covariance , c , Aineq , bineq , Aeq , beq , lb , ub , [ ] , o pt ion s ) ;
59
http://www.ma-xy.com 3 http://www.ma-xy.com