Portfolio Optimization : R - PortfolioAnalytics


PortfolioAnalytics in R can be used to specify a portfolio object, add constraints and objects, and run optimizations models.

1.   Data : For constructing Portfolio models using PortfolioAnalytics, top 6 companies of L&T India value fund were selected. ( as per the following latest reports.)

Share prices of these six companies were down loaded from BSE website . The daily prices for last ten years ( Jan 2007 to July 2017 ) were considered for the current study.

2. The returns of data points were derived from taking log transformation of share prices. The partial list of such returns is given below;

3.Creating the Portfolio Object : The portfolio object can be constructed by using the portfolio.spec function. The main argument to portfolio.spec is assets, this is a required argument.. If initial weights are not specified, an equal weight portfolio will be assumed.When first created, the portfolio object has an element named assets with the initial weights, an element named category-labels, an element named weight_seq with sequence of weights if specified, an empty constraints list and an empty objectives list.

                              $assets

                              Reliance  HDFC     ITC          ICICI

                              0.25        0.25        0.25        0.25

4. Adding Constraints to the Portfolio : The add.constraint function is the main interface for adding and/or updating constraints to the portfolio object. This function allows the user to specify the portfolio to add the constraints to, the type of constraints, arguments for the constraint, and whether or not to enable the constraint (enabled=TRUE is the default).

4.1 Sum of Weights Constraint : The weight_sum constraint specifies the constraint on the sum of the weights.We add a constraint that the weights must sum to 1, or the full investment constraint.

The sum of the weights equal 1, i.e. the full investment constraint. The full investment constraint can be specified with type="full_investment". This automatically sets min_sum=1 and max_sum=1.

4.2 Box Constraint : To specify upper and lower bounds on the weights of the assets. Here we add box constraints for the asset weights so that the minimum weight of any asset must be greater than or equal to 0.05 and the maximum weight of any asset must be less than or equal to 0.4.

(Note :  Minimum and Maximum weights can be specified for each assets separately.)

4.3 Group Constraint : Group constraints are currently supported by the ROI, DEoptim, and random portfolio solvers. The first 3 assets are grouped together labeled GroupA and the fourth asset is in its own group labeled GroupB.

Group-Min : The sum of the weights in GroupA must be greater than or equal to 0.1 and the sum of the weights in GroupB must be greater than or equal to 0.15.

Group-Max : The sum of the weights in GroupA must be less than or equal to 0.85 and the sum of the weights in GroupB must be less than or equal to 0.55.

4.4. Position Limit Constraint : The position limit constraint allows to specify limits on the number of assets with nonzero, long, or short positions. All position limit constraints are fully supported for DEoptim and random solvers.

4.5 Diversification Constraint : The diversification constraint is implemented for the global optimizers by applying a penalty if the diversification value is more than 5% away from div_target. We are considering div_target as 0.75.

4.6 Turnover Constraint : The turnover is calculated from a set of initial weights. The initial weights can be specified, by default they are the initial weights in the portfolio object. The turnover constraint is implemented for the global optimizers by applying a penalty if the turnover value is more than 5% away from turnover target. ( we consider turnover target as  15%).

4.7 Target Return Constraint : The target return constraint allows to specify a target mean return. In our study it is considered as 0.009.

4.8 Factor Exposure Constraint : The factor exposure constraint allows to set upper and lower bounds on exposures to risk factors. Here we specify a vector for Group B with betas of the assets and market risk exposure range of 0.7 to 0.9.

( Beta values : Reliance = 1.12, , HDFC = 1.17,ITC= 0.64, , ICICI = 1.52 ,Axis = 1.83, SBI = 1.28)

4.9 Transaction Cost Constraint : The transaction cost constraint allows to specify proportional transaction costs. Transaction costs are supported as a penalty for the global numeric solvers. Here we add the transaction cost contraint with the proportional transaction cost value of 1.5%.

The summary of Porfolio specifications are given below ;

**************************************************
PortfolioAnalytics Portfolio Specification 
**************************************************

Call:
portfolio.spec(assets = fund.names)

Number of assets: 4 
Asset Names
[1] "Reliance" "HDFC"     "ITC"      "ICICI"   

Constraints
Enabled constraint types
                               - weight_sum 
                               - box 
                               - weight_sum 
                               - box 
                               - group 
                               - position_limit 
                               - diversification 
                               - turnover 
                               - return 
                               - diversification 
                               - turnover 
                               - factor_exposure 
                               - transaction_cost 



5.  Adding Objectives : The add.objective function is the main function for adding and/or updating business objectives to the portfolio object. It allows to specify the portfolio to add the objectives to, the type (currently ’return’, ’risk’, ’risk budget’, or ’weight concentration’), name of the objective function, arguments to the objective function, and whether or not to enable the objective.

a.   Portfolio Risk Objective : The portfolio risk objective allows to specify a risk function to

 minimize Here we add a risk objective to minimize portfolio expected tail loss with a confidence level of 0.95.

