Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-090

Erlang PDF (Convolution of Exponentials)

Problem

The sum of n independent Exponential(lam) variables is Erlang distributed. Return its density at x:

f(x) = lam^n * x^(n - 1) * e^(-lam * x) / (n - 1)! for x >= 0.

Return 0.0 for nonpositive lam, n < 1 or x < 0.

Hint

Convolving exponential densities n times gives the Erlang shape.

Test cases

case 1
in: [1,1,1]
expected: 0.36787944117144233
case 2
in: [2,2,1]
expected: 0.5413411329464508
case 3
in: [1,3,2]
expected: 0.2706705664732254
case 4
in: [1,1,0]
expected: 1
Starter codePython
import math


def erlang_pdf(lam, n, x):
    # Your code here
    pass

4 test cases · run your code against them in the editor