G07 Chapter Contents
G07 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG07BEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G07BEF computes maximum likelihood estimates for parameters of the Weibull distribution from data which may be right-censored.

## 2  Specification

 SUBROUTINE G07BEF ( CENS, N, X, IC, BETA, GAMMA, TOL, MAXIT, SEBETA, SEGAM, CORR, DEV, NIT, WK, IFAIL)
 INTEGER N, IC(*), MAXIT, NIT, IFAIL REAL (KIND=nag_wp) X(N), BETA, GAMMA, TOL, SEBETA, SEGAM, CORR, DEV, WK(N) CHARACTER(1) CENS

## 3  Description

G07BEF computes maximum likelihood estimates of the parameters of the Weibull distribution from exact or right-censored data.
For $n$ realisations, ${y}_{i}$, from a Weibull distribution a value ${x}_{i}$ is observed such that
 $xi≤yi.$
There are two situations:
 (a) exactly specified observations, when ${x}_{i}={y}_{i}$ (b) right-censored observations, known by a lower bound, when ${x}_{i}<{y}_{i}$.
The probability density function of the Weibull distribution, and hence the contribution of an exactly specified observation to the likelihood, is given by:
 $fx;λ,γ=λγxγ-1exp-λxγ, x>0, for ​λ,γ>0;$
while the survival function of the Weibull distribution, and hence the contribution of a right-censored observation to the likelihood, is given by:
 $Sx;λ,γ=exp-λ xγ, x> 0, for ​ λ ,γ> 0.$
If $d$ of the $n$ observations are exactly specified and indicated by $i\in D$ and the remaining $\left(n-d\right)$ are right-censored, then the likelihood function, $\text{Like ​}\left(\lambda ,\gamma \right)$ is given by
 $Likeλ,γ∝λγd ∏i∈Dxiγ-1 exp-λ∑i=1nxiγ .$
To avoid possible numerical instability a different parameterisation $\beta ,\gamma$ is used, with $\beta =\mathrm{log}\left(\lambda \right)$. The kernel log-likelihood function, $L\left(\beta ,\gamma \right)$, is then:
 $Lβ,γ=dlogγ+dβ+γ-1∑i∈Dlogxi-eβ∑i=1nxiγ.$
If the derivatives $\frac{\partial L}{\partial \beta }$, $\frac{\partial L}{\partial \gamma }$, $\frac{{\partial }^{2}L}{{\partial \beta }^{2}}$, $\frac{{\partial }^{2}L}{\partial \beta \partial \gamma }$ and $\frac{{\partial }^{2}L}{{\partial \gamma }^{2}}$ are denoted by ${L}_{1}$, ${L}_{2}$, ${L}_{11}$, ${L}_{12}$ and ${L}_{22}$, respectively, then the maximum likelihood estimates, $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$, are the solution to the equations:
 $L1β^,γ^=0$ (1)
and
 $L2β^,γ^=0$ (2)
Estimates of the asymptotic standard errors of $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$ are given by:
 $seβ^=-L22 L11L22-L122 , seγ^=-L11 L11L22-L122 .$
An estimate of the correlation coefficient of $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$ is given by:
 $L12L12L22 .$
Note:  if an estimate of the original parameter $\lambda$ is required, then
 $λ^=expβ^ and seλ^=λ^seβ^.$
The equations (1) and (2) are solved by the Newton–Raphson iterative method with adjustments made to ensure that $\stackrel{^}{\gamma }>0.0$.

## 4  References

Gross A J and Clark V A (1975) Survival Distributions: Reliability Applications in the Biomedical Sciences Wiley
Kalbfleisch J D and Prentice R L (1980) The Statistical Analysis of Failure Time Data Wiley

## 5  Parameters

1:     CENS – CHARACTER(1)Input
On entry: indicates whether the data is censored or non-censored.
${\mathbf{CENS}}=\text{'N'}$
Each observation is assumed to be exactly specified. IC is not referenced.
${\mathbf{CENS}}=\text{'C'}$
Each observation is censored according to the value contained in ${\mathbf{IC}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{CENS}}=\text{'N'}$ or $\text{'C'}$.
2:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge 1$.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i}\right)$ contains the $\mathit{i}$th observation, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{X}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,n$.
4:     IC($*$) – INTEGER arrayInput
Note: the dimension of the array IC must be at least ${\mathbf{N}}$ if ${\mathbf{CENS}}=\text{'C'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{CENS}}=\text{'C'}$, then ${\mathbf{IC}}\left(\mathit{i}\right)$ contains the censoring codes for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{IC}}\left(i\right)=0$, the $i$th observation is exactly specified.
If ${\mathbf{IC}}\left(i\right)=1$, the $i$th observation is right-censored.
If ${\mathbf{CENS}}=\text{'N'}$, then IC is not referenced.
Constraint: if ${\mathbf{CENS}}=\text{'C'}$, then ${\mathbf{IC}}\left(\mathit{i}\right)=0$ or $1$, for $\mathit{i}=1,2,\dots ,n$.
5:     BETA – REAL (KIND=nag_wp)Output
On exit: the maximum likelihood estimate, $\stackrel{^}{\beta }$, of $\beta$.
6:     GAMMA – REAL (KIND=nag_wp)Input/Output
On entry: indicates whether an initial estimate of $\gamma$ is provided.
If ${\mathbf{GAMMA}}>0.0$, it is taken as the initial estimate of $\gamma$ and an initial estimate of $\beta$ is calculated from this value of $\gamma$.
If ${\mathbf{GAMMA}}\le 0.0$, then initial estimates of $\gamma$ and $\beta$ are calculated, internally, providing the data contains at least two distinct exact observations. (If there are only two distinct exact observations, then the largest observation must not be exactly specified.) See Section 8 for further details.
On exit: contains the maximum likelihood estimate, $\stackrel{^}{\gamma }$, of $\gamma$.
7:     TOL – REAL (KIND=nag_wp)Input
On entry: the relative precision required for the final estimates of $\beta$ and $\gamma$. Convergence is assumed when the absolute relative changes in the estimates of both $\beta$ and $\gamma$ are less than TOL.
If ${\mathbf{TOL}}=0.0$, then a relative precision of $0.000005$ is used.
Constraint:  or ${\mathbf{TOL}}=0.0$.
8:     MAXIT – INTEGERInput
On entry: the maximum number of iterations allowed.
If ${\mathbf{MAXIT}}\le 0$, then a value of $25$ is used.
9:     SEBETA – REAL (KIND=nag_wp)Output
On exit: an estimate of the standard error of $\stackrel{^}{\beta }$.
10:   SEGAM – REAL (KIND=nag_wp)Output
On exit: an estimate of the standard error of $\stackrel{^}{\gamma }$.
11:   CORR – REAL (KIND=nag_wp)Output
On exit: an estimate of the correlation between $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$.
12:   DEV – REAL (KIND=nag_wp)Output
On exit: the maximized kernel log-likelihood, $L\left(\stackrel{^}{\beta },\stackrel{^}{\gamma }\right)$.
13:   NIT – INTEGEROutput
On exit: the number of iterations performed.
14:   WK(N) – REAL (KIND=nag_wp) arrayWorkspace
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{CENS}}\ne \text{'N'}$ or $\text{'C'}$, or ${\mathbf{N}}<1$, or ${\mathbf{TOL}}<0.0$, or , or ${\mathbf{TOL}}>1.0$.
${\mathbf{IFAIL}}=2$
 On entry, the $i$th observation, ${\mathbf{X}}\left(i\right)\le 0.0$, for some $i=1,2,\dots ,n$, or the $i$th censoring code, ${\mathbf{IC}}\left(i\right)\ne 0$ or $1$, for some $i=1,2,\dots ,n$ and ${\mathbf{CENS}}=\text{'C'}$.
${\mathbf{IFAIL}}=3$
On entry, there are no exactly specified observations, or the routine was requested to calculate initial values and there are either less than two distinct exactly specified observations or there are exactly two and the largest observation is one of the exact observations.
${\mathbf{IFAIL}}=4$
The method has failed to converge in MAXIT iterations. You should increase TOL or MAXIT.
${\mathbf{IFAIL}}=5$
Process has diverged. The process is deemed divergent if three successive increments of $\beta$ or $\gamma$ increase or if the Hessian matrix of the Newton–Raphson process is singular. Either different initial estimates should be provided or the data should be checked to see if the Weibull distribution is appropriate.
${\mathbf{IFAIL}}=6$
A potential overflow has been detected. This is an unlikely exit usually caused by a large input estimate of $\gamma$.

## 7  Accuracy

Given that the Weibull distribution is a suitable model for the data and that the initial values are reasonable the convergence to the required accuracy, indicated by TOL, should be achieved.

The initial estimate of $\gamma$ is found by calculating a Kaplan–Meier estimate of the survival function, $\stackrel{^}{S}\left(x\right)$, and estimating the gradient of the plot of $\mathrm{log}\left(-\mathrm{log}\left(\stackrel{^}{S}\left(x\right)\right)\right)$ against $x$. This requires the Kaplan–Meier estimate to have at least two distinct points.
The initial estimate of $\stackrel{^}{\beta }$, given a value of $\stackrel{^}{\gamma }$, is calculated as
 $β^=logd∑i=1nxiγ^ .$

## 9  Example

In a study, $20$ patients receiving an analgesic to relieve headache pain had the following recorded relief times (in hours):
(See Gross and Clark (1975).) This data is read in and a Weibull distribution fitted assuming no censoring; the parameter estimates and their standard errors are printed.

### 9.1  Program Text

Program Text (g07befe.f90)

### 9.2  Program Data

Program Data (g07befe.d)

### 9.3  Program Results

Program Results (g07befe.r)