Reputation: 31
I'm trying to write a code to get the probability of a certain scenario. There are 52 cards broken down into 4 Suit piles. 1 card is randomly drawn from each pile to make a 4 card combination and then the cards are put back into their piles. How do you work out the probability of the combination having only 1 King? I have tried the following but I think I am doing something wrong
cards <- c(2:10,'J','Q', 'K','A')
v <- sample(rep(cards,1:13),1000,replace=T)
cat('The probability of getting a King is approximately:',sum(v=='K')/length(v),'\n')
Upvotes: 3
Views: 425
Reputation: 18683
Don't use sample
for calculating exact probabilities.
The probability of the 4-card combination having only 1 King can be easily calculated using the Binomial formula since the experiment satisfies all the criteria for a Binomial experiment.
P(x) = choose(n, x) * p^x * (1 - p)^(n-x)
where, n = number of trials, x = number of times a specific outcome occurs out of n trials, p = probability of a success.
In R:
n <- 4
x <- 1
p <- 1/13
choose(n, x) * p^x * (1 - p)^(n-x)
# [1] 0.2420083
https://en.wikipedia.org/wiki/Binomial_distribution
Upvotes: 1
Reputation: 729
As I understand your question, you can work it out using this code. This works for a single given draw of 1 card from each pile, or subsequent given draws after replacement. Doesn't work if you're interested in probability over multiple draws, or subsequent draws without replacement. It's not a repeated sampling based way to calculate.
All possible draw combos i.e. King or not king from each pile:
Hearts <- rep(c((rep("k",1)),(rep("n",1))),8)
Spades <- rep(c((rep("k",2)),(rep("n",2))),4)
Clubs <- rep(c((rep("k",4)),(rep("n",4))),2)
Diamonds <- rep(c((rep("k",8)),(rep("n",8))),1)
pile.possibilities <- data.frame(Hearts,Spades,Clubs,Diamonds)
And draw probabilities per pile:
pile.possibilities$H.prob <- ifelse (pile.possibilities$Hearts == "k", (1/13), (12/13))
pile.possibilities$S.prob <- ifelse (pile.possibilities$Spades == "k", (1/13), (12/13))
pile.possibilities$C.prob <- ifelse (pile.possibilities$Clubs == "k", (1/13), (12/13))
pile.possibilities$D.prob <- ifelse (pile.possibilities$Diamonds == "k", (1/13), (12/13))
Combined probability per combo:
pile.possibilities$Combo.prob <- pile.possibilities$H.prob *
pile.possibilities$S.prob *
pile.possibilities$C.prob *
pile.possibilities$D.prob
A certainty that you will have one of these combos.
> sum(Pile.combo.prob)
[1] 1
Filter your combinations of interest:
pile.possibilities$one.king.combo <- paste(pile.possibilities$Hearts,pile.possibilities$Spades,pile.possibilities$Clubs,pile.possibilities$Diamonds,sep = "")
pile.possibilities$one.king.combo <- sapply(strsplit(pile.possibilities$one.king, NULL), function(x) paste(sort(x), collapse = ''))
one.king.probability<- sum(subset(pile.possibilities, one.king.combo == "knnn")$Combo.prob)
one.king.probability
[1] 0.2420083
#Final data frame used
> pile.possibilities
Hearts Spades Clubs Diamonds H.prob S.prob C.prob D.prob Combo.prob one.king.combo
1 k k k k 0.07692308 0.07692308 0.07692308 0.07692308 3.501278e-05 kkkk
2 n k k k 0.92307692 0.07692308 0.07692308 0.07692308 4.201534e-04 kkkn
3 k n k k 0.07692308 0.92307692 0.07692308 0.07692308 4.201534e-04 kkkn
4 n n k k 0.92307692 0.92307692 0.07692308 0.07692308 5.041840e-03 kknn
5 k k n k 0.07692308 0.07692308 0.92307692 0.07692308 4.201534e-04 kkkn
6 n k n k 0.92307692 0.07692308 0.92307692 0.07692308 5.041840e-03 kknn
7 k n n k 0.07692308 0.92307692 0.92307692 0.07692308 5.041840e-03 kknn
8 n n n k 0.92307692 0.92307692 0.92307692 0.07692308 6.050208e-02 knnn
9 k k k n 0.07692308 0.07692308 0.07692308 0.92307692 4.201534e-04 kkkn
10 n k k n 0.92307692 0.07692308 0.07692308 0.92307692 5.041840e-03 kknn
11 k n k n 0.07692308 0.92307692 0.07692308 0.92307692 5.041840e-03 kknn
12 n n k n 0.92307692 0.92307692 0.07692308 0.92307692 6.050208e-02 knnn
13 k k n n 0.07692308 0.07692308 0.92307692 0.92307692 5.041840e-03 kknn
14 n k n n 0.92307692 0.07692308 0.92307692 0.92307692 6.050208e-02 knnn
15 k n n n 0.07692308 0.92307692 0.92307692 0.92307692 6.050208e-02 knnn
16 n n n n 0.92307692 0.92307692 0.92307692 0.92307692 7.260250e-01 nnnn
Upvotes: 2