Source code for statsmodels.sandbox.distributions.transform_functions

# -*- coding: utf-8 -*-
"""Nonlinear Transformation classes


Created on Sat Apr 16 16:06:11 2011

Author: Josef Perktold
License : BSD
"""
from __future__ import print_function
import numpy as np


[docs]class TransformFunction(object): def __call__(self, x): self.func(x)
## Hump and U-shaped functions
[docs]class SquareFunc(TransformFunction): '''class to hold quadratic function with inverse function and derivative using instance methods instead of class methods, if we want extension to parameterized function '''
[docs] def func(self, x): return np.power(x, 2.)
[docs] def inverseplus(self, x): return np.sqrt(x)
[docs] def inverseminus(self, x): return 0.0 - np.sqrt(x)
[docs] def derivplus(self, x): return 0.5/np.sqrt(x)
[docs] def derivminus(self, x): return 0.0 - 0.5/np.sqrt(x)
[docs]class NegSquareFunc(TransformFunction): '''negative quadratic function '''
[docs] def func(self, x): return -np.power(x,2)
[docs] def inverseplus(self, x): return np.sqrt(-x)
[docs] def inverseminus(self, x): return 0.0 - np.sqrt(-x)
[docs] def derivplus(self, x): return 0.0 - 0.5/np.sqrt(-x)
[docs] def derivminus(self, x): return 0.5/np.sqrt(-x)
[docs]class AbsFunc(TransformFunction): '''class for absolute value transformation '''
[docs] def func(self, x): return np.abs(x)
[docs] def inverseplus(self, x): return x
[docs] def inverseminus(self, x): return 0.0 - x
[docs] def derivplus(self, x): return 1.0
[docs] def derivminus(self, x): return 0.0 - 1.0
## monotonic functions # more monotone functions in families.links, some for restricted domains
[docs]class LogFunc(TransformFunction):
[docs] def func(self, x): return np.log(x)
[docs] def inverse(self, y): return np.exp(y)
[docs] def deriv(self, x): return 1./x
[docs]class ExpFunc(TransformFunction):
[docs] def func(self, x): return np.exp(x)
[docs] def inverse(self, y): return np.log(y)
[docs] def deriv(self, x): return np.exp(x)
[docs]class BoxCoxNonzeroFunc(TransformFunction):
[docs] def __init__(self, lamda): self.lamda = lamda
[docs] def func(self, x): return (np.power(x, self.lamda) - 1)/self.lamda
[docs] def inverse(self, y): return (self.lamda * y + 1)/self.lamda
[docs] def deriv(self, x): return np.power(x, self.lamda - 1)
[docs]class AffineFunc(TransformFunction):
[docs] def __init__(self, constant, slope): self.constant = constant self.slope = slope
[docs] def func(self, x): return self.constant + self.slope * x
[docs] def inverse(self, y): return (y - self.constant) / self.slope
[docs] def deriv(self, x): return self.slope
[docs]class ChainFunc(TransformFunction):
[docs] def __init__(self, finn, fout): self.finn = finn self.fout = fout
[docs] def func(self, x): return self.fout.func(self.finn.func(x))
[docs] def inverse(self, y): return self.f1.inverse(self.fout.inverse(y))
[docs] def deriv(self, x): z = self.finn.func(x) return self.fout.deriv(z) * self.finn.deriv(x)
#def inverse(x): # return np.divide(1.0,x) # #mux, stdx = 0.05, 0.1 #mux, stdx = 9.0, 1.0 #def inversew(x): # return 1.0/(1+mux+x*stdx) #def inversew_inv(x): # return (1.0/x - 1.0 - mux)/stdx #.np.divide(1.0,x)-10 # #def identit(x): # return x if __name__ == '__main__': absf = AbsFunc() absf.func(5) == 5 absf.func(-5) == 5 absf.inverseplus(5) == 5 absf.inverseminus(5) == -5 chainf = ChainFunc(AffineFunc(1,2), BoxCoxNonzeroFunc(2)) print(chainf.func(3.)) chainf2 = ChainFunc(BoxCoxNonzeroFunc(2), AffineFunc(1,2)) print(chainf.func(3.))