Skip to content

Commit f746622

Browse files
committed
rabbits population simulation
1 parent 12bc00b commit f746622

File tree

8 files changed

+325
-1
lines changed

8 files changed

+325
-1
lines changed

ml/FuelConsumption.csv

+1
Large diffs are not rendered by default.

ml/r2.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import matplotlib.pyplot as plt
2+
import pandas as pd
3+
import pylab as pl
4+
import numpy as np
5+
6+
df = pd.read_csv("FuelConsumption.csv")
7+
8+
# take a look at the dataset
9+
# print(df.head())
10+
11+
# # summarize the data
12+
# print(df.describe())
13+
14+
cdf = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
15+
# cdf.head(9)
16+
17+
viz = cdf[['CYLINDERS','ENGINESIZE','CO2EMISSIONS','FUELCONSUMPTION_COMB']]
18+
# viz.hist()
19+
# plt.show()
20+
21+
# plt.scatter(cdf.CYLINDERS, cdf.CO2EMISSIONS, color='blue')
22+
# plt.xlabel("Cylinders")
23+
# plt.ylabel("Emission")
24+
# plt.show()
25+
26+
msk = np.random.rand(len(df)) < 0.8
27+
train = cdf[msk]
28+
test = cdf[~msk]
29+
30+
from sklearn import linear_model
31+
regr = linear_model.LinearRegression()
32+
train_x = np.asanyarray(train[['ENGINESIZE']])
33+
train_y = np.asanyarray(train[['CO2EMISSIONS']])
34+
regr.fit (train_x, train_y)
35+
# The coefficients
36+
print ('Coefficients: ', regr.coef_)
37+
print ('Intercept: ',regr.intercept_)
38+
39+
# plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS, color='blue')
40+
# plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r')
41+
# plt.xlabel("Engine size")
42+
# plt.ylabel("Emission")
43+
# plt.show()
44+
45+
from sklearn.metrics import r2_score
46+
47+
test_x = np.asanyarray(test[['ENGINESIZE']])
48+
test_y = np.asanyarray(test[['CO2EMISSIONS']])
49+
test_y_ = regr.predict(test_x)
50+
51+
print("Mean absolute error: %.2f" % np.mean(np.absolute(test_y_ - test_y)))
52+
print("Residual sum of squares (MSE): %.2f" % np.mean((test_y_ - test_y) ** 2))
53+
print("R2-score: %.2f" % r2_score(test_y_ , test_y) )
54+
55+

optimization/find_combination.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
3+
def find_combination(choices, total):
4+
"""
5+
choices: a non-empty list of ints
6+
total: a positive int
7+
8+
Returns result, a numpy.array of length len(choices)
9+
such that
10+
* each element of result is 0 or 1
11+
* sum(result*choices) == total
12+
* sum(result) is as small as possible
13+
In case of ties, returns any result that works.
14+
If there is no result that gives the exact total,
15+
pick the one that gives sum(result*choices) closest
16+
to total without going over.
17+
"""
18+
numbered = {}
19+
for i in range(len(choices)):
20+
numbered[i] = [choices[i], 0]
21+
choices = sorted(choices)
22+
temp = 0
23+
taken = []
24+
for i in range(len(choices)-1, -1, -1):
25+
if temp + choices[i] <= total:
26+
temp += choices[i]
27+
taken.append(choices[i])
28+
positions = []
29+
for t in taken:
30+
for n in numbered.keys():
31+
if numbered[n][0] == t and numbered[n][1] == 0:
32+
positions.append(n)
33+
numbered[n][1] = 1
34+
break
35+
result = []
36+
for i in range(len(choices)):
37+
if i in positions:
38+
result.append(1)
39+
else:
40+
result.append(0)
41+
42+
return np.array(result)
43+
44+
results = find_combination([10, 10, 11, 11, 11], 20)
45+
print(results)

stats/balls.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,22 @@ def noReplacementSimulation(numTrials):
1616
same_color += 1
1717
return same_color/numTrials
1818

