Prev: W8 Next: W10

📗 Tuesday lectures: 4:00 to 4:50, Zoom, TopHat: Link (or Google Form: Form if TopHat not working). MATLAB.
📗 Programming Homework: P5

Slide:


# Pseudo Randomness

📗 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.
TopHat Discussion
📗 Select a random choice:
➩ A:
➩ B:
➩ C:
➩ D:
➩ E:

# Random Number Generation

📗 rand() generates a uniform random number between \(0\) and \(1\), including \(0\), not including \(1\).
rand() * u generates a uniform random number between \(0\) and \(u\), including \(0\), not including \(u\).
rand() * (u - l) + l generates a uniform random number between \(l\) and \(u\), including \(l\), not including \(u\).
rand(n, m) creates an \(n \times m\) matrix of random numbers between \(0\) and \(1\).
📗 randi(n) generates a uniform random integer between \(1\) and \(n\), including \(1\) and \(n\).
randi([l, u]) generates a uniform random integer between \(l\) and \(u\), including \(l\) and \(u\).
📗 randperm(n) generates a random permutation of 1:n.
randperm(n, k) with \(k \leq n\) generates a random sample from 1:n of size \(k\), sampled without replacement.

# Discrete Random Variable

📗 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.
➩ \(F\left(1\right) = \mathbb{P}\left\{X = 1\right\}\).
➩ \(F\left(x\right) = F\left(x - 1\right) + \mathbb{P}\left\{X = x\right\}, x > 1\).

# Inverse Transform Sampling

📗 The inverse transform sampling (also called CDF inversion method) can be used to generate a realization of the random variable \(X\).
➩ Generate \(u \sim \text{Uniform}\left(0, 1\right)\).
➩ Compute CDF of \(X\), call it \(F\left(x\right)\).
➩ Find the largest \(x\) such that \(F\left(x\right) < u\).

# Inverse Transform Sampling Loops

📗 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.
TopHat Quiz
📗 cdf = cumsum([0.3, 0.4, 0.3]);
📗 rng(0); u = rand(); has value \(u = 0.8147\).
📗 sum(cdf <= u) + 1
➩ A: \(0\)
➩ B: \(1\)
➩ C: \(2\)
➩ D: \(3\)

TopHat Quiz
📗 cdf = cumsum([0.3, 0.4, 0.3]);
📗 rng(1); u = rand(); has value \(u = 0.4170\).
📗 sum(cdf <= u) + 1
➩ A: \(0\)
➩ B: \(1\)
➩ C: \(2\)
➩ D: \(3\)


# Contiuous Random Variable

📗 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}\).

# Simulation

📗 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.
➩ A: sum(mod(randi(12, 1, 1000), 2) == 1)
➩ B: sum(mod(randi(6, 1, 1000) + randi(6, 1, 1000), 2) == 1)
➩ C: mean(mod(randi(12, 1, 1000), 2) == 1)
➩ D: mean(mod(randi(6, 1, 1000) + randi(6, 1, 1000), 2) == 1)

TopHat Quiz
📗 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.





Last Updated: April 18, 2025 at 6:18 PM