[R-SIG-Finance] use rows and cols of a matrix as dates

Sven D sduve at hotmail.com
Thu Mar 14 09:21:11 CET 2013


Dear All,

I am trying to evaluate a gas storage contract and I am trying to use R do
the work for me. What I am trying to do is to generate a matrix like:

gy <- matrix(0, nrow=13, ncol=13)
gy <- xts(gy, order.by = timeBasedSeq('201304/201404'))

# these numbers are an imaginariy gas forward curve
forwardcurve <-
c(26.5,26.45,26.4,26.25,26.25,26.25,26.21,27.54,27.91,28.14,28.17,27.91)

# populate first row and first col with the forward curve

gy[1,c(2:13)] <- forwardcurve
gy[c(2:13),1] <- forwardcurve

# calculate the monthly spreads

for(i in 2:13) gy[i,c(i:13)] <- (gy[1, c(i:13)] - gy[i,1])

# the matrix should look like this

> gy
          [,1] [,2]  [,3] [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11]
[,12] [,13]
Mar 2013  0.00 26.5 26.45 26.4 26.25 26.25 26.25 26.21 27.54 27.91 28.14
28.17 27.91
Apr 2013 26.50  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
May 2013 26.45  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Jun 2013 26.40  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Jul 2013 26.25  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Aug 2013 26.25  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Sep 2013 26.25  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Oct 2013 26.21  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Nov 2013 27.54  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Dec 2013 27.91  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Jan 2014 28.14  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Feb 2014 28.17  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00
Mar 2014 27.91  0.0  0.00  0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00 
0.00  0.00

I now need to xts the columns as well, is that possibel?

I tried

gy <- xts(colnames(gy), order.by = timeBasedSeq('201304/201404'))

but this doesnt give me the required result at all. 

Can anyone help?


Best



Sven






--
View this message in context: http://r.789695.n4.nabble.com/use-rows-and-cols-of-a-matrix-as-dates-tp4661287.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list