-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinomialToNormal.py~
More file actions
76 lines (56 loc) · 1.49 KB
/
BinomialToNormal.py~
File metadata and controls
76 lines (56 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
""" Binomial to normal distribution"""
from __future__ import division # help avoid mistake for integer division
import pylab as pl
import random
import math as m
import numpy as np
def gaussian(x, mu, sigma):
"""return the value of the gaussian function"""
y = 1./np.sqrt(2*m.pi*sigma**2)*np.exp(-(x - mu)**2/(2*sigma**2))
return y
if __name__=='__main__':
succes = 0
i = 1
j = 1
N = 1000
p = 1./6. #probability to have six-faced
q = 1-p # probability of failure
n = 100
y=[]
# loop over 1000 exp
while i <= N:
# Loop over 100 tosses
for j in range(1,n+1):
# Toss the dice
D = random.randrange(1,7)
if D == 6:
succes = succes +1
else:
succes = succes
y.append(succes)
i = i +1
succes=0
#print y
# plot Gaussian distribution in the same curve
mu = n*p
sigma = n*p*q
#mu = 0
#sigma = 1
#t=range(1,1001)
#y1=[]
x = np.linspace(0,100,1000)
y1 = 1/m.sqrt(2*m.pi*sigma**2)*np.exp(-(x-mu)**2/2*sigma**2)
#y1.append(a)
#y = gaussian(x, 83.33, 16.6)
#print "gaussian(4, 2, 1.5) = %.5f" %y
#pylab.plot(t,y)
#pylab.show()
pl.hist(y,bins=100) # plot the first function with red (r) dashed lines (-)
pl.hold('on')
pl.plot(x, 8000*y1, 'r-') # plot the second function with blue (b) circles (o)
pl.xlabel('t')
pl.ylabel('y')
pl.legend(['gaussian','histogram'])
pl.title('Plotting two curves in the same plot')
pl.savefig("binomialToGaussian.png") #figure will be saved in current working directory
pl.show()