How many ternary strings of length n have exactly k1's? (Here, a ternary string is a string consisting of only 0's, 1's, and 2's.)
Solution.
To specify such a string, there are two steps:
I have to tell you where the 1's go
I have to tell you what the remaining numbers are.
For (1), I have to choose the locations of the 1's, so there are (kn) ways to specify the 1's. For the remaining n−k numbers, I have two choices: 0 or 2, so there are 2n−k ways to specify the remaining characters. Overall, the number of strings is
(kn)×2n−k.
Example 2.
Calculate ∑k=0n(kn).
Solution.
This one is sort of a trick. If you remember the binomial theorem, it says
(a+b)n=k=0∑n(kn)anbn−k.
If we set a=b=1, then
k=0∑n(kn)=(1+1)n=2n.
Example 3.
(BOGO sort)
You randomly reorder 1,2,…,n repeatedly until you get the original ordering back. What is the expected number of times you have to reorder the numbers until this happens?
Solution.
This problem is asking us to find the expectation of some random variable, so there are two steps:
Figure out what the random variable is.
Calculate its expectation.
The random variable here is the number of times you reorder the numbers, which I'll call N. Notice that the value of N is the number of attempts we need before we succeed at getting the numbers in their original order, so N is a geometric random variable with probability of success p=n!1 (there are n! different orderings of these numbers, and we only want 1 of them). Thus,
E[N]=p1=n!.
Remark.
During discussion, someone asked me to derive the expectation of a geometric random variable, so let X∼Geom(p). By definition,
P(X=k)=(1−p)k−1p,k≥1.
The expectation is then
E[X]=k=1∑∞k(1−p)k−1p.
To calculate this sum, recall the geometric series:
k=0∑∞xk=1−x1⟹k=1∑∞kxk−1=(1−x)21.
(I got the second equation by taking the derivative on both sides.) If you plug in x=1−p, then you get
Let X equal the number of flips of a fair coin that are required to observe the same face on consecutive flips.
Find the pmf of X.
Find the mgf of X.
Use the mgf of X to find the mean and variance of X.
Find P(X≤3), P(X≥5), P(X=3).
Solution.
Generally, before trying to calculate the pmf directly, you'll want to try it for a few numbers first to get an idea of what the answer should be.
If X=2, this means that the our flips were HH or TT. These both occur with probability 41, so you get P(X=2)=41⋅2=21.
If X=3, then the flips were either THH or HTT. Like before, these both have probability 81, so P(X=3)=81⋅2=41.
For general X=k, you may have noticed that the final two flips determine the rest of the flips: if our sequence ends with THH, then the remaining ones have to alternate between T and H, and similarly for HTT. So, no matter what, there are only 2 ways to win, and these occur with probability 2k1 (we flipped a coin k times), so
(The sum in the third line is a geometric series, which we know the formula for. See the remark below.) This formula is valid when the common ratio is less than 1, i.e.,