-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal.py
231 lines (198 loc) · 7.11 KB
/
Final.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 3 14:37:18 2018
@author: Haoyuan
"""
#%% Problem 2
import random, pylab
xVals = []
yVals = []
wVals = []
for i in range(1000):
xVals.append(random.random())
yVals.append(random.random())
wVals.append(random.random())
xVals = pylab.array(xVals)
yVals = pylab.array(yVals)
wVals = pylab.array(wVals)
xVals = xVals + xVals
zVals = xVals + yVals
tVals = xVals + yVals + wVals
#pylab.plot(sorted(xVals),yVals)
#pylab.plot(zVals)
pylab.hist(tVals,10,lab)
#%% Problem 3
def drawing_without_replacement_sim(numTrials):
'''
Runs numTrials trials of a Monte Carlo simulation
of drawing 3 balls out of a bucket containing
4 red and 4 green balls. Balls are not replaced once
drawn. Returns a float - the fraction of times 3
balls of the same color were drawn in the first 3 draws.
'''
valid=0
for i in range(numTrials):
l=[0,0,0,0,1,1,1,1]
for i in range(3):
draw=random.choice(l)
l.remove(draw)
if sum(l)==1 or sum(l) == 4:
valid+=1
return valid/numTrials
drawing_without_replacement_sim(1000)
#%% Problem 4
import random, pylab
from itertools import groupby
# You are given this function
def getMeanAndStd(X):
mean = sum(X)/float(len(X))
tot = 0.0
for x in X:
tot += (x - mean)**2
std = (tot/len(X))**0.5
return mean, std
# You are given this class
class Die(object):
def __init__(self, valList):
""" valList is not empty """
self.possibleVals = valList[:]
def roll(self):
return random.choice(self.possibleVals)
# Implement this -- Coding Part 1 of 2
def makeHistogram(values, numBins, xLabel, yLabel, title=None):
"""
- values, a sequence of numbers
- numBins, a positive int
- xLabel, yLabel, title, are strings
- Produces a histogram of values with numBins bins and the indicated labels
for the x and y axis
- If title is provided by caller, puts that title on the figure and otherwise
does not title the figure
"""
pylab.hist(values,numBins)
if title!=None:
pylab.title(title)
pylab.xlabel(xLabel)
pylab.ylabel(yLabel)
pylab.show()
makeHistogram([1,2], 4, "Aaa", "Bbb")
# Implement this -- Coding Part 2 of 2
def getAverage(die, numRolls, numTrials):
"""
- die, a Die
- numRolls, numTrials, are positive ints
- Calculates the expected mean value of the longest run of a number
over numTrials runs of numRolls rolls.
- Calls makeHistogram to produce a histogram of the longest runs for all
the trials. There should be 10 bins in the histogram
- Choose appropriate labels for the x and y axes.
- Returns the mean calculated
"""
longruns=[]
for i in range(numTrials):
rollresult=[]
for j in range(numRolls):
rollresult.append(die.roll())
longruns.append(max(len(list(g)) for k, g in groupby(rollresult)))
makeHistogram(longruns,10,'Longest Run','Times Occur')
return round(getMeanAndStd(longruns)[0],3)
# One test case
print(getAverage(Die([1,2,3,4,5,6,6,6,7]), 500, 10000))
#%% Problem 6 important!!!!!!!!!!!!!!!
import numpy as np
def find_combination(choices, total):
"""
choices: a non-empty list of ints
total: a positive int
Returns result, a numpy.array of length len(choices)
such that
* each element of result is 0 or 1
* sum(result*choices) == total
* sum(result) is as small as possible
In case of ties, returns any result that works.
If there is no result that gives the exact total,
pick the one that gives sum(result*choices) closest
to total without going over.
"""
subset=[]
for i in range(2**len(choices)):
subset.append(np.array(list('0'*(len(choices)-len(bin(i)[2:]))+bin(i)[2:]),dtype=int))
print (subset[1])
for array in subset:
if np.dot(choices,array)==total:
solution.append(array)
elif len(solution)==0:
choice=[1,2,3,4]
find_combination(choice,4)
#%% Problem 8
import random
import pylab
# Global Variables
MAXRABBITPOP = 1000
CURRENTRABBITPOP = 500
CURRENTFOXPOP = 30
def rabbitGrowth():
"""
rabbitGrowth is called once at the beginning of each time step.
It makes use of the global variables: CURRENTRABBITPOP and MAXRABBITPOP.
The global variable CURRENTRABBITPOP is modified by this procedure.
For each rabbit, based on the probabilities in the problem set write-up,
a new rabbit may be born.
Nothing is returned.
"""
# you need this line for modifying global variables
global CURRENTRABBITPOP
copyrabbitpop=CURRENTRABBITPOP
if CURRENTRABBITPOP>10:
for i in range(copyrabbitpop):
if random.random()<=float(1-CURRENTRABBITPOP/MAXRABBITPOP):
CURRENTRABBITPOP+=1
def foxGrowth():
"""
foxGrowth is called once at the end of each time step.
It makes use of the global variables: CURRENTFOXPOP and CURRENTRABBITPOP,
and both may be modified by this procedure.
Each fox, based on the probabilities in the problem statement, may eat
one rabbit (but only if there are more than 10 rabbits).
If it eats a rabbit, then with a 1/3 prob it gives birth to a new fox.
If it does not eat a rabbit, then with a 1/10 prob it dies.
Nothing is returned.
"""
# you need these lines for modifying global variables
global CURRENTRABBITPOP
global CURRENTFOXPOP
copyfoxpop=CURRENTFOXPOP
if CURRENTFOXPOP>10:
if CURRENTRABBITPOP>10:
for i in range (copyfoxpop):
if random.random()<=CURRENTRABBITPOP/MAXRABBITPOP:
CURRENTRABBITPOP-=1
if random.random()<=1/3.0:
CURRENTFOXPOP+=1
else:
if random.random()<=9/10.0:
CURRENTFOXPOP-=1
def runSimulation(numSteps):
"""
Runs the simulation for `numSteps` time steps.
Returns a tuple of two lists: (rabbit_populations, fox_populations)
where rabbit_populations is a record of the rabbit population at the
END of each time step, and fox_populations is a record of the fox population
at the END of each time step.
Both lists should be `numSteps` items long.
"""
rabbit,fox=[],[]
for i in range(numSteps):
rabbitGrowth()
foxGrowth()
rabbit.append(CURRENTRABBITPOP)
fox.append(CURRENTFOXPOP)
pylab.plot(rabbit)
pylab.plot(fox)
coeffr = pylab.polyfit(range(len(rabbit)), rabbit, 2)
pylab.plot(pylab.polyval(coeffr, range(len(rabbit))),label='rabbit')
coefff = pylab.polyfit(range(len(fox)), fox, 2)
pylab.plot(pylab.polyval(coefff, range(len(fox))),label='fox')
pylab.legend()
return rabbit,fox
runSimulation(200)