Double for loop doesn't work

45 Views Asked by At

I have 100000 iid normal variables X1, . . . , X100000 from N(0,1) and for i ∈ {2, . . . , 100000} and want to plot the graph of the function \

$$ R_i = \frac{\max{X_j}}{\sqrt{2 \log i}} $$

where j=1,...,i. And Repeat the above experiment 10 times and plot the respective trajectories of Ri on the same graph. I tried this code

sample<- rnorm(100000,mean=0,sd=1)
i=2:100000
r=0
for (i in 2:100000){
  for (j in 1:i){
    r[i]=(max(sample[j]))/(sqrt(2*log(i)))
  }
}
plot(r)

But the results are not logical. I think the for loop doesn't work properly in max(X_j) but I don't know where. Also how to repeat it 10 times, could you please help? Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

This is more of a programming question as it is not quite about the law of the iterated logarithm.

R has a cummax function so you could do something like

set.seed(2020)
r <- function(samplesize){
  s <- rnorm(samplesize, mean=0, sd=1)
  cummax(s) / sqrt(2*log(1:samplesize))
  }
simulations <- replicate(10, r(10^5))
matplot(simulations, type="l") 

to get something like

enter image description here