# How to sample from discrete 3D distribution

I am trying to implement a particle filter to track multiple objects. During the propagation phase I need to take N samples from a three dimensional probability distribution, which does not fit known distribution types. In a 1D distribution I would just create the cumulative distribution function, invert it and take random samples from that - can I do something similar with higher dimensional distributions?

I am implementing in Java by the way, so just giving me an R function is not going to help me too much.

• Does it have countably finite number of possible values for each dimension? – Tim May 8 '17 at 8:23
• Yes, in fact it is limited to quite a manageable 240x180x90 cases. – Mr Squid May 8 '17 at 8:24

Your distribution takes form of large $I \times J \times K$ table with values mapped to probabilities

$$\Pr(X = x, Y = y, Z = z) = p_{ijk}$$

instead of thinking of it as of a 3-dimensional object, think of it as of a table, i.e.

X  Y  Z  Pr
x1 y1 z1 p111
x1 y1 z2 p112
...
xI yJ zK pIJK


now to draw samples from it you need only to sample rows of such table with probabilities equal to $p_{ijk}$, so you just need an algorithm for sampling from univariate categorical distribution!

• Thanks a lot, that makes a lot of sense but somehow I didn't think of it. Thanks! – Mr Squid May 8 '17 at 23:34