Exponential

class surpyval.parametric.exponential.Exponential_(name)

Bases: surpyval.parametric.parametric_fitter.ParametricFitter

Class used to generate the Exponential class.

from surpyval import Exponential
Hf(x, failure_rate)

Cumulative hazard rate for the Exponential Distribution.

\[f(x) = \lambda x\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

hf – The cumulative hazard rate of the Exponential distribution for each value of x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> x = np.array([1, 2, 3, 4, 5])
>>> Exponential.Hf(x, 3)
array([ 3,  6,  9, 12, 15])
cs(x, X, failure_rate)

Conditional survival function for the Exponential Distribution:

\[R(x) = e^{-\lambda x}\]

The Exponential distribution is memoryless, and hence is the same as the regular survival distribution.

Parameters:
  • x (numpy array or scalar) – The value(s) at which the function will be calculated
  • X (numpy array or scalar) – The value(s) at which each value(s) in x was known to have survived
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

cs – the conditional survival probability.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> x = np.array([1, 2, 3, 4, 5])
>>> Exponential.cs(x, 5, 3)
array([4.97870684e-02, 2.47875218e-03, 1.23409804e-04, 6.14421235e-06,
       3.05902321e-07])
df(x, failure_rate)

Density function for the Exponential Distribution:

\[f(x) = \lambda e^{-\lambda x}\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

df – The density of the Exponential distribution for each value of x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> x = np.array([1, 2, 3, 4, 5])
>>> Exponential.df(x, 3)
array([1.49361205e-01, 7.43625653e-03, 3.70229412e-04, 1.84326371e-05,
       9.17706962e-07])
entropy(failure_rate)

Calculates the entropy of the Exponential distribution.

\[S = 1 - \ln \left ( \lambda \right )\]
Parameters:failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:entropy – The entropy(ies) of the Exponential distribution
Return type:scalar or numpy array

Examples

>>> from surpyval import Exponential
>>> Exponential.entropy(3)
-0.09861228866810978
ff(x, failure_rate)

CDF (or unreliability or failure) function for the Exponential Distribution:

\[F(x) = 1 - e^{-\lambda x}\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

ff – The value(s) of the CDF for each value of x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> x = np.array([1, 2, 3, 4, 5])
>>> Exponential.ff(x, 3)
array([0.95021293, 0.99752125, 0.99987659, 0.99999386, 0.99999969])
fit(x=None, c=None, n=None, t=None, how='MLE', offset=False, zi=False, lfp=False, tl=None, tr=None, xl=None, xr=None, fixed=None, heuristic='Turnbull', init=[], rr='y', on_d_is_0=False, turnbull_estimator='Fleming-Harrington')

The central feature to SurPyval’s capability. This function aimed to have an API to mimic the simplicity of the scipy API. That is, to use a simple fit() call, with as many or as few parameters as is needed.

Parameters:
  • x (array like, optional) – Array of observations of the random variables. If x is None, xl and xr must be provided.
  • c (array like, optional) – Array of censoring flag. -1 is left censored, 0 is observed, 1 is right censored, and 2 is intervally censored. If not provided will assume all values are observed.
  • n (array like, optional) – Array of counts for each x. If data is proivded as counts, then this can be provided. If None will assume each observation is 1.
  • t (2D-array like, optional) – 2D array like of the left and right values at which the respective observation was truncated. If not provided it assumes that no truncation occurs.
  • how ({'MLE', 'MPP', 'MOM', 'MSE', 'MPS'}, optional) –

    Method to estimate parameters, these are:

    • MLE : Maximum Likelihood Estimation
    • MPP : Method of Probability Plotting
    • MOM : Method of Moments
    • MSE : Mean Square Error
    • MPS : Maximum Product Spacing
  • offset (boolean, optional) – If True finds the shifted distribution. If not provided assumes not a shifted distribution. Only works with distributions that are supported on the half-real line.
  • tl (array like or scalar, optional) – Values of left truncation for observations. If it is a scalar value assumes each observation is left truncated at the value. If an array, it is the respective ‘late entry’ of the observation
  • tr (array like or scalar, optional) – Values of right truncation for observations. If it is a scalar value assumes each observation is right truncated at the value. If an array, it is the respective right truncation value for each observation
  • xl (array like, optional) – Array like of the left array for 2-dimensional input of x. This is useful for data that is all intervally censored. Must be used with the xr input.
  • xr (array like, optional) – Array like of the right array for 2-dimensional input of x. This is useful for data that is all intervally censored. Must be used with the xl input.
  • fixed (dict, optional) – Dictionary of parameters and their values to fix. Fixes parameter by name.
  • heuristic ({'"Blom", "Median", "ECDF", "Modal", "Midpoint", "Mean", "Weibull", "Benard", "Beard", "Hazen", "Gringorten", "None", "Tukey", "DPW", "Fleming-Harrington", "Kaplan-Meier", "Nelson-Aalen", "Filliben", "Larsen", "Turnbull"}) – Plotting method to use, if using the probability plotting, MPP, method.
  • init (array like, optional) – initial guess of parameters. Useful if method is failing.
  • rr (('y', 'x')) – The dimension on which to minimise the spacing between the line and the observation. If ‘y’ the mean square error between the line and vertical distance to each point is minimised. If ‘x’ the mean square error between the line and horizontal distance to each point is minimised.
  • on_d_is_0 (boolean, optional) – For the case when using MPP and the highest value is right censored, you can choosed to include this value into the regression analysis or not. That is, if False, all values where there are 0 deaths are excluded from the regression. If True all values regardless of whether there is a death or not are included in the regression.
  • turnbull_estimator (('Nelson-Aalen', 'Kaplan-Meier', or 'Fleming-Harrington'), str, optional) – If using the Turnbull heuristic, you can elect to use either the KM, NA, or FH estimator with the Turnbull estimates of r, and d. Defaults to FH.
Returns:

model – A parametric model with the fitted parameters and methods for all functions of the distribution using the fitted parameters.

Return type:

Parametric

Examples

>>> from surpyval import Weibull
>>> import numpy as np
>>> x = Weibull.random(100, 10, 4)
>>> model = Weibull.fit(x)
>>> print(model)
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : MLE
Parameters          :
     alpha: 10.551521182640098
      beta: 3.792549834495306
>>> Weibull.fit(x, how='MPS', fixed={'alpha' : 10})
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : MPS
Parameters          :
     alpha: 10.0
      beta: 3.4314657446866836
>>> Weibull.fit(xl=x-1, xr=x+1, how='MPP')
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : MPP
Parameters          :
     alpha: 9.943092756713078
      beta: 8.613016934518258
>>> c = np.zeros_like(x)
>>> c[x > 13] = 1
>>> x[x > 13] = 13
>>> c = c[x > 6]
>>> x = x[x > 6]
>>> Weibull.fit(x=x, c=c, tl=6)
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : MLE
Parameters          :
     alpha: 10.363725328793413
      beta: 4.9886821457305865
fit_from_df(df, x=None, c=None, n=None, xl=None, xr=None, tl=None, tr=None, **fit_options)

The central feature to SurPyval’s capability. This function aimed to have an API to mimic the simplicity of the scipy API. That is, to use a simple fit() call, with as many or as few parameters as is needed.

Parameters:
  • df (DataFrame) – DataFrame of data to be used to create surpyval model
  • x (string, optional) – column name for the column in df containing the variable data. If not provided must provide both xl and xr
  • c (string, optional) – column name for the column in df containing the censor flag of x. If not provided assumes all values of x are observed.
  • n (string, optional) – column name in for the column in df containing the counts of x. If not provided assumes each x is one observation.
  • tl (string or scalar, optional) – If string, column name in for the column in df containing the left truncation data. If scalar assumes each x is left truncated by that value. If not provided assumes x is not left truncated.
  • tr (string or scalar, optional) – If string, column name in for the column in df containing the right truncation data. If scalar assumes each x is right truncated by that value. If not provided assumes x is not right truncated.
  • xl (string, optional) – column name for the column in df containing the left interval for interval censored data. If left interval is -Inf, assumes left censored. If xl[i] == xr[i] assumes observed. Cannot be provided with x, must be provided with xr.
  • xr (string, optional) – column name for the column in df containing the right interval for interval censored data. If right interval is Inf, assumes right censored. If xl[i] == xr[i] assumes observed. Cannot be provided with x, must be provided with xl.
  • fit_options (dict, optional) – dictionary of fit options that will be passed to the fit method, see that method for options.
Returns:

model – A parametric model with the fitted parameters and methods for all functions of the distribution using the fitted parameters.

Return type:

Parametric

Examples

>>> import surpyval as surv
>>> df = surv.datasets.BoforsSteel.df
>>> model = surv.Weibull.fit_from_df(df, x='x', n='n', offset=True)
>>> print(model)
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : MLE
Offset (gamma)      : 39.76562962867477
Parameters          :
     alpha: 7.141925216146524
      beta: 2.6204524040137844
from_params(params, gamma=None, p=None, f0=None)

Creating a SurPyval Parametric class with provided parameters.

Parameters:
  • params (array like) – array of the parameters of the distribution.
  • gamma (scalar, optional) – offset value for the distribution. If not provided will fit a regular, unshifted/not offset, distribution.
Returns:

model – A parametric model with the fitted parameters and methods for all functions of the distribution using the fitted parameters.

Return type:

Parametric

Examples

>>> from surpyval import Weibull
>>> model = Weibull.from_params([10, 4])
>>> print(model)
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : given parameters
Parameters          :
     alpha: 10
      beta: 4
>>> model = Weibull.from_params([10, 4], gamma=2)
>>> print(model)
Parametric SurPyval Model
=========================
Distribution        : Weibull
Fitted by           : given parameters
Offset (gamma)      : 2
Parameters          :
     alpha: 10
      beta: 4
hf(x, failure_rate)

Instantaneous hazard rate for the Exponential Distribution.

\[f(x) = \lambda\]

The failure rate for the exponential distribution is constant. So this function only returns the input failure rate in the same shape as x.

Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

hf – The instantaneous hazard rate of the Exponential distribution for each value of x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> x = np.array([1, 2, 3, 4, 5])
>>> Exponential.hf(x, 3)
array([3, 3, 3, 3, 3])
mean(failure_rate)

Calculates the mean of the Exponential distribution with given parameters.

\[E = \frac{1}{\lambda}\]
Parameters:failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:mean – The mean(s) of the Exponential distribution
Return type:scalar or numpy array

Examples

>>> from surpyval import Exponential
>>> Exponential.mean(3)
0.3333333333333333
moment(n, failure_rate)

Calculates the n-th moment of the Exponential distribution.

\[E = \frac{n!}{\lambda^{n}}\]
Parameters:
  • n (integer or numpy array of integers) – The ordinal of the moment to calculate
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

moment – The moment(s) of the Exponential distribution

Return type:

scalar or numpy array

Examples

>>> from surpyval import Exponential
>>> Exponential.moment(2, 3)
0.2222222222222222
qf(p, failure_rate)

Quantile function for the Exponential Distribution:

\[q(p) = \frac{-\ln\left ( p \right )}{\lambda}\]
Parameters:
  • p (numpy array or scalar) – The percentiles at which the quantile will be calculated
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

q – The quantiles for the Exponential distribution at each value p.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> p = np.array([.1, .2, .3, .4, .5])
>>> Exponential.qf(p, 3)
array([0.76752836, 0.5364793 , 0.40132427, 0.30543024, 0.23104906])
random(size, failure_rate)

Draws random samples from the distribution in shape size

Parameters:
  • size (integer or tuple of positive integers) – Shape or size of the random draw
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

random – Random values drawn from the distribution in shape size

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> Exponential.random(10, 3)
array([0.32480264, 0.03186663, 0.41807108, 0.74221745, 0.06133774,
       0.2128422 , 0.36299424, 0.12250138, 0.61431089, 0.02266754])
>>> Exponential.random((5, 5), 3)
array([[0.25425552, 0.16867629, 0.21692401, 0.07020826, 0.03676643],
       [0.65528908, 0.20774767, 0.00625475, 0.04122388, 0.07089254],
       [1.22844679, 0.36199751, 0.564159  , 1.86811492, 0.08132478],
       [0.33541878, 0.38614518, 0.09285907, 0.33422975, 0.32515494],
       [0.03529228, 0.63134988, 0.45528738, 0.05037512, 0.7338039 ]])
sf(x, failure_rate)

Surival (or Reliability) function for the Exponential Distribution:

\[R(x) = e^{-\lambda x}\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • failure_rate (numpy array or scalar) – The scale parameter for the Exponential distribution
Returns:

sf – The scalar value of the survival function of the distribution if a scalar was passed. If an array like object was passed then a numpy array is returned with the value of the survival function at each corresponding value in the input array.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import Exponential
>>> x = np.array([1, 2, 3, 4, 5])
>>> Exponential.sf(x, 3)
array([4.97870684e-02, 2.47875218e-03, 1.23409804e-04, 6.14421235e-06,
       3.05902321e-07])