Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:59:05+00:00 2026-06-06T19:59:05+00:00

I’m having trouble understanding the documentation for SciPy’s scipy.stats.hypergeom functions. In my program, I

  • 0

I’m having trouble understanding the documentation for SciPy’s scipy.stats.hypergeom functions. In my program, I consider various decks of cards and try to find the probability of various draws. The hypergeom class seems to have exactly this, but its documentation assumes a bunch of terminology knowledge that I don’t have. Googling leads me to Wikipedia and Wolfram MathWorld, both of which assume that if you’re asking about this kind of thing, you’ve read everything from the dang Principia Mathematica forward and just need a little refresher – so they’re not actually helpful. Because this problem is “how do I apply this specific chunk of code to my problem?” I’m asking Stack Overflow.

I have a problem of the form “if you have a deck of N cards, M of which are the card of interest, what are the odds of having at least 1 copy of the card of interest in the top Q cards?” I also have a problem of the form “if you have a deck of N cards, M of which are the card of interest, how many cards must you draw from the deck to have a 90% chance of one of them being a copy of the card of interest?” The former problem is very close to the example problem given in the SciPy documentation, but it’s not the same thing, and the list of methods is all jargon to me – I can’t actually tell which of them is the one that I need. I also can’t tell which method to use for the latter type of problem.

What do the methods of scipy.stats.hypergeom actually do, what are their arguments, and how can I apply them to my problems? Pretend I’m a moderately bright high-school student and not a mathematics PhD candidate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:59:06+00:00Added an answer on June 6, 2026 at 7:59 pm
    scipy.stats.hypergeom.pmf(k, M, n, N)
    

    returns the probability that: from M cards, n of which are marked, if you randomly choose N cards without replacement, exactly k cards will be marked.

    So you can get your desired answer (using your variable-names) by

    def pick_Q(N, M, Q):
        """
        Given a deck of N cards, where M are marked,
        and Q cards are taken randomly without replacement,
        return the probability that at least one marked card is taken.
        """
        return sum(scipy.stats.hypergeom.pmf(k, N, M, Q) for k in xrange(1,Q+1))
    

    (the sum of the odds that 1 card is marked, 2 cards are marked, 3 cards are marked… N cards are marked).

    Luckily, there is a quicker way – the probability that at least one marked card is taken is the flip side of the probability that no marked card is picked. So instead you can do

    def pick_Q(N, M, Q):
        """
        Given a deck of N cards, where M are marked,
        and Q cards are taken randomly without replacement,
        return the probability that at least one marked card is taken.
        """
        return 1. - scipy.stats.hypergeom.pmf(0, N, M, Q)
    

    For your second question, there don’t appear to be any functions that do what you want; however, you can start with

    def how_many_to_pick(N, M, prob):
        """
        Given a deck of N cards, M of which are marked,
        how many do you have to pick randomly without replacement
        to have at least prob probability of picking at least one marked card?
        """
        for q in xrange(1, M+1):
            if pick_Q(N, M, q) >= prob:
                return q
        raise ValueError("Could not find a value for q")
    

    Edit:

    scipy.stats.hypergeom.cdf(k, M, n, N)
    

    Given a deck of M cards, n of which are marked, picking N randomly without replacement, find the odds that k or fewer marked cards are picked. (You can think of this as the integral of .pmf)

    Then .sf(k, M, n, N) is the flip side of .cdf – the odds that more than k marked cards were picked.

    For example,

     k      pmf(k,52,13,4)   cdf(k,52,13,4)   sf(k,52,13,4)
         (exactly k picked)  ( <= k picked)   ( > k picked)
    ---  -----------------  ---------------  --------------
     0       0.303817527      0.303817527      0.696182473
     1       0.438847539      0.742665066      0.257334934
     2       0.213493397      0.956158463      0.043841537
     3       0.041200480      0.997358944      0.002641056
     4       0.002641056      1.000000000      0.000000000
    

    Edit2:

    actually, this gives another way of writing the pick_Q function – ‘picking 1 or more marked cards’ can be rephrased as ‘picking more than 0 marked cards’, so

    def pick_Q(N, M, Q):
        """
        Given a deck of N cards, where M are marked,
        and Q cards are taken randomly without replacement,
        return the probability that at least one marked card is taken.
        """
        return scipy.stats.hypergeom.sf(0, N, M, Q)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.