Created
June 12, 2020 16:46
-
-
Save 2hands10fingers/a81a3e2ed9aadc2401b7f0972838c02d to your computer and use it in GitHub Desktop.
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
import math | |
class Stats: | |
def __init__(self, dataset): | |
self.dataset = dataset | |
self.data_length = len(dataset) | |
self.mean = sum(dataset) / self.data_length | |
self.variance = None | |
self.SD = None | |
self.CV = None | |
self.k = None | |
self.current_result = None | |
def curr_res(self, result): | |
self.current_result = result | |
def get_variance(self, exception=None): | |
if self.dataset: | |
variances = [ (i - self.mean) ** 2 for i in self.dataset ] | |
total_sum_squares = round(sum(variances), 2) | |
division_exception = -1 if exception == None else 0 | |
variance = round(total_sum_squares / (self.data_length - division_exception ),2) | |
self.variance = variance | |
self.curr_res(variance) | |
return self | |
def get_standard_devi(self): | |
if self.variance: | |
SD = round(math.sqrt(self.variance), 2) | |
self.SD = SD | |
self.curr_res(SD) | |
return self | |
def get_CV(self, unit_num): | |
CV = (self.SD * unit_num) / self.mean | |
self.CV = CV | |
self.curr_res(CV) | |
return self | |
def get_K(self, extra_set): | |
lower_bound, upper_bound = extra_set | |
if not self.SD: | |
SD = self.standard_devi(self.variance(self.dataset)) | |
self.k = (lower_bound - self.mean) / SD | |
else: | |
self.k = (lower_bound - self.mean) / self.SD | |
self.curr_res(self.k) | |
return self | |
data = [12,14,11,18,10.5,11.3,12,14,11,9] | |
my_stat = Stats(data) | |
my_stat \ | |
.get_variance() \ | |
.get_standard_devi() \ | |
.get_CV(5) \ | |
.get_K((12.36, 15.35)) | |
print(my_stat.current_result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment