I faced this exercise that couldn't solve: An urn contains three red balls, two green balls, and one white. Three balls are drawn sequentially without replacement from the urn. Their colors are recorded. List the sample space using R.
I tried:
combn(c(rep("R",3), rep("G",2),"W"),3)
but this function does not account for the order of the elements and reads the three R like R1,R2 and R3. As a result there are duplicate lines in the output.
I need the function to produce this sequence:
Ω = {"GGR" "GGW" "GRG" "GRR" "GRW" "GWG" "GWR" "RGG" "RGR" "RGW" "RRG" "RRR" "RRW" "RWG" "RWR" "WGG" "WGR" "WRG" "WRR"}
The thing is, if order matters, then you want permutations rather than combinations. Permutations generally explode quickly and become unmanageable. I'm sure this extremely inefficient, but it seems to work.
balls<-c(rep("R",3), rep("G",2),"W")
permn <- function(x, n) {
if (n<1) return(vector(class(x)))
do.call(rbind, lapply(1:length(x), function(i) {
cbind(x[i], permn(x[-i], n-1))
})
)
}
x <- permn(balls, 3)
unique(sort(apply(x, 1, paste, collapse="")))
And it returns
[1] "GGR" "GGW" "GRG" "GRR" "GRW" "GWG" "GWR" "RGG" "RGR" "RGW" "RRG"
[12] "RRR" "RRW" "RWG" "RWR" "WGG" "WGR" "WRG" "WRR"
as desired.
The permn function works recursively. You pass in a list of values (x) and how many items you want to choose from that list (n). If you're choosing at least one value, then we set up a loop whereby we select each of the elements. Then, after we've chosen one value, we need to select n-1 more from the remaining items. So we call the function again, this time removing the value we've just selected and reducing the number of items we need to choose.
Up to this point we've actually been ignoring the values in the set (we've assumed they are all unique). But since in this case all the balls of a certain color are indistinguishable, we need to collapse our results. Since permn actually returns a matrix, we will collapse the rows from a vector like c("G","G","R") to the string "GGR" and then just take the unique values.
Of course not every outcome is equally likely. If we wanted to see how often they occur, you could do
sort(prop.table(table(apply(x, 1, paste, collapse=""))))
which would also calculate the probabilities of each of the elements in the sample space
GGW GWG WGG GGR GRG GRW
0.01666667 0.01666667 0.01666667 0.05000000 0.05000000 0.05000000
GWR RGG RGW RRR RRW RWG
0.05000000 0.05000000 0.05000000 0.05000000 0.05000000 0.05000000
RWR WGR WRG WRR GRR RGR
0.05000000 0.05000000 0.05000000 0.05000000 0.10000000 0.10000000
RRG
0.10000000
You can also use the function urnsamples from the prob package like this:
unique(urnsamples(x, size = 3, replace = F, ordered = T))
There are several sample space and probability functions in the "prob" package:
https://cran.r-project.org/web/packages/prob/vignettes/prob.pdf
You can use the following code to bring up the pdf file for this package:
vignette("prob")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With