I’m trying to code this expression in python but I’m having some difficulty.

This is the code I have so far and wanted some advice.
x = 1x2 vector
mu = 1x2 vector
Sigma = 2x2 matrix
xT = (x-mu).transpose()
sig = Sigma**(-1)
dotP = dot(xT ,sig )
dotdot = dot(dotP, (x-mu))
E = exp( (-1/2) dotdot )
Am I on the right track? Any suggestions?
Sigma ** (-1)isn’t what you want. That would raise each element ofSigmato the-1power, i.e.1 / Sigma, whereas in the mathematical expression it means the inverse, which is written in Python asnp.linalg.inv(Sigma).(-1/2) dotdotis a syntax error; in Python, you need to always include*for multiplication, or just do- dotdot / 2. Since you’re probably using python 2, division is a little wonky; unless you’ve donefrom __future__ import division(highly recommended),1/2will actually be0, because it’s integer division. You can use.5to get around that, though like I said I do highly recommend doing the division import.This is pretty trivial, but you’re doing the
x-musubtraction twice where it’s only necessary to do once. Could save a little speed if your vectors are big by doing it only once. (Of course, here you’re doing it in two dimensions, so this doesn’t matter at all.)Rather than calling
the_array.transpose()(which is fine), it’s often nicer to usethe_array.T, which is the same thing.I also wouldn’t use the name
xT; it implies to me that it’s the transpose ofx, which is false.I would probably combine it like this: