A. Loading Data

data(edhec)
# Use the first 4 columns in edhec for a returns object
returns <- edhec[, 1:6]
colnames(returns) <- c("CA", "CTAG", "DS", "EM", "EQMN", "ED")
fund.names <- colnames(returns)
print(head(returns, 5))
##                CA    CTAG      DS      EM   EQMN      ED
## 1997-01-31 0.0119  0.0393  0.0178  0.0791 0.0189  0.0213
## 1997-02-28 0.0123  0.0298  0.0122  0.0525 0.0101  0.0084
## 1997-03-31 0.0078 -0.0021 -0.0012 -0.0120 0.0016 -0.0023
## 1997-04-30 0.0086 -0.0170  0.0030  0.0119 0.0119 -0.0005
## 1997-05-31 0.0156 -0.0015  0.0233  0.0315 0.0189  0.0346
colMeans(returns)
##          CA        CTAG          DS          EM        EQMN          ED 
## 0.005500364 0.004158182 0.006621818 0.006245818 0.004356000 0.006216364
x = round(var(returns), 6)
print(x)
##             CA      CTAG        DS       EM     EQMN       ED
## CA    0.000266 -0.000008  0.000201 0.000312 0.000065 0.000198
## CTAG -0.000008  0.000536 -0.000008 0.000030 0.000037 0.000005
## DS    0.000201 -0.000008  0.000285 0.000423 0.000080 0.000260
## EM    0.000312  0.000030  0.000423 0.001032 0.000133 0.000441
## EQMN  0.000065  0.000037  0.000080 0.000133 0.000064 0.000084
## ED    0.000198  0.000005  0.000260 0.000441 0.000084 0.000279
x = round(cor(returns), 6)
print(x)
##             CA      CTAG        DS       EM     EQMN       ED
## CA    1.000000 -0.020397  0.730564 0.595816 0.494521 0.727190
## CTAG -0.020397  1.000000 -0.020666 0.040761 0.197965 0.012591
## DS    0.730564 -0.020666  1.000000 0.779393 0.590578 0.922729
## EM    0.595816  0.040761  0.779393 1.000000 0.514851 0.822511
## EQMN  0.494521  0.197965  0.590578 0.514851 1.000000 0.623350
## ED    0.727190  0.012591  0.922729 0.822511 0.623350 1.000000
matplot(returns)

B. Create the Portfolio Ojbect

myport1 <- portfolio.spec(assets=fund.names)
print.default(myport1)
## $assets
##        CA      CTAG        DS        EM      EQMN        ED 
## 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 
## 
## $category_labels
## NULL
## 
## $weight_seq
## NULL
## 
## $constraints
## list()
## 
## $objectives
## list()
## 
## $call
## portfolio.spec(assets = fund.names)
## 
## attr(,"class")
## [1] "portfolio.spec" "portfolio"

C. Constraints

myport1 <- add.constraint(portfolio=myport1, type="weight_sum", 
                        min_sum=1, max_sum=1)

myport1 <- add.constraint(portfolio=myport1, type="box", 
                        min=0.05, max=0.4)

D. Objective

myport1 <- add.objective(myport1, type="risk", name="StdDev")
myport1
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = fund.names)
## 
## Number of assets: 6 
## Asset Names
## [1] "CA"   "CTAG" "DS"   "EM"   "EQMN" "ED"  
## 
## Constraints
## Enabled constraint types
##      - weight_sum 
##      - box 
## 
## Objectives:
## Enabled objective names
##      - StdDev

E. Optimize

solution <- optimize.portfolio(returns, myport1, 
                                 optimize_method="ROI", trace=TRUE)
solution
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
## 
## Call:
## optimize.portfolio(R = returns, portfolio = myport1, optimize_method = "ROI", 
##     trace = TRUE)
## 
## Optimal Weights:
##     CA   CTAG     DS     EM   EQMN     ED 
## 0.2439 0.2061 0.0500 0.0500 0.4000 0.0500 
## 
## Objective Measure:
## StdDev 
## 0.0103

Maximize mean return with ROI

base_port <- portfolio.spec(assets=fund.names)
base_port <- add.constraint(portfolio=base_port, type="leverage", 
                            min_sum=0.99, max_sum=1.01)
base_port <- add.constraint(portfolio=base_port, type="box", min=0.05, max=0.65)

maxret <- add.objective(portfolio=base_port, type="return", name="mean")
opt_maxret <- optimize.portfolio(R=returns, portfolio=maxret, 
                                 optimize_method="ROI", trace=TRUE)
print(opt_maxret)
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
## 
## Call:
## optimize.portfolio(R = returns, portfolio = maxret, optimize_method = "ROI", 
##     trace = TRUE)
## 
## Optimal Weights:
##   CA CTAG   DS   EM EQMN   ED 
## 0.05 0.05 0.65 0.16 0.05 0.05 
## 
## Objective Measure:
##     mean 
## 0.006315
colMeans(returns)
##          CA        CTAG          DS          EM        EQMN          ED 
## 0.005500364 0.004158182 0.006621818 0.006245818 0.004356000 0.006216364
names(opt_maxret)
##  [1] "weights"            "objective_measures" "opt_values"        
##  [4] "out"                "call"               "portfolio"         
##  [7] "R"                  "data_summary"       "elapsed_time"      
## [10] "end_t"
opt_maxret$weights
##   CA CTAG   DS   EM EQMN   ED 
## 0.05 0.05 0.65 0.16 0.05 0.05
opt_maxret$objective_measures$mean
##        mean 
## 0.006315058
sum(colMeans(returns)*opt_maxret$weights)
## [1] 0.006315058

Minimize variance with ROI

minvar <- add.objective(portfolio=base_port, type="risk", name="var")
opt_minvar <- optimize.portfolio(R=returns, portfolio=minvar, 
                                 optimize_method="ROI", trace=TRUE)
print(opt_minvar)
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
## 
## Call:
## optimize.portfolio(R = returns, portfolio = minvar, optimize_method = "ROI", 
##     trace = TRUE)
## 
## Optimal Weights:
##     CA   CTAG     DS     EM   EQMN     ED 
## 0.0623 0.1277 0.0500 0.0500 0.6500 0.0500 
## 
## Objective Measure:
##   StdDev 
## 0.009046