Skip to content

Instantly share code, notes, and snippets.

@fbrundu

fbrundu/hyper.md

Last active Oct 12, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@izaak-coleman izaak-coleman commented Oct 12, 2020

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

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