[R-SIG-Finance] Using optimize.portfolio

Brian G. Peterson br|@n @end|ng |rom br@verock@com
Fri Jun 5 22:05:43 CEST 2020


Foger,
See the vignette for custom moment and objective functions:

On Fri, 2020-06-05 at 14:16 -0400, Roger Bos wrote:
> All,
> I am comparing optimize.portfolio from the PortfolioAnalytics package
> toportfolio.optim from the tseries package.  portfolio.optim seems a
> biteasier to use, but I like the set up of optimize.portfolio.  I
> have createda minimal reprex below that compares the output of both
> in case that helpsanswer my questions.Here are my two primary
> questions:
> 1) What if I wanted to pass a custom covariance matrix
> tooptimize.portfolio, like from a risk model.  Is that possible?  I
> can passit to portfolio.optim because covmat is one of the
> parameters.2) What if I wanted to pass forecasted returns to
> optimize.portfolio?  Howwould that be done.
> If there is anything that can be improved in this example, that would
> behelpful as well.  Thank you in advance for any assistance, Roger.
> ###
> library(PortfolioAnalytics)library(tidyquant)
> symbols <-
> c("MSFT","AAPL","AMZN","NVDA","CSCO","ADBE","AMGN","ORCL","QCOM","GIL
> D")
> getYahooReturns <- function(symbols, return_column = "Ad") {  returns
> <- list()  for (symbol in symbols) {    getSymbols(symbol, from =
> '2000-01-01', adjustOHLC = TRUE, env =.GlobalEnv, auto.assign =
> TRUE)    return <-
> Return.calculate(Ad(get(symbol)))    colnames(return) <-
> gsub("\\.Adjusted", "", colnames(return))    returns[[symbol]] <-
> return  }  returns <- do.call(cbind, returns)  return(returns)}
> returns <- getYahooReturns(symbols)returns <- returns[-1,
> ]returns[is.na(returns)] <- 0
> # portfolio.optim from tseries packagelibrary(tseries)sigma <-
> cov(returns)reslow <- rep(0, ncol(returns))reshigh <- rep(1,
> ncol(returns))popt <- portfolio.optim(x = returns, covmat = sigma,
> reslow = reslow,reshigh = reshigh)popt$pw
> pspec <- portfolio.spec(assets = symbols)pspec <-
> add.constraint(portfolio=pspec, type="box",                    min =
> 0, max = 1, min_sum = 0.99, max_sum = 1.01)pspec <-
> add.objective(portfolio=pspec,                       type="return",  
>                      name="mean")pspec <-
> add.objective(portfolio=pspec,                       type="risk",    
>                    name="var")
> opt <- optimize.portfolio(R = returns,                   portfolio =
> pspec,                   optimize_method = "DEoptim",
> )data.frame(optimize.portfolio = opt$weights, portfolio.optim
> =round(popt$pw, 3))
> 	[[alternative HTML version deleted]]
> _______________________________________________R-SIG-Finance using r-
> project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.--
> Also note that this is not the r-help list where general R questions
> should go.
https://cran.r-project.org/web/packages/PortfolioAnalytics/vignettes/custom_moments_objectives.pdf
You can simply pass mu and sigma, but you probably want to do a little
bit more work to make it more extensible.

Hopefully this is enough of a pointer.  If not, I can work on an
example based on your example later.

Regards,

Brian
-- 
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list