LogNormal

class surpyval.parametric.lognormal.LogNormal_(name)

Bases: surpyval.parametric.parametric_fitter.ParametricFitter

Hf(x, mu, sigma)

Cumulative hazard rate for the LogNormal Distribution:

\[H(x) = -\ln \left ( R(x) \right )\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

Hf – The value(s) of the cumulative hazard rate at x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> x = np.array([1, 2, 3, 4, 5])
>>> LogNormal.Hf(x, 3, 4)
array([0.25699427, 0.33137848, 0.3816556 , 0.4205543 , 0.45264333])
cs(x, X, mu, sigma)

Conditional survival function for the LogNormal Distribution:

\[R(x, X) = \frac{R(x + X)}{R(X)}\]
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
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

cs – the conditional survival probability at x

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> x = np.array([1, 2, 3, 4, 5])
>>> LogNormal.cs(x, 5, 3, 4)
array([0.97287811, 0.9496515 , 0.92933892, 0.91129122, 0.89505592])
df(x, mu, sigma)

Density function for the LogNormal Distribution:

\[f(x) = \frac{1}{x \sigma \sqrt{2\pi}}e^{-\frac{1}{2}\left ( \frac{\ln x - \mu}{\sigma} \right )^{2}}\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

df – The value(s) of the density function at x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> x = np.array([1, 2, 3, 4, 5])
>>> LogNormal.df(x, 3, 4)
array([0.07528436, 0.04222769, 0.02969364, 0.02298522, 0.01877747])
ff(x, mu, sigma)

Failure (CDF or unreliability) function for the LogNormal Distribution:

\[F(x) = \Phi \left( \frac{\ln(x) - \mu}{\sigma} \right )\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

ff – The value(s) of the failure function at x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> x = np.array([1, 2, 3, 4, 5])
>>> LogNormal.ff(x, 3, 4)
array([0.22662735, 0.28206661, 0.31726986, 0.34331728, 0.36405509])
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, mu, sigma)

Instantaneous hazard rate for the LogNormal Distribution:

\[h(x) = \frac{f(x)}{R(x)}\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

hf – The value(s) of the instantaneous hazard rate at x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> x = np.array([1, 2, 3, 4, 5])
>>> LogNormal.hf(x, 3, 4)
array([0.09734551, 0.05881839, 0.04349249, 0.03500202, 0.02952687])
mean(mu, sigma)

Quantile function for the LogNormal Distribution:

\[E = e^{\mu + \frac{\sigma^2}{2}}\]
Parameters:
  • p (numpy array or scalar) – The percentiles at which the quantile will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

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

Return type:

scalar or numpy array

Examples

>>> from surpyval import LogNormal
>>> LogNormal.mean(3, 4)
59874.14171519782
moment(n, mu, sigma)

n-th (non central) moment of the LogNormal distribution

\[E = ... complicated.\]
Parameters:
  • n (integer or numpy array of integers) – The ordinal of the moment to calculate
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

moment – The moment(s) of the LogNormal distribution

Return type:

scalar or numpy array

Examples

>>> from surpyval import LogNormal
>>> LogNormal.moment(2, 3, 4)
3.1855931757113756e+16
qf(p, mu, sigma)

Quantile function for the LogNormal Distribution:

\[q(p) = e^{\mu + \sigma \Phi^{-1} \left( p \right )}\]
Parameters:
  • p (numpy array or scalar) – The percentiles at which the quantile will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

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

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> p = np.array([.1, .2, .3, .4, .5])
>>> LogNormal.qf(p, 3, 4)
array([ 0.11928899,  0.69316658,  2.46550819,  7.29078766, 20.08553692])
random(size, mu, sigma)

Draws random samples from the distribution in shape size

Parameters:
  • size (integer or tuple of positive integers) – Shape or size of the random draw
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal 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 LogNormal
>>> LogNormal.random(10, 3, 4)
array([1.74605298e+00, 1.90729963e+02, 1.90090366e+03, 2.59154042e-02,
       3.71460694e-02, 3.38580771e+03, 7.58826512e+04, 7.23252303e+00,
       1.21226718e+03, 4.15054624e+00])
>>> LogNormal.random((5, 5), 3, 4)
array([[4.59689256e+00, 2.91472936e-01, 4.66833783e+02, 9.88539048e+01,
        3.88094471e+01],
       [7.10705735e-01, 5.00788529e-02, 2.49032431e+01, 2.19196376e+01,
        2.05043988e+02],
       [1.32193999e+03, 7.38943238e-01, 5.16503535e-01, 9.09249819e+02,
        2.69407879e+03],
       [7.29473033e+00, 5.68246498e+03, 1.74464896e+00, 1.26043004e+00,
        3.84009666e+03],
       [1.47997384e+00, 2.21809242e+02, 1.32564564e+02, 8.06883052e-02,
        1.05118538e+02]])
sf(x, mu, sigma)

Surival (or Reliability) function for the LogNormal Distribution:

\[R(x) = 1 - \Phi \left( \frac{\ln(x) - \mu}{\sigma} \right )\]
Parameters:
  • x (numpy array or scalar) – The values at which the function will be calculated
  • mu (numpy array or scalar) – The location parameter for the LogNormal distribution
  • sigma (numpy array or scalar) – The scale parameter for the LogNormal distribution
Returns:

sf – The value(s) of the survival function at x.

Return type:

scalar or numpy array

Examples

>>> import numpy as np
>>> from surpyval import LogNormal
>>> x = np.array([1, 2, 3, 4, 5])
>>> LogNormal.sf(x, 3, 4)
array([0.77337265, 0.71793339, 0.68273014, 0.65668272, 0.63594491])