Skip to content

Instantly share code, notes, and snippets.

@fbrundu
Last active December 19, 2023 14:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbrundu/cfa675c1d79b4ade4724 to your computer and use it in GitHub Desktop.
Save fbrundu/cfa675c1d79b4ade4724 to your computer and use it in GitHub Desktop.
Calculate hypergeometric probability with Python SciPy

A poker hand consists of 5 cards dealt at random without replacement from a standard deck of 52 cards of which 26 are red and the rest black. A poker hand is dealt. Find the chance that the hand contains three red cards and two black cards.

To achieve it, we use the hypergeometric probability mass function. We want 3 cards from the set of 26 red cards and 2 from the set of 26. So the parameters for the hypergeometric function are:

M = 52  # Total number of cards
n = 26  # Number of Type I cards (e.g. red cards) 
N = 5   # Number of draws (5 cards dealt in one poker hand)
k = 3   # Number of Type I cards we want in one hand

In SciPy we can get the probability p with

import scipy.stats as ss
hpd = ss.hypergeom(M, n, N)
p = hpd.pmf(k)
@izaak-coleman
Copy link

XD literally just came across this when searching for "calculate hypergeometric test in python". :P

@brianpenghe
Copy link

Thank you for the three-line code

@lourdes-github
Copy link

Thank you for the three-line code

@frpnz
Copy link

frpnz commented Dec 19, 2023

Old man bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment