user3474223
user3474223

Reputation: 1

Rerun of formula to make many columns of data (matrix using functions rep() & Matrix())

The R code i initially have is below which uses constants in a formula to run a model and and create simulations of data using one constant figure wb$yfit and X that involves a random normal variable at every data point. The goal is to use not just create one column of data, but Z amounts of columns where at the rows coincide with wb$yfit and but vary due to the X at each [,#].

N <- length(wb$time) # No. of historical observations
theta <- theta.hat
sigma <- sigma.hat
stdev <- sqrt ( sigma ^2/(2* theta )*(1- exp (-2* theta )))
X <- vector(mode="numeric", length =N)
X[1] <- wb$Ln_price_[1] - wb$yfit[1]

for (i in 2:N){
    X[i] <- exp (- theta ) * X[i -1] + stdev * rnorm (1 ,0 ,1)
}

NBP.sim <- wb$yfit + X
plot (wb$time, NBP.sim, ylim =c (3 ,5) ,type ="l",
      col="red",xlab =" Time ",ylab ="EUR / MWh ",las =1)
lines (NBP.sim , col=" blue ", type ="l")

An idea for coding it is as such, but i cant get it to work:

N <- 10
days <- length(wb$time)
X <- rep(wb$Ln_price_[1] - wb$yfit[1],N)
TTF.sim.Q <- matrix(0, nrow =N, ncol=days)

for(i in 2: days ){
    ## Simulating the OU part of the log price
    X[i] <- exp (- theta ) * X[i-1] + stdev * rnorm (N ,0 ,1)
    ## Adding the deterministic part to the stochastic 
    ## part to get the final simulated spot price
    TTF.sim.Q[,i] <- wb$yfit[i] + X 
}

## Plotting prices in EUR /MWh
matlines(wb$time, t(TTF.sim.Q),type ="l",
         lty= rep (1, ncol(t(TTF.sim.Q))), ylim =c (3 ,5)) 

Helps is much appreciated!!!


The R code i initially have is below which uses a formula to run a model and and create simulations of data using figures wb$yfit and X that involves a random normal variable at every data point. This results in a column of 731 simulated prices, using wb$yfit and adding the random flow of X. This X is a standard deviation and mean reversion component to the price, so the reason for the random variable is that prices move up or down at random.

*The last part of the code draws this simulation on a plot to show how the price moves over time.

The goal is to use not just create one column of data, but Z amounts of columns where the row (time) coincide with wb$yfit (different values at different time) and but vary due to the X as it includes a random variable.

So how can i make 10 simulations of the price instead of just one that i have created, which in my mind means that i want to create 10 simulations meaning 10 columns of data using the same function that was used in the code above. Each column would represent a simulation of 731 prices over time(each point being wb$yfit[n] + X[n] the next point would be wb$yfit[n+1] + X[n+1]). So the price at each point in time in a column should be different due to the random variable included in the calc. of X. Thus finally i want to show these 10 simulations in the plot.

Any idea how i can create this?

Id assume its still using rep() and Matrix(). (I guess the big problem is to get an independent random # generated for each X depending on the previous X, as well as adding it to the time relevant WB$yfit) Thanks again..

N <- length(wb$time) # No. of historical observations
theta <- theta.hat
sigma <- sigma.hat
stdev <- sqrt ( sigma ^2/(2* theta )*(1- exp (-2* theta )))
X <- vector(mode="numeric", length =N)
X[1] <- wb$Ln_price_[1] - wb$yfit[1]

for (i in 2:N){
    X[i] <- exp (- theta ) * X[i -1] + stdev * rnorm (1 ,0 ,1)
}

NBP.sim <- wb$yfit + X
plot (wb$time, NBP.sim, ylim =c (3 ,5) ,type ="l",
      col="red",xlab =" Time ",ylab ="EUR / MWh ",las =1)
lines (NBP.sim , col=" blue ", type ="l")

An idea for coding it is as such, but i cant get it to work:

N <- 10
days <- length(wb$time)
X <- rep(wb$Ln_price_[1] - wb$yfit[1],N)
TTF.sim.Q <- matrix(0, nrow =N, ncol=days)

for(i in 2: days ){
    ## Simulating the OU part of the log price
    X[i] <- exp (- theta ) * X[i-1] + stdev * rnorm (N ,0 ,1)
    ## Adding the deterministic part to the stochastic 
    ## part to get the final simulated spot price
    TTF.sim.Q[,i] <- wb$yfit[i] + X 
}

## Plotting prices in EUR /MWh
matlines(wb$time, t(TTF.sim.Q),type ="l",
         lty= rep (1, ncol(t(TTF.sim.Q))), ylim =c (3 ,5)) 

Helps is much appreciated!!!

What i have tried up until now is:

N <- 10

days <- length(wb$time)

X <- rep(wb$Ln_price_[1] - wb$yfit[1],N)

TTF.sim.Q <- matrix(0, nrow =N, ncol=days)

for(i in 2: days ){ + X[i] <- exp (- theta ) * X[i-1] + stdev * rnorm (N ,0 ,1)# Simulating the OU part of the log price

TTF.sim.Q[,i] <- wb$yfit[i] + X # Adding the deterministic part to the stochastic part to get the final simulated spot price }

Error in TTF.sim.Q[, i] <- wb$yfit[i] + X : number of items to replace is not a multiple of replacement length In addition: Warning messages: 1: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 2: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 3: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 4: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 5: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 6: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 7: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 8: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 9: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length 10: In X[i] <- exp(-theta) * X[i - 1] + stdev * rnorm(N, 0, 1) : number of items to replace is not a multiple of replacement length

Check on the results-->

data.frame(X) X 1 -0.07735493 2 -0.08452771 3 -0.09945222 4 -0.04269141 5 -0.03170452 6 -0.04693956 7 0.01079676 8 0.03830161 9 0.06544999 10 0.08219423 11 0.09241736

Also when i tried the code:

N <- 10

days <- length(wb$time)

X <- rep(wb$Ln_price_[1] - wb$yfit[1],N)

TTF.sim.Q <- matrix(0, nrow =N, ncol=days)

erri <- rnorm(length(days))

for(i in 2: days ){ + ## Simulating the OU part of the log price + X[i] <- exp (- theta ) * X[i-1] + stdev * erri[i] + ## part to get the final simulated spot price + TTF.sim.Q[i] <- wb$yfit[i] + X[i] + }

data.frame(X) X 1 -0.07735493 2 NA 3 NA

and soo on.... with the NA

TTF.sim.Q[i] [1] NA

and the TTF.sim.Q gave me nothing

Upvotes: 0

Views: 236

Answers (1)

IRTFM
IRTFM

Reputation: 263331

It may not be working because rnorm(N,0,1) is producing 10 element vectors when wehen you are indexing as though X is a vector. Try this (which calls rnorm only once) and should be more efficient as well as more successful. (You are also committing the same error with addition of an unsubscripted X in the next assignment.)

erri <- rnorm(length(days)
for(i in 2: days ){
    ## Simulating the OU part of the log price
    X[i] <- exp (- theta ) * X[i-1] + stdev * erri[i]
    ## part to get the final simulated spot price
    TTF.sim.Q[i] <- wb$yfit[i] + X[i] 
}

Or if you want to do multiple columns at once, then make everything 10 elements wide and use [i,j] indexing with the column index empty:

TTF.sim.Q <- matrix(0, nrow =N+1, ncol=days)
errij <- matrix( rnorm(days*N), ncol=N)
for(i in 2: days ){
    ## Simulating the OU part of the log price
    X[i,] <- exp (- theta ) * X[i-1, ] + stdev * errij[ i, ]
    ## Adding the deterministic part to the stochastic 
    ## part to get the final simulated spot price
    TTF.sim.Q[i, ] <- c( wb$yfit[i] , X[i,]) # needs to be N+1 columns wide
}

Upvotes: 0

Related Questions