Python Data Structure Recommendations for a 2D Grid - python

I am looking to implement an Ant Colony Optimization algorithm in Python, though am new to both Python and Object Oriented Programming so the learning curve has been rather steep. At this point, I am stuck as to how to address the following situation:
As ants walk around a 2D grid, they will encounter obstacles, pheromone deposits by other ants, food, etc. What data structure do I use to represent this 2D world and the aforementioned properties of each cell?
I had tried a 2D array, thinking that array[x-coord][y-coord] could point to a {} (dictionary) with the appropriate properties (Obstacle: 'Yes / 'No', Pheromone Level: X %, etc.). Unfortunately, though NumPy lets me create a 2D array, I cannot assign dictionary objects to the various coordinates.
from numpy import *
myArray = array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])
myArray[2][2]={}
Returns:
Traceback (most recent call last):
File "/Users/amormachine/Desktop/PythonTest.py", line 7, in <module>
myArray[2][2]={}
TypeError: long() argument must be a string or a number, not 'dict'
[Finished in 0.6s with exit code 1]
I am not committed to either dictionaries or this paradigm for implementing this project and would certainly appreciate the wisdom of the group.

sure you can, you just cant if your dtype is int ... so make your array with objects and you can use objects...
In [43]: a = [[{},{},{}],[{},{},{}]]
In [44]: a = numpy.array(a)
In [45]: a[1][1] = {'hello':'world','something':5}
In [46]: a
Out[46]:
array([[{}, {}, {}],
[{}, {'hello': 'world', 'something': 5}, {}]], dtype=object)
although not sure whay you will gain using numpy with objects, you may be better off just leaving it as a list of lists

In plain Python I would be going for the list-of-dicts approach but with NumPy I find it more natural to work with separate arrays for different attributes rather than trying to keep things in one structure.
import numpy as np
grid_shape = (120,80)
# example of random initialization with this grid shape
pheremone_level = np.random.rand(*grid_shape)
obstacle = np.random.rand(*grid_shape) > 0.8
As #bitwise says it entirely depends on what operations you want to perform. Generally the "correct" way in NumPy will be much closer to how you would write it in Matlab than non-NumPy Python. Unfortunately I'm not familiar with how Ant Colony Optimization works so I can't say what's more suitable.

I was looking for something related to structured 2D grids and google led me to this page.
Although my solution is not entirely related to grids for what has been asked in the question, and I didn't want to repeat a question for 'structured 2D grid' data structure, I'm posting my solution here. I hope it will be useful to the audience searching for 2D structured grid and redirected here by search engines
Note: the method returns only the cell vertices and the vertex connectivity of each cells. Other quantities like cell volume, cell centroid, circumcircle, incircle, etc as needed for the application can be easily generated by adding additional routines
import numpy as np
import matplotlib.pyplot as plt
def create_structured_grid(corner1=None, corner2=None, nx=5, ny=5, plt_=True, annotate=True):
"""
creates a structured grid of rectangular lattice
input:
------
corner1 : [x_start, y_start]
corner2 : [x_end, y_end]
nx : numpts in x
ny : numpts in y
plt_ : boolean whether to plot or not
annotate: whether to annotate the grid points or not
output:
-------
vertex_array : numpy.array((numpts, dim),dtype=float) of vertices
connectivity : numpy.array((num_cells, 2**dim), dtyp=int) of
vertex connectivity for each cell
plots : additionally plots if boolean values are true
"""
#corner1 = np.array([0.0, 0.0])
#corner2 = np.array([1.0, 1.0])
dim = len(corner1) #currently only for 2D,
x_pts = np.linspace(corner1[0], corner2[0], nx)
y_pts = np.linspace(corner1[1], corner2[1], ny)
Xv, Yv = np.meshgrid(x_pts, y_pts)
numpts = nx*ny
vertex_array = np.zeros((numpts, 2), dtype=float)
vertex_array[:,0] = np.reshape(Xv, numpts)
vertex_array[:,1] = np.reshape(Yv, numpts)
num_cells = int(nx-1)*(ny-1)
connectivity = np.zeros((num_cells, int(2**dim)), dtype=int)
rows = ny-1
cols = nx-1
for row in range(rows):
for col in range(cols):
num = nx*row + col
connectivity[cols*row + col] = [num+0, num+1, num+nx, num+nx+1]
if plt_:
X,Y = vertex_array.T
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')
plt.scatter(X,Y, marker='o', s=50, color='g', alpha=1.0)
plt.plot(Xv,Yv, linewidth=2, color='k')
plt.plot(Yv,Xv, linewidth=2, color='k')
if annotate:
for idx, cc in enumerate(vertex_array):
plt.text(cc[0], cc[1], str(idx), color='k', verticalalignment='bottom', horizontalalignment='right', fontsize='medium')
plt.show(block=False)
return vertex_array, connectivity
A call to function can be like this:
c1 = np.array([0.0, 0.0])
c2 = np.array([1.0, 1.0])
vertices, connctivity = create_structured_grid(corner1=c1, corner2=c2, nx=4, ny=4)
vertices = array([[ 0. , 0. ],
[ 0.33333333, 0. ],
[ 0.66666667, 0. ],
[ 1. , 0. ],
[ 0. , 0.33333333],
[ 0.33333333, 0.33333333],
[ 0.66666667, 0.33333333],
[ 1. , 0.33333333],
[ 0. , 0.66666667],
[ 0.33333333, 0.66666667],
[ 0.66666667, 0.66666667],
[ 1. , 0.66666667],
[ 0. , 1. ],
[ 0.33333333, 1. ],
[ 0.66666667, 1. ],
[ 1. , 1. ]])
connectivity = array([[ 0, 1, 5, 6],
[ 1, 2, 6, 7],
[ 2, 3, 7, 8],
[ 4, 5, 9, 10],
[ 5, 6, 10, 11],
[ 6, 7, 11, 12],
[ 8, 9, 13, 14],
[ 9, 10, 14, 15],
[10, 11, 15, 16]])

