|
#rec.py is a decorator class written by George Sakkis. See(http://code.activestate.com/recipes/496691/) |
|
import math,functools |
|
from rec import tail_recursive |
|
|
|
def _count(l, test): |
|
@tail_recursive |
|
def helper(l, test, count): |
|
if not l: return count |
|
if test(l[0]): count += 1 |
|
return helper(l[1:],test, count) |
|
return helper(l, test, 0) |
|
|
|
def freq(l, binWidth, lim=0): |
|
#the number of classes and test condition of the last class change if a limit is given. |
|
if lim == 0: |
|
num_of_classes = math.ceil(max(l)/binWidth) |
|
last_test = lambda x: True if binWidth*(num_of_classes-1) <= x <= binWidth*num_of_classes else False |
|
else: |
|
num_of_classes = lim//binWidth + 1 |
|
last_test = lambda x: True if binWidth*(lim//binWidth) <= x else False |
|
|
|
#bottom:inclusive, top:exclusive. |
|
test = lambda bottom, top, x: True if bottom <= x < top else False |
|
|
|
result = [_count(l, functools.partial(test, binWidth*i, binWidth*(i+1))) for i in range(num_of_classes-1)] |
|
result.append(_count(l, last_test)) |
|
return result |
|
|
|
#freqList: a list generated by freq. |
|
def comulativeFreq(freqList): |
|
return [sum(freqList[:i]) for i in range(1, len(freqList)+1)] |
|
|
|
#freqList: a list generated by freq. |
|
def relativeFreq(freqList): |
|
total = sum(freqList) |
|
return [freqList[i]/total for i in range(len(freqList))] |
|
|
|
#freqList: a list generated by freq. |
|
def comulativeRelativeFreq(freqList): |
|
rel = relativeFreq(freqList) |
|
return [sum(rel[:i]) for i in range(1, len(rel)+1)] |
|
|
|
#n: the number of samples. When the data is a list l, n = len(l) |
|
def sturges(n): |
|
return 1 + math.log(n,2) |