Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fit(self, data=None):
if data==None and hasattr(self, 'parent_Fit'):
data = self.parent_Fit.data
data = trim_to_range(data, xmin=self.xmin, xmax=self.xmax)
self.n = len(data)
from numpy import log, sum
if not self.discrete and not self.xmax:
self.alpha = 1 + ( self.n / sum( log( data / self.xmin ) ))
if not self.in_range():
Distribution.fit(self, data, suppress_output=True)
self.KS(data)
elif self.discrete and self.estimate_discrete and not self.xmax:
self.alpha = 1 + ( self.n / sum( log( data / ( self.xmin - .5 ) ) ))
if not self.in_range():
Distribution.fit(self, data, suppress_output=True)
self.KS(data)
else:
Distribution.fit(self, data, suppress_output=True)
if not self.in_range():
self.noise_flag=True
else:
self.noise_flag=False
#Simplified so as not to throw a nan from infs being divided by each other
from sys import float_info
from numpy import inf
loglikelihoods[loglikelihoods==-inf] = log(10**float_info.min_10_exp)
else:
loglikelihoods = Distribution.loglikelihoods(self, data)
return loglikelihoods
def _generate_random_continuous(self, r):
from numpy import log
# return ( (self.xmin**self.beta) -
# (1/self.Lambda) * log(1-r) )**(1/self.beta)
return (1/self.Lambda)* ( (self.Lambda*self.xmin)**self.beta -
log(1-r) )**(1/self.beta)
class Truncated_Power_Law(Distribution):
def parameters(self, params):
self.alpha = params[0]
self.parameter1 = self.alpha
self.parameter1_name = 'alpha'
self.Lambda = params[1]
self.parameter2 = self.Lambda
self.parameter2_name = 'lambda'
@property
def name(self):
return "truncated_power_law"
def _initial_parameters(self, data):
from numpy import log, sum, mean
alpha = 1 + len(data)/sum( log( data / (self.xmin) ))
from numpy import log
# likelihoods = exp(-Lambda*data)*\
# Lambda*exp(Lambda*xmin)
loglikelihoods = log(self.Lambda) + (self.Lambda*(self.xmin-data))
#Simplified so as not to throw a nan from infs being divided by each other
from sys import float_info
loglikelihoods[loglikelihoods==0] = log(10**float_info.min_10_exp)
else:
loglikelihoods = Distribution.loglikelihoods(self, data)
return loglikelihoods
def _generate_random_continuous(self, r):
from numpy import log
return self.xmin - (1/self.Lambda) * log(1-r)
class Streched_Exponential(Distribution):
def parameters(self, params):
self.Lambda = params[0]
self.parameter1 = self.Lambda
self.parameter1_name = 'lambda'
self.beta = params[1]
self.parameter2 = self.beta
self.parameter2_name = 'beta'
@property
def name(self):
return "stretched_exponential"
def _initial_parameters(self, data):
from numpy import mean
return (1/mean(data), 1)
def pdf(self, data=None):
if data==None and hasattr(self, 'parent_Fit'):
data = self.parent_Fit.data
if not self.discrete and self.in_range() and not self.xmax:
data = trim_to_range(data, xmin=self.xmin, xmax=self.xmax)
from numpy import exp
# likelihoods = exp(-Lambda*data)*\
# Lambda*exp(Lambda*xmin)
likelihoods = self.Lambda*exp(self.Lambda*(self.xmin-data))
#Simplified so as not to throw a nan from infs being divided by each other
from sys import float_info
likelihoods[likelihoods==0] = 10**float_info.min_10_exp
else:
likelihoods = Distribution.pdf(self, data)
return likelihoods
[self._double_search_discrete(R) for R in r]
).astype('float')
return x
def _double_search_discrete(self, r):
#Find a range from x1 to x2 that our random probability fits between
x2 = int(self.xmin)
while self.ccdf(data=[x2]) >= (1 - r):
x1 = x2
x2 = 2*x1
#Use binary search within that range to find the exact answer, up to
#the limit of being between two integers.
x = bisect_map(x1, x2, self.ccdf, 1-r)
return x
class Power_Law(Distribution):
def __init__(self, estimate_discrete=True, **kwargs):
self.estimate_discrete = estimate_discrete
Distribution.__init__(self, **kwargs)
def parameters(self, params):
self.alpha = params[0]
self.parameter1 = self.alpha
self.parameter1_name = 'alpha'
@property
def name(self):
return "power_law"
@property
def sigma(self):
def __init__(self, estimate_discrete=True, **kwargs):
self.estimate_discrete = estimate_discrete
Distribution.__init__(self, **kwargs)
if data==None and hasattr(self, 'parent_Fit'):
data = self.parent_Fit.data
if not self.discrete and self.in_range() and not self.xmax:
data = trim_to_range(data, xmin=self.xmin, xmax=self.xmax)
from numpy import log
loglikelihoods = (
log((data*self.Lambda)**(self.beta-1) *
self.beta * self. Lambda) +
(self.Lambda*self.xmin)**self.beta -
(self.Lambda*data)**self.beta)
#Simplified so as not to throw a nan from infs being divided by each other
from sys import float_info
from numpy import inf
loglikelihoods[loglikelihoods==-inf] = log(10**float_info.min_10_exp)
else:
loglikelihoods = Distribution.loglikelihoods(self, data)
return loglikelihoods
return likelihoods
def _generate_random_continuous(self, r):
def helper(r):
from numpy import log
from numpy.random import rand
while 1:
x = self.xmin - (1/self.Lambda) * log(1-r)
p = ( x/self.xmin )**-self.alpha
if rand()
@property
def _pdf_discrete_normalizer(self):
C = 1.0 - self._cdf_xmin
if self.xmax:
C -= 1 - self._cdf_base_function(self.xmax+1)
C = 1.0/C
return C
def _generate_random_continuous(self, r):
return self.xmin * (1 - r) ** (-1/(self.alpha - 1))
def _generate_random_discrete_estimate(self, r):
x = (self.xmin - 0.5) * (1 - r) ** (-1/(self.alpha - 1)) + 0.5
from numpy import around
return around(x)
class Exponential(Distribution):
def parameters(self, params):
self.Lambda = params[0]
self.parameter1 = self.Lambda
self.parameter1_name = 'lambda'
@property
def name(self):
return "exponential"
def _initial_parameters(self, data):
from numpy import mean
return 1/mean(data)
def _in_standard_parameter_range(self):
return self.Lambda>0