New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add overlap() method to statistics.NormalDist() #80350
Comments
------ How to use it ------ What percentage of men and women will have the same height in two normally distributed populations with known means and standard deviations? # http://www.usablestats.com/lessons/normal
>>> men = NormalDist(70, 4)
>>> women = NormalDist(65, 3.5)
>>> men.overlap(women)
0.5028719270195425 The result can be confirmed empirically with a Monte Carlo simulation: >>> from collections import Counter
>>> n = 100_000
>>> overlap = Counter(map(round, men.samples(n))) & Counter(map(round, women.samples(n)))
>>> sum(overlap.values()) / n
0.50349 The result can also be confirmed by numeric integration of the probability density function: >>> dx = 0.10
>>> heights = [h * dx for h in range(500, 860)]
>>> sum(min(men.pdf(h), women.pdf(h)) for h in heights) * dx
0.5028920586287203 ------ Code ------ def overlap(self, other):
'''Compute the overlap coefficient (OVL) between two normal distributions.
if not isinstance(other, NormalDist):
return NotImplemented
X, Y = self, other
X_var, Y_var = X.variance, Y.variance
if not X_var or not Y_var:
raise StatisticsError('overlap() not defined when sigma is zero')
dv = Y_var - X_var
if not dv:
return 2.0 * NormalDist(fabs(Y.mu - X.mu), 2.0 * X.sigma).cdf(0)
a = X.mu * Y_var - Y.mu * X_var
b = X.sigma * Y.sigma * sqrt((X.mu - Y.mu)**2 + dv * log(Y_var / X_var))
x1 = (a + b) / dv
x2 = (a - b) / dv
return 1.0 - (fabs(Y.cdf(x1) - X.cdf(x1)) + fabs(Y.cdf(x2) - X.cdf(x2))) ---- Future ---- The concept of an overlap coefficient (OVL) is not specific to normal distributions, so it is possible to extend this idea to work with other distributions if needed. |
Another cross-check can be had with this nomogram: https://www.rasch.org/rmt/rmt101r.htm |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: