Skip to content
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

Closed
rhettinger opened this issue Mar 2, 2019 · 3 comments
Closed

Add overlap() method to statistics.NormalDist() #80350

rhettinger opened this issue Mar 2, 2019 · 3 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir

Comments

@rhettinger
Copy link
Contributor

BPO 36169
Nosy @tim-one, @rhettinger, @mdickinson, @stevendaprano, @applio
PRs
  • bpo-36169 : Add overlap() method to statistics.NormalDist #12149
  • 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:

    assignee = None
    closed_at = <Date 2019-03-07.07:00:03.569>
    created_at = <Date 2019-03-02.23:04:32.531>
    labels = ['3.8', 'library']
    title = 'Add overlap() method to statistics.NormalDist()'
    updated_at = <Date 2019-03-07.07:00:03.568>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2019-03-07.07:00:03.568>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-03-07.07:00:03.569>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2019-03-02.23:04:32.531>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36169
    keywords = ['patch']
    message_count = 3.0
    messages = ['337020', '337026', '337367']
    nosy_count = 5.0
    nosy_names = ['tim.peters', 'rhettinger', 'mark.dickinson', 'steven.daprano', 'davin']
    pr_nums = ['12149']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue36169'
    versions = ['Python 3.8']

    @rhettinger
    Copy link
    Contributor Author

    ------ 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.
        Measures the agreement between two normal probability distributions.
        Returns a value between 0.0 and 1.0 giving the overlapping area in
        the two underlying probability density functions.
    
        '''
    
        # See: "The overlapping coefficient as a measure of agreement between
        # probability distributions and point estimation of the overlap of two
        # normal densities" -- Henry F. Inman and Edwin L. Bradley Jr
        # http://dx.doi.org/10.1080/03610928908830127
    
        # Also see:
        # http://www.iceaaonline.com/ready/wp-content/uploads/2014/06/MM-9-Presentation-Meet-the-Overlapping-Coefficient-A-Measure-for-Elevator-Speeches.pdf
    
            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.

    @rhettinger rhettinger added 3.8 only security fixes stdlib Python modules in the Lib dir labels Mar 2, 2019
    @rhettinger
    Copy link
    Contributor Author

    Another cross-check can be had with this nomogram: https://www.rasch.org/rmt/rmt101r.htm

    @rhettinger
    Copy link
    Contributor Author

    New changeset 318d537 by Raymond Hettinger in branch 'master':
    bpo-36169 : Add overlap() method to statistics.NormalDist (GH-12149)
    318d537

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant