The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Need help carrying out a probability program
Discuss Need help carrying out a probability program in the Python Programming forum on Dev Shed. Need help carrying out a probability program Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 12th, 2013, 09:51 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 24 m
Reputation 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!
|

February 12th, 2013, 11:38 AM
|
 |
Contributing User
|
|
|
|
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
__________________
[code] Code tags[/code] are essential for python code!
Last edited by b49P23TIvg : February 13th, 2013 at 12:03 PM.
Reason: Program logic error corrected
|

February 12th, 2013, 11:24 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 24 m
Reputation Power: 0
|
|
Quote: | 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!
|

February 13th, 2013, 09:05 AM
|
 |
Contributing User
|
|
|
|
|
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.
|

February 13th, 2013, 09:54 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 24 m
Reputation Power: 0
|
|
Quote: | 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
|

February 13th, 2013, 12:04 PM
|
 |
Contributing User
|
|
|
|
|
The program I posted is modified to perhaps answer your question, also there was a logic error which is fixed.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|