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

statistics.geometric_mean gives incorrect results for mixed int/float inputs #72514

Closed
mdickinson opened this issue Oct 1, 2016 · 6 comments
Closed
Assignees
Labels
3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error

Comments

@mdickinson
Copy link
Member

BPO 28327
Nosy @mdickinson, @stevendaprano, @iritkatriel
Files
  • geometric_mean_int_float.patch
  • geometric_mean_int_float_v2.patch
  • 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 = 'https://github.com/stevendaprano'
    closed_at = <Date 2021-08-20.10:02:48.549>
    created_at = <Date 2016-10-01.12:18:35.892>
    labels = ['type-bug', '3.7']
    title = 'statistics.geometric_mean gives incorrect results for mixed int/float inputs'
    updated_at = <Date 2021-08-20.10:02:48.548>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2021-08-20.10:02:48.548>
    actor = 'mark.dickinson'
    assignee = 'steven.daprano'
    closed = True
    closed_date = <Date 2021-08-20.10:02:48.549>
    closer = 'mark.dickinson'
    components = []
    creation = <Date 2016-10-01.12:18:35.892>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['44906', '44908']
    hgrepos = []
    issue_num = 28327
    keywords = ['patch']
    message_count = 6.0
    messages = ['277808', '277809', '277814', '277815', '399948', '399951']
    nosy_count = 3.0
    nosy_names = ['mark.dickinson', 'steven.daprano', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28327'
    versions = ['Python 3.7']

    @mdickinson
    Copy link
    Member Author

    The following calculations should all be giving the same result:

    >>> import statistics
    >>> statistics.geometric_mean([2, 3, 5, 7])
    3.80675409583932
    >>> statistics.geometric_mean([2, 3, 5, 7.0])
    1.6265765616977859
    >>> statistics.geometric_mean([2, 3, 5.0, 7.0])
    2.4322992790977875
    >>> statistics.geometric_mean([2, 3.0, 5.0, 7.0])
    3.201085872943679
    >>> statistics.geometric_mean([2.0, 3.0, 5.0, 7.0])
    3.80675409583932

    (Correct result is 3.80675409583932.)

    The culprit is this line in statistics._product:

    mant, scale = 1, 0  #math.frexp(prod)  # FIXME
    

    ... and indeed, we should be starting from prod rather than 1 here. But simply using math.frexp has potential for failure if the accumulated integer product overflows a float.

    @mdickinson mdickinson added the 3.7 (EOL) end of life label Oct 1, 2016
    @mdickinson mdickinson added the type-bug An unexpected behavior, bug, or error label Oct 1, 2016
    @mdickinson
    Copy link
    Member Author

    Here's a fix. I was planning to add tests, but as far as I can tell geometric_mean currently has no tests at all. Steve, is that correct? That seems like something that should be fixed before the 3.6 release.

    @stevendaprano
    Copy link
    Member

    Looks good for me.

    Thanks for catching this: I knew it was a bug, but then I ran into the issue that I could no longer build 3.6 before I could fix it, and between that and various issues in the real world I never got back to this.

    @mdickinson
    Copy link
    Member Author

    New patch, with a (very slightly) cleaner implementation of _frexp_gen.

    @iritkatriel
    Copy link
    Member

    I can't reproduce this now:

    >>> statistics.geometric_mean([2, 3, 5, 7])
    3.80675409583932
    >>> statistics.geometric_mean([2, 3, 5, 7.0])
    3.80675409583932
    >>> statistics.geometric_mean([2, 3, 5.0, 7.0])
    3.80675409583932
    >>> statistics.geometric_mean([2, 3.0, 5.0, 7.0])
    3.80675409583932
    >>> statistics.geometric_mean([2.0, 3.0, 5.0, 7.0])
    3.80675409583932
    >>>

    The current geometric_mean was added in PR12638. Is this issue about a previous version?

    @mdickinson
    Copy link
    Member Author

    Is this issue about a previous version?

    Yep. Sorry for failing to close this earlier.

    @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.7 (EOL) end of life type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants