A bag contains 40 skittles: 16 yellow, 14 red, and 10 orange. You draw 3 skittles at random (without replacement) from the bag. What is the probability that you get 2 skittles of one color and another skittle of a different color?
# A bag contains 40 skittles: 16 yellow, 14 red, and 10 orange.
<- c(rep("Yellow", 16), rep("Red", 14), rep("Orange", 10))
skittles
# Skittles amount
length(skittles)
## [1] 40
skittles
## [1] "Yellow" "Yellow" "Yellow" "Yellow" "Yellow" "Yellow" "Yellow" "Yellow"
## [9] "Yellow" "Yellow" "Yellow" "Yellow" "Yellow" "Yellow" "Yellow" "Yellow"
## [17] "Red" "Red" "Red" "Red" "Red" "Red" "Red" "Red"
## [25] "Red" "Red" "Red" "Red" "Red" "Red" "Orange" "Orange"
## [33] "Orange" "Orange" "Orange" "Orange" "Orange" "Orange" "Orange" "Orange"
Single experiment of draw 3 skittles at random (without replacement) from the bag:
set.seed(20211222) # For reproducibility
<- sample(x = skittles,
draw_one size = 3,
replace = FALSE)
draw_one
## [1] "Red" "Yellow" "Yellow"
Possible outputs from each experiment:
Two approaches:
Repeat the experiment a large enough number of times, equivalent to doing it over and over. Stochastic process.
# 1. Experiment
draw_one
## [1] "Red" "Yellow" "Yellow"
# 2. Duplicate elements
duplicated(draw_one)
## [1] FALSE FALSE TRUE
# 3. If the sum of logical duplicate elements is 1, then there are two skittles of one color and another skittle of a different color
sum(duplicated(draw_one))
## [1] 1
Using the replicate
function to repeat the experiment a million times:
library(dplyr) # if_else function
<- replicate(n = 1000000, {
results # 1. draw 3 skittles at random
<- sample(x = skittles,
draws size = 3,
replace = FALSE)
# 2.
<- sum(duplicated(draws))
sum # 3. If the sum is different to 1 then takes a value zero
<- if_else(sum == 1, 1, 0)
experiment })
The mean of the output vector results is the probability of getting 2 skittles of one color and another skittle of a different color:
mean(results)
## [1] 0.668082
The probability that you get 2 skittles of one color and another skittle of a different color from a bag that contains 40 skittles: 16 yellow, 14 red, and 10 orange is 0.668 or 66.8%
There are three ways to select two colors (a, b) given the condition one is repeated:
# Two yellow skittles and another of different color
16/40)*(15/39)*(24/38))*3 +
((# Two red skittles and another of different color
14/40)*(13/39)*(26/38))*3 +
((# Two orange skittles and another of different color
10/40)*(9/39)*(30/38))*3 ((
## [1] 0.6676113