Solved – How to generate a Weibull distribution with inverse transform

cumulative distribution functionquantilesweibull distribution

Given $X\sim \text{Weibull}(\lambda,k)$, generate samples from the Weibull distribution using the inverse transform.

We know $F_X(x) = 1-\text{e}^{-(x/\lambda)^k}$ for $x\ge 0$ with $\lambda,k > 0$.

Best Answer

Start with the CDF, replace $F_X(x)$ with $U\sim U(0,1)$, $X$ for $x$, and solve for $X$.

$$\begin{align} U &= 1-\text{e}^{-(X/\lambda)^k} \\ 1-U &=\text{e}^{-(X/\lambda)^k} \\ -\text{ln}(1-U) &= \left(\frac{X}{\lambda}\right)^k \\ \left[-\text{ln}(1-U)\right]^{\frac{1}{k}} &= \frac{X}{\lambda} \\ X &= \lambda\left[-\text{ln}(1-U)\right]^{\frac{1}{k}} \quad \quad \square \end{align}$$

% MATLAB 2017a
% Inverse Transform for Weibull distribution
% Parameters
lambda = 1.5;
k = 2;
n = 1000;                             % number of samples to generate

% Generation
U = rand(n,1);                        % U ~ Uniform(0,1)
X = lambda*((-log(1-U)).^(1/k));      % X ~ Weibull(lambda,k)