Logistic Distribution
The logistic distribution looks like a normal distribution, a symmetric bell, but with heavier tails: extreme values are somewhat more likely. Its cumulative distribution function is the logistic (sigmoid) function, the S-shaped curve at the heart of logistic regression and of many neural networks. It is also used to model growth processes and in the Elo rating system for chess.
| Parameter | Meaning | Default |
|---|---|---|
loc |
mean (centre of the bell) | 0.0 |
scale |
spread | 1.0 |
size |
output shape | one value |
Generating logistic data
import numpy as np
rng = np.random.default_rng(42)
print(rng.logistic(loc=0, scale=1, size=6).round(3))
print(rng.logistic(loc=50, scale=4, size=(2, 3)).round(1))[ 1.231 -0.246 1.804 0.835 -2.264 3.689]
[[54.6 55.2 42.3]
[49.2 47.9 60.2]]
Note that scale is not the standard deviation here. The standard deviation of a logistic distribution is scale · π / √3, about 1.81 × scale.
import numpy as np
rng = np.random.default_rng(42)
x = rng.logistic(10, 2, size=200_000)
print(f"mean {x.mean():.3f}")
print(f"std {x.std():.3f} (theory {2 * np.pi / np.sqrt(3):.3f})")mean 10.000
std 3.632 (theory 3.628)
Logistic vs normal
To compare fairly, give both distributions the same mean and the same standard deviation. The logistic one has a sharper peak and fatter tails:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
rng = np.random.default_rng(42)
scale = 1.0
sd = scale * np.pi / np.sqrt(3) # same std for both
logi = rng.logistic(0, scale, 100_000)
norm = rng.normal(0, sd, 100_000)
xs = np.linspace(-8, 8, 400)
plt.figure(figsize=(6, 3.5))
plt.plot(xs, gaussian_kde(norm[:20_000])(xs), label="normal", color="#4dabcf")
plt.plot(xs, gaussian_kde(logi[:20_000])(xs), label="logistic", color="#e07a5f")
plt.legend()
plt.tight_layout()
plt.show()
print(f"share beyond 3 std: normal {np.mean(abs(norm) > 3 * sd):.4f}, "
f"logistic {np.mean(abs(logi) > 3 * sd):.4f}")share beyond 3 std: normal 0.0027, logistic 0.0090
The printed line quantifies the "heavier tails": values more than three standard deviations from the centre happen about three times as often in the logistic distribution as in the normal one.
The sigmoid connection
The share of logistic values below x is given exactly by the sigmoid 1 / (1 + e^(−(x − loc)/scale)). We can check the empirical cumulative share against it:
import numpy as np
rng = np.random.default_rng(42)
sample = rng.logistic(0, 1, 100_000)
def sigmoid(z):
return 1 / (1 + np.exp(-z))
for x in (-2, 0, 1, 3):
print(f"x={x:>2}: simulated {np.mean(sample < x):.3f} sigmoid {sigmoid(x):.3f}")x=-2: simulated 0.119 sigmoid 0.119
x= 0: simulated 0.497 sigmoid 0.500
x= 1: simulated 0.731 sigmoid 0.731
x= 3: simulated 0.952 sigmoid 0.953
In logistic regression, the model computes a score and passes it through this sigmoid to obtain a probability between 0 and 1.
Test yourself
How does the logistic distribution differ from the normal distribution?
- It is not symmetric
- It has heavier tails
- It is discrete
- It has no mean
Which function is the cumulative distribution of the standard logistic distribution?
- The sine function
- The sigmoid function
- The exponential function
- The square root
In rng.logistic(loc=0, scale=2), is scale the standard deviation?
- Yes
- No, the std is scale x pi / sqrt(3)
- No, the std is scale squared
- No, scale is the mean