Skip to content

Instantly share code, notes, and snippets.

@sakethramanujam
Last active September 19, 2020 14:41
Show Gist options
  • Save sakethramanujam/eaef95239edcb6ded213951f0773a560 to your computer and use it in GitHub Desktop.
Save sakethramanujam/eaef95239edcb6ded213951f0773a560 to your computer and use it in GitHub Desktop.
TBD
import math
import pandas as pd
import numpy as np
from typing import List, Tuple, Union, Dict
from scipy.stats import skew, kurtosis
from .errors import StepSizeError, StepTypeError, SeriesError
class Statistics:
"""
Methods to calculate statistics all or one at a time
Arguments
---------
overlap: float
0 -> 0.99
"""
def __init__(self, series: list, step: int, overlap: float= None):
self.step = step
self.series = series
self.length = len(self.series)
self.overlap = overlap
self.init()
def init(self):
if not self.overlap:
self.stride = self.step
else:
self.stride = int(self.step*self.overlap)
def mean(self)-> Union[int, list]:
"""
Given a series, calculate its mean
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
average = np.mean(series)
return average
else:
averages = []
for i in range(0, length, stride):
temp = series[i:i+step]
average = np.mean(temp)
averages.append(average)
return averages
def deviation(self)-> Union[int, list]:
"""
Given a series, calculate its standard deviation
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
std = np.std(series)
return std
else:
standard_deviations = []
for i in range(0, length, stride):
temp = series[i:i+step]
deviation = np.std(temp)
standard_deviations.append(deviation)
return standard_deviations
def variance(self)-> Union[int, list]:
"""
Given a series, calculate its variance
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
variances = np.var(series)
return variances
else:
variances = []
for i in range(0, length, stride):
temp = series[i:i+step]
variance = np.var(temp)
variances.append(variance)
return variances
def skewness(self)-> Union[int, list]:
"""
Given a series, calculate its skewness
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
skewness = skew(series)
return skewness
else:
skewnesses = []
for i in range(0, length, stride):
temp = series[i:i+step]
skewness = skew(temp)
skewnesses.append(skewness)
return skewnesses
def kurtosis(self)-> Union[int, list]:
"""
Given a series, calculate its kurtosis
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
kurt = kurtosis(series)
return kurt
else:
kurts = []
for i in range(0, length, stride):
temp = series[i:i+step]
kurt = kurtosis(temp)
kurts.append(kurt)
return kurts
def minimum(self)-> Union[int, list]:
"""
Given a series, calculate its minimums
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
minimum = min(series)
return minimum
else:
minimums = []
for i in range(0, length, stride):
temp = series[i:i+step]
minimum = min(temp)
minimums.append(minimum)
return minimums
def maximum(self) -> Union[int, list]:
"""
Given a series, calculate its maximums
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
return max(series)
else:
maximums = []
for i in range(0, length, stride):
temp = series[i:i+step]
maxval = max(temp)
maximums.append(maxval)
return maximums
def rangeval(self)-> Union[int, list]:
"""
Given a series, calculate its ranges
"""
step = self.step
length = self.length
series = self.series
stride = self.stride
if step is (None or 0 or length):
rangevalue = max(series) - min(series)
return rangevalue
rangevals = []
for i in range(0, length, stride):
temp = series[i:i+step]
rangevalue = max(temp)-min(temp)
rangevals.append(rangevalue)
return rangevals
def all(self) -> Dict[str, object]:
"""
Given a series, calculate all stats at one go
"""
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