Search code examples
rstatistics-bootstrap

Implementing the bootstrap method for resampling the data set. Assuming that log prices follow random walk but using ARMA model


#install.packages("quantmod")
#install.packages("dataframes2xls")
#install.packages("bootstrap")
#install.packages("fArma")
library(bootstrap)
library(quantmod)
library(dataframes2xls)
library(fArma)

require(TTR)

getSymbols("SNE",src="yahoo",from = as.Date("2011-04-20"), to =as.Date("2015-04-22")) 

SNElog <- diff( log( Cl( SNE ) ) )
SNElog <- SNElog[-1,]

SNElogT <- as.ts( tail(SNElog, 1000))

SNElogTimeArma <- armaFit( formula=~arima(0,1,0), data=SNElogT )

SNE.Adjusted.boot.sum <- numeric(1000)
for(i in 1:1000)
{
  this.samp <- SNElog [ sample(1000,1000,replace=T, prob=??? )]
  SNE.Adjusted.boot.sum[i] <- sum(this.samp)
}

This is my code.

My professor requirement: Implement the bootstrap method for resampling the data set, assuming that log prices follow random walk using an ARMA model.

Random walk just reminds my of ARIMA(0,1,0), But I have no idea how to combine the bootstrap with ARMA model.


Solution

  • Simply put, bootstrap is just recursively generating samples with replacement so as to fit a model. Then their performance is aggregated.

    Below is a quick trial to obtain bootstrap coefficients, assuming ARIMA(1, 0, 1). As it is not specified clearly, I'm not sure the actual requirement.

    library(fArma)
    set.seed(1237)
    price <- diff(sample(log(100:120), 101, replace = TRUE))
    
    # bootstrap
    boot <- function(trial, formula, data) {
      mod <- armaFit(formula, sample(data, trial, replace = TRUE))
      c(mod@fit$coef)
    }
    
    coef <- do.call(rbind, lapply(rep(length(price), 2), boot, formula = ~ arima(1,0,1), data = price))
    apply(coef, 2, mean)
    
            ar1         ma1   intercept 
    -0.66724275  0.67331811 -0.00551791 
    

    Note that I only made 2 random samples (rep(length(price), 2)) and your result will be different with a different setup or even with the same setup - recall that bootstrap generates random samples.

    The key idea of bootstrap is in armaFit(formula, sample(data, trial, replace = TRUE)) where the model is fit to bootstrap sample, not the actual data.

    I hope it is helpful.