How to gradually transform n number generation in normal distribution in R

Asked

Viewed 59 times

4

I have developed a code to solve the image issue, which is to try to approximate the percentage of 68.2% numbers generated in the normal distribution that are between -1 and 1. Follow the code:

x <- rnorm(100); x <- x[-1 <= x & x <= 1]
r1<-length(x)/100

y <- rnorm(1000); y <- y[-1 <= y & y <= 1]
r2<-length(y)/1000

z <- rnorm(10000); z <- z[-1 <= z & z <= 1]
r3<-length(z)/10000

print(r1*100)
print(r2*100)
print(r3*100)

But I would like to transform this code into something more gradual, where 100:1000000 would be generated within the normal distribution and the value in percentage would be printed as the value gradually grew from 100 to 1000000. To solve this problem I thought of creating a counter within the while function, but I don’t know how to increase the value of numbers generated within the parenthesis gradually as if it were a simple, accurate counter of a light in that part. inserir a descrição da imagem aqui

2 answers

7


You see, if you start with the value 100 and gradually increase to 1000000 by a gradual addition, say 20 to 20, you will not "ever" to 1000000, because that is a lot of steps. What you need is a geometric progression, that is, a multiplicative effect. You consider that multiplying by 10 is very fast, and so I would like to be able to control the step.

So let’s set your initial and final limits.

qt.inicial <- 100
qt.final <- 1000000

And also the number of steps desired:

num.passos <- 20

Now think: I must multiply the initial number (qt.inicial) a certain number of times (num.passos) by a certain factor, in order to reach the final number (qt.final). That is, Qt.inicial * factor num.passos = Qt.final. Therefore, to know the desired factor, it is enough to resort to radiciation: factor = (Qt.final / Qt.initial) (1 / num.steps)

fator <- (qt.final / qt.inicial) ^ (1 / num.passos)

So now we put the rest of our logic in a loop:

for(i in 0:num.passos) {
  qt <- round(qt.inicial * fator ^ i, 0)
  x <- rnorm(qt); x <- x[-1 <= x & x <= 1]
  r <- length(x) / qt

  print(paste0("Para a quantidade = ", qt, " o resultado é: ", r*100))
}

Running this code, I got the following output:

[1] "Para a quantidade = 100 o resultado é: 65"
[1] "Para a quantidade = 158 o resultado é: 64.5569620253165"
[1] "Para a quantidade = 251 o resultado é: 75.2988047808765"
[1] "Para a quantidade = 398 o resultado é: 66.0804020100502"
[1] "Para a quantidade = 631 o resultado é: 68.1458003169572"
[1] "Para a quantidade = 1000 o resultado é: 67.9"
[1] "Para a quantidade = 1585 o resultado é: 66.4984227129338"
[1] "Para a quantidade = 2512 o resultado é: 68.6305732484076"
[1] "Para a quantidade = 3981 o resultado é: 69.304194925898"
[1] "Para a quantidade = 6310 o resultado é: 68.4310618066561"
[1] "Para a quantidade = 10000 o resultado é: 67.7"
[1] "Para a quantidade = 15849 o resultado é: 68.3197678086946"
[1] "Para a quantidade = 25119 o resultado é: 68.3466698515068"
[1] "Para a quantidade = 39811 o resultado é: 68.4584662530456"
[1] "Para a quantidade = 63096 o resultado é: 68.4718524153671"
[1] "Para a quantidade = 1e+05 o resultado é: 68.415"
[1] "Para a quantidade = 158489 o resultado é: 68.2924366990769"
[1] "Para a quantidade = 251189 o resultado é: 68.2764770750312"
[1] "Para a quantidade = 398107 o resultado é: 68.1969922658984"
[1] "Para a quantidade = 630957 o resultado é: 68.3056056117929"
[1] "Para a quantidade = 1e+06 o resultado é: 68.219"

Adjusting the value of the variable num.passos, you can get the gradation you prefer.

  • Thank you so much, really what I had trying to idealize! I thank you so much!

1

Complementing the idea of user2332849 and filling to observe the convergence.

qt.inicial <- 10000
qt.final <- 1000000
num.passos <- 2000
n <- round(seq(qt.inicial, qt.final, length.out = num.passos))
ex <- c()

for(i in 1:num.passos) {
  x <- rnorm(n[i]); x <- x[-1 <= x & x <= 1]
  r <- length(x) / n[i] * 100
  ex <- c(ex, r)
}

mean(ex) # [1] 68.27124
plot(ex)
  • I took the liberty of modifying the code to facilitate interpretation.

Plot da convergência

Browser other questions tagged

You are not signed in. Login or sign up in order to post.