% Boyd & Vandenberghe "Convex Optimization"
% Joëlle Skaf - 04/24/08
% (a figure is generated)
%
% The investment strategy x that maximizes the long term growth rate
%           R = sum_{j=1}^m pi_j*log(p_j^Tx)
% is called the log-optimal investment strategy, and can be found by
% solving the optimization problem
%           maximize    sum_{j=1}^m pi_j log(p_j^Tx)
%           subject to  x >= 0, sum(x) = 1,
% where p_ij is the return of asset i over one period in scenario j and
% pi_j is the probability of scenario j. There are n assets and m scenarios.
% We consider the case of equiprobable scenarios.
%
% The log-optimal long term growth rate is found and compared to the one
% obtained with a uniform allocation strategy, i.e., x_i=(1/n).
% Sample trajectories ofthe accumulated wealth for the optimal strategy and
% the uniform one are plotted.


% Input data
P = [3.5000    1.1100    1.1100    1.0400    1.0100;
     0.5000    0.9700    0.9800    1.0500    1.0100;
     0.5000    0.9900    0.9900    0.9900    1.0100;
     0.5000    1.0500    1.0600    0.9900    1.0100;
     0.5000    1.1600    0.9900    1.0700    1.0100;
     0.5000    0.9900    0.9900    1.0600    1.0100;
     0.5000    0.9200    1.0800    0.9900    1.0100;
     0.5000    1.1300    1.1000    0.9900    1.0100;
     0.5000    0.9300    0.9500    1.0400    1.0100;
     3.5000    0.9900    0.9700    0.9800    1.0100];

[m,n] = size(P);
Pi = ones(m,1)/m;
x_unif = ones(n,1)/n; % uniform resource allocation

% Find the log-optimal investment policy
cvx_begin
    variable x_opt(n)
    maximize sum(Pi.*log(P*x_opt))
    sum(x_opt) == 1
    x_opt >= 0
cvx_end

% Long-term growth rates
R_opt = sum(Pi.*log(P*x_opt));
R_unif = sum(Pi.*log(P*x_unif));
display('The long term growth rate of the log-optimal strategy is: ');
disp(R_opt);
display('The long term growth rate of the uniform strategy is: ');
disp(R_unif);

% Generate random event sequences
rand('state',10);
N = 10;  % number of random trajectories
T = 200; % time horizon
w_opt = []; w_unif = [];
for i = 1:N
    events = ceil(rand(1,T)*m);
    P_event = P(events,:);
    w_opt = [w_opt [1; cumprod(P_event*x_opt)]];
    w_unif = [w_unif [1; cumprod(P_event*x_unif)]];
end

% Plot wealth versus time
figure
semilogy(w_opt,'g')
hold on
semilogy(w_unif,'r--')
grid
axis tight
xlabel('time')
ylabel('wealth')
 
Calling Mosek 9.1.9: 36 variables, 15 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 15              
  Cones                  : 10              
  Scalar variables       : 36              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 1
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 15              
  Cones                  : 10              
  Scalar variables       : 36              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 4
Optimizer  - Cones                  : 10
Optimizer  - Scalar variables       : 35                conic                  : 30              
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 10                after factor           : 10              
Factor     - dense dim.             : 0                 flops                  : 4.58e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.0e+00  1.1e+00  1.5e+01  0.00e+00   1.350234740e+01   -8.051020016e-01  1.0e+00  0.00  
1   1.9e-01  2.1e-01  9.3e-01  6.72e-01   1.712662212e+00   -1.181631184e+00  1.9e-01  0.01  
2   4.7e-02  5.2e-02  1.1e-01  2.36e+00   2.856263594e-01   -9.752726878e-02  4.7e-02  0.01  
3   6.3e-03  7.0e-03  5.2e-03  1.18e+00   5.192195689e-02   4.471645465e-03   6.3e-03  0.01  
4   2.6e-04  2.9e-04  4.2e-05  1.03e+00   2.411322106e-02   2.216544110e-02   2.6e-04  0.01  
5   2.1e-05  2.3e-05  9.4e-07  1.00e+00   2.315827542e-02   2.300255456e-02   2.1e-05  0.01  
6   1.5e-06  1.6e-06  1.8e-08  1.00e+00   2.308374137e-02   2.307274665e-02   1.5e-06  0.01  
7   1.6e-07  1.8e-07  6.3e-10  1.00e+00   2.307890232e-02   2.307770840e-02   1.6e-07  0.01  
8   1.7e-08  1.9e-08  2.2e-11  1.00e+00   2.307838501e-02   2.307825640e-02   1.7e-08  0.01  
9   6.8e-09  7.6e-09  5.1e-12  1.13e+00   2.307834382e-02   2.307829450e-02   7.1e-09  0.01  
10  4.7e-09  3.2e-09  1.3e-12  1.11e+00   2.307832859e-02   2.307830820e-02   3.3e-09  0.01  
11  6.9e-09  1.7e-09  5.0e-13  9.89e-01   2.307832350e-02   2.307831275e-02   1.9e-09  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 2.3078323495e-02    nrm: 1e+00    Viol.  con: 2e-09    var: 4e-10    cones: 0e+00  
  Dual.    obj: 2.3078312755e-02    nrm: 1e+00    Viol.  con: 0e+00    var: 7e-10    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 11        time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0230783
 
The long term growth rate of the log-optimal strategy is: 
    0.0231

The long term growth rate of the uniform strategy is: 
    0.0114