b.   Portfolio Return Objective : The return objective allows to specify a return function to maximize. Here we add a return objective to maximize the portfolio mean return.

c.    Portfolio Risk Budget Objective : The portfolio risk objective allows to specify constraints to minimize component contribution (i.e. equal risk contribution) or specify upper and lower bounds on percentage risk contribution. Here we specify that no asset can contribute more than 40% to total portfolio risk.

d.   Portfolio Weight Concentration Objective : The weight concentration objective allows to specify an objective to minimize concentration as measured by the Herfindahl-Hirschman Index.

**************************************************
PortfolioAnalytics Portfolio Specification 
**************************************************

Call:
portfolio.spec(assets = fund.names)

Number of assets: 4 
Asset Names
[1] "Reliance" "HDFC"     "ITC"      "ICICI"   

Constraints
Enabled constraint types
                               - weight_sum 
                               - box 
                               - weight_sum 
                               - box 
                               - group 
                               - position_limit 
                               - diversification 
                               - turnover 
                               - return 
                               - diversification 
                               - turnover 
                               - factor_exposure 
                               - transaction_cost 
                               - box 
                               - group 
                               - position_limit 
                               - diversification 
                               - turnover 
                               - return 
                               - factor_exposure 
                               - transaction_cost 

Objectives:
Enabled objective names
                               - ETL 
                               - mean 
                               - ETL 
                               - HHI 
                               - mean 
                               - ETL 
                               - HHI 
                               - HHI 

5.1 Random Portfolios: PortfolioAnalytics has three methods to generate random portfolios.


a). The ’sample’ method to generate random portfolios is based on an idea by Pat Burns. This is the most flexible method, but also the slowest, and can generate portfolios to satisfy leverage, box, group, and position limit constraints.


b). The simplex method is useful to generate random portfolios with the full investment constraint, where the sum of the weights is equal to 1, and min box constraints. Values for min_sum and max_sum of the leverage constraint will be ignored, the sum of weights will equal 1. All other constraints such as the box constraint max, group and position limit constraints will be handled by elimination. If the constraints are very restrictive, this may result in very few feasible portfolios remaining. Another key point to note is that the solution may not be along the vertexes depending on the objective. For example, a risk budget objective will likely place the portfolio somewhere on the interior.

c) The grid search method only satisfies the min and max box constraints. The min_sum and max_sum leverage constraint will likely be violated and the weights in the random portfolios should be normalized. Normalization may cause the box constraints to be violated and will be penalized in constrained_objective.

The following chart Figure-1 shows coverage of feasible space of various random portfolio methods. It can be observed that ‘grid’ method is mostly at the interior part due to normalization, ‘simplex’ method concentrates around assets.

Figure -1 

We can use the fev argument controls the face-edge-vertex biasing. Higher values for fev will result in the weights vector more concentrated on a single asset. This can be seen in the following charts Figure-2. Also, we can see in Figure-3, Simplex method has more concentration around assets.

Figure -2 :

Figure-3 :

6.1.Maximize mean return with ROI : We will add two more stocks to the portfolio and build objective to maximize mean return. ( two stocks added are Axis Bank and SBI ).

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = maxret, optimize_method = "ROI", 
    trace = TRUE)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
    0.16     0.05     0.65     0.05     0.05     0.05 

Objective Measure:
    mean 
0.001688 

We will plot the weights and optimal portfolio in risk-return space. The weights and a risk-reward scatter plot can be plotted separately as shown below with the chart (Figure-4).

Figure-4 :



6.2.Minimize variance with ROI : For the portfolio of six stocks, we will run optimization function to Minimize the variance with ROI.

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = minvar, optimize_method = "ROI", 
    trace = TRUE)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
  0.3029   0.0500   0.4871   0.0500   0.0500   0.0500 

Objective Measure:
StdDev 
0.0734 

Figure-5 :

6.3. Maximize quadratic utility with ROI : We are adding mean and var objectives for quadratic utility. Note that the risk aversion parameter ( 0.25 ) for quadratic utility is specifed in the objective.

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = qu, optimize_method = "ROI", 
    trace = TRUE)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
    0.14     0.05     0.65     0.05     0.05     0.05 

Objective Measure:
    mean 
0.001662 


 StdDev 
0.07629 

Figure-6:



6.4 Minimize expected tail loss (ETL) with ROI: We add ETL objective and run the optimization model.

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = etl, optimize_method = "ROI", 
    trace = TRUE)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
  0.3598   0.0500   0.3783   0.0500   0.0618   0.0901 

Objective Measure:
   ETL 
0.1955 

Figure-7:


6.5.Maximize mean return per unit ETL with random portfolios : We will add Mean and ETL objectives and run the model.

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = meanETL, optimize_method = "random", 
    search_size = 2000, trace = TRUE)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
   0.050    0.414    0.356    0.050    0.074    0.050 

