3 # corestats.py (COREy STATS)
4 # Copyright (c) 2006-2007, Corey Goldberg (corey@goldb.org)
5 # http://www.goldb.org/corestats.html
7 # statistical calculation class
8 # for processing numeric sequences
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Lesser General Public
14 # License as published by the Free Software Foundation; either
15 # version 2.1 of the License, or (at your option) any later version.
17 # Slightly modified for use in gPodder
21 def __init__(self
, sequence
):
22 # sequence of numbers we will process
23 # convert all items to floats for numerical processing
24 self
.sequence
= [float(item
) for item
in sequence
]
27 if len(self
.sequence
) < 1:
30 return sum(self
.sequence
)
33 return len(self
.sequence
)
36 if len(self
.sequence
) < 1:
39 return min(self
.sequence
)
42 if len(self
.sequence
) < 1:
45 return max(self
.sequence
)
48 if len(self
.sequence
) < 1:
51 return sum(self
.sequence
) / len(self
.sequence
)
54 if len(self
.sequence
) < 1:
58 return self
.sequence
[len(self
.sequence
) // 2]
61 if len(self
.sequence
) < 1:
65 sdsq
= sum([(i
- avg
) ** 2 for i
in self
.sequence
])
66 stdev
= (sdsq
/ (len(self
.sequence
) - 1)) ** .5
69 def percentile(self
, percentile
):
70 if len(self
.sequence
) < 1:
72 elif percentile
>= 100:
73 raise ValueError('percentile must be < 100')
75 element_idx
= int(len(self
.sequence
) * (percentile
/ 100.0))
77 value
= self
.sequence
[element_idx
]