# Function to choose k according to Steps 1, 2, and 3 in the paper
ARCHmX_choosek = function(X, Y, range, B, alpha)
{
  rate=rep(0,length(range))
  for(l in 1:length(range))
  {
    reject = 0
    for(i in 1:B)
    {
      newresi=sample(Y,length(Y),replace=FALSE)
      if (ARCHmX_anova_test_univariate(X, abs(newresi),range[l]) < 0.05)
        reject = reject+1
    }
    rate[l]=abs(sum(reject/B)-alpha)
  }
  range[rate==min(rate)][1] #Choose smaller window size if more than one window provides the same performance.
  
}