### Thread: Need help carrying out a probability program

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
3
Rep Power
0

#### Need help carrying out a probability program

Hi guys, I've done a good amount of research on this, but I couldn't seem to find the answer. If I could just get a tutorial on doing this, then I can do it myself - I just need to know how!
As for background, I learned basic Python (though the vast majority of it was using turtle, even though I wanted to learn other tools) a year ago or so, but I mostly forgot it. So just in case, pretend I'm a beginner to Python. I'm just gonna copy and paste my problem from the question I posted on Yahoo Answers:

I had an idea for a project in Python, but I'm having trouble finding out how to do it.

Basically, the project is designed to give you a random life - It would start with country - I would give the program a probability of living in a country. Then, based on that country's statistics, I will find out the probability of being a male or a female. After that, I would do class, money, family, etc. Each aspect would be dependent on the last result.

Anyway, so I'm wondering how to carry out the probability part. So, the program would run it, and find a random country as an output based on the probability statistics, then gender based on that, etc.

Thanks for all answers, if you have any questions about my question, I'll be more than happy to answer them!
2. Code:
```import random
import bisect

POPULATION_BY_MAJOR_POLITICAL_BOUNDARY = dict(USA = (
#(member_of_group , population),
('California',  38041430,),
('Texas'     ,  26059203,),
('New York'  ,  19570261,),
('Florida'   ,  19317568,),
('Illinois'  ,  12875255,),
))

def choose(ITERABLE_OF_2TUPLES):
CUMSUM = [0]
for T in ITERABLE_OF_2TUPLES:
CUMSUM.append(CUMSUM[-1]+T[1])
A = random.randrange(CUMSUM[-1])
B = bisect.bisect_left(CUMSUM[1:],A)
return ITERABLE_OF_2TUPLES[B][0]

# pretend you just ran choose to select a country.  choose returned 'USA'
# COUNTRY = choose(POPULATION_BY_COUNTRY)
COUNTRY = 'USA'

# use the result of choose as input to another selection
for i in range(88):
LOCAL_POLITICAL_REGION = choose(POPULATION_BY_MAJOR_POLITICAL_BOUNDARY[COUNTRY])
print(LOCAL_POLITICAL_REGION)
#etcetera```
Last edited by b49P23TIvg; February 13th, 2013 at 12:03 PM. Reason: Program logic error corrected
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
3
Rep Power
0
Originally Posted by b49P23TIvg
Code:
```import random
import bisect

DATA = (
#(member_of_group , population),
('California',  38041430,),
('Texas'     ,  26059203,),
('New York'  ,  19570261,),
('Florida'   ,  19317568,),
('Illinois'  ,  12875255,),
)

def choose(ITERABLE_OF_2TUPLES):
CUMSUM = [0]
for T in ITERABLE_OF_2TUPLES:
CUMSUM.append(CUMSUM[-1]+T[1])
A = random.randrange(CUMSUM[-1])
B = bisect.bisect_left(CUMSUM,A)
return ITERABLE_OF_2TUPLES[B][0]

choose(DATA)```
thanks! i really appreciate it. i'll try and dissect that code and use it.

just a quick question - what if i have more than one probability parameter? can i combine the statistics without having to make a lot of groups? what i mean is that i would list countries and population, then each country would have certain statistics for gender (for example, 51% men, 49% women), class (for example, 2% are high class, etc.), and religion (75% christian, etc.). i want to include these in the random choosing and assign probability to them because of the percentages.

thanks for the response!
4. I'm just showing that if the groups are in ratios
3 A's to 2 B's to 5 C's then the probability of a single random sample from the group being A is

3 / (3+2+5)

And I wrote code to express that.

I would use the code by first picking a country,
next choosing sex,
then income.

In other words, I'd keep this simple by calling the function as is for successive distributions.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
3
Rep Power
0
Originally Posted by b49P23TIvg
I'm just showing that if the groups are in ratios
3 A's to 2 B's to 5 C's then the probability of a single random sample from the group being A is

3 / (3+2+5)

And I wrote code to express that.

I would use the code by first picking a country,
next choosing sex,
then income.

In other words, I'd keep this simple by calling the function as is for successive distributions.
ok, i see - thanks. just need some clarification on the code.

by any chance, can i use what the tool "choose" got as an output as a variable? for example, if i run the program, and get a state, then i want to use whatever state i get into the next program (which will be sex, or something else).

also, how do i write the output? i tried using print(choose(DATA)) but it keeps choosing random states even if i set the population to 0.

related to the last question, i think i'm having some trouble deciphering and understanding the code and why everything is there - that might be why i'm getting weird outputs even if i change population. a short explanation would be awesome, but otherwise i can always follow the code to some extent as long as it works!

thank you so much, and sorry for the beginner questions, i'm really rusty on python lol
6. The program I posted is modified to perhaps answer your question, also there was a logic error which is fixed.