Last active
September 16, 2015 01:32
-
-
Save Deathnerd/efa236180b954a443653 to your computer and use it in GitHub Desktop.
A Python class I wrote to do the boring parts of my Stats homework
This file contains hidden or 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
from math import sqrt | |
class Stats(object): | |
def __init__(self, dataset): | |
""" | |
A basic statistics class. Takes in a dataset of a list of numbers | |
""" | |
self.dataset = dataset | |
@property | |
def mean(self): | |
""" | |
Returns the mean (average) of the dataset | |
""" | |
return sum(self.dataset)/float(len(self.dataset)) | |
@property | |
def std_dev(self): | |
""" | |
Returns the standard deviation for the dataset | |
""" | |
m = self.mean | |
return sqrt((sum([(x-m)**2 for x in self.dataset]))/(len(self.dataset)-1)) | |
@property | |
def range(self): | |
""" | |
The range of the dataset | |
""" | |
return max(self.dataset) - min(self.dataset) | |
def chebychev(self, k): | |
""" | |
Returns the chebychev percentage for standard deviation with a given k | |
value as a decimal between 1 and 0 | |
""" | |
return 1.00-(1.00/(k**2)) | |
def chebychev_mean(self, k=1): | |
""" | |
Returns the percentage of items in the dataset that are within k | |
times of the standard deviation as a decimal between 1 and 0 | |
""" | |
m = self.mean | |
sdev = self.std_dev | |
bounds = [m-(sdev*k), m+(sdev*k)] | |
return len([x for x in dataset if (x >= bounds[0] and x <= bounds[1])])/float(len(dataset)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment