0

I am facing problems with addition of two xts objects.

Object 1 : bk

head(bk)
            iroqu
1962-07-03     0
1962-07-05     0
1962-07-06     0
1962-07-09     0
1962-07-10     0
1962-07-11     0

Object 2 : calculated as weight <- lag(crp(xik, c(alphas[i], 1-alphas[i])), 1) in for loop

                [,1]
1962-07-03        NA
1962-07-05 0.9374210
1962-07-06 0.9367212
1962-07-09 0.9452369
1962-07-10 0.9464487
1962-07-11 1.0819963

When I am doing inside for loop,

bk <- bk + alphas[i] * weight

bk becomes

bk
Data:
numeric(0)

Index:
numeric(0)

I checked that alphas[i] * weight is not a problem and dimensions of the vectors also match. Why I am not able to add two objects using + sign? Is there a way to add these? I want effectively only 1 column.

The full example I am trying is from blog http://optimallog.blogspot.in/2012/06/universal-portfolio-part-4.html first code snippet

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Yantraguru
  • 3,604
  • 3
  • 18
  • 21
  • 1
    My guess is that the timezones of `bk` and `weight` are different, but I can't be sure since you did not provide a [reproducible example](http://stackoverflow.com/q/5963269/271616). – Joshua Ulrich Aug 04 '14 at 17:44
  • I am trying out example from this blog link http://optimallog.blogspot.in/2012/06/universal-portfolio-part-4.html code snippet starts with fig 8.4 of Cover "Univ.... and ends with first image – Yantraguru Aug 04 '14 at 18:12
  • That code does not run. `nyse.cover.1962.1984` is not defined (but can be loaded with `data`), and `crps` is not defined (and I have no idea what it should be). – Joshua Ulrich Aug 05 '14 at 11:27

1 Answers1

1

From what I can tell from the post, the problem is what I suggested in my comment: the two objects have different timezones (and therefore the index values are different). Date-indexed xts objects should have a index timezone of "UTC".

library(logopt)
data(nyse.cover.1962.1984)
x <- nyse.cover.1962.1984

# change the index timezone attribute
indexTZ(x) <- "UTC"
# force a recalculation of the actual index values
index(x) <- index(x)

xik <- x[,c("iroqu","kinar")]
nDays <- dim(xik)[1]
Days <- 1:nDays
pik <- cumprod(xik)
alphas <- seq(0,1,by=0.05)
bk <- xik[,1] * 0
w <- xik[,1] * 0
# crps should be alphas???
crps <- alphas
for (i in 1:length(crps)) {
  # we calculate bk by weighting the b by the realized wealth lagged one
  weight <- lag(crp(xik, c(alphas[i], 1-alphas[i])), 1)
  bk <- bk + alphas[i] * weight
  w <- w + weight
}
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • thanks Joshua! 1) Yes data needs to be loaded. I missed that step. 2) alphas are weights, you are right to replace crps by alphas. – Yantraguru Aug 05 '14 at 17:43