Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:51:08+00:00 2026-06-01T03:51:08+00:00

I am doing a simulation of a GARCH model. The model itself is not

  • 0

I am doing a simulation of a GARCH model. The model itself is not too relevant, what I would like to ask you is about optimizing the simulation in R. More than anything if you see any room for vectorization, I have thought about it but I cannot see it. So far what I have is this:

Let:

#    ht=cond.variance in t
#    zt= random number 
#    et = error term
#    ret= return
#    Horizon= n periods ahead

So this is the code:

randhelp= function(horizon=horizon){
    ret <- zt <- et <- rep(NA,horizon)#initialize ret and zt et
    for( j in 1:horizon){
      zt[j]= rnorm(1,0,1)
      et[j] = zt[j]*sqrt(ht[j])
      ret[j]=mu + et[j]

      ht[j+1]= omega+ alpha1*et[j]^2 + beta1*ht[j]
    }
    return(sum(ret))
  }

I want to do a simulation of the returns 5 periods from now, so I will run this let’s say 10000.

#initial values of the simulation
ndraws=10000
horizon=5 #5 periods ahead
ht=rep(NA,horizon) #initialize ht
ht[1] = 0.0002
alpha1=0.027
beta1 =0.963
mu=0.001
omega=0


sumret=sapply(1:ndraws,function(x) randhelp(horizon))

I think this is running reasonably fast but I would like to ask you if there is any way of approaching this problem in a better way.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:51:09+00:00Added an answer on June 1, 2026 at 3:51 am

    Instead of using numbers in your loop, you can use vectors of size N:
    that removes the loop hidden in sapply.

    randhelp <- function(
      horizon=5, N=1e4, 
      h0 = 2e-4, 
      mu = 0, omega=0,
      alpha1 = 0.027,
      beta1  = 0.963
    ){
      ret <- zt <- et <- ht <- matrix(NA, nc=horizon, nr=N)
      ht[,1] <- h0
      for(j in 1:horizon){
        zt[,j]   <- rnorm(N,0,1)
        et[,j]   <- zt[,j]*sqrt(ht[,j])
        ret[,j]  <- mu + et[,j]
        if( j < horizon )
          ht[,j+1] <- omega+ alpha1*et[,j]^2 + beta1*ht[,j]
      }
      apply(ret, 1, sum)
    }
    x <- randhelp(N=1e5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I'm doing a simulation about a Mall I have many classes in
I'm just programming a Minecraft Redstone Simulator for Android. I'm doing the simulation with
Here's the situation: I've got a class that is doing too much. It's mainly
I have been doing a little recreational holiday computing. My mini-project was a simulation
I'm doing a simulation where I must calculate many averages and I thought that
I am not much into gaming but i am learning and doing some practicles
I'd like to model genres of games in a way such that genres can
I'm trying to implement a simulation for a lattice model (lattice boltzmann) in Python.
I'm currently doing a simulation of a hard disk drive IOs in C++, and
i'm doing simulation which generates thousands of result objects. Each object size is around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.