2 Discrete Probability Spaces
2.1 Bernoulli trials
Generating a sequence of Bernoulli trials of a given length and with a given probability parameter
= 10 # length of the sequence
n = 0.5 # probability parameter
p = sample(c("H","T"), n, replace = TRUE, prob = c(p,1-p)) X
noquote(X)
[1] T T T H T T H H T T
The number of heads in that sequence
sum(X == "H")
[1] 3
2.2 Sampling without replacement
An urn with \(r\) red ball and \(b\) blue balls represented as a vector
= 7
r = 10
b = c(rep("R", r), rep("B", b))
urn noquote(urn)
[1] R R R R R R R B B B B B B B B B B
Sampling uniformly at random without replacement from that urn a given number of times
= 5
n = sample(urn, n)
X noquote(X)
[1] B R B B R
2.3 Pólya’s urn model
A direct (and naive) implementation of Pólya’s urn model
= function(n, r, b){
polyaUrn # n: number of draws
# r: number of red balls
# b: number of blue balls
= c(rep("R", r), rep("B", b))
urn = character(n) # stores the successive draws
X for (i in 1:n){
= sample(urn, 1)
draw = draw
X[i] if (draw == "R"){
= c("R", urn)
urn else {
} = c(urn, "B")
urn
}
}return(X)
}
= polyaUrn(10, 2, 3)
X noquote(X)
[1] R B B R B B R B R B
2.4 Factorials and binomials coefficients
Factorials are quickly very large. To see this, we plot the first few in logarithmic scale (in base 10).
= 200
n plot(0:n, log10(factorial(0:n)), type = "l", lwd = 2, ylab = "factorials (log in base 10)", xlab = "integers")
The factorials quickly exceed the maximum possible number in R, which is given by the following.
$double.xmax .Machine
[1] 1.797693e+308
Binomial coefficients can also be very large even for moderate values of the integers defining them.
par(mfrow = c(2, 2), mai = c(0.8, 0.5, 0.2, 0.2))
= 10
n plot(0:n, log10(choose(n, 0:n)), type = "l", lwd = 2, ylab = "binomials (log in base 10)", xlab = "k (out of n = 10)")
= 20
n plot(0:n, log10(choose(n, 0:n)), type = "l", lwd = 2, ylab = "binomials (log in base 10)", xlab = "k (out of n = 10)")
= 50
n plot(0:n, log10(choose(n, 0:n)), type = "l", lwd = 2, ylab = "binomials (log in base 10)", xlab = "k (out of n = 50)")
= 100
n plot(0:n, log10(choose(n, 0:n)), type = "l", lwd = 2, ylab = "binomials (log in base 10)", xlab = "k (out of n = 100)")