[R-SIG-Finance] xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing

Simon Hovmark @imonhovm@rk @ending from gm@il@com
Fri Oct 12 00:01:32 CEST 2018


So I got it to work even with -100x compounding returns and I’ve have tried to figure out why, so I could leave a solution. But no luck.

But just som intuition on why I replace NAs with -100. I’ve a dataset of 14 different European stock indices from 2000 to present with the historical components. However, as some stocks are not always in the indices they should also not always be part of the optimization. PerformanceAnalytics replaces per default NAs with 0’s which means that even though there are no return value for the excluded stock, it is still (wrongfully) included in the optimization every month because it just looks like a 0% return.

I tried to find a way to work around this using a constraint but it is my understanding that constraints in PerformanceAnalytics cannot be customized. So my very ugly solution was to replace NAs with -100, so the stocks in that way would get penalized in the optimization. However, this is far from ideal and do also not work perfectly.

Any suggestions on how to solve this problem or should I create a new thread?

-Simon

> Den 26. sep. 2018 kl. 08.05 skrev braverock [via R] <ml+s789695n4752761h50 using n4.nabble.com>:
> 
> As Amit already said, zero is a better answer than -100.  log returns 
> don't behave well with -100x compounding returns.  This is what is 
> causing Inf/-Inf problems.  there is a 'zerofill' utility function 
> available in PerformanceAnalytics to make this replacement easier.   
> 
> see: 
> 
> ?zerofill 
> 
> If you are *also* having a problem with order.by, your sample data 
> would b e insufficient to reproduce it, since the index in the subset 
> you pasted seems well-formed. 
> 
> If Amit's and my suggestion to replace NA with 0 in your returns 
> doesn't solve the problem, then please reply to this thread with a 
> fully reproducible example (including data e.g. using dput function the 
> or reprex package). 
> 
> Regards, 
> 
> Brian 
>   
> -- 
> Brian G. Peterson 
> http://braverock.com/brian/ <http://braverock.com/brian/>
> Ph: 773-459-4973 
> IM: bgpbraverock 
> 
> On Tue, 2018-09-25 at 17:03 -0400, Simon Hovmark wrote:
> 
> > I am trying to run the following optimize.portfolio.rebalancing: 
> > 
> > opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1, 
> >                                             optimize_method="ROI", 
> >                                             #momentFUN = 
> > tranch1_boudt, 
> >                                             rebalance_on = 
> > rebal.freq, 
> >                                             training_period = 
> > training.period, 
> >                                             rolling_window = 
> > rolling.window) 
> > But when I use summary(opt) I get the following error: 
> > 
> > xts(x, order.by = order.by, frequency = frequency, ...) :'order.by' 
> > cannot contain 'NA', 'NaN', or 'Inf' 
> > I can see that other has had a similar problem, but I've not been 
> > able to solve it using their answers. When I sum NA, NaN and InF on 
> > returns$dato I get 0. 
> > 
> > A subset of my data is here: 
> > 
> > dato       stock_1    stock_2    stock_3 
> > 1999-10-14 -0.002006019 0.016164145 -100 
> > 1999-10-15 0.000000000 0.000000000 -100 
> > 1999-10-18 -0.036813973 -0.049017341 -100 
> > 1999-10-19 0.016529302 0.000000000 -100 
> > 1999-10-20 0.016260521 0.011996238 -100 
> > 1999-10-21 0.008032172 0.005806736 -100 
> > 1999-10-22 0.000000000 0.000000000 -100 
> > 1999-10-25 0.039220713 0.023164955 -100 
> > 1999-10-26 0.028437935 0.002152853 -100 
> > 1999-10-27 -0.032291505 0.014941580 -100 
> > 1999-10-28 0.030420597 0.011061477 -100 
> > 1999-10-29 0.000000000 0.000000000 -100 
> > 1999-11-02 0.027702603 -0.003410734 -100 
> > 1999-11-03 0.007259560 -0.007650743 -100 
> > 1999-11-04 0.003610112 0.000000000 -100 
> > 1999-11-05 0.000000000 0.000000000 -100 
> > 1999-11-08 0.014311514 0.005546033 -100 
> > 1999-11-09 0.007079676 -0.002373106 -100 
> > 1999-11-10 0.039763233 0.024512309 -100 
> > 1999-11-11 -0.001696353 -0.018721296 -100 
> > 1999-11-12 0.000000000 0.000000000 -100 
> > And here is my full code. 
> > 
> > rebal.freq <- "years" 
> > training.period <- 0 
> > rolling.window <- 120 
> > 
> > returns <- read_excel("HEX.xlsx", sheet = 1, col_names = TRUE) 
> > returns <- xts(returns[,-1], order.by= returns[,1]) 
> > returns <- Return.calculate(returns, method = "log") 
> > returns <- returns[-1,] 
> > 
> > returns[!is.finite(returns)] <- NA 
> > returns[!is.finite(returns)] <- NA 
> > returns <- na.fill(returns, fill = -100) 
> > sum(is.nan(returns$dato)) #returns 0 
> > sum(is.infinite(returns$dato)) #returns 0 
> > sum(is.na(returns$dato)) #returns 0 
> > 
> > fund.names <- colnames(returns) 
> > tranch1 <- portfolio.spec(assets = fund.names) 
> > tranch1 <- add.constraint(portfolio = tranch1, type = "leverage") 
> > tranch1 <- add.constraint(portfolio = tranch1, type = "long_only") 
> > tranch1 <- add.objective(portfolio=tranch1, type="return", 
> > name="mean") 
> > tranch1 <- add.objective(portfolio=tranch1, type="risk", 
> > name="StdDev") 
> > 
> > opt <- optimize.portfolio.rebalancing(R=returns, portfolio=tranch1, 
> >                                             optimize_method="ROI", 
> >                                             #momentFUN = 
> > tranch1_boudt, 
> >                                             rebalance_on = 
> > rebal.freq, 
> >                                             training_period = 
> > training.period, 
> >                                             rolling_window = 
> > rolling.window) 
> > 
> > 
> > summary(opt) 
> > And my sessioninfo: 
> > 
> > R version 3.3.1 (2016-06-21) 
> > Platform: x86_64-apple-darwin13.4.0 (64-bit) 
> > Running under: OS X 10.13.3 (unknown) 
> > 
> > locale: 
> > [1] C 
> > 
> > attached base packages: 
> > [1] stats     graphics  grDevices 
> > utils     datasets  methods   base     
> > 
> > other attached packages: 
> > [1] RColorBrewer_1.1- 
> > 2            readxl_0.1.1                        DEoptimR_1.0- 
> > 8               
> > [4] 
> > PortfolioAnalytics_1.0.3636   PerformanceAnalytics_1.4.3541     forea 
> > ch_1.4.4                 
> > [7] xts_0.10-1                    zoo_1.7-13                   
> > 
> > loaded via a namespace (and not attached): 
> > [1] 
> > compiler_3.3.1   parallel_3.3.1   tools_3.3.1      Rcpp_0.12.9       
> >    codetools_0.2-14 
> > [6] grid_3.3.1       iterators_1.0.8  DEoptim_2.2-4    lattice_0.20- 
> > 34 
> > EDIT When I read in the Excel file, then class(returns$Dato) returns 
> > 
> > class(returns$Dato) [1] "POSIXct" "POSIXt" 
> > Then instead of the below 
> > 
> > returns <- xts(returns[,-1], order.by= returns[,1]) 
> > I tried using 
> > 
> > returns <- xts(returns[, -1], order.by=as.Date(paste(returns$Dato, 
> > "%m/%d/%Y"))) 
> > and run the optimization but summary(opt) again returned 
> > 
> > xts(x, order.by = order.by, frequency = frequency, ...) :’order.by' 
> > cannot contain 'NA', 'NaN', or 'Inf' 
> > 
> > 
> > [[alternative HTML version deleted]] 
> > 
> > _______________________________________________ 
> > [hidden email] <x-msg://73/user/SendEmail.jtp?type=node&node=4752761&i=0> mailing list 
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance <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. 
> >
> 
> _______________________________________________ 
> [hidden email] <x-msg://73/user/SendEmail.jtp?type=node&node=4752761&i=1> mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance <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. 
> 
> 
> If you reply to this email, your message will be added to the discussion below:
> http://r.789695.n4.nabble.com/xts-order-by-cannot-contain-NA-NaN-or-Inf-in-optimize-portfolio-rebalancing-tp4752748p4752761.html <http://r.789695.n4.nabble.com/xts-order-by-cannot-contain-NA-NaN-or-Inf-in-optimize-portfolio-rebalancing-tp4752748p4752761.html>
> To unsubscribe from xts 'order.by' cannot contain 'NA', 'NaN', or 'Inf' in optimize.portfolio.rebalancing, click here <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4752748&code=c2ltb25ob3ZtYXJrQGdtYWlsLmNvbXw0NzUyNzQ4fC0yMTAwNDE2NDQz>.
> NAML <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list