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

Brian G. Peterson bri@n @ending from br@verock@com
Wed Sep 26 14:05:16 CEST 2018


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/
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]]
> 
> _______________________________________________
> 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.
>



More information about the R-SIG-Finance mailing list