BoxCox Transformation

This commit is contained in:
Petrônio Cândido 2017-10-25 10:09:28 -02:00
parent e9b86d7025
commit 2b038968f7

View File

@ -8,15 +8,14 @@ class Transformation(object):
Data transformation used to pre and post processing of the FTS Data transformation used to pre and post processing of the FTS
""" """
def __init__(self, parameters): def __init__(self, **kwargs):
self.isInversible = True self.is_invertible = True
self.parameters = parameters self.minimal_length = 1
self.minimalLength = 1
def apply(self,data,param,**kwargs): def apply(self, data, param, **kwargs):
pass pass
def inverse(self,data, param,**kwargs): def inverse(self,data, param, **kwargs):
pass pass
def __str__(self): def __str__(self):
@ -28,11 +27,11 @@ class Differential(Transformation):
Differentiation data transform Differentiation data transform
""" """
def __init__(self, parameters): def __init__(self, parameters):
super(Differential, self).__init__(parameters) super(Differential, self).__init__()
self.lag = parameters self.lag = parameters
self.minimalLength = 2 self.minimal_length = 2
def apply(self, data, param=None,**kwargs): def apply(self, data, param=None, **kwargs):
if param is not None: if param is not None:
self.lag = param self.lag = param
@ -75,7 +74,7 @@ class Differential(Transformation):
class Scale(Transformation): class Scale(Transformation):
def __init__(self, min=0, max=1): def __init__(self, min=0, max=1):
super(Scale, self).__init__([min, max]) super(Scale, self).__init__()
self.data_max = None self.data_max = None
self.data_min = None self.data_min = None
self.transf_max = max self.transf_max = max
@ -130,12 +129,23 @@ class AdaptiveExpectation(Transformation):
return inc return inc
def boxcox(original, plambda): class BoxCox(Transformation):
n = len(original) def __init__(self, plambda):
if plambda != 0: super(BoxCox, self).__init__()
modified = [(original[t] ** plambda - 1) / plambda for t in np.arange(0, n)] self.plambda = plambda
def apply(self, data, param=None, **kwargs):
if self.plambda != 0:
modified = [(dat ** self.plambda - 1) / self.plambda for dat in data]
else: else:
modified = [math.log(original[t]) for t in np.arange(0, n)] modified = [np.log(dat) for dat in data]
return np.array(modified)
def inverse(self, data, param=None, **kwargs):
if self.plambda != 0:
modified = [np.exp(np.log(dat * self.plambda + 1) ) / self.plambda for dat in data]
else:
modified = [np.exp(dat) for dat in data]
return np.array(modified) return np.array(modified)