How do I model handling times with an occasional very long one?¶
Manual work is usually quick, with now and then a job that takes far longer. A lognormal service time has that shape. The script below runs the same packing queue twice, once with a lognormal time and once with a constant of the same mean.
import math
from nagasim import build as nb
def wait_with(service):
m = nb.Model(time_limit=480, warmup=0)
m.add_resource(name="Packer")
arrive = m.create(distribution=nb.Exponential(rate=0.5))
arrive >> m.process(resource_name="Packer", distribution=service) >> m.dispose()
return m.run(replications=20, seed=1).kpi("resource/Packer/Time in Queue Avg")
# mu and sigma belong to the underlying normal, so the mean is exp(mu + sigma^2 / 2).
mu, sigma = 0.1, 0.8
mean = math.exp(mu + sigma ** 2 / 2)
print(f"lognormal(mu={mu}, sigma={sigma}) has mean {mean:.2f} minutes")
print(f"lognormal service: mean wait {wait_with(nb.Lognormal(mu=mu, sigma=sigma)):.2f} minutes")
print(f"constant service: mean wait {wait_with(nb.Constant(value=mean)):.2f} minutes")
Save it as recipe-lognormal-tail.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.
lognormal(mu=0.1, sigma=0.8) has mean 1.52 minutes
lognormal service: mean wait 3.72 minutes
constant service: mean wait 2.23 minutes
How it works¶
Lognormal(mu, sigma)takes the mean and spread of the logarithm of the time, so its own mean isexp(mu + sigma^2 / 2).- Both services average 1.52 minutes, but the lognormal queue waits longer, because the long jobs hold the packer while others line up.
- Use it when your data are positive and right-skewed; the fit recipe shows how to test that.
Elements used¶
Back to all recipes.