# Thread: 5 cards changing with different probability. Probability of 0-5 cards changing?

1. ## 5 cards changing with different probability. Probability of 0-5 cards changing?

We have 5 white cards, numbered 1 to 5.

Each card can change to blue but this will have a different probability of happening for each card, e.g.:

card 1: 5% probability of turning into blue
card 2: 3% "
card 3: 4% "
card 4: 5% "
card 5: 1% "

Is it possible to calculate the chance of having: no blue cards, 1 blue card, 2 blue cards, 3 blue cards, 4 blue cards, all (5) blue cards, respectively? (the sum of these probabilities will obviously be 100%).

N.B.: for e.g. a "3-blue-cards" combination, any 3 of the 5 cards can be changed, e.g. cards 1,2,3 or 1,3,5 or 2,4,5 etc...

Thanks

2. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

We must assume all cards are independent from each other.

1) for a given # of cards $n \in 0,~1~\dots 5$, list all the combinations of changing cards that gets you this number

2) for each set from above examine the cards that changed. If the card changed use it's probability of change from your table. If it didn't change use (1-prob).

Form the product of all these probabilities for each combination associated with $n$ cards changing

Sum these products for all the combinations associated with $n$ cards changing.

For example

0 cards changing has probability

$p_0= (1-0.05)(1-0.03)(1-0.04)(1-0.05)(1-0.01)$

1 card changing has probability

$p_1 = \\(0.05)(1-0.03)(1-0.04)(1-0.05)(1-0.01) + \\(1-0.05)(0.03)(1-0.04)(1-0.05)(1-0.01) + \\ (1-0.05)(1-0.03)(0.04)(1-0.05)(1-0.01)+ \\(1-0.05)(1-0.03)(1-0.04)(0.05)(1-0.01)+ \\(1-0.05)(1-0.03)(1-0.04)(1-0.05)(0.01)$

and 5 cards changing has probability

$p_5 = (0.05)(0.03)(0.04)(0.05)(0.01)$

2,3, and 4 cards changing are move involved and I leave these to you. I highly suggest using software.

3. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

Thanks very much romsek,

any suggestion on the software I may use ?

Cheers.

4. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

Originally Posted by PDC
Thanks very much romsek,

any suggestion on the software I may use ?

Cheers.

there you go.

I don't know what software you have available. You could probably make excel work.

5. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

wow! thanks. will give it a go.

6. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

Originally Posted by PDC
We have 5 white cards, numbered 1 to 5.
Each card can change to blue but this will have a different probability of happening for each card, e.g.:
card 1: 5% probability of turning into blue
card 2: 3% "
card 3: 4% "
card 4: 5% "
card 5: 1% "
Is it possible to calculate the chance of having: no blue cards, 1 blue card, 2 blue cards, 3 blue cards, 4 blue cards, all (5) blue cards, respectively? (the sum of these probabilities will obviously be 100%).
There is also a mathematical model for this. It can be done with bit-strings (zeros & ones) of length five/
The string $01011$ mean cards two,four & five turn blue with probability
$\underbrace {[(.05)^0(.95)^1]}_{card\,1}\underbrace {[(.03)^1(.97)^0]}_{card\,2}\underbrace {[(.04)^0(.96)^1]}_{card\,3}\underbrace {[(.05)^1(.95)^0]}_{card\,4}\underbrace {[(.01)^1(.99)^0]}_{card\,5}$

To list a these possible bit strings it takes a $32\times 5$ matrix, $M(j,k)=\left\lceil {\dfrac{j}{{{2^{5 - k}}}}} \right\rceil {\bmod _2}$

This is like programming a truth table with five variables. As for software I personally would chose MathCad as I find it much easier to use.

7. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

Hey romsek,
thanks again for your script. I downloaded the WM trial version and ran your script successfully for up to 10 cards. The result comes in about 1 second. If I use 11 cards or more it freezes for a long time before saying that there is insufficient memory. I am wondering if the problem is actually the memory (I have an old hp laptop, 6 GB RAM) or is there anything in your script (which I am not embarassed to say that I can't decipher) limiting the number of cards to max 10 ?

My objective would be to run it on 25 cards.

Thanks for any comment you can provide on this.

8. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

Originally Posted by PDC
Hey romsek,
thanks again for your script. I downloaded the WM trial version and ran your script successfully for up to 10 cards. The result comes in about 1 second. If I use 11 cards or more it freezes for a long time before saying that there is insufficient memory. I am wondering if the problem is actually the memory (I have an old hp laptop, 6 GB RAM) or is there anything in your script (which I am not embarassed to say that I can't decipher) limiting the number of cards to max 10 ?

My objective would be to run it on 25 cards.

Thanks for any comment you can provide on this.
I'll take a look

9. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

certainly nothing limiting it to 10.

Execution time starts to crawl at around 20 cards.

One thing to note is that the probability of say 8 or more cards changing starts to get pretty close to 0.

That's with a uniform distribution but even with a non-uniform one at some point well less than 25 cards the probability of some number of cards or more changing is going to be negligible.

The sheet can be modified to only look at cases where N or fewer cards change, N around 10 or so.

I suspect it's an artifact of the trial version rather than the full version. My pc has 16 GB though so maybe you do have too little RAM. Don't know.

What happens is that as you get a lot of cards the number of ways to have a fair number of cards change starts to get huge.

I'll continue to investigate as time allows.

10. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

ok try this

adjust ncards, probs, pThresh as desired

note that calcs for ncards > 20 still crawl, even on my pc.

-------------------------------------------------------------------------------------------------------------------------
(* the cards *)
ncards = 20;
cards = Array[# &, ncards];

(* the probability of changing for each card *)
(* I just selected random probabilities. You would set these to your system *)
probs = Array[RandomReal[{0, 1}] &, ncards];
probs = probs/(probs /. List -> Plus);
notprobs = 1 - probs;

(* Probability threshold. This will stop the loop when probabilities fall below this level *)
pThresh = 10^-6;

(* a function that takes a combination list and generates the probability of that combination occurring *)
comboProb[c_] := Product[If[Cases[c, k] == {}, notprobs[[k]], probs[[k]]], {k, 1, ncards}];

(* Initialize array of probabilities of k cards changing in case you want to save results *)
changeProb = ConstantArray[0, ncards+1];

(* Find the probabilities for k cards changing. Do this in a loop rather than in tables to save memory *)
For[k = 0, k <= ncards, k++,
combos = Subsets[cards, {k}];
p = Sum[comboProb[combos[[i]]], {i, 1, Length[combos]}];
Print[{k, p}];

(* set this probability in the changeProb array *)
changeProb[[k + 1]] = p;

(* check to see if probabilities have gotten small enough to stop computing them *)
If[p < pThresh, Print["Probabilility threshold reached. Stopping."]; Break[]];
];

changeProb // MatrixForm
------------------------------------------------------------------------------------------------------------------------

11. ## Re: 5 cards changing with different probability. Probability of 0-5 cards changing?

That's amazing.

Thank you so much...also for your comment on why my laptop was crashing. Chances are that only a "huge beast" could deal with the 25 cards...however your new script really works perfectly for what I need to do.

Many thanks again.