Objective Measures:
     mean 
0.0002855 


   ETL 
0.1816 

Figure-8 :


The optimization model had 2000 iterations and other output from random portfolios is stored in a matrix of the weights and objective measures at each iteration.

Figure-9 :


Since in the Figure-9, HDFC is showing risk-contribution grater than 40%. Hence, risk budget objective is added to limit risk contibution percentage to 40%.



6.6 Maximize mean return per unit ETL with ETL risk budgets : We will add objectives to maximize mean return per unit ETL with 40% limit ETL risk budgets.

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = rb_meanETL, optimize_method = "DEoptim", 
    search_size = 2000, trace = TRUE, traceDE = 5)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
   0.452    0.156    0.036    0.084    0.096    0.174 

Objective Measures:
     mean 
-0.001031 


   ETL 
0.2419 

contribution :
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
0.081373 0.065835 0.006252 0.010681 0.015256 0.062456 

pct_contrib_MES :
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
 0.33646  0.27221  0.02585  0.04416  0.06308  0.25824 

Figure-10:



6.7 Maximize mean return per unit ETL with ETL equal contribution to risk:

Add objective to maximize mean return per unit ETL with ETL equal contribution to risk.

***********************************
PortfolioAnalytics Optimization
***********************************

Call:
optimize.portfolio(R = all, portfolio = eq_meanETL, optimize_method = "DEoptim", 
    search_size = 2000, trace = TRUE, traceDE = 5)

Optimal Weights:
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
  0.1667   0.1667   0.1667   0.1667   0.1667   0.1667 

Objective Measures:
    mean 
-0.00238 


   ETL 
0.2761 

contribution :
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
 0.01246  0.08867  0.03349  0.03907  0.08002  0.02233 

pct_contrib_MES :
Reliance     HDFC      ITC    ICICI      SBI     AXIS 
 0.04515  0.32122  0.12133  0.14153  0.28987  0.08091 

Figure-10:

We chart the contribution to risk in percentage terms. It is clear in this chart that the optimization results in a near equal risk contribution portfolio.

Figure-11 :


The opt_meanETL, opt_rb_meanETL, and opt_eq_meanETL optimizations are similar and can be easily compared. opt meanETL Objective to maximize mean return per unit ETL. The constraints are full investment and box constraints such that the minimum weight of any asset is 0.05 and maximum weight of any asset is 0.65

opt rb meanETL Objective to maximize mean return per unit ETL with risk budget objective to limit maximum percent risk 40%. The constraints are full investment and long only constraints. opt eq meanETL Objective to maximize mean return per unit ETL with equal contribution to risk. The constraints are full investment and long only constraints. Combine the optimizations for easy comparison.

> extractWeights(opt_combine)
           Reliance      HDFC       ITC     ICICI       SBI      AXIS
meanETL   0.0500000 0.4140000 0.3560000 0.0500000 0.0740000 0.0500000
rbmeanETL 0.4520000 0.1560000 0.0360000 0.0840000 0.0960000 0.1740000
eqmeanETL 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667


The following charts( Figure-13, Figure-14, and Figure-15) compares three ETL objectives’ for different assets.

Figure-13 :

Figure-14 :

Figure - 15:








Arun Soni

co-founder @ phaseinvest | alpha for traditional & crypto portfolios

7 年

Excellent tutorial, thank you ever so much.

回复

要查看或添加评论,请登录

Jagadeesh Padaki的更多文章

  • What is Supervised Learning?

    What is Supervised Learning?

    Machine Learning can happen in two ways. Supervised Learning Unsupervised Learning What is Supervised Learning? Example…

  • Python Trainer

    Python Trainer

    Urgently required Python Trainer for 5 days. Please send your past training details along with your resume to…

  • Decision Tree

    Decision Tree

    Decision Tree is a powerful tool to classify data into meaningful decision alternates. It can be as simple as mentioned…

  • Catch up Machine Learning..

    Catch up Machine Learning..

    India reaching $20 billion E-commerce market. As compared to USA $354 billion, India has huge potential for Growth in…

    1 条评论
  • ANOVA

    ANOVA

    Analysis of Variance ( ANOVA ) deals with multivariate data. It can be used to test whether Means of various groups of…

  • Loan Approval Prediction: Using R

    Loan Approval Prediction: Using R

    Nowadays, Banks wish to automate the loan eligibility process (real time) based on customer details such as Income…

  • Stock Trading Strategies using Support Vector Machine (SVM)

    Stock Trading Strategies using Support Vector Machine (SVM)

    The specific kernel function we use in this study is the radial kernel. It classifies test examples based on the…

    3 条评论
  • Cluster Analysis for Marketing Decisions: Customer Segmentation

    Cluster Analysis for Marketing Decisions: Customer Segmentation

    Clustering is a statistical tool to form groups (clusters) of similar observations. Between the groups (clusters) they…

社区洞察

其他会员也浏览了