The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Get array of counts of random number to allow calculation of chi-square
Discuss Get array of counts of random number to allow calculation of chi-square in the Python Programming forum on Dev Shed. Get array of counts of random number to allow calculation of chi-square 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:
|
|
|

November 22nd, 2012, 09:14 AM
|
|
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")
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
|

November 22nd, 2012, 09:29 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 35
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 ]
|

November 22nd, 2012, 09:37 AM
|
|
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
|
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
|
|
|
|
|