[R-SIG-Finance] PortfolioAnalytics question re: showing results

matt at considine.net matt at considine.net
Fri Mar 18 14:13:00 CET 2016


Hi Brian,

Thanks for the offer of some code.  I had wanted to try to figure this 
out for myself, but I'm not making the headway I thought.  IF you have 
some code or a worked example you can send, I'd be appreciative.

That said here is what I am working with.   Perhaps someone can suggest 
what I am doing wrong?

Goal : generate/plot an efficient frontier (with annualized axes) using 
PortfolioAnalytics, using monthly return data.  (Ideally, I'd also want 
to isolate the tangency/max Sharpe portfolio, a portfolio with max 
return at a specific risk level and a portfolio with a min risk at a 
specific return.  But I'll deal with that later.)

Code : I tried to use code from some of the presentations, demos 
(DEoptim and random portfolios, specifically) and vignettes.  Also, I'm 
using the latest version of the code from R-forge.

#-----------------------------
library(PortfolioAnalytics)

# Define pamean function
pamean1 <- function(R, weights, n=60, geometric=FALSE){
   as.vector(sum(Return.annualized(last(R,n), 
geometric=geometric)*weights))
}

# Define pasd function
pasd1 <- function(R, weights=NULL){
   as.numeric(StdDev(R=R, weights=weights)*sqrt(12)) # hardcoded for 
monthly data
}

data(edhec)

# Use the first 4 columns in edhec for a returns object
R <- edhec[, 1:4]
colnames(R) <- c("CA", "CTAG", "DS", "EM")
head(R, 5)

# Get a character vector of the fund names
funds <- colnames(R)

# Construct initial portfolio with basic constraints.
init.portf <- portfolio.spec(assets=funds)
init.portf <- add.constraint(portfolio=init.portf, 
type="full_investment")
init.portf <- add.constraint(portfolio=init.portf, type="box", min=0.0, 
max=1.0)

# Portfolio with standard deviation as an objective
#SD.portf <- add.objective(portfolio=init.portf, type="risk", 
name="pasd1") #pasd1 doesn't work?
#SD.portf <- add.objective(portfolio=SD.portf, type="return", 
name="mean") #pamean1 doesn't work?

#Ok, let's try this :
#Add measure 1, annualized return
SD.portf <- add.objective(portfolio=init.portf,
                             type="return", # the kind of objective this 
is
                             name="pamean1", # name of the function
                             enabled=TRUE, # enable or disable the 
objective
                             multiplier=0 # calculate it but don't use it 
in the objective
)

# Add measure 2, annualized standard deviation
SD.portf <- add.objective(portfolio=init.portf,
                             type="risk", # the kind of objective this is
                             name="pasd1", # to minimize from the sample
                             enabled=TRUE, # enable or disable the 
objective
                             multiplier=0 # calculate it but don't use it 
in the objective
)

#Create efficient frontier
init.portf.ef <- create.EfficientFrontier(R=R, portfolio=SD.portf, 
type="mean-StdDev")

#This chart never seems to show annualized axes
chart.EfficientFrontier(init.portf.ef, match.col="StdDev")

sd.moments <- set.portfolio.moments(R, SD.portf)
names(sd.moments) #returning NULL with pasd1/pamean1
print(sd.moments) #returning NULL with pasd1/pamean1

#Just a reality check to see what the axes ranges should roughly look 
like
ra <- Return.annualized(R[, , drop = FALSE], scale = 12, geometric = 
FALSE)
sda <- StdDev.annualized(R[, , drop = FALSE], scale = 12)
sra <- SharpeRatio.annualized(R[, , drop = FALSE], scale = 12, Rf = 
0.00, geometric = FALSE)

pamean1(R)
ra

pasd1(R)
sda
#----------------------------------
Regards,
Matt



More information about the R-SIG-Finance mailing list