Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 09:51 AM
synapsus synapsus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 synapsus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m
Reputation Power: 0
Red face 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!

Reply With Quote
  #2  
Old February 12th, 2013, 11:38 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 sec
Reputation Power: 383
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

Reply With Quote
  #3  
Old February 12th, 2013, 11:24 PM
synapsus synapsus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 synapsus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #4  
Old February 13th, 2013, 09:05 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 sec
Reputation Power: 383
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.

Reply With Quote
  #5  
Old February 13th, 2013, 09:54 AM
synapsus synapsus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 synapsus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old February 13th, 2013, 12:04 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 sec
Reputation Power: 383
The program I posted is modified to perhaps answer your question, also there was a logic error which is fixed.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help carrying out a probability program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap