[R-SIG-Finance] Using optimize.portfolio

Roger Bos roger@bo@ @end|ng |rom gm@||@com
Fri Jun 5 22:31:23 CEST 2020


Thanks Brian.  That should get me started.  Appreciate the help.

On Fri, Jun 5, 2020, 4:05 PM Brian G. Peterson <brian using braverock.com> wrote:

> Foger,
>
> See the vignette for custom moment and objective functions:
>
> 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
>
> On Fri, 2020-06-05 at 14:16 -0400, Roger Bos wrote:
>
> All,
>
>
> I am comparing optimize.portfolio from the PortfolioAnalytics package to
>
> portfolio.optim from the tseries package.  portfolio.optim seems a bit
>
> easier to use, but I like the set up of optimize.portfolio.  I have created
>
> a minimal reprex below that compares the output of both in case that helps
>
> answer my questions.
>
> Here are my two primary questions:
>
>
> 1) What if I wanted to pass a custom covariance matrix to
>
> optimize.portfolio, like from a risk model.  Is that possible?  I can pass
>
> it to portfolio.optim because covmat is one of the parameters.
>
> 2) What if I wanted to pass forecasted returns to optimize.portfolio?  How
>
> would that be done.
>
>
> If there is anything that can be improved in this example, that would be
>
> helpful 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","GILD")
>
>
> 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 package
>
> library(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.
>
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list