NumPy modelling of Belief Tables?

Charles Boncelet boncelet at udel.edu
Thu Apr 6 22:53:06 EDT 2000


Magnus Lie Hetland wrote:

>
>
> If, for instance, the table t1 has the dimensions X and Y, while t2 has the
> dimensions X and Z, then for each x, y, z in X, Y, X,
>
>         (t1 * t2)[x,y,z] = t1[x,y] * t2[x,z].
>
> This may be simple to do in NumPy (it is certainly easy to do
> algorithmically) - I just can't see how...

Two ideas:
1. loop over the columns (rows) of one of the matrices and let
numpy broadcasting work for you.  Then convert back to a single
numpy object.

2.try multiply.outer(t1,t2) or multiply.outer(t2,t1) followed
by some diagonal magic:
    tt = multiply.outer(t2,t1)
    t = diagonal(tt,axis1=1,axis2=3)
(You may need to rearrange things to get the output in the order
you want.  Bear in mind that tt is a 4d object, so this may be
slow.)

>
>
> Division is about the same thing, except that 0/0 = 0... For tables with the
> same dimensions, I guess one could use something like
>
>         t1 / where(equal(t2,0),1,t2)
>
> where 1 is just an arbitrary value. (I'm not sure how this solution
> generalizes when the dimensions differ...)

This should work.  (Note to others: he is assuming that t2=0 implies t1=0.)

>
>
> The other operation needed is marginalization of the tables. When a table
> is marginalized over some of its dimensions, the elements having equal
> coordinates in the remaining dimensions (i.e. the ones not being
> marginalized
> over) are added. For instance, if you have a two-dimensional table, t, over
> the variables X and Y, and you want to marginalize out Y, then for each
> x in X,
>
>         m[x] = sum with x in Y { t[x,y] },
>
> where m is the marginalized table.
>

"sum" is your friend here. If a=array([0,1],[2,3]), the sum(a)=array([2.4])
and sum(a,1)=array([1,5]).  Just specify the axis over which you want to
sum.

If you produce anything interesting, please publish it if you can.

    Charlie Boncelet

------
Charles Boncelet, University of Delaware,
On sabbatical at ADFA, Canberra Australia,
Home Page: http://www.ece.udel.edu/~boncelet/






More information about the Python-list mailing list