19-
print(noReplacementSimulation(11142))
19+
def drawing_without_replacement_sim(numTrials):
20+
'''
21+
Runs numTrials trials of a Monte Carlo simulation
22+
of drawing 3 balls out of a bucket containing
23+
4 red and 4 green balls. Balls are not replaced once
24+
drawn. Returns a float - the fraction of times 3
25+
balls of the same color were drawn in the first 3 draws.
26+
'''
27+
balls = [1, 0, 1, 0, 1, 0, 1, 0]
28+
same_color = 0
29+
for trial in range(numTrials):
30+
choice = sum(random.sample(balls, 3))
31+
if choice == 3 or choice == 0:
32+
same_color += 1
33+
return same_color/numTrials
34+
35+
print("Fraction from 6 balls: ", noReplacementSimulation(11142))
36+
print("Fraction from 8 balls: ", drawing_without_replacement_sim(11142))
2037

stats/d2.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pylab as plt
2+
import numpy as np
3+
4+
a = np.random.normal(10, 3, 1000)
5+
b = np.random.normal(10, 5, 1000)
6+
plt.figure('a')
7+
plt.hist(a, 10)
8+
9+
plt.figure('b')
10+
plt.hist(b, 10)
11+
plt.show()

stats/d3.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import random, pylab
2+
xVals = []
3+
yVals = []
4+
wVals = []
5+
for i in range(1000):
6+
xVals.append(random.random())
7+
yVals.append(random.random())
8+
wVals.append(random.random())
9+
xVals = pylab.array(xVals)
10+
yVals = pylab.array(yVals)
11+
wVals = pylab.array(wVals)
12+
xVals = xVals + xVals
13+
zVals = xVals + yVals
14+
tVals = xVals + yVals + wVals
15+
16+
pylab.plot(sorted(xVals), sorted(yVals))
17+
pylab.show()

stats/die.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import random, pylab
2+
import numpy as np
3+
4+
# You are given this function
5+
def getMeanAndStd(X):
6+
mean = sum(X)/float(len(X))
7+
tot = 0.0
8+
for x in X:
9+
tot += (x - mean)**2
10+
std = (tot/len(X))**0.5
11+
return mean, std
12+
13+
# You are given this class
14+
class Die(object):
15+
def __init__(self, valList):
16+
""" valList is not empty """
17+
self.possibleVals = valList[:]
18+
def roll(self):
19+
return random.choice(self.possibleVals)
20+
21+
# Implement this -- Coding Part 1 of 2
22+
def makeHistogram(values, numBins, xLabel, yLabel, title=None):
23+
"""
24+
- values, a sequence of numbers
25+
- numBins, a positive int
26+
- xLabel, yLabel, title, are strings
27+
- Produces a histogram of values with numBins bins and the indicated labels
28+
for the x and y axis
29+
- If title is provided by caller, puts that title on the figure and otherwise
30+
does not title the figure
31+
"""
32+
if title != None:
33+
pylab.title(title)
34+
35+
pylab.xlabel(xLabel)
36+
pylab.ylabel(yLabel)
37+
pylab.hist(values, numBins)
38+
pylab.show()
39+
40+
41+
# Implement this -- Coding Part 2 of 2
42+
def getAverage(die, numRolls, numTrials):
43+
"""
44+
- die, a Die
45+
- numRolls, numTrials, are positive ints
46+
- Calculates the expected mean value of the longest run of a number
47+
over numTrials runs of numRolls rolls.
48+
- Calls makeHistogram to produce a histogram of the longest runs for all
49+
the trials. There should be 10 bins in the histogram
50+
- Choose appropriate labels for the x and y axes.
51+
- Returns the mean calculated
52+
"""
53+
values = []
54+
longest = []
55+
for trial in range(numTrials):
56+
for roll in range(numRolls):
57+
values.append(die.roll())
58+
temp = values[0]
59+
count = 1
60+
maxCount = 1
61+
for i in range(len(values)-1):
62+
if temp == values[i+1]:
63+
count += 1
64+
else:
65+
temp = values[i+1]
66+
count = 1
67+
68+
if count > maxCount:
69+
maxCount = count
70+
longest.append(maxCount)
71+
values = []
72+
73+
makeHistogram(longest, 10, "Mean of runs", "Longest runs")
74+
75+
return round(getMeanAndStd(longest)[0], 3)
76+
77+
78+
79+
80+
# a = np.random.normal(10, 5, 100)
81+
# makeHistogram(a, 10, "X", "Y", "Graphik")
82+
83+
84+
# One test case
85+
print(getAverage(Die([1,2,3,4,5,6,6,6,7]), 500, 10000))

