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

New Free Tools on Dev Shed!

#1
November 22nd, 2012, 10:14 AM
 RECrerar
Registered User

Join Date: Nov 2012
Posts: 4
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")

# 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

#2
November 22nd, 2012, 10:29 AM
 Quackajack
Contributing User

Join Date: Jul 2012
Posts: 39
Time spent in forums: 10 h 52 m 39 sec
Reputation Power: 2
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 ]

#3
November 22nd, 2012, 10:37 AM
 RECrerar
Registered User

Join Date: Nov 2012
Posts: 4
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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Get array of counts of random number to allow calculation of chi-square