-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from pgdr/median-submission
Median submission
- Loading branch information
Showing
4 changed files
with
84 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,62 @@ | ||
class TS(object): | ||
"""A class for representing time series. | ||
Remarkably close to just being a list. But has mean() and median(). | ||
class TS(object): | ||
Set accuracy = 3 to get rounding to third decimal. | ||
""" | ||
|
||
def __init__(self): | ||
def __init__(self, data = None, accuracy=None): | ||
self.data = [] | ||
|
||
self.accuracy = accuracy | ||
if data: | ||
for x in data: | ||
self.append(x) | ||
|
||
def __len__(self): | ||
return len(self.data) | ||
|
||
def __getitem__(self, idx): | ||
return self.data[idx] | ||
|
||
|
||
def append(self , d): | ||
if self.accuracy: | ||
d = round(d, self.accuracy) | ||
self.data.append( d ) | ||
|
||
def __repr__(self): | ||
return str(self) | ||
|
||
def __str__(self): | ||
if len(self) < 10: | ||
return str(self.data) | ||
tmp = self.data[:4] | ||
tmp += self.data[-4:] | ||
fmt = 'TS[{}, {}, {}, {}, ..., {}, {}, {}, {}]' | ||
return fmt.format(*tmp) | ||
|
||
def mean(self): | ||
return sum(self.data) / len(self) | ||
if len(self) == 0: | ||
raise ValueError('Arithmetic mean of empty time series.') | ||
m = sum(self.data) / float(len(self)) | ||
if self.accuracy: | ||
return round(m, self.accuracy) | ||
return m | ||
|
||
def median(self): | ||
if len(self) == 0: | ||
raise ValueError('Median of empty time series.') | ||
tmp = sorted(self.data) | ||
return tmp[len(self)//2] | ||
|
||
def stddev(self): | ||
ld = len(self.data) | ||
if ld < 2: | ||
raise ValueError('Standard deviation of too few values') | ||
m = self.mean() | ||
ss = sum((x-m)**2 for x in self.data) | ||
std = (ss/float(ld))**0.5 | ||
if self.accuracy: | ||
return round(std, self.accuracy) | ||
return std |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters