# NAG CL Interfaceg07bec (estim_​weibull)

## 1Purpose

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

## 2Specification

 #include
 void g07bec (Nag_CestMethod cens, Integer n, const double x[], const Integer ic[], double *beta, double *gamma, double tol, Integer maxit, double *sebeta, double *segam, double *corr, double *dev, Integer *nit, NagError *fail)
The function may be called by the names: g07bec, nag_univar_estim_weibull or nag_estim_weibull.

## 3Description

g07bec computes maximum likelihood estimates of the parameters of the Weibull distribution from exact or right-censored data.
For $n$ realizations, ${y}_{i}$, from a Weibull distribution a value ${x}_{i}$ is observed such that
 $xi≤yi.$
There are two situations:
1. (a)exactly specified observations, when ${x}_{i}={y}_{i}$
2. (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$.
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

## 5Arguments

1: $\mathbf{cens}$Nag_CestMethod Input
On entry: indicates whether the data is censored or non-censored.
${\mathbf{cens}}=\mathrm{Nag_NoCensored}$
Each observation is assumed to be exactly specified. ic is not referenced.
${\mathbf{cens}}=\mathrm{Nag_Censored}$
Each observation is censored according to the value contained in ${\mathbf{ic}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{cens}}=\mathrm{Nag_NoCensored}$ or $\mathrm{Nag_Censored}$.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 1$.
3: $\mathbf{x}\left[{\mathbf{n}}\right]$const double Input
On entry: ${\mathbf{x}}\left[\mathit{i}-1\right]$ contains the $\mathit{i}$th observation, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
Constraint: ${\mathbf{x}}\left[\mathit{i}-1\right]>0.0$, for $\mathit{i}=1,2,\dots ,n$.
4: $\mathbf{ic}\left[\mathit{dim}\right]$const Integer Input
Note: the dimension, dim, of the array ic must be at least
• ${\mathbf{n}}$ when ${\mathbf{cens}}=\mathrm{Nag_Censored}$;
• $1$ otherwise.
On entry: if ${\mathbf{cens}}=\mathrm{Nag_Censored}$, ${\mathbf{ic}}\left[\mathit{i}-1\right]$ contains the censoring codes for the $\mathit{i}$th observation, for $\mathit{i}=1,2,\dots ,n$.
If ${\mathbf{ic}}\left[i-1\right]=0$, the $i$th observation is exactly specified.
If ${\mathbf{ic}}\left[i-1\right]=1$, the $i$th observation is right-censored.
If ${\mathbf{cens}}=\mathrm{Nag_NoCensored}$, ic is not referenced.
Constraint: if ${\mathbf{cens}}=\mathrm{Nag_Censored}$, then ${\mathbf{ic}}\left[\mathit{i}-1\right]=0$ or $1$, for $\mathit{i}=1,2,\dots ,n$.
5: $\mathbf{beta}$double * Output
On exit: the maximum likelihood estimate, $\stackrel{^}{\beta }$, of $\beta$.
6: $\mathbf{gamma}$double * 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$, 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, the largest observation must not be exactly specified.) See Section 9 for further details.
On exit: contains the maximum likelihood estimate, $\stackrel{^}{\gamma }$, of $\gamma$.
7: $\mathbf{tol}$double 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$, a relative precision of $0.000005$ is used.
Constraint: or ${\mathbf{tol}}=0.0$.
8: $\mathbf{maxit}$Integer Input
On entry: the maximum number of iterations allowed.
If ${\mathbf{maxit}}\le 0$, a value of $25$ is used.
9: $\mathbf{sebeta}$double * Output
On exit: an estimate of the standard error of $\stackrel{^}{\beta }$.
10: $\mathbf{segam}$double * Output
On exit: an estimate of the standard error of $\stackrel{^}{\gamma }$.
11: $\mathbf{corr}$double * Output
On exit: an estimate of the correlation between $\stackrel{^}{\beta }$ and $\stackrel{^}{\gamma }$.
12: $\mathbf{dev}$double * Output
On exit: the maximized kernel log-likelihood, $L\left(\stackrel{^}{\beta },\stackrel{^}{\gamma }\right)$.
13: $\mathbf{nit}$Integer * Output
On exit: the number of iterations performed.
14: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_CONVERGENCE
The chosen method has not converged in $〈\mathit{\text{value}}〉$ iterations. You should either increase tol or maxit.
NE_DIVERGENCE
The process has diverged. The process is deemed divergent if three successive increments of $\beta$ or $\gamma$ increase. Either different initial estimates should be provided or the data should be checked to see if the Weibull distribution is appropriate.
NE_INITIALIZATION
Unable to calculate initial values. This is due to there being either less than two distinct exactly specified observations or exactly two and the largest observation is one of the exact observations.
NE_INT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_INT_ARRAY_ELEM_CONS
On entry, $i=〈\mathit{\text{value}}〉$ and ${\mathbf{ic}}\left[i-1\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ic}}\left[i-1\right]=0$ or $1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_OBSERVATIONS
On entry, there are no exactly specified observations.
NE_OVERFLOW
Potential overflow detected. This is an unlikely error exit usually caused by a large input estimate of $\gamma$.
NE_REAL
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: or ${\mathbf{tol}}=0.0$.
NE_REAL_ARRAY_ELEM_CONS
On entry, $i=〈\mathit{\text{value}}〉$ and ${\mathbf{x}}\left[i-1\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}\left[i-1\right]>0.0$.
NE_SINGULAR
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.

## 7Accuracy

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.

## 8Parallelism and Performance

g07bec is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

## 9Further Comments

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γ^ .$

## 10Example

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.

### 10.1Program Text

Program Text (g07bece.c)

### 10.2Program Data

Program Data (g07bece.d)

### 10.3Program Results

Program Results (g07bece.r)