3
$\begingroup$

I have a $n \times n$ distribution grid that represents the likelihood of finding a person within an area (see below). From this I would like to be able to create $x$ samples that fit this distribution.

Ideally $50\lt x \lt200$, and $10 \lt n \lt 50$. This is to simulate some search and rescue scenarios for a few path planning algorithms based on this distribution grid.

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ What's a nxn distribution? Are you generating matrices? What do you "have" a joint pdf? All stuff that needs to be included in your question. $\endgroup$
    – Three Diag
    Nov 5 '20 at 13:33
  • $\begingroup$ Also how big would n be for practical purpose? That might change the answer. $\endgroup$
    – Three Diag
    Nov 5 '20 at 13:33
  • $\begingroup$ @ThreeDiag sorry, you're right. I've edited my question. $\endgroup$ Nov 5 '20 at 13:58
5
$\begingroup$

If I translate in mathematical terms what I believe you said, we have that if we designate by by $(a_{i,j})_{1\le i,j\le n}$ the cases of your grid and $X$ a person, then you have the data of the probability that person $X$ pop up in case $a_{i,j}$. i.e., you have access to the $n^2$ numbers $(p_{i,j})_{1\le i,j\le n}$ such that $$\mathbb{P}(X \text{ is in the case } a_{i,j})=p_{i,j}.$$ and you want to be able to sample from this distribution ($\mathbb{P}$ stands for "the probability that").

This is a multinomial distribution (not multimodal, multimodal means another thing, see wikipedia). Just draw according to a multinomial distribution with parameters $(p_{1,1},p_{1,2}, \dots,p_{1,n},p_{2,n},\dots,p_{n,n})$, the fact that this is in 2D does not really matter, you can use the multinomial right away saying that if you draw a $1$ from the multinomial, your person is in case $a_{1,1}$, if you draw a $n$ he is in case $a_{1,n}$, if you draw a $(n+1)$ he is in case $a_{1, 2}$, if you draw $(3\times n+6)$, he is in case $a_{6,3}$... (you can enumerate the cases of your grid as you wish, I only give an example).

Drawing from a multinomial distribution is relatively easy using python or matlab.

$\endgroup$
1
  • $\begingroup$ Hi yes that's perfect! that's something that I hadn't even considred. So if I understand what you're saying is that I can use $(x,y)$ as a sort of key. $\endgroup$ Nov 5 '20 at 14:56
2
$\begingroup$

Using what @TMat said, I got this code to work using numpy

img = Image.open("path/to/img")
img = np.array(img)[:,:,0]
prob = (np.array(img)/np.sum(img)).flatten()

x,y = np.meshgrid(np.arange(0,img.shape[0]),np.arange(0,img.shape[1]))
x,y = x.flatten(),y.flatten()
xy  = np.vstack((x,y)).T

xy_indices = np.arange(len(xy))

choices = np.random.choice(xy_indices, 200, p=prob)

points = xy[choices]

enter image description here

$\endgroup$
3
  • $\begingroup$ Yes it is exactly so. Your numpy implementation is great the only tricky part is to be sure that your first flatten (for the probability) gives the same enumeration as the flatten then vstack you used to construct xy. And I wonder if you do not have a problem there, using your code I think I would have used the transposed matrix prob = (np.array(img)/np.sum(img)).T.flatten() or something like that. As is, I beleive you exchanged the columns and the lines but in your case this has no consequence because the image seems to be symmetric. $\endgroup$
    – TMat
    Nov 5 '20 at 15:32
  • $\begingroup$ try to check that img[xy[9]] corresponds to prob[9] (up to normalization) when img is not symmetric (img.T != img). Maybe I am wrong, I didn't check myself. $\endgroup$
    – TMat
    Nov 5 '20 at 15:35
  • $\begingroup$ @TMat yup that's exactly what happened when I tested this with a more complex image. Worked a charm! Thanks! $\endgroup$ Nov 5 '20 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.