Reputation: 131
I have a matrix pmatrix
sigma = 0.03
alpha = 0.01
sims = 6
N = 10
pmatrix = matrix(NA, ncol=sims, nrow = N)
for (i in 1:N){
x = rnorm(sims)
pmatrix[i,] <- x
}
And I need to use this matrix in order to get the xt values from the following expression:
xt = 0
for (i in 1:10){
xt[i+1] = xt[i] * exp(-alpha*1) + sqrt(((sigma^2)/2*alpha)*(1-exp(-2*alpha*1)))*pmatrix[i]
}
However the following loop only returns a xt vector. Ideally I would like to obtain a matrix which consists of 10 rows (N- number of years) and 6 columns (sims - the number of simulated scenarios). I believe it is doable via a second loop or an apply function.
Upvotes: 1
Views: 168
Reputation: 11514
Just as an extended comment: the issue you were facing came from the fact that you were subsetting xt
wrongly. If you want to do something on a matrix rowwise, use the entire row, which you can retrieve using xt[1,]
as opposed to xt[1]
. See:
sigma = 0.03; alpha = 0.01; sims = 6; N = 10
pmatrix = matrix(rnorm(sims*N), ncol=sims, nrow = N)
xt <- matrix(0, ncol=sims, nrow = N)
xt[1] # just one element
[1] 0
xt[1,] # entire row
[1] 0 0 0 0 0 0
Then it works also with your approach:
for(i in 2:N){
xt[i,] <- xt[i-1,] * exp(-alpha) + sqrt(((sigma^2)/2*alpha)*(1-exp(-2*alpha)))*pmatrix[i-1,]
}
head(xt)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.0000000000 0.000000e+00 0.0000000000 0.000000e+00 0.000000e+00 0.0000000000
[2,] 0.0005102757 9.680876e-05 0.0006264992 2.147689e-05 9.432744e-05 0.0004370840
[3,] 0.0005035357 -4.704802e-04 0.0003954507 -5.083994e-04 4.237845e-04 0.0004007277
[4,] 0.0009345963 -2.699634e-04 0.0003559880 -3.877696e-04 5.337181e-04 0.0005230792
[5,] 0.0016501564 -1.947569e-04 0.0005003010 -6.680216e-05 6.098233e-04 0.0007106022
[6,] 0.0015317653 4.316999e-04 0.0011361772 -2.209149e-04 6.881100e-04 0.0005893373
Upvotes: 1
Reputation: 23899
Hope this suits you. Sometimes its easier to use an apply
function on column or row indices instead of a data object itself.
sigma = 0.03
alpha = 0.01
sims = 6
N = 10
pmatrix <- matrix(rnorm(N * sims), N)
xt <- matrix(nrow=N, ncol=6)
xt[1,] <- 0
sapply(2:N, FUN = function(x) {
xt[x,] <<- xt[x-1,] * exp(-alpha*1) + sqrt(((sigma^2)/2*alpha)*(1-exp(-2*alpha*1)))*pmatrix[x-1,]
})
> xt
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.0000000000 0.000000e+00 0.000000e+00 0.0000000000 0.0000000000 0.000000e+00
[2,] -0.0006488202 4.730257e-04 4.707051e-04 0.0002174562 0.0001655868 -2.063875e-04
[3,] -0.0007110547 3.792143e-04 3.922429e-04 0.0009465164 -0.0001667539 -1.165253e-05
[4,] -0.0003679911 -3.596447e-05 5.490986e-06 0.0013176437 -0.0006049390 2.276431e-04
[5,] -0.0007176342 1.754809e-05 3.647631e-04 0.0015136978 -0.0010303508 4.773186e-04
[6,] -0.0007918234 -3.065909e-05 -3.703564e-05 0.0015006314 -0.0005650229 7.792698e-05
[7,] -0.0008442265 -2.808698e-04 7.808261e-05 0.0015505998 -0.0005407453 -3.106797e-04
[8,] -0.0010265038 8.540579e-05 2.547632e-04 0.0017364697 -0.0007112818 -3.972706e-04
[9,] -0.0004011710 7.346707e-05 7.471667e-04 0.0014031268 -0.0008266330 -4.296555e-04
[10,] -0.0001490369 -3.189111e-04 1.133248e-03 0.0013038771 -0.0011771068 -2.719285e-04
Upvotes: 2