Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
unlisted
ubuntu2204
Kernel: SageMath 10.6
#test the RWG simulator nTrials = 100000 data = [randint(0,1) for _ in range(nTrials)] #a list of 100,000 random zeros (tails) and ones (heads) print(data.count(0), data.count(1)) # how many of each? Should be about equal...
49829 50171
#statistics of flipping a fair coin def statsCoin(nFlips): flips = [randint(0,1) for _ in range(nFlips)] first = flips[0] last = flips[-1] heads = flips.count(1) #compute longest streak streak = 1 #first flip starts a streak biggestStreak = streak # so far so good start = flips[0] pos = 1 while pos < nFlips: if flips[pos]==start: streak += 1 if streak > biggestStreak: biggestStreak = streak else: streak = 1 start = flips[pos] pos += 1 return (first, last,heads, biggestStreak)
nTrials = 100000 data=[] for k in range(nTrials): data.append((statsCoin(10))) firstData=[d[0] for d in data] headsData=[d[2] for d in data] streakData=[d[3] for d in data] [float(streakData.count(i)/nTrials) for i in [1..10]]
[0.00202, 0.17206, 0.36032, 0.24896, 0.12284, 0.05383, 0.02433, 0.00942, 0.00416, 0.00206]
[float(firstData.count(i)/nTrials) for i in range(1)]
[0.50081]
[float(headsData.count(i)/nTrials) for i in range(10)]
[0.00096, 0.00955, 0.04364, 0.11691, 0.20541, 0.24755, 0.20348, 0.1166, 0.04449, 0.01031]
sum([0.00096, 0.00955, 0.04364, 0.11691, 0.20541, 0.24755, 0.20348, 0.1166, 0.04449, 0.01031])
0.998900000000000
3
3
#fair random walk simulation # how long to first return? # assume we start t=0 at location =0 def time_to_first_return(): location = 2*randint(0,1)-1 t = 1 while not location==0: t += 1 location += 2*randint(0,1)-1 return t nTrials = 1000 data = [time_to_first_return() for _ in range(nTrials)] print(data)
[2, 72, 2, 2, 2, 52, 510, 972, 2, 2, 54, 12, 8, 4, 8, 12, 436, 60, 22, 2, 2, 2, 2, 14, 8, 4, 4, 2, 118, 6, 74, 8, 2, 2, 10, 2, 2, 18, 4, 2, 10, 14, 6, 22, 2, 4, 2, 56, 10, 4, 2, 2, 2, 2, 2, 6, 8, 16, 10, 2, 6, 2, 12, 2, 2, 4, 2, 8, 8, 2, 4, 2, 4, 2, 6, 4, 2, 2, 2, 2698, 746, 16, 2, 2, 2, 8, 46, 2, 2, 162, 4, 4, 54, 198, 6, 2, 8, 126, 84, 68, 2, 2, 18, 2, 2, 2, 4, 2, 4, 4, 2, 2, 2, 4, 70, 2, 98, 26, 17044, 2, 2, 2, 32, 2, 6, 10, 2, 6, 8688, 12, 82, 2, 50, 2, 120, 2, 8, 12, 2, 2, 2, 98, 2, 2, 2, 12, 2, 6, 2, 2, 12, 2, 2, 74, 10, 10, 2, 2, 2, 8, 16, 4, 2, 4, 2, 26, 224, 2, 6, 36, 2, 10, 2, 66, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 30, 2, 2, 46, 2, 4, 2, 26, 2, 2782, 2, 112, 2, 24, 2, 2, 12, 4, 6, 2, 10, 40, 2, 6, 2, 2, 2, 206, 4, 14, 4, 4, 12, 64, 8, 2, 4, 64, 2, 2, 4, 2, 2, 44, 52, 8, 6, 2, 2, 2, 2, 4, 8, 6, 442, 1878, 8, 2, 2, 44, 6, 40, 2, 2, 2, 8, 6, 2, 104, 4, 2, 2, 2, 2, 318, 2, 2, 6, 8, 2, 4, 6, 2, 2, 2, 2, 2, 2, 2, 2, 28, 2, 2, 10, 4, 2, 4, 6, 32, 4, 2, 10408, 10, 4, 4, 20, 4, 2, 4, 2, 14, 388, 4, 2, 160, 2, 12, 4, 2, 4754, 6, 2, 8, 12, 18, 102, 2, 2, 2, 10, 2, 2, 62, 4, 18, 6, 2, 2, 2, 6, 2, 2, 8, 2, 2, 4, 74, 2, 4, 2, 2, 2, 2, 482, 2, 2, 2, 204, 2, 2, 4, 2, 2, 12, 14, 6, 2, 26, 2, 2, 2, 2, 2, 62, 68954, 4, 2, 2, 2, 20, 2, 4, 6, 2, 62, 2, 2, 2, 2, 6, 4, 10, 2, 2, 2, 2, 90, 205884, 4, 2, 4, 2, 2, 2, 2, 2, 6, 4, 2, 20, 2, 8, 8, 2, 2, 308, 2, 122, 2, 190, 22, 14, 2, 4, 8, 4, 4, 2, 2, 2, 2, 2, 4, 2, 35592738, 6, 28, 2, 4, 264, 6, 6, 2, 2, 6, 2, 2, 10, 2, 2, 8, 4, 2, 2, 30, 94, 2, 2, 6, 2, 58, 4, 4, 386, 2, 4, 4, 26, 2, 668, 2, 12, 72, 8, 2, 20, 2, 2, 2, 6, 2, 34, 6, 6, 2, 20, 2, 22, 4, 4, 2, 282, 2, 2, 2, 2, 4, 2, 2, 2, 2, 12, 2, 4, 2, 4, 2, 4, 2, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 4, 524, 4, 2, 18, 2, 2, 2, 2, 1566, 2, 20, 1162, 2, 6, 6, 2, 12, 6, 8, 2, 2, 2, 4, 304, 20, 12, 2, 2, 8, 6, 24, 2, 14, 484, 4, 942, 4, 12, 2, 8, 2, 170, 8, 6, 1108, 2, 2, 484, 6, 4, 4, 2, 10, 6, 2, 62, 2, 4, 4, 4, 4, 6, 16, 28, 10, 4, 2, 2, 52, 2, 2, 344, 10, 2, 2, 4, 764, 2, 36, 4, 2, 2, 10, 2, 4, 14, 2, 2, 2, 2, 2, 10, 6, 2, 24, 2, 2, 18, 152, 120, 2, 72, 2, 6, 2, 4, 2, 2, 2, 4, 118, 2, 6, 4, 2, 2, 4, 2, 10, 8, 2, 674, 4, 2, 2, 6, 2, 6, 4758, 2, 2, 6, 12, 20, 4, 4, 8, 6, 34, 2, 1208, 2, 2, 2, 2, 2, 2, 2, 4, 292, 2, 6, 2, 2, 2, 2, 2, 6, 4, 6, 26, 4, 142, 52, 16, 4, 4, 6, 4, 2, 2, 10, 4, 2, 2, 24, 2, 8, 10704, 22, 6, 2, 4, 336, 2, 14, 10, 2, 8, 2, 4, 16, 1130, 6, 2, 2, 2, 4, 2, 22, 2, 2, 36, 8, 2, 8, 1406, 18, 2, 2, 2, 18, 2, 2, 6, 2, 4, 6, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 28, 4, 2, 1554, 12, 2, 16, 146, 20, 4, 2, 2, 7682, 6, 6, 2, 34, 2, 2, 96, 2, 2, 14, 2, 4, 2, 150, 824, 4, 2, 2, 2, 16, 2, 4, 18, 68, 2, 14, 2, 14, 6, 2, 2, 2, 2, 4, 2, 2, 6, 2, 4, 2, 10, 78, 12, 4, 2, 4, 2, 4, 22, 2, 4, 38, 108, 2, 6, 2, 8, 2, 2, 2, 2, 4, 2, 2, 2, 4, 4, 12, 4, 60, 2, 98, 2, 2, 2, 4, 14, 2, 4, 2, 4, 8, 4, 2, 2, 12, 2, 2, 2, 2, 130, 2, 6, 2, 4, 4, 14, 12, 2, 2, 12, 132, 6, 4, 38, 2, 6, 12, 6, 8, 6, 2, 2, 2, 4, 2, 2, 6, 2, 2, 4, 2, 2, 2, 30, 2, 10, 2, 2, 2, 2, 2, 2, 16, 2, 2, 2, 222, 4, 24, 2, 20, 2, 12, 2, 6, 2, 2, 2, 138, 14, 94, 2, 2, 8, 4, 2, 4, 2, 2, 2, 12, 3572, 2, 6, 4, 4, 8, 16, 2, 2, 2, 10, 6, 4, 6, 2, 2, 4, 54, 194, 25780, 2, 2, 18, 4, 622, 2, 140, 2, 18, 4, 14, 42, 52, 6, 2, 2, 2, 2, 2, 2, 6, 8, 2, 2656, 2, 12, 8, 2, 34, 2, 2, 4, 2, 1422, 2, 2, 598, 2, 2, 2, 2, 680, 2, 6, 4, 2, 8, 66, 2, 6, 4, 2, 6, 6, 2, 2, 4, 2, 2, 4, 58, 560, 772, 14, 2, 2, 6, 2]
[data.count(2*k) for k in [1..20]]
[474, 139, 79, 42, 25, 28, 17, 10, 11, 10, 7, 5, 6, 4, 3, 2, 4, 3, 2, 2]
#fair random walk simulation # how long to first return? # assume we start t=0 at location =0 def time_to_first_return(): location = 2*randint(0,1)-1 t = 1 while not location==0: t += 1 location += 2*randint(0,1)-1 return t nTrials = 200 data = [time_to_first_return() for _ in range(nTrials)] print(data)
[2, 2, 2, 96, 6, 2, 2, 130, 2, 2, 12, 2, 2, 10, 2, 2, 2, 2, 2, 8, 2, 2, 1172, 20, 2, 2, 2, 2, 8, 2, 2, 2, 82, 6, 6814, 62, 4, 20, 2, 2, 4, 2, 2, 4, 2, 2, 4, 486, 2, 6, 4, 2, 2, 2, 2, 2, 2, 98, 28, 2, 2, 24, 6, 12, 24, 2, 2, 2, 8, 2, 18, 46, 12, 24, 2, 2, 2, 34, 4, 2, 2, 2, 2, 44, 64, 2, 2, 2, 3314, 480, 10, 2, 4, 6, 2, 2, 2, 2, 2, 10, 2, 2, 6, 24, 2, 290, 8, 2, 124, 2, 8, 16, 2, 38, 2, 18, 2, 4, 332, 40, 74, 2, 2, 2, 2, 8, 2, 2, 34, 2, 2, 2, 40, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 24, 2, 6, 2, 2, 2, 4, 6, 6, 2, 12, 2, 6, 4, 6, 4, 4, 4, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 28, 4, 2, 10, 2, 4, 6, 4, 12, 2, 2, 6, 2, 36, 2, 4, 36, 34, 6, 244, 2, 2, 4, 4, 4, 2, 2, 4, 20]
[data.count(2*k) for k in [1..20]]
[108, 24, 14, 6, 4, 5, 0, 1, 2, 3, 0, 5, 0, 2, 0, 0, 3, 2, 1, 2]
big=[a for a in data if a>1000] big.sort() big
[1108, 1130, 1162, 1208, 1406, 1422, 1554, 1566, 1878, 2656, 2698, 2782, 3572, 4754, 4758, 7682, 8688, 10408, 10704, 17044, 25780, 68954, 205884, 35592738]
# simple 1-dim RW nSteps = 10000 nWalks = 8 canvas = Graphics() for w in range(nWalks): y = 0 path=[y] for _ in range(nSteps): y += 2*randint(0,1)-1 path.append(y) canvas += list_plot(path,plotjoined=True, color=Color(w/nWalks,1-w/nWalks,.2)) show(canvas)
Image in a Jupyter notebook
#gather lotsa data nSteps = 1000 nTrials = 1000 #collect end distance, number of returns, first return end_dist =[] num_ret = [] first_ret = [] for _ in range(nTrials): end=0; nr=0; fr=0 #fr will change from 0 if there is a return for step in [1..nSteps]: end += 2*randint(0,1)-1 if end == 0: nr += 1 if nr ==1: fr = step end_dist.append(abs(end)) num_ret.append(nr) first_ret.append(fr)
for k in [10..20]: print (end_dist[k], num_ret[k], first_ret[k])
60 2 2 18 34 82 4 19 2 18 48 2 16 64 4 42 18 2 40 0 0 44 11 2 46 15 2 34 25 8 56 20 2
histogram(end_dist)
Image in a Jupyter notebook
hret1000=histogram(num_ret)
sum(end_dist)
25668
hret1000.save("histret1000.pdf")
sum(end_dist)
24746
histogram(num_ret)
Image in a Jupyter notebook
h1000ret=histogram(num_ret) h1000ret.save("histo1000numret.pdf")
num_ret.count(0)
34
histogram(first_ret)
Image in a Jupyter notebook
first_ret.count(0)
29
max(first_ret)
972
import numpy as np nSteps = 100 nTrials = 1000 def b(n): return float(binomial(2*n,n)/2^(2*n)) #collect end distance, number of returns, first return end_dist =[] #num_ret = [] #first_ret = [] for _ in range(nTrials): end=0#fr will change from 0 if there is a return for step in [1..nSteps]: end += 2*randint(0,1)-1 end_dist.append(abs(end)) print(np.mean(end_dist),b(nSteps/2)*nSteps)
7.9314 7.958923738717877