We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Populating a map based on population density
Page Index Toggle Pages: 1
Populating a map based on population density (Read 445 times)
Populating a map based on population density
Jul 5th, 2006, 2:14am
 
I'm interested in taking a map and populating it with a fixed number of people, based on population density data. Effectively, I want to simulate drawing N names out of a hat, and sticking pins in each persons home town.

My initial thought is to randomly choose a location, and determine a probability (between 0 and 1) from the density, generate a random number between 0 and 1, and if the number is less than the probability populate the spot. If not, I move on. I'd stop when I've reached my N people.

An issue is how to normalize the probabilities. I could normalize it such that the sum of all probabilities is 1 (a person has a 100% chance of being somewhere), but all the probabilites would be very small and I'd get a lot of misses. I could normalize by the maximum density, but then the the big cities would always be populated if chosen. I could normalize by a mean density, but then I have to deal with probabilities greater than 1.


The simple test case is to imagine an 200x200 grid unit map with a flat population density. If I were to normalize by the sum of probabilities, each space would only have a 1/40000 chance of being accepted. But there is already a 1/40000 probability involved in randomly choosing the site, before I even think of accepting or not accepting the choice. I think this favours the normalize-by-maximum option, but I'm not sure.


Is there a more elegant way of doing this?
Re: Populating a map based on population density
Reply #1 - Jul 17th, 2006, 8:37pm
 
If I understand, here's a possible approach:  store cumulative probabilities in a lookup map.

Create an array of 40000 (200 x 200) floats to hold your a probability map.  Normalize your probabilites.  At each index of the probability map, store the cumulative probability so far.

Then to select a location, generate a random number 0..1, scan the table and find the last index where cumulative probability is less than target.  That index is then your cell location:  (x=idx%200, y=idx/200)

(linear scan of table could be optimized with binary search as grid size increases)

Pick a large enough sampling of such random numbers and you should get a distribution of locations corresponding to your original probabilities.
Page Index Toggle Pages: 1