Skip to content

Instantly share code, notes, and snippets.

@sakethramanujam
Last active July 24, 2020 16:55
Show Gist options
  • Save sakethramanujam/e1d86a28f7ad331ff8e91df89615c99e to your computer and use it in GitHub Desktop.
Save sakethramanujam/e1d86a28f7ad331ff8e91df89615c99e to your computer and use it in GitHub Desktop.
A Dataclass example
series = [1,2,3,4,5]
stats = Statistics(series)
mean = stats.mean()
dev = stats.dev()
import numpy as np
from scipy.stats import skew, kurtosis
from .errors import (StepSizeError,
StepTypeError,
SeriesError)
def create_stats(series: list, step: int):
if step:
if type(step) is not int:
raise StepTypeError("Type of step invalid")
elif step > self.length:
raise StepSizeError("Invalid step size ")
elif series == None or len(series) == 0:
raise SeriesError("Empty/Invalid Series")
else:
return Statistics(series=series, step=step)
class Statistics:
def __init__(self, series: list, step: int):
self.step = step
self.series = series
self.length = len(self.series)
def mean(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
average = np.mean(series)
return average
else:
averages = []
for i in range(0, length, step):
temp = series[i:i+step]
average = np.mean(temp)
averages.append(average)
return averages
def deviation(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
std = np.std(series)
return std
else:
standard_deviations = []
for i in range(0, length, step):
temp = series[i:i+step]
deviation = np.std(temp)
standard_deviations.append(deviation)
return standard_deviations
def variance(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
variances = np.var(series)
return variances
else:
variances = []
for i in range(0, length, step):
temp = series[i:i+step]
variance = np.var(temp)
variances.append(variance)
return variances
def skewness(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
skewness = skew(series)
return skewness
else:
skewnesses = []
for i in range(0, length, step):
temp = series[i:i+step]
skewness = skew(temp)
skewnesses.append(skewness)
return skewnesses
def kurtosis(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
kurt = kurtosis(series)
return kurt
else:
kurts = []
for i in range(0, length, step):
temp = series[i:i+step]
kurt = kurtosis(temp)
kurts.append(kurt)
return kurts
def minimum(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
minimum = min(series)
return minimum
else:
minimums = []
for i in range(0, length, step):
temp = series[i:i+step]
minimum = min(temp)
minimums.append(minimum)
return minimums
def maximum(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
return max(series)
else:
maximums = []
for i in range(0, length, step):
temp = series[i:i+step]
maxval = max(temp)
maximums.append(maxval)
return maximums
def rangeval(self):
step = self.step
length = self.length
series = self.series
if step is (None or 0 or length):
rangevalue= max(series) - min(series)
return rangevalue
rangevals = []
for i in range(0, length, step):
temp = series[i:i+step]
rangevalue = max(temp)-min(temp)
rangevals.append(rangevalue)
return rangevals
def all(self):
stats = {
'means': self.mean(),
'deviations': self.deviation(),
'minimum': self.minimum(),
'maximum': self.maximum(),
'range': self.rangeval(),
'skewness': self.skewness(),
'kurtosis': self.kurtosis()
}
return stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment