statistics module added

parent e550021c
......@@ -113,7 +113,10 @@ SOURCES = \
language/python/module/fnmatch.py \
language/python/module/optparse.py \
language/python/module/selectors.py \
language/python/module/socket.py
language/python/module/socket.py \
language/python/module/numbers.py \
language/python/module/fractions.py \
language/python/module/statistics.py
......
......@@ -473,8 +473,8 @@ This is the copyright information of the file ported over to scheme
;; REs insist on real strings, so we can too.
(cond
((isinstance value str)
(let ((m (_parser (scm-str value))))
(if (not (bool m))
(let ((m (_parser (scm-str value))))
(if (not (bool m))
(let ((context (if (eq? context None)
(getcontext)
context)))
......@@ -6649,7 +6649,7 @@ This is the copyright information of the file ported over to scheme
;; The default context prototype used by Context()
;; Is mutable, so that new contexts can have different default values
(define DefaultContext
(set! DefaultContext
(Context
#:prec 28
#:rounding ROUND_HALF_EVEN
......@@ -6660,6 +6660,7 @@ This is the copyright information of the file ported over to scheme
#:capitals 1
#:clamp 0))
(setcontext DefaultContext)
;; Pre-made alternate contexts offered by the specification
;; Don't change these; the user should be able to select these
;; contexts and be able to reproduce results from other implementations
......@@ -7011,4 +7012,4 @@ This is the copyright information of the file ported over to scheme
;; _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
(define _PyHASH_10INV (pow 10 (- _PyHASH_MODULUS 2) _PyHASH_MODULUS))
(setcontext DefaultContext)
......@@ -14,7 +14,6 @@
(f-seq (f-tag "a") (f-out 7))
(f-seq (f-tag "b") (f-out 8))
(f-seq (f-tag "f") (f-out 12))
(f-seq (f-tag "n") (f-out 10))
(f-seq (f-tag "r") (f-out 13))
(f-seq (f-tag "t") (f-out 9))
(f-seq (f-tag "v") (f-out 11))
......@@ -36,7 +35,6 @@
(f-seq (f-tag "\\")
(f-or!
f-nl
(f-let ((x byte-special))
(f-out (integer->char x)))
(f-seq (f-tag "N")
......@@ -47,8 +45,7 @@
(f-out (integer->char x))))
(f-seq (f-tag "U")
(f-let ((x he8))
(f-out (integer->char x))))
(f-reg! "."))))))
(f-out (integer->char x)))))))))
(define-syntax-rule (mk n tag str) (define n (f-seq tag (f-tag str))))
......
......@@ -78,10 +78,10 @@ A single exception is defined: StatisticsError is a subclass of ValueError.
"""
__all__ = [ 'StatisticsError',
__all__ = [ 'StatisticsError','geometric_mean','multimode',
'pstdev', 'pvariance', 'stdev', 'variance',
'median', 'median_low', 'median_high', 'median_grouped',
'mean', 'mode', 'harmonic_mean',
'mean', 'fmean', 'mode', 'harmonic_mean',
]
import collections
......@@ -290,6 +290,41 @@ def _fail_neg(values, errmsg='negative value'):
# === Measures of central tendency (averages) ===
def geometric_mean(data):
return math.pow(math.pow(data),1/len(data))
def multimode(x):
def k(x):
return x[0]
d={}
j = 0
for i in x:
(n,m) = d.get(i,[0,-j])
d[i] = [n+1,m]
j = j + 1
l = []
for k,v in d:
l.append([v,k])
l.sort(key=k, reverse=True)
if l:
ret = []
k = l[0][0][0]
for x in l:
if k == x[0][0]:
ret.append(x[1])
else:
break
else:
ret = []
return ret
def mean(data):
"""Return the sample arithmetic mean of data.
......@@ -315,6 +350,8 @@ def mean(data):
assert count == n
return _convert(total/n, T)
def fmean(data):
return mean(data)
def harmonic_mean(data):
"""Return the harmonic mean of data.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment