Application Center - Maplesoft

App Preview:

Probabilities without replacement

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Probabilities without replacement

Some fast comments on the problem of selection without replacement. When the text is computing probabilities of picking a pair when choosing two letters from either a flat or standard English distribution of characters they compute the probability sampling with replacement. When they approximate the probability with pairs of letters from ciphertext they do sampling without replacement. It is instructive to look at the probabilities one gets doing the flat and standard English samples without replacement.

First we want the probabilities for each of the letters, both with a typical distribution and with a flat distribution.

> typprobs := linalg[vector]
([.08167, .01492, .02782, .04253, .12702, .02228, .02015, .06094,
.06966, .00153, .00772, .04025, .02406, .06749, .07507, .01929,
.00095, .05987, .06327, .09056, .02758, .00978, .02360, .00150,
.01974, .00074]);
flatprobs := linalg[vector](26,i->evalf(1/26));

typprobs := VECTOR([.8167e-1, .1492e-1, .2782e-1, ....
typprobs := VECTOR([.8167e-1, .1492e-1, .2782e-1, ....

flatprobs := VECTOR([.3846153846e-1, .3846153846e-1...
flatprobs := VECTOR([.3846153846e-1, .3846153846e-1...
flatprobs := VECTOR([.3846153846e-1, .3846153846e-1...

Next we want a set of functions that can be used to get the probability of a random pair of letters being a pair.

> pairs := x -> x*max(x-1,0):
#computes the number of pairs that can be obtained from x elements
expect := (vec,n) -> linalg[scalarmul](vec,n):
#computes the number of expected occurences for each letter
# given a probability vector and a total number of letters.
pairsvec := vec -> map(pairs,vec):
sumvec := vec -> sum(vec[i],i=1..26):
#Computes the number of pairs for a given distribution of letters.
probn := (numpairs, numchars) -> numpairs/(pairs(numchars)):
#Computes probabilities for pairs and total number of characters.
nreplaceprob := (vec,n) -> probn(sumvec(pairsvec(expect(vec,n))),n):
#Combines all the earlier functions into a single command.

We are ready to look at the probabilities for two letters chosen without replacement being a pair, both with a typical probability distribution and with a flat distribution.

We will start with a sample of size 200.

> nreplaceprob(typprobs,200);
nreplaceprob(flatprobs,200);

.6081840050e-1

.3362968688e-1

Notice that both probabilities are lower than the probabilities given in the book.
Compare what happens if we use a sample of size 100,000.

> nreplaceprob(typprobs,100000);
nreplaceprob(flatprobs,100000);

.6548735447e-1

.3845192307e-1

This is closer to what the values that the book uses.

Let us look at a variety of sample sizes to compare the probabilities of a fair in both a typical ands a flat distribution.

> print(`n`,`typ pairs`,`prob typical`,`flat pairs`,`flat prob`,`total pairs`);
for k from 1 to 20 do
print(k*50,round(nreplaceprob(typprobs,k*50)*pairs(k*50)),
nreplaceprob(typprobs,k*50),
round(nreplaceprob(flatprobs,k*50)*k*50*(k*50-1)),
nreplaceprob(flatprobs,k*50),
k*50*(k*50-1)):
od:

n, `typ pairs`, `prob typical`, `flat pairs`, `flat...

50, 115, .4681085061e-1, 46, .1883830456e-1, 2450

100, 556, .5611890070e-1, 285, .2874902881e-1, 9900...

150, 1324, .5925052868e-1, 715, .3200826020e-1, 223...

200, 2421, .6081840050e-1, 1338, .3362968688e-1, 39...

250, 3844, .6175660491e-1, 2154, .3459993829e-1, 62...

300, 5596, .6238102861e-1, 3162, .3524569075e-1, 89...

350, 7674, .6282653430e-1, 4362, .3570641388e-1, 12...

400, 10080, .6316038446e-1, 5754, .3605166760e-1, 1...

450, 12814, .6341988043e-1, 7338, .3632002745e-1, 2...

500, 15875, .6362737335e-1, 9115, .3653460768e-1, 2...

550, 19264, .6379707133e-1, 11085, .3671010226e-1, ...

600, 22979, .6393843922e-1, 13246, .3685629905e-1, ...

650, 27023, .6405802467e-1, 15600, .3697996918e-1, ...

700, 31394, .6416076478e-1, 18146, .3708594686e-1, ...

750, 36092, .6424984932e-1, 20885, .3717777554e-1, ...

800, 41118, .6432778412e-1, 23815, .3725811105e-1, ...

850, 46472, .6439653945e-1, 26938, .3732898422e-1, ...

900, 52153, .6445764679e-1, 30254, .3739197403e-1, ...

950, 58161, .6451231501e-1, 33762, .3744832618e-1, ...

1000, 64497, .6456151091e-1, 37462, .3749903745e-1,...

Notice how the probabilities move toward the values listed in the book, but there is still a noticable difference at n = 1000. Consider what values we get when the values of n are powers of 10.

> print(`n`, `typ prob`, `flat prob`);
for k from 1 to 15 do
print(`10^`||k,nreplaceprob(typprobs,10^k), nreplaceprob(flatprobs,10^k)):
od:

n, `typ prob`, `flat prob`

`10^1`, .3813422666e-2, 0

`10^2`, .5611890070e-1, .2874902881e-1

`10^3`, .6456151091e-1, .3749903745e-1

`10^4`, .6540324082e-1, .3836537506e-1

`10^5`, .6548735447e-1, .3845192307e-1

`10^6`, .6549576501e-1, .3846057693e-1

`10^7`, .6549660606e-1, .3846144242e-1

`10^8`, .6549669014e-1, .3846152891e-1

`10^9`, .6549669858e-1, .3846153745e-1

`10^10`, .6549669940e-1, .3846153842e-1

`10^11`, .6549669950e-1, .3846153844e-1

`10^12`, .6549669950e-1, .3846153845e-1

`10^13`, .6549669950e-1, .3846153845e-1

`10^14`, .6549669950e-1, .3846153845e-1

`10^15`, .6549669950e-1, .3846153845e-1

The values are within 1 % of the with replacement values when the sample is at least 10,000 characters.

If we want to get fancy we notice that a passage cannot have half of a z in it. We want to modify our distribution to only allow integrer values. For the flat distribution we put the extras in the first slots available. For the typical distributions we will use rounding.

> flatdis := proc(n)
local flatletdis, count, extras:
flatletdis := linalg[vector](26):
extras := n mod 26;
for count from 1 to extras do
flatletdis[count] := ceil(n/26):
od:
for count from (extras + 1) to 26 do
flatletdis[count] := floor(n/26):
od:
flatletdis;
end:

Consider the distributions obtained for 491 letters.

> flat2 := flatdis(491):
print(flat2);

flat2 := flatletdis

VECTOR([19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,...

> wholeletdis := (vec, n) -> map(round,expect(vec,n)):

> letcount := wholeletdis(typprobs,491);
sum(letcount[i],i=1..26);

letcount := VECTOR([40, 7, 14, 21, 62, 11, 10, 30, ...

491

> pairprobfromdist := (dist,n) ->
evalf(probn(sumvec(pairsvec(dist)),n)):

We will find that insisting on whole numbers only for the number of letters decreases the expected probability of a pair with a standard distribution and increases the probability of a pair in a flat distribution.

> pairprobfromdist(wholeletdis(typprobs,491),491);
nreplaceprob(typprobs,491);

.6326114967e-1

.6359314963e-1

> pairprobfromdist(flatdis(491),491);
nreplaceprob(flatprobs,491);

.3651024565e-1

.3649921508e-1

> print(`n`,`typ prob - whole`,`prob typical`,`flat prob - whole`,`flat prob`,`total pairs`);
for k from 1 to 20 do
print(k*50,pairprobfromdist(wholeletdis(typprobs,k*50),k*50),
nreplaceprob(typprobs,k*50),
pairprobfromdist(flatdis(k*50),k*50),
nreplaceprob(flatprobs,k*50),
k*50*(k*50-1)):
od:

n, `typ prob - whole`, `prob typical`, `flat prob -...

50, .4408163265e-1, .4681085061e-1, .1959183673e-1,...

100, .5676767677e-1, .5611890070e-1, .2909090909e-1...

150, .5798657718e-1, .5925052868e-1, .3221476510e-1...

200, .6030150754e-1, .6081840050e-1, .3376884422e-1...

250, .6223293173e-1, .6175660491e-1, .3469879518e-1...

300, .6247491639e-1, .6238102861e-1, .3531772575e-1...

350, .6262791650e-1, .6282653430e-1, .3575931232e-1...

400, .6315789474e-1, .6316038446e-1, .3609022556e-1...

450, .6319227914e-1, .6341988043e-1, .3634743875e-1...

500, .6406412826e-1, .6362737335e-1, .3655310621e-1...

550, .6377214771e-1, .6379707133e-1, .3672131148e-1...

600, .6387868670e-1, .6393843922e-1, .3686143573e-1...

650, .6435462842e-1, .6405802467e-1, .3697996918e-1...

700, .6419783364e-1, .6416076478e-1, .3708972001e-1...

750, .6410324878e-1, .6424984932e-1, .3718380062e-1...

800, .6437734668e-1, .6432778412e-1, .3726533166e-1...

850, .6428323980e-1, .6439653945e-1, .3733665905e-1...

900, .6470646397e-1, .6445764679e-1, .3739957978e-1...

950, .6445122289e-1, .6451231501e-1, .3745549332e-1...

1000, .6471271271e-1, .6456151091e-1, .3750550551e-...

>