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 November 22nd, 2012, 09:14 AM
RECrerar RECrerar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 RECrerar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 19 sec
Reputation Power: 0
Get array of counts of random number to allow calculation of chi-square

Hey Guys,

I'm trying to learn some python and as part of this I am tring to generate 100 weighted random numbers and then check that the output that I get is within the expected range.

So far I have a function that will generate the weighted random number and return a list of all returned values and the count of all the returned values. The code I have so far is below along with the returned output.

code: (unit_test1 is the function I intend to analyse the output in)

Code:
import random
import bisect
from collections import Counter

class RandomGen(object):
# Values that may be returned by next_num()
  _random_nums = [-2, -1, 0, 1, 2]
# Probability of the occurence of random_nums
  _probabilities = [0.01, 0.2, 0.58, 0.2, 0.01]
  
  def next_num(self):
    """
    Returns one of the randomNums. When this method is called
    multiple times over a long period, it should return the
    numbers roughly with the initialized probabilities.
    """
    totals = []
    running_total = 0
    
    # Create a cummulative frequency range.
    for p in self._probabilities:
      running_total += p
      totals.append(running_total)

    # If not warn the user before continuing.
    if running_total != 1:
      print("Warning: Total probabilities to not sum to 1")
      print("Normalising probabilities.  Check your input.")

    # Generate a randdom number with the range 0 to running_total  
    rand=random.random() * running_total

    # Find the index to return
    idx = bisect.bisect_right(totals, rand)
    return self._random_nums[idx]

  def unit_test1(self):
    """
    The aim of this test it to check that the observed values
    are within the expected chi square range
    """

    
pass

allValues = []
randomNum = RandomGen()
numRuns = 100

# Run the program 100 times and output the sums of each value.
for i in range(numRuns):
  value = randomNum.next_num()
  allValues.append(value)

# Display the results to the user.
print Counter(allValues)
print allValues


example output
Code:
Counter({0: 64, -1: 22, 1: 14})
[-1, 0, 0, -1, 0, 0, 0, 1, -1, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 0, 0, 0, -1, 0, -1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, -1, -1, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, -1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, -1, -1, -1, -1, 0, 0, 0, -1, 0, -1, 1, 1, -1, 0, 0, 1]


The next bit is where I am stuck. In order to calculate the chi-square I think I need my output data to be in the format:

Code:
observed_nums = [ 0, 22, 64, 14, 0 ]


How do I go from the output I have to this format?
Also from searching on line it looks like the for loop to calculate my random values may not be the most efficient way to do this. I have used this method as it is all I know how to do. If you have any suggestions on how to improve the code in general it would be appreciated.

Thanks

Reply With Quote
  #2  
Old November 22nd, 2012, 09:29 AM
Quackajack Quackajack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 35 Quackajack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 55 m 6 sec
Reputation Power: 1
I may have misread what you are trying to achieve but the following should work:
Code:
mydict = Counter(allValues)
observed_nums = [ mydict.get(i,0) for i in RandomGen._random_nums ]

Reply With Quote
  #3  
Old November 22nd, 2012, 09:37 AM
RECrerar RECrerar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 RECrerar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Quackajack
I may have misread what you are trying to achieve but the following should work:
Code:
mydict = Counter(allValues)
observed_nums = [ mydict.get(i,0) for i in RandomGen._random_nums ]


Yep, that's perfect. Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Get array of counts of random number to allow calculation of chi-square

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