Related

How to plot a (22, 3, 2) matrix list of lists as a scatter plot?

I am trying to plot the below sensitivity matrix into a plot that consists of each list as sub-plot in it. Preferably as a function and look like this one in seaborn tutorial.
I tried a similar way to plot the list of list but it only give me a single plot without subplots:
def scatter_plot(sen1_obs):
x = []
y = []
for i in sen1_obs:
x.append(i[0])
y.append(i[1])
plt.scatter(x,y)
plt.show()
And execute:
scatter_plot(sen1_obs)
My list look like this :
sen1_obs
Out[2]:
[array([[ 0.00040725, 0.00011072],
[ 0.0008145 , 0.00022144],
[-0.00040725, -0.00011072]]),
array([[ 8.32091781e-04, -8.06469105e-05],
[ 1.66418356e-03, -1.61293821e-04],
[-8.32091781e-04, 8.06469105e-05]]),
array([[ 0.00121915, 0.00033146],
[ 0.00243829, 0.00066291],
[-0.00121915, -0.00033146]]),
array([[ 0.00252797, -0.00024503],
[ 0.00505593, -0.00049006],
[-0.00252797, 0.00024503]]),
array([[ 0.00202743, 0.00055121],
[ 0.00405486, 0.00110242],
[-0.00202743, -0.00055121]]),
array([[ 0.00430218, -0.00041706],
[ 0.00860436, -0.00083411],
[-0.00430218, 0.00041706]]),
array([[ 3.08460052e-04, 8.38668514e-05],
[ 6.16920103e-04, 1.67733703e-04],
[-3.08460052e-04, -8.38668514e-05]]),
array([[ 0.00132295, -0.00012862],
[ 0.0026459 , -0.00025724],
[-0.00132295, 0.00012862]]),
array([[ 0.00089098, 0.00024225],
[ 0.00178196, 0.0004845 ],
[-0.00089098, -0.00024225]]),
array([[ 0.00402754, -0.00039161],
[ 0.00805507, -0.00078323],
[-0.00402754, 0.00039161]]),
array([[ 0.00149702, 0.00040703],
[ 0.00299405, 0.00081405],
[-0.00149702, -0.00040703]]),
array([[ 0.00665672, -0.00064722],
[ 0.01331343, -0.00129443],
[-0.00665672, 0.00064722]]),
array([[ 0.00879632, 0.00016573],
[ 0.01759264, 0.00033147],
[-0.00879632, -0.00016573]]),
array([[ 0.00728856, -0.00055898],
[ 0.01457712, -0.00111797],
[-0.00728856, 0.00055898]]),
array([[ 0.00913137, -0.00029702],
[ 0.01826274, -0.00059403],
[-0.00913137, 0.00029702]]),
array([[ 0.00463152, 0.00026766],
[ 0.00926303, 0.00053532],
[-0.00463152, -0.00026766]]),
array([[ 0.00315 , 0.00014372],
[ 0.0063 , 0.00028743],
[-0.00315 , -0.00014372]]),
array([[ 0.00607034, 0.00023261],
[ 0.01214067,
You can try something like this. I don't have a great way to use your data so disregard my first few lines of code that are making the data I am working with. But, essentially what you are doing is making a 5x5 grid to house your 22 subplots. You then create a list of lists for their positions in the subplot (we'll call it pos). You then iterate over all the arrays in your variable en1_obs and all the lists within your arrays. Delete the left over subplots (25-22 so 3 unused subplots) and you have your figure:
######### This is just code to make a (22,3,2) matrix to work with #########
import random
en1_obs = []
for x in range(22):
longlists = []
for y in range(3):
lists = []
for z in range(2):
lists.append(random.randint(1, 60))
longlists.append(lists)
en1_obs.append(np.array(longlists))
############################################################################
### This is the important code
plt.rcParams["figure.figsize"] = (20,10)
width = 5
height = 5
fig, ax = plt.subplots(height,width)
pos = [[x,y] for x in range(height) for y in range(width)]
count = 0
for i in en1_obs:
ax[pos[count][0],pos[count][1]].scatter(i[:, 0], i[:, 1])
count +=1
fig.delaxes(ax[4,2])
fig.delaxes(ax[4,3])
fig.delaxes(ax[4,4])
plt.show()
The first point is to transform the data, so I am transforming it by changing the dimensions. The graph is drawn in a loop procedure with that transformed array. Empty graphs are manually deleted, so please adjust them. The number of data presented does not match the number of graphs, but please adjust the number of graphs to match my code to your data.
fig, axs = plt.subplots(5, 4, figsize=(9,9))
fig.subplots_adjust(wspace=0.5,hspace=0.5)
def scatter_plot(data):
for d,ax in zip(data, axs.ravel()):
xy = d.flatten().reshape(2,3, order='F')
ax.scatter(xy[0], xy[1])
fig.delaxes(axs[4,1])
fig.delaxes(axs[4,2])
fig.delaxes(axs[4,3])
plt.show()
scatter_plot(data)

Calculate means of array with specific elements

I'm implementing the Nearest Centroid Classification algorithm and I'm kind of blocked on how to use numpy.mean in my case.
So suppose I have some spherical datasets X:
[[ 0.39151059 3.48203037]
[-0.68677876 1.45377717]
[ 2.30803493 4.19341503]
[ 0.50395297 2.87076658]
[ 0.06677012 3.23265678]
[-0.24135103 3.78044279]
[-0.05660036 2.37695381]
[ 0.74210998 -3.2654815 ]
[ 0.05815341 -2.41905942]
[ 0.72126958 -1.71081388]
[ 1.03581142 -4.09666955]
[ 0.23209714 -1.86675298]
[-0.49136284 -1.55736028]
[ 0.00654881 -2.22505305]]]
and the labeled vector Y:
[0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1.]
An example with 100 2D data points gives the following result:
The NCC algorithm consists of first calculating the class mean of each class (0 and 1: that's blue and red) and then calculating the nearest class centroid for the next data point.
This is my current function:
def mean_ncc(X,Y):
# find unique classes
m_cids = np.unique(Y) #[0. 1.]
# compute class means
mu = np.zeros((len(cids), X.shape[1])) #[[0. 0.] [0. 0.]] (in the case where Y has 2 unique points (0 and 1)
for class_idx, class_label in enumerate(cids):
mu[class_idx, :] = #problem here
return mu
So here I want an array containing the class means of '0' (blue) points and '1' (red) points:
How can I specify the number of elements of X whose mean I want to calculate?
I would like to do something like this:
for class_idx, class_label in enumerate(m_cids):
mu[class_idx, :] = np.mean(X[only the elements,that contains the same class_label], axis=0)
Is it possible or is there another way to implement this?
You could use something like this:
import numpy as np
tags = [0, 0, 1, 1, 0, 1]
values = [5, 4, 2, 5, 9, 8]
tags_np = np.array(tags)
values_np = np.array(values)
print(values_np[tags_np == 1].mean())
EDIT: You will surely need to look more into the axis parameter for the mean function:
import numpy as np
values = [[5, 4],
[5, 4],
[4, 3],
[4, 3]]
values_np = np.array(values)
tags_np = np.array([0, 0, 1, 1])
print(values_np[tags_np == 0].mean(axis=0))

Draw an ellipse on a figure and get coordinates_Python

I'm working on Python 2.7. I have to define some Areas of Interest (AoI) on a picture. Basically, I'm trying to do this drawing an ellipse (or more) on a specific part of the picture and to get the coordinates (x; y) of its contour. I want to save these coordinates on a file, in order to use them later to see whether (or not) my data are inside this area.
This is my code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse, Circle
from matplotlib.path import Path
# Get an example image
img = imread('sposa.png')
# Create a figure. Equal aspect so circles look circular
fig,ax = plt.subplots(1)
ax.set_aspect('equal')
# Show the image
ax.imshow(img)
ax.set_xlim(0,1600)
ax.set_ylim(0,1200)
# Now, loop through coord arrays, and create a circle at each x,y pair
ellipse = Ellipse((1000, 400), width=400, height=100, edgecolor='white',facecolor='none',linewidth=2)
ax.add_patch(ellipse)
path = ellipse.get_path()
# Show the image
plt.show()
When I run the code, I get this (that is exactly what I want):
However, when I print the path in order to check it, I get the following output, which (I suppose) is exclusively related to the ellipse.
Path(array([[ 0. , -1. ],
[ 0.2652031 , -1. ],
[ 0.51957987, -0.89463369],
[ 0.70710678, -0.70710678],
[ 0.89463369, -0.51957987],
[ 1. , -0.2652031 ],
[ 1. , 0. ],
[ 1. , 0.2652031 ],
[ 0.89463369, 0.51957987],
[ 0.70710678, 0.70710678],
[ 0.51957987, 0.89463369],
[ 0.2652031 , 1. ],
[ 0. , 1. ],
[-0.2652031 , 1. ],
[-0.51957987, 0.89463369],
[-0.70710678, 0.70710678],
[-0.89463369, 0.51957987],
[-1. , 0.2652031 ],
[-1. , 0. ],
[-1. , -0.2652031 ],
[-0.89463369, -0.51957987],
[-0.70710678, -0.70710678],
[-0.51957987, -0.89463369],
[-0.2652031 , -1. ],
[ 0. , -1. ],
[ 0. , -1. ]]), array([ 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 79], dtype=uint8))
However, I need a list of coordinates of the ellipse in relation to the pixel of the picture (1600 X 1200). I'm probably using the wrong function or there is something that does not match between the picture and the ellipse.
I should obtain something like this (this is an example from a previous experiment):
[ Path(array([[ 1599. , 868.86791294],
[ 1598. , 868.87197971],
[ 1597. , 868.8801087 ],
...,
[ 1597. , 675.30378536],
[ 1598. , 675.31373204],
[ 1599. , 675.31870792]]), None)]
665
Can anyone help me?
Thank you in advance,
R
You should use Ellipse.get_path().vertices, however it's not in the correct coordinates system. To transform it, apply ellipse.get_patch_transform().transform to it. See below a working example
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
from matplotlib.path import Path
from matplotlib.patches import PathPatch
img = plt.imread("image.jpg")
fig, ax = plt.subplots(1)
ax.set_aspect('equal')
ax.imshow(img)
# Create the base ellipse
ellipse = Ellipse((300, 300), width=400, height=100,
edgecolor='white', facecolor='none', linewidth=2)
# Get the path
path = ellipse.get_path()
# Get the list of path vertices
vertices = path.vertices.copy()
# Transform the vertices so that they have the correct coordinates
vertices = ellipse.get_patch_transform().transform(vertices)
# You can then save the vertices array to a file: csv, pickle... It's up to you
plt.show()
the path array appears to be a coarse normalized circle - I'd ignore it
you have the ellipse info already
ellipse = Ellipse((1000, 400), width=400, height=100, ...)
I would just do a sin, cos paramaterized ellipse based on the 1st few numbers in Ellipse((1000, 400), width=400, height=100which are the center point and axis lengths if you want to draw a hi res ellipse explicitly
for a membership test (x - x_0)^2/a^2 + (y - y_0)^2/b^2 <= 1 is probably best where a, b
are 1/2 of the respective width=400, height=100
of course you would only need to test pixel indices within the bounding rectangle

Generating a spline using scipy from a numpy array containing x and y coordinates [duplicate]

I have a set of points pts which form a loop and it looks like this:
This is somewhat similar to 31243002, but instead of putting points in between pairs of points, I would like to fit a smooth curve through the points (coordinates are given at the end of the question), so I tried something similar to scipy documentation on Interpolation:
values = pts
tck = interpolate.splrep(values[:,0], values[:,1], s=1)
xnew = np.arange(2,7,0.01)
ynew = interpolate.splev(xnew, tck, der=0)
but I get this error:
ValueError: Error on input data
Is there any way to find such a fit?
Coordinates of the points:
pts = array([[ 6.55525 , 3.05472 ],
[ 6.17284 , 2.802609],
[ 5.53946 , 2.649209],
[ 4.93053 , 2.444444],
[ 4.32544 , 2.318749],
[ 3.90982 , 2.2875 ],
[ 3.51294 , 2.221875],
[ 3.09107 , 2.29375 ],
[ 2.64013 , 2.4375 ],
[ 2.275444, 2.653124],
[ 2.137945, 3.26562 ],
[ 2.15982 , 3.84375 ],
[ 2.20982 , 4.31562 ],
[ 2.334704, 4.87873 ],
[ 2.314264, 5.5047 ],
[ 2.311709, 5.9135 ],
[ 2.29638 , 6.42961 ],
[ 2.619374, 6.75021 ],
[ 3.32448 , 6.66353 ],
[ 3.31582 , 5.68866 ],
[ 3.35159 , 5.17255 ],
[ 3.48482 , 4.73125 ],
[ 3.70669 , 4.51875 ],
[ 4.23639 , 4.58968 ],
[ 4.39592 , 4.94615 ],
[ 4.33527 , 5.33862 ],
[ 3.95968 , 5.61967 ],
[ 3.56366 , 5.73976 ],
[ 3.78818 , 6.55292 ],
[ 4.27712 , 6.8283 ],
[ 4.89532 , 6.78615 ],
[ 5.35334 , 6.72433 ],
[ 5.71583 , 6.54449 ],
[ 6.13452 , 6.46019 ],
[ 6.54478 , 6.26068 ],
[ 6.7873 , 5.74615 ],
[ 6.64086 , 5.25269 ],
[ 6.45649 , 4.86206 ],
[ 6.41586 , 4.46519 ],
[ 5.44711 , 4.26519 ],
[ 5.04087 , 4.10581 ],
[ 4.70013 , 3.67405 ],
[ 4.83482 , 3.4375 ],
[ 5.34086 , 3.43394 ],
[ 5.76392 , 3.55156 ],
[ 6.37056 , 3.8778 ],
[ 6.53116 , 3.47228 ]])
Actually, you were not far from the solution in your question.
Using scipy.interpolate.splprep for parametric B-spline interpolation would be the simplest approach. It also natively supports closed curves, if you provide the per=1 parameter,
import numpy as np
from scipy.interpolate import splprep, splev
import matplotlib.pyplot as plt
# define pts from the question
tck, u = splprep(pts.T, u=None, s=0.0, per=1)
u_new = np.linspace(u.min(), u.max(), 1000)
x_new, y_new = splev(u_new, tck, der=0)
plt.plot(pts[:,0], pts[:,1], 'ro')
plt.plot(x_new, y_new, 'b--')
plt.show()
Fundamentally, this approach not very different from the one in #Joe Kington's answer. Although, it will probably be a bit more robust, because the equivalent of the i vector is chosen, by default, based on the distances between points and not simply their index (see splprep documentation for the u parameter).
Your problem is because you're trying to work with x and y directly. The interpolation function you're calling assumes that the x-values are in sorted order and that each x value will have a unique y-value.
Instead, you'll need to make a parameterized coordinate system (e.g. the index of your vertices) and interpolate x and y separately using it.
To start with, consider the following:
import numpy as np
from scipy.interpolate import interp1d # Different interface to the same function
import matplotlib.pyplot as plt
#pts = np.array([...]) # Your points
x, y = pts.T
i = np.arange(len(pts))
# 5x the original number of points
interp_i = np.linspace(0, i.max(), 5 * i.max())
xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y, 'ko')
plt.show()
I didn't close the polygon. If you'd like, you can add the first point to the end of the array (e.g. pts = np.vstack([pts, pts[0]])
If you do that, you'll notice that there's a discontinuity where the polygon closes.
This is because our parameterization doesn't take into account the closing of the polgyon. A quick fix is to pad the array with the "reflected" points:
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
#pts = np.array([...]) # Your points
pad = 3
pts = np.pad(pts, [(pad,pad), (0,0)], mode='wrap')
x, y = pts.T
i = np.arange(0, len(pts))
interp_i = np.linspace(pad, i.max() - pad + 1, 5 * (i.size - 2*pad))
xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)
fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y, 'ko')
plt.show()
Alternately, you can use a specialized curve-smoothing algorithm such as PEAK or a corner-cutting algorithm.
Using the ROOT Framework and the pyroot interface I was able to generate the following image
With the following code(I converted your data to a CSV called data.csv so reading it into ROOT would be easier and gave the columns titles of xp,yp)
from ROOT import TTree, TGraph, TCanvas, TH2F
c1 = TCanvas( 'c1', 'Drawing Example', 200, 10, 700, 500 )
t=TTree('TP','Data Points')
t.ReadFile('./data.csv')
t.SetMarkerStyle(8)
t.Draw("yp:xp","","ACP")
c1.Print('pydraw.png')
To fit a smooth closed curve through N points you can use line segments with the following constraints:
Each line segment has to touch its two end points (2 conditions per line segment)
For each point the left and right line segment have to have the same derivative (2 conditions per point == 2 conditions per line segment)
To be able to have enough freedom for in total 4 conditions per line segment the equation of each line segment should be y = ax^3 + bx^2 + cx + d. (so the derivative is y' = 3ax^2 + 2bx + c)
Setting the conditions as suggested would give you N * 4 linear equations for N * 4 unknowns (a1..aN, b1..bN, c1..cN, d1..dN) solvable by matrix inversion (numpy).
If the points are on the same vertical line special (but simple) handling is required since the derivative will be "infinite".

Combination of two Binomial Draws Or "Group and sum matrix by value"

Consider two urns, E and U. There are holy grails and crappy grails in each of these. Denote the holy ones with H.
Say we draw out of both urns, xe times out of E, and xu times out of U - how many holy grails are we going to find? This is easily solvable for any pair (xe, xu). But I'd like to do this for grids of draws out of xe and xu.
What is the most efficient way to do this in Python using standard packages?
Here is my approach.
import numpy as np
import scipy.stats as stats
binomial = stats.binom.pmf
# define the grids of E, U to search
numberOfE = np.arange(3)
numberOfHolyE = np.arange(3)
numberOfU = np.arange(5)
numberOfHolyU = np.arange(5)
# mesh it
E, U, EH, UH = np.meshgrid(numberOfE, numberOfU, numberOfHolyE, numberOfHolyU, indexing='ij')
# independent draws from both urns. Probabilities are 0.9 and 0.1
drawsE = binomial(EH, E, 0.9)
drawsU = binomial(UH, U, 0.1)
# joint probability of being at a specific grid point
prob = drawsE * drawsU
totalHigh = EH + UH
This is how far I've come:
In [77]: prob[1,1,:]
Out[77]:
array([[ 0.09, 0.01, 0. , 0. , 0. ],
[ 0.81, 0.09, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ]])
In [78]: totalHigh[1,1,:]
Out[78]:
array([[0, 1, 2, 3, 4],
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6]])
I think, that, these matrices mean the following:
Take a look at where totalHigh has value 1: if I draw one time from both urns, I have a 0.81 probability of drawing one high from E and zero from U, and 0.01 the other way around. That means, the total probability of drawing one guy conditional on drawing once from both urns is 0.82.
Which brings me to my second question:
Conditional on doing it this way, How do I sum up these probabilities efficiently, conditional on the first two dimensions? I effectively want to transform these 4D matrices into 3D matrices.

Categories