stats/rabbits.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import random
2+
import pylab
3+
4+
# Global Variables
5+
MAXRABBITPOP = 1000
6+
CURRENTRABBITPOP = 50
7+
CURRENTFOXPOP = 300
8+
9+
def rabbitGrowth():
10+
"""
11+
rabbitGrowth is called once at the beginning of each time step.
12+
13+
It makes use of the global variables: CURRENTRABBITPOP and MAXRABBITPOP.
14+
15+
The global variable CURRENTRABBITPOP is modified by this procedure.
16+
17+
For each rabbit, based on the probabilities in the problem set write-up,
18+
a new rabbit may be born.
19+
Nothing is returned.
20+
"""
21+
# you need this line for modifying global variables
22+
global CURRENTRABBITPOP
23+
24+
if CURRENTRABBITPOP >= 10:
25+
for rabbit in range(CURRENTRABBITPOP):
26+
if (1.0 - (CURRENTRABBITPOP / MAXRABBITPOP)) > random.random():
27+
CURRENTRABBITPOP += 1
28+
29+
def foxGrowth():
30+
"""
31+
foxGrowth is called once at the end of each time step.
32+
33+
It makes use of the global variables: CURRENTFOXPOP and CURRENTRABBITPOP,
34+
and both may be modified by this procedure.
35+
36+
Each fox, based on the probabilities in the problem statement, may eat
37+
one rabbit (but only if there are more than 10 rabbits).
38+
39+
If it eats a rabbit, then with a 1/3 prob it gives birth to a new fox.
40+
41+
If it does not eat a rabbit, then with a 1/10 prob it dies.
42+
43+
Nothing is returned.
44+
"""
45+
# you need these lines for modifying global variables
46+
global CURRENTRABBITPOP
47+
global CURRENTFOXPOP
48+
49+
for fox in range(CURRENTFOXPOP):
50+
if CURRENTRABBITPOP > 10:
51+
if (CURRENTRABBITPOP / MAXRABBITPOP) > random.random():
52+
CURRENTRABBITPOP -= 1
53+
if (1/3.0) >= random.random():
54+
CURRENTFOXPOP += 1
55+
else:
56+
if 0.1 >= random.random() and CURRENTFOXPOP > 10:
57+
CURRENTFOXPOP -= 1
58+
59+
def runSimulation(numSteps):
60+
"""
61+
Runs the simulation for `numSteps` time steps.
62+
63+
Returns a tuple of two lists: (rabbit_populations, fox_populations)
64+
where rabbit_populations is a record of the rabbit population at the
65+
END of each time step, and fox_populations is a record of the fox population
66+
at the END of each time step.
67+
68+
Both lists should be `numSteps` items long.
69+
"""
70+
rabbit_populations = []
71+
fox_populations = []
72+
73+
for step in range(numSteps):
74+
rabbitGrowth()
75+
foxGrowth()
76+
rabbit_populations.append(CURRENTRABBITPOP)
77+
fox_populations.append(CURRENTFOXPOP)
78+
79+
rabbit_coeff = pylab.polyfit(range(len(rabbit_populations)), rabbit_populations, 2)
80+
fox_coeff = pylab.polyfit(range(len(fox_populations)), fox_populations, 2)
81+
82+
pylab.plot(pylab.polyval(rabbit_coeff, range(len(rabbit_populations))))
83+
pylab.plot(pylab.polyval(fox_coeff, range(len(fox_populations))))
84+
85+
pylab.plot(rabbit_populations, 'g')
86+
pylab.plot(fox_populations, 'r')
87+
pylab.show()
88+
89+
return (rabbit_populations, fox_populations)
90+
91+
92+
93+
runSimulation(200)

0 commit comments

Comments
 (0)