Author Topic: Random sums Problem  (Read 4029 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Random sums Problem
« on: October 10, 2020, 09:45:39 am »
Summing the result of each draw from the RND function, how many draws are expected to occur until the sum exceeds 1?

That is, on average, if I do

RND + RND + RND + RND + ...

when does that sum pass 1? Well, of course the average of RND is 0.5, so we should expect RND + RND alone to more-or-less do the job. Right?

Let's try some code...

Code: QB64: [Select]
  1. DIM y, z AS LONG
  2. y = 0
  3. FOR z = 0 TO 2 ^ 16
  4.     x = 0
  5.     DO WHILE (x < 1)
  6.         y = y + 1
  7.         x = x + RND
  8.     LOOP
  9.     PRINT y / z

After 2^16 iterations, the average number of random draws settles around 2.72, which is clearly indicative of Euler’s constant lurking about. The proof was quite a pain in the ass, actually... Two pages:
ss1.png
* ss1.png (Filesize: 132.46 KB, Dimensions: 1920x1080, Views: 237)
ss2.png
* ss2.png (Filesize: 101.11 KB, Dimensions: 1920x1080, Views: 255)
« Last Edit: October 10, 2020, 10:47:17 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Random sums Problem
« Reply #1 on: October 10, 2020, 11:49:17 am »
So while this is still fresh in your head, how many randoms multiplied together does it take for the expected product to go below 1/n ?

RND * RND * RND.... <= 1/N

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Random sums Problem
« Reply #2 on: October 10, 2020, 12:47:16 pm »
Quote
Summing the result of each draw from the RND function, how many draws are expected to occur until the sum exceeds 1?

That is, on average, if I do

RND + RND + RND + RND + ...

when does that sum pass 1? Well, of course the average of RND is 0.5, so we should expect RND + RND alone to more-or-less do the job. Right?

RND never actually gives us a value of 1, so the base range would be 0 to 0.99999999, which would average to 0.49999999.   I’d expect it to take a bit more than 2 additions, on average, to pass 1.  Honestly, I wouldn’t have expected it to take 2.72 additions on average, but I wonder how much of that is a result of QB64’s RND not being a true random value, but a pseudorandom formula instead.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Random sums Problem
« Reply #3 on: October 10, 2020, 01:26:21 pm »
Code: QB64: [Select]
  1. trials = 1000000
  2. FOR i = 1 TO trials
  3.     tot = 0: cnt = 0
  4.     WHILE tot < 1
  5.         tot = tot + RND
  6.         cnt = cnt + 1
  7.     WEND
  8.     totCnt = totCnt + cnt
  9. PRINT totCnt / trials, 2.7183 - totCnt / trials '4 decimals
  10.  
  11.  

Pretty darn close for single precision.

Offline Rubidium

  • Newbie
  • Posts: 10
    • View Profile
Re: Random sums Problem
« Reply #4 on: October 10, 2020, 06:21:40 pm »
Wow, this was reaaaallly hard for me to accept.

I was also thinking that the ~2.72 MUST HAVE BEEN due to the way qb64 creates a RANDOM number.

After a couple of tests, I realize my idea of how random numbers add up WAS wrong, and so I thought I'd share.

examining the sum of 3 random numbers,

I was thinking that adding 3 random numbers, where each random number has an equal distribution between 0 and 1,

would create a probability distribution space ranging between 0 and 3 equally, which would imply that the probability of the sum of 3 random numbers

adding up to less than 1 would simply be 1/3, which is WRONG because that is equivalent to thinking that the sum of 3 random numbers is on average equal to 1

random number x 3, which is a fallacy!

the volume boundaries you attached explain well how to properly understand the sum of random numbers by accepting that each

random number deserves it's own dimension and that the probability of the sum of n random numbers less than 1 is indeed equal to 1/n!


very beautiful to see Euler's number appear here as the sum of probabilities. Thanks for updating my view on numbers STxAxTIC!


Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Random sums Problem
« Reply #5 on: October 11, 2020, 01:58:44 am »
Hi guys, thanks for checking this out.

@bplus
So while this is still fresh in your head, how many randoms multiplied together does it take for the expected product to go below 1/n ?

RND * RND * RND.... <= 1/N

You question is equivalent to asking when is N * RND * RND * RND * ... < 1. The answer is 1+log(N). I can write up a proof tomorrow, but for now be convinced by this code:

Code: QB64: [Select]
  1. DIM n, n0 AS DOUBLE
  2. DIM x, z AS LONG
  3. x = 0
  4.     n0 = 51 ' Put anything here
  5.     n = n0
  6.     DO
  7.         n = n * RND
  8.         x = x + 1
  9.         IF (n < 1) THEN EXIT DO
  10.     LOOP
  11.     z = z + 1
  12.     PRINT (x / z) - (1 + LOG(n0)) ' Should be zero
  13.     _DELAY .01
  14.  



@Steve
I wonder how much of that is a result of QB64’s RND not being a true random value, but a pseudorandom formula instead.
No effect at all - RND is random enough for this.



@Rubidium
I like that you played around with the whole idea and had to really convince yourself - I'm stubborn like that too!
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Random sums Problem
« Reply #6 on: October 11, 2020, 10:08:21 am »
Quote
You question is equivalent to asking when is N * RND * RND * RND * ... < 1. The answer is 1+log(N). I can write up a proof tomorrow, but for now be convinced by this code:

Yeah, I am convinced!

Code: QB64: [Select]
  1. _TITLE "1 + LOG(N) = number of *RND for product < 1 over N test" 'b+ 2020-10-11
  2. nTrials = 1000000
  3. FOR n = 1 TO 20
  4.     tot = 0
  5.     FOR t = 1 TO nTrials
  6.         r = n: cnt = 0
  7.         WHILE r >= 1
  8.             r = r * RND
  9.             cnt = cnt + 1
  10.         WEND
  11.         tot = tot + cnt
  12.     NEXT
  13.     PRINT n, tot / nTrials, 1 + LOG(n), tot / nTrials - (1 + LOG(n))
  14.  
  15.  
« Last Edit: October 11, 2020, 10:09:32 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Random sums Problem
« Reply #7 on: October 11, 2020, 03:58:41 pm »
Alrighty, here's the pudding. I'm mostly happy with the wording, the flow may change trivially, but the gist is here:
2020-10-11.png
* 2020-10-11.png (Filesize: 211.58 KB, Dimensions: 1920x1080, Views: 224)
You're not done when it works, you're done when it's right.