Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

()&:/#""

"(&* '

E6???A F @
'')!$(#"'"& ,"!$& ) 
#$)*#*%$((%()0(%
)*(+*%$%*((%()$*)*#*))
$%(#":+))$;

E6???A E A
"&(" #&! ,'(&)((
dist, numSamples = [], 1000000

for i in range(numSamples):
dist.append(random.gauss(0, 100))

weights = [1/numSamples]*len(dist)
v = pylab.hist(dist, bins = 100,
weights = [1/numSamples]*len(dist))
pylab.xlabel('x')
pylab.ylabel('Relative Frequency')

print('Fraction within ~200 of mean =',


sum(v[0][30:70]))

E6???A F B
)($)(

)(*
&&(%.#*%$
*%

Fraction within ~200 of mean = 0.957147

E6???A F C
1'3&$$"4
)*(+*%$)$/  



:);
(%"*/%($%#,(""/$*-$*-%
,"+)
$)+(,-(*,"+)%$*.9.)"
*-$#$#+#$#.#+#,"+%*,("
(+$(+(,*-$*-%&%$*)4)&(%"*/%
.#&"""$-*$**($

E6???A F D
#& #&! '(&)(#"

def gaussian(x, mu, sigma):


factor1 = (1.0/(sigma*((2*pylab.pi)**0.5)))
factor2 = pylab.e**-(((x-mu)**2)/(2*sigma**2))
return factor1*factor2

 
xVals, yVals = [], [] 
mu, sigma = 0, 1        
 
x = -4
while x <= 4:
xVals.append(x)
yVals.append(gaussian(x, mu, sigma))
x += 0.05
pylab.plot(xVals, yVals)
pylab.title('Normal Distribution, mu = ' + str(mu)\
+ ', sigma = ' + str(sigma))

E6???A F E
)($)(

(,"+)%$/9.)
&(%"*)3
/($)*)6

664(,*,%
+#+"*,
)*(+*%$
+$*%$6

$-+)
$*(*%$*%
$*(&(*

E6???A F F
&''#"
/ "((/%$*$)#/+)+"#*#*"
+$*%$)+)/)$*)*)$$$()
)&/6$*(*6'+ )+&*%%+((+#$*)
N +$*%$%(#*%*%$*(*
N $+#((&()$*$*"%-("#*%*$*(*%$4
N $+#((&()$*$*+&&("#*%*$*(*%$4
$
N $%&*%$"*+&")+&&"/$,"+)%(""(+#$*)4
.&**()*4%*+$*%$*%$*(*
)&/6$*(*6'+ (*+($)*+&"
N &&(%.#*%$*%()+"*
N )*#*%)%"+*((%(

E6???A F G
"(!$& ) 
import scipy.integrate

def gaussian(x, mu, sigma)



def checkEmpirical(numTrials):
for t in range(numTrials):
mu = random.randint(-10, 10)
sigma = random.randint(1, 10)
print('For mu =', mu, 'and sigma =', sigma)
for numStd in (1, 1.96, 3):
area = scipy.integrate.quad(gaussian,
mu-numStd*sigma,
mu+numStd*sigma,
(mu, sigma))[0]
print(' Fraction within', numStd,
'std =', round(area, 4))

E6???A F H
') ('

%(#+LH$)#LE
(*%$-*$@)* L?6EGAF
(*%$-*$@6HE)* L?6HD
(*%$-*$B)* L?6HHFB
%(#+L9E$)#LD
(*%$-*$@)* L?6EGAF
(*%$-*$@6HE)* L?6HD
(*%$-*$B)* L?6HHFB
%(#+LA$)#LE
(*%$-*$@)* L?6EGAF
(*%$-*$@6HE)* L?6HD
(*%$-*$B)* L?6HHFB

E6???A F @?
*&,#,
' #&! '(&)(#"'
+("%*2
 #*#*"
&(%&(*)

E6???A F @@
)( #( '(&)(#"& #&!
#&("-%(!)%($%(#")*(+*%$)
+*(*%+*%#)%)&$)%(%+"**-"
$%(#""/)*(+*3
 %4*/(+$%(#"/)*(+*
N %+*%#)'+""/&(%"
%4-/%)*#&("(+"-%(!(3

E6???A F @A
,(!$& ) #&-
+)-(()%$$
$%*%+*)$")&$4
+*%+**#$%
)*%)&$)
$*$*(""#*
*%(#&&")

E6???A F @B
"(& 
!(#&!3
4
,$)+$*"/"()#&"5
@;#$)%*)#&")$)*%)#&"):*
)#&"#$);-""&&(%.#*"/$%(#""/
)*(+*4
A;)$%(#")*(+*%$-"",#$"%)*%
*#$%*&%&+"*%$4$
B;,($%*)#&"#$)-"""%)*%
*,($%*&%&+"*%$,/*)#&"
)06

E6???A F @C
"
#&#"(")#)'
def plotMeans(numDice, numRolls, numBins, legend, color, style):
means = []
for i in range(numRolls//numDice):
vals = 0
for j in range(numDice):
vals += 5*random.random()
means.append(vals/float(numDice))
pylab.hist(means, numBins, color = color, label = legend,
weights = pylab.array(len(means)*[1])/len(means),
hatch = style)
return getMeanAndStd(means)

mean, std = plotMeans(1, 1000000, 19, '1 die', 'b', '*')


print('Mean of rolling 1 die =', str(mean) + ',', 'Std =', std)
mean, std = plotMeans(50, 1000000, 19, 'Mean of 50 dice', 'r', '//')
print('Mean of rolling 50 dice =', str(mean) + ',', 'Std =', std)
pylab.title('Rolling Continuous Dice')
pylab.xlabel('Value')
pylab.ylabel('Probability')
pylab.legend()

E6???A F @D
)($)(
$%(%""$@LA6CHFDHDFDDAG4* L@6CCBH?CDEBB
$%(%""$D?LA6CHHGD?D@FHG4* L?6A?CGGFAFCECD

E6???A F @E
&, (#&#) ((
numTrials = 1000000
numSpins = 200
game = FairRoulette()

means = []
for i in range(numTrials):
means.append(findPocketReturn(game, 1, numSpins,
False)[0])

pylab.hist(means, bins = 19,


weights = [1/len(means)]*len(means))
pylab.xlabel('Mean Return')
pylab.ylabel('Probability')
pylab.title('Expected Return Betting a Pocket 200 Times')

E6???A F @F
(("#("&#) ((

E6???A F @G
#&

*%)$7*#**(-**)&%*)*(+*%$%
,"+)&&$)*%

-(*(/$*%)*#**#$%&%&+"*%$
+)$)+$*"/"()#&")
 ""%-)+)*%+)*#&("(+"-$
%#&+*$%$$$*(,")

E6???A F @H
Pi

circumference 2
=Π area = Π * radius
diameter

6.0002 LECTURE 7 21
" $,&)'

C:G8H; LB6@E
Image of the Rhind Papyrus is in the public domain. Source: Wikimedia Commons.

E6???A F AA
57766&'
(&

“And he made a molten sea, ten cubits


from the one brim to the other: it was
round all about, and his height was five
cubits: and a line of thirty cubits did
compass it round about.”
—1 Kings 7.23

E6???A F AB
5966&'
(&3&!'4

AAB8F@M&MAA8F

E6???A F AC
58666&'
(&3)#"2
$ 4

) LA<ALC
 L1(A L1


  
   



     

    


  

  
  

  

  
  

  
  

  

E6???A F AD
5866&'
(&

**&)588---6/%+*+6%#8-*3,L% E
G

E6???A F AE
&,"##

E6???A F AF
!) (")#"2
$  (#

def throwNeedles(numNeedles):
inCircle = 0
for Needles in range(1, numNeedles + 1, 1):
x = random.random()
y = random.random()
if (x*x + y*y)**0.5 <= 1.0:
inCircle += 1
return 4*(inCircle/float(numNeedles))

E6???A F AG
!) (")#"2
$  (#.#"(0

def getEst(numNeedles, numTrials):


estimates = []
for t in range(numTrials):
piGuess = throwNeedles(numNeedles)
estimates.append(piGuess)
sDev = stdDev(estimates)
curEst = sum(estimates)/len(estimates)
print('Est. = ' + str(curEst) +\
', Std. dev. = ' + str(round(sDev, 6))\
+ ', Needles = ' + str(numNeedles))
return (curEst, sDev)

E6???A F AH
!) (")#"2
$  (#.#"(0

def estPi(precision, numTrials):


numNeedles = 1000
sDev = precision
while sDev >= precision/2:
curEst, sDev = getEst(numNeedles,
numTrials)
numNeedles *= 2
return curEst

estPi(0.005, 100)

E6???A F B?
)($)(

Est. = 3.1484400000000012, Std. dev. = 0.047886, Needles = 1000


Est. = 3.1391799999999987, Std. dev. = 0.035495, Needles = 2000
Est. = 3.1410799999999997, Std. dev. = 0.02713, Needles = 4000
Est. = 3.141435, Std. dev. = 0.016805, Needles = 8000
Est. = 3.141355, Std. dev. = 0.0137, Needles = 16000
Est. = 3.1413137500000006, Std. dev. = 0.008476, Needles = 32000
Est. = 3.141171874999999, Std. dev. = 0.007028, Needles = 64000
Est. = 3.1415896874999993, Std. dev. = 0.004035, Needles = 128000
Est. = 3.1417414062499995, Std. dev. = 0.003536, Needles = 256000
Est. = 3.14155671875, Std. dev. = 0.002101, Needles = 512000

E6???A F B@
"(' #(##"#)
 %*)+$**%&(%+%%$)-(
 *%,()%$*%",***)"%)*%(*

$*))4)#"")*$(,*%$#&")**-
("%)*%**(+,"+%

*3

E6???A F BA
'(#&&((#((
HDK%**#-(+$*))#+"*%$4--""
)*#****,"+%&)*-$
B6@BFCBGFDGFD$B6@CDEFCEFGFD3
*&(%"*/%?6HD**+","+% )
*-$B6@BFCBGFDGFD$B6@CDEFCEFGFD3
%* ( *+""/ %((*
+* %$"/ %$ %*) )**#$* $  $(( (%#
%+( )#+"*%$
 


  

E6???A F BB
"(&#))

def throwNeedles(numNeedles):
inCircle = 0
for Needles in range(1, numNeedles + 1, 1):
x = random.random()
y = random.random()
if (x*x + y*y)**0.5 <= 1.0:
inCircle += 1
return 2*(inCircle/float(numNeedles))

E6???A F BC
"& ,') "%)
%)*#**(%)%#(%$4
N !$$"%)$(%$44)+***(%))/
*%"+"*$")%#&"*"/-*$
N !)*%($%#&%$*)**"-*$
N **(*%$%*&%$*)**""-*$
N +"*&"/*(%/
/*%)*#*$*(")

E6???A F BD
"3+4

E6???A F BE
"#!#"('

E6???A F BF
MIT OpenCourseWare
https://ocw.mit.edu

6.0002 Introduction to Computational Thinking and Data Science


Fall 2016

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

You might also like