Volatility updating with Heston-Nandi model
First, in R we use &lt;- for assignment. It has its advantages.
It is prefered that you don't use T and F as variable names as these are
aliases to TRUE and FALSE.
Instead of this P &lt;-  rep(P_0, T) you could have P &lt;- vector(mode = &quot;numeric&quot;, length = T).
This is just a suggestion: But there is no reason to not name your parameters:
para &lt;- c(0.1, 0.2, 0.3, 0.4, 0.5, 0.7) # (beta_0,beta_1, beta_2, beta_3, r, gamma)
para &lt;- c(beta_0 = 0.1,  beta_1 = 0.2, beta_2 = 0.3, beta_4 = 0.4, r = 0.5, gamma = 0.7) # (beta_0,beta_1, beta_2, beta_3, r, gamma)

Then, you may access them like para[&quot;gamma&quot;], but not para$gamma.
Also, there is no need for para_vol&lt;-para[1:6] in the loop, but I wager this is remnant
from other attempts.
In terms of performance, the pre-allocation I used did a 70 ms to 50 ms. Although,
I'm not confident in those numbers, as I did it fairly unscientifically.
Also, the referenced post shows a more direct way of calculating this.
Collapsed version of the code I ended up with:

library(fGarch)

T &lt;-  3000
# For the example I simulate a GARCH
#process parameters
eta &lt;-  0.2 #eta = 0 is equivalent to Geometric Brownian Motion
mu &lt;-  100 #the mean of the process

#GARCH volatility model
specs &lt;-  garchSpec(model = list(
  omega = 0.000001,
  alpha = 0.5,
  beta = 0.4
))
sigma &lt;-  garchSim(spec = specs, n = T)

# library(ggfortify)
# autoplot(sigma)

P_0 &lt;- mu #starting price, known
# P &lt;-  rep(P_0, T)
P &lt;- vector(mode = &quot;numeric&quot;, length = T)
P[1] &lt;- P_0

for (i in 2:T) {
  P[i] &lt;-  P[i - 1] + eta * (mu - P[i - 1]) + sigma[i] * P[i - 1]
}
# DOESN'T WORK
# P[-T] &lt;-  P[-T] + eta * (mu - P[-T]) + sigma[-1] * P[-T]

# Set the parameters :
para &lt;-
  c(0.1, 0.2, 0.3, 0.4, 0.5, 0.7) # (beta_0,beta_1, beta_2, beta_3, r, gamma)
# Iteration to obtain the volatility associate to the model :

vol &lt;- vector(&quot;numeric&quot;, length = length(P))
vol[1] &lt;-  sd(P)
for (i in 2:length(P)) {
  # para_vol &lt;- para[1:6]
  vol[i] &lt;-
    para_vol[1] + (para_vol[2] * vol[i - 1]) + (para_vol[3] /
                                                  vol[i - 1]) * (P[i - 1] - para_vol[5] - (para_vol[4] + para_vol[6]) * vol[i - 1])
}
vol

# autoplot(vol)