📗 Truly random numbers are difficult or impossible to generate.
📗 A sequence of pseudo-random numbers is a deterministic sequence with complicated pattern that looks random to users who do not know the pattern therefore cannot predict the next number in the sequence.
📗 A simple pseudo-random number generator is the Linear Congruential Generator (LCG).
➩ Start with a seed \(x_{0}\).
➩ Compute \(x_{n+1} = \left(a x_{n} + c\right) \mod m\) for some \(m, a, c\) unknown to the user.
📗 The resulting sequence \(\dfrac{x_{0}}{m}, \dfrac{x_{1}}{m}, ...\) is approximately uniformly distributed between \(0\) and \(1\), including \(0\), not including \(1\).
➩ For example, Java uses \(m = 2^{48}, a = 25214903917, c = 11\).
➩ MATLAB uses another more complicated algorithm called Mersenne Twister.
📗 A discrete random variable is a random variable that takes on a finite (or countable) number of values with positive probabilities.
➩ The probability that the random variable \(X\) takes on value \(x \in \left\{1, 2, 3, ...\right\}\) is denoted by \(f\left(x\right) = \mathbb{P}\left\{X = x\right\}\). The function \(f\) is called the probability mass function.
➩ A random number generated based on the probabilities specified by \(f\) is called a realization of the \(X\).
📗 The cumulative probability that the random variable \(X\) takes on value less than \(x\) is denoted by \(F\left(x\right) = \mathbb{P}\left\{X \leq x\right\} = \displaystyle\sum_{i=1}^{x} \mathbb{P}\left\{X = i\right\}\). The function \(F\) is called the cumulative distribution function (CDF).
📗 The CDF can be efficiently computed using a for loop.
📗 Use a for loop to compute the CDF based on the probabilities stored in a vector \(p\), where \(p_{i} = \mathbb{P}\left\{X = i\right\}, i = 1, 2, ..., n\).
➩ f = zeros(n);
➩ f(1) = p(1);
➩ for t = 1:n
➩ f(t) = f(t - 1) + p(t);
➩ end
📗 A while loop is used when the loop stops after an unknown number of iterations until some condition is met.
📗 Use a while loop to compute the inverse of the CDF stored in a vector \(f\), where \(f_{i} = \mathbb{P}\left\{X \leq i\right\}\).
➩ t = 1;
➩ while f(t) <= x
➩ t = t + 1;
➩ end
📗 Built-in functions can be used in place of the loops.
➩ f = cumsum(x) finds the same CDF.
➩ t = sum(f <= x) + 1 or find(x < f, 1) finds the same inverse CDF.
📗 A continuous random variable is a random variable that takes on uncountably infinite number of values.
📗 The (theoretical) probability that the random variable is equal to any number is \(0\).
📗 Inverse transform sampling can also be used to generate a random value of the variable.
➩ The CDF of a distribution \(X\) taking values \((-\infty, \infty)\) is given by \(F\left(x\right) = \mathbb{P}\left\{X \leq x\right\}\).
➩ The derivative of this function is called the probability density function \(f\left(x\right) = F'\left(x\right)\), meaning \(F\left(x\right) = \displaystyle\int_{-\infty}^{x} f\left(\hat{x}\right) d \hat{x}\).
📗 Direct computation of the probability of an event is sometimes difficult, and simulation can be used to approximate this probability by repeating the same random process a large number of times and find the fraction of times the event occurs.
📗 The same code can produce a different output every time it is executed.
📗 In order to make a simulation reproducible, the best practice is to always set a seed at the beginning of the simulation using rng(seed).
TopHat Quiz
📗 Estimate the probability that the values from two dice are the same.
➩ A: sum(rand(1, 1000) * 6 == rand(1, 1000) * 6)
➩ B: sum(randi(6, 1, 1000) == randi(6, 1, 1000))
➩ C: mean(rand(1, 1000) * 6 == rand(1, 1000) * 6)
➩ D: mean(randi(6, 1, 1000) == randi(6, 1, 1000))
TopHat Quiz
📗 Estimate the probability that the values from two dice sum up to an odd number.
📗 Estimate the average number of times it takes to throw a die until it lands six.
📗 s = zeros(1, 1000);
📗 for t = 1:1000
➩ A: while randi(6) = 6
➩ B: while randi(6) == 6
➩ C: while randi(6) ~= 6
➩ D: while randi(6) != 6
📗 s(t) = s(t) + 1;
📗 end
📗 end; mean(s) + 1
TopHat Quiz
📗 Estimate the average number of times it takes to throw a loaded die with probabilities \(\begin{bmatrix} 0.1 & 0.2 & 0.4 & 0.2 & 0 & 0.1 \end{bmatrix}\) until it lands six.
📗 s = zeros(1, 1000);
📗 for t = 1:1000
➩ A: while rand() <= 0.1
➩ B: while rand() > 0.1
➩ C: while rand() <= 0.9
➩ D: while rand() > 0.9
📗 s(t) = s(t) + 1;
📗 end
📗 end; mean(s) + 1
📗 Notes and code adapted from the course taught by Professors Beck Hasti and Michael O'Neill.
📗 You can expand all TopHat Quizzes and Discussions: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google form Form at the end of the lecture.