In [2]:
%pylab inline
%autocall
from matplotlib import mpl,pyplot,colors
import numpy as np
import itertools
The next cell imports the or and and operators. We then give these more convenient names for when we call them in the future.
In [3]:
import operator
OR = operator.__or__
AND =operator.__and__
In [4]:
# list
class Prob(object):
def __init__(self, vals):
self.vals = vals
def values(self):
return self.vals
def size(self):
return len(self.vals)
@staticmethod
def function(string):
def f(x):
return eval (string)
return f
def combs(a,b):
"""returns all combinations of two probability objects"""
return list(itertools.product(*[a.values(),b.values()]))
class Dice(Prob):
def __init__(self, sides = 6):
Prob.__init__(self, arange(1,sides+1))
# check if a value is odd
@staticmethod
def odd():
def isOdd(n):
return n % 2 == 1
return isOdd
# check if a value is even
@staticmethod
def even():
def isEven(n):
return n % 2 == 0
return isEven
class Coin(Prob):
vals = ['Heads', 'Tails']
def __init__(self):
Prob.__init__(self,Coin.vals)
# check if a coin came up 'Heads'
@staticmethod
def heads():
def isHead(side):
return side == 'Heads'
return isHead
# check if a coin came up 'Tails'
@staticmethod
def tails():
def isTail(side):
return side == 'Tails'
return isTail
def evaluate(t1,t2, op):
# get all combinations of the probability objects
each = combs(t1[0], t2[0])
# turns true or false into +/-5 for Colour Map
def g(x):
if x:
return 5
else:
return -5
# run the rules on the elements of each and use op to combine them
bools = [op(t1[1](element[0]) , t2[1](element[1])) for element in each]
# use g to convert to number
# convert list to numpy array so it can be reshaped
zvals = np.array([g(boolean) for boolean in bools])
return zvals.reshape(t1[0].size(), t2[0].size())
In [5]:
def show(a,b,op):
zvals = evaluate(a,b,op)
plot(a[0],b[0], zvals)
def compare(d1, d2, rule):
c = combs(d1, d2)
print [(x,y, eval(rule)) for x,y in c]
def g(x):
if x:
return 5
else:
return -5
zvals = np.array([g(eval(rule)) for x,y in c]).reshape(d1.size(), d2.size())
plot(d1,d2, zvals)
def plot(p2,p1,zvals):
fig, ax = pyplot.subplots()
# make a color map of fixed colors
cmap = mpl.colors.ListedColormap(['blue','red'])
bounds=[2,6,2,6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# tell imshow about color map so that only set colors are used
img = pyplot.pcolormesh(zvals, cmap = cmap,norm=norm)
#pyplot.grid(b=True, which='major', color='black', linestyle='-')
m = max(p1.size(), p2.size())
pyplot.axis([0,m,0,m])
ax.tick_params(axis='x',which='minor',bottom='off')
ax.tick_params(axis='y',which='minor',bottom='off')
px = p1.size()
xlocations = np.array(range(px))
xminorlocations = np.array(range(px))+0.5
py = p2.size()
ylocations = np.array(range(py))
yminorlocations = np.array(range(py))+0.5
pyplot.xticks(xminorlocations, p1.values(), rotation=0, size=15)
ax.set_xticks(xlocations, minor=True)
pyplot.yticks(yminorlocations, p2.values(), rotation=0, size=15)
ax.set_yticks(ylocations, minor=True)
grid(True, which='minor', linestyle='-')
pyplot.show()
Creating Our Probability Objects
A coin or dice can be created by calling Coin() or Dice() respectively. A different-sided dice can be created by pass ing a number to Dice(). Here we will create a 12-sided dice.
In [6]:
d = Dice()
d12 = Dice(12)
c = Coin()