Created
April 8, 2018 00:41
-
-
Save antonio-catalano/70513b0f2dbddfdae4846baf68c696cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' In what follows all number are uniformly distributed on [0,1]. Start with | |
a number So and the keep on generating the sequence S1,S2....until, for some | |
N, Sn > So for the first time. What is the expected value E(N) of that N ? ''' | |
import statistics | |
import random | |
# we proceed with Montecarlo method | |
Series_of_n = [] #we creat a list of 10 million elements (n = 10.000.000) | |
for i in range (10000000): | |
So = float(random.uniform(0,1)) # So of the ith trial | |
n = 1 # the first attempt after generating the initial number So | |
while True: | |
Sn = float(random.uniform(0,1)) | |
if Sn > So: #for every 10 million trial, we block the while cicle we at the first Sn > So | |
Series_of_n.append(n) | |
break | |
else: | |
n += 1 #if Sn < So, the while cicle continues and n = n + 1 | |
print(statistics.mean(Series_of_n)) #the mean of the elements in the list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment