Probability of Selecting Matched Pairs and the Hypergeometric Distribution

The problem

Consider a case where we have a bag of marbles of size . The bag consists of black marbles and white marbles. In this example meaning there is less black marbles than white. Each black marble has a number between 1 and is paired with a white marble which are labeled between 1 and . From the bag of marbles we take a sample of size . The problem is, what is the probability of selecting at least 1 pair of marbles i.e. at least 1 black and white marble with the same label?

For this example consider the following values:

marbles

marbles drawn from the bag

black marbles in the bag

This can be broken into two parts:

The probability of selecting black marbles in the samples and

The probability of selecting at least 1 white marble in with the same label as one of the selected black marbles.

The first part of the problem is a straight forward hypergeometric distribution. Hypergeometric distributions are described in general as the probability of drawing successes in a sample of size from a known population of with total successes. The probability that black marbles are selected is given by,

Given a sample size of and black marbles in the population there is the potential to select anywhere between 0 and 10 black marbles.

The maximum likelihood occurs at meaning on average we can expect to select 4 black marbles from the bag with a sample size of .

The second part of the problem will be simplified to,

The probability of selecting exactly matches in samples given black marbles are selected.

This can also be formulated as a hypergeometric distribution.

The condition of black marbles is important since it alters the probability of getting a matching white, for example if marbles are selected there is a 0% chance of getting a match, similarly if there is a 0% chance of drawing a match. It can also be shown the maximum number of matches possible for this problem is which makes sense.

Combining these probabilities the joint probability of selecting black marbles and matches can be calculated.

In order to find the probability of selecting matches the joint distribution needs to be summed over all possible values of to solve for the marginal distribution .

The original problem is stated as, what is the probability of selecting at least 1 match? This is similar to the birthday paradox problem. The probability of selecting at least 1 match is the same as 1 minus the probability of selecting 0 matches i.e. .

The probability that we will selected at least 1 match is 0.5761024. Using this formulation we can also compute the expected number of matches in the sample.

1

2

3

4

pm<-sapply(0:n,function(x)sum(prob.match(N,n,K,k.seq,x)))

expected.value<-sum(k.seq*pm)

expected.value

1

## [1] 0.7346939

In a sample of 10 we would expect to draw (0, 1) matches. In real world problems this can help you decide on how large your sample needs to be to find specific matches in the population.

Simulation

To check these results this scenario will be simulated 10,000 times.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# simulate matches

simulate.matches<-function(N,n,K){

pop<-1:N

selected.sample<-sample(pop,n)

black<-sample(pop,K)

white<-sample(pop[-black],K)

black_id<-black%in%selected.sample

white_id<-white%in%selected.sample

selected.pairs<-white_id+black_id>1# if white and black both selected this will equal 2

return(sum(selected.pairs))

}

# run sim

draws<-replicate(1e4,simulate.matches(N,n,K))

mean(draws>0)

1

## [1] 0.5691

1

2

mean(draws)

1

## [1] 0.7276

The approximate probability of selecting at least 1 match is 0.5691 and the expected number of matches is 0.7276 based on 10,000 simulations. This is very close to our analytical solution above.

In the real world

Neat examples like this rarely occur in the real world. More often we are dealing with much larger numbers. For example, consider a case where we are required to sample the population of a country and are aiming to sample people who are hosts to a contagion. For story telling purposes person A who carries the contagion can only infect one other person, person B (not realistic but the problem is mathematically similar to something I’ve recently worked on). There are no other indicators in a structured form on the database to simply query the people in the population who carry the contagion or have received it from someone else. However by manually inspecting the persons record we are able to deduce this information. Therefore we take a sample of the population and hope to select A-B pairs.

To replicate this the parameters are set to be,

The problem here is the number of ways you can a sample 200k out of 6 million records is too ridiculous to even try and quantify, it’s far beyond the number of atoms in the known universe. R will simply return NaN. In cases with such large numbers a normal approximation to the hypergeometric distribution is applied.

Normal approximation

To make a normal approximation to the hypergeomtetric distribution the mean and standarad deviation of the hypergeometric are calculated.

Since the normal distribution is continuous, to accurately approximate the probability of selecting successes the mean of the probabilities evaluated at and . This will be tested on the smaller example first.

There is an 85.6% chance of drawing at least 1 match, which isn’t particularly high when taking a sample of 200k. On average there will be between 1 and 2 matches. If you are required to detect these matched pairs in the data with a relatively low population of successes you will need to take a very large sample. It is important for situations like this to be communicated well to manage expectations. Dealing with large amounts of data can add a huge overhead to your work if the infrastructure isn’t there to support it. But knowing the odds of selecting the matched pairs you can make better decisions about how much sample to take or take more targetted samples at the beginning to speed up analysis.

Estimating the number of successes in the population

Often you don’t know how many successes there are in the population and the goal is to estimate this number. Assume we have checked every unit in the sample and we have found 10 matches. This is considerably more than what we would have predicted when assuming and therefore necessary to re-estimate how many cases there are in the population. Given there is inherent uncertainty in the estimate it is good practice to estimate the distribution of likely values. We sample from the probability distribution using the grid method.

If we observed 10 matches in the sample of 200k units, it is likley there are 9400 error pairs in the population with 95% of the most likely values lying in the range (5100, 16102.5).

Bayesian approach

The better way is to take a Bayesian approach to estimate by assuming a distribution for and (which we have done) and define a beta-binomial distribution for .

Gelman et al explains the beta-binomial arises from first sampling and then drawing . The benefit of a Bayesian approach is any prior beliefs we have about the population of carriers can be incorporated into the model. Prior information is introduced through the hyperparameters and . By setting we are assuming a non-informative prior and effectively get the same result as above.

Let’s assume our prior knowledge suggests it could be as high as 1 in 50 people are carriers therfore we set . Using the metropolis-hastings algorithm can be estimated by taking draws from the posterior.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

N<-6e6

n<-2e5

m<-10

K.post<-function(par){

# set pars

K<-par[1]

# likelihood calculation

loglik<-log(prob.match.norm(N,n,K,seq(2*m,max(2*m,min(K,n)),1),m))

# priors

K.prior<-dbeta(K/N,2,50,log=T)

# return posterior likelihood

return(sum(loglik,K.prior))

}

# run metropolis hastings

theta0<-1200

runMH<-Metro_Hastings(li_func=K.post,par=theta0,par_names="K")

1

2

3

4

5

6

7

8

9

10

## [1] "updating: 10%"

## [1] "updating: 20%"

## [1] "updating: 30%"

## [1] "updating: 40%"

## [1] "updating: 50%"

## [1] "updating: 60%"

## [1] "updating: 70%"

## [1] "updating: 80%"

## [1] "updating: 90%"

## [1] "updating: 100%"

1

2

3

4

5

6

7

8

9

10

# plot results

df<-data.frame(iter=1:length(runMH$trace),K.trace=runMH$trace)

grid.arrange(

ggplot(df,aes(x=K.trace))+

geom_histogram(fill="turquoise",col="grey20",size=0.1)+

geom_vline(xintercept=median(df$K.trace),lty=2),

ggplot(df,aes(x=iter,y=K.trace))+

geom_line()+

geom_hline(yintercept=median(df$K.trace),col="red",lty=2),nrow=1)

1

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

1

2

3

# credible interval

quantile(runMH$trace,c(0.025,0.5,0.975))

1

2

## 2.5% 50% 97.5%

## 5532.401 10283.820 18508.274

The influence of the prior has shifted the range of possible values of higher giving our best estimate of the number of carriers in the population.