EE 4521/5521, Fall 2026
Homework 2
Assigned: Wednesday September 30, 2026
Due: Wednesday October 14, 2026
Professor Jarvis Haupt
This is our second homework assignment of the semester. This assignment has some analytical responses, and some "coding-type" problems. Again, I think it's easiest to complete them in a notebook format. This will give you some experience working with markdown language (and $\LaTeX$ equation editing - the sooner you can get up to speed with that, the better, in my opinion!).
As with the last assignment, you are to create a new Colab (or Jupyter) notebook (or create a copy of this one and modify it appropriately) in which you solve each of the following problems. You should upload your completed assignment, which will be a notebook with the .ipynb extension, through the course Canvas site.
My preference is that you name your homework assignment using the following format:
YourLastName EE4521 Homework 2.ipynbYourLastName EE5521 Homework 2.ipynbNotice that "exporting" your Colab notebook to an .ipynb file is fairly straightforward. In the File menu, select the Download .ipynb option to save your file for uploading.
A few notes on this assignment:
Problem 1: (Distribution Functions)
In lecture we discussed probability models (e.g., PDFs and PMFs for continuous and discrete random variables, respectively). A related (and useful!) concept is the so-called cumulative distribution function or CDF. The CDF of a (scalar, real-valued) random variable $X$ is defined as $$F_X(x) = \mathrm{Pr}(X\leq x)$$ for each $x\in \mathbb{R}$. Notice that the capital letter $F$ is typically reserved for this quantity, the subscript denotes the random variable ($X$ here), and the argument of the function ($x$ here) is a parameter. The CDF is a function of $x$.
One unique aspect of CDFs is that one need not make any distinction between continuous and discrete random variables in order to specify the CDF. It turns out that CDFs for discrete random variables are right-continuous (with left limits), and piecewise constant with jump discontinuities at each value $x$ for which $\mbox{Pr}(X=x)>0$. CDFs for continuous random variables are continuous, piecewise differentiable functions of $x$. There are also random variables called "mixed random variables" whose CDFs have characteristics of both of these models. One can recover probability models (PDFs and PMFs) from CDFs by (appropriately defined) differentiation.
That said, our aim here is to visualize the CDFs for some of the models we discussed in class. Specifically, for this problem you should:
scipy.stats package - you may find built-in functions that directly evaluate the CDF, or something related to it, that you can use.Your response here should be in the form of one or more code boxes with their associated output figures.
# Problem 1 Part 1
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import t
x = np.linspace(-5, 5, 1000) # Create array of 1000 evenly spaced numbers from -5 to 5
plt.figure(figsize=(6, 4))
for df in [1, 2, 5]:
plt.plot(x, t.cdf(x, df), label=f"df = {df}") # Plot one t distribution graph for each degree of freedom (df)
plt.title("Student's t-Distributions CDF")
plt.xlabel("x")
plt.ylabel("CDF")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Problem 1 Part 1 Output:
# Problem 1 Part 2
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
x = np.arange(-1, 6) # Creates array of integers from -1 through 5
y = np.concatenate(([0], binom.cdf(np.arange(0, 6), n=5, p=0.5))) # Calculate CDF of the list -1 through 5 with n=5 trials and p=0.5 (probability = 50%) success
plt.figure(figsize=(6, 4))
plt.step(x, y, where="post", label="Binomial(5, 0.5)") # Step happens after each X value
plt.title("CDF of a Binomial(5, 0.5) Random Variable")
plt.xlabel("x")
plt.ylabel("CDF")
plt.xticks(np.arange(-1, 6))
plt.ylim(-0.03, 1.03)
plt.legend()
plt.grid(True, alpha=0.3) # Semi transparent grid lines
plt.tight_layout()
plt.show()
Problem 1 Part 2 Output:
# Problem 1 Part 3
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
x = np.linspace(-5, 5, 1000)
plt.figure(figsize=(6, 4))
plt.plot(x, norm.cdf(x), label="Mean=0, S.D.=1") # Using normal R.V. CDF function from scipy
plt.title("CDF of a Standard Normal Random Variable")
plt.xlabel("x")
plt.ylabel("CDF")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Problem 1 Part 3 Output:
Problem 2: (Probabilities of Intervals)
One nice feature of the CDF is that you can use it to compute probabilites that the random variable $X$ lies in a certain interval. Specifically, it is (perhaps?) easy to see that for real values $a<b$, $$\mbox{Pr}(X\in(a,b]) = F_X(b)-F_X(a),$$ where we use the convention that parentheses mean the point is not included and brackets mean the point is included in the interval. Directly from the definition we have that for $a\in \mathbb{R}$, $$F_X(a) = \mbox{Pr}(X\leq a) = \mbox{Pr}(X \in (-\infty,a]).$$ (Note that it's common to use parentheses when the limits are $\pm \infty$ since it doesn't really make sense to consider $\infty$ as a fixed quantity.) It follows that $$\mbox{Pr}(X > a) = \mbox{Pr}(X \in (a, \infty)) = 1-F_X(a).$$
Use these ideas to calcualte the following probabilites. That is, compute each of these quantities using Python, e.g., using the correct interpretation of how each claim can be expressed in terms of CDFs.
Note: In the last part, the inequalities are not exactly of the form we mentioned above. Does it matter here? Remember that a continuous random variable takes any fixed value with probability 0. Use this idea to rewrite the probability in the last part in an equivalent form, for which you can use the analytical results above.
Note: for the last part, you WILL have to do a little bit of extra work to find the probability of the right event. (The answers for the last two parts are not the same here.) Think about what this event is saying, and the fact that Poisson random variables can only take integer values from $0$ to $\infty$.
# Problem 2 Part 1
from scipy.stats import t
# P(X > 0)
p1 = 1 - t.cdf(0, df=2)
# P(X > 4)
p2 = 1 - t.cdf(4, df=2)
# P(-1 <= X <= 1)
p3 = t.cdf(1, df=2) - t.cdf(-1, df=2) # Calculates Fx(1)-Fx(-1)
print("P(X > 0) =", p1)
print("P(X > 4) =", p2)
print("P(-1 <= X <= 1) =", p3)
Problem 2 Part 1 Output:
P(X > 0) = 0.5
P(X > 4) = 0.028595479208968322
P(-1 <= X <= 1) = 0.5773502691896257
# Problem 2 Part 2
from scipy.stats import poisson
# P(X > 0)
p1 = 1 - poisson.cdf(0, mu=2)
# P(X >= 0)
p2 = 1 - poisson.cdf(-1, mu=2) # Go one number below to include X equals that number
# P(1 < X <= 3)
p3 = poisson.cdf(3, mu=2) - poisson.cdf(1, mu=2) # Does not include X=1, only above 1
# P(1 <= X <= 3)
p4 = poisson.cdf(3, mu=2) - poisson.cdf(0, mu=2) # Includes probability of X=1
print("P(X > 0) =", p1)
print("P(X >= 0) =", p2)
print("P(1 < X <= 3) =", p3)
print("P(1 <= X <= 3) =", p4)
Problem 2 Part 2 Output:
P(X > 0) = 0.8646647167633873
P(X >= 0) = 1.0
P(1 < X <= 3) = 0.4511176107887091
P(1 <= X <= 3) = 0.7217881772619343
# Problem 2 Part 3
from scipy.stats import norm
u = 0
sigma = 2
# P(X > 0)
p1 = 1 - norm.cdf(0, loc=u, scale=sigma)
# P(X > sigma)
p2 = 1 - norm.cdf(sigma, loc=u, scale=sigma)
# P(|X| > sigma)
p3 = norm.cdf(-sigma, loc=u, scale=sigma) + 1 - norm.cdf(sigma, loc=u, scale=sigma)
# P(X > 2*sigma)
p4 = 1 - norm.cdf(2*sigma, loc=u, scale=sigma)
# P(|X| <= u)
p5 = norm.cdf(u, loc=u, scale=sigma) - norm.cdf(-u, loc=u, scale=sigma)
print("P(X > 0) =", p1)
print("P(X > sigma) =", p2)
print("P(|X| > sigma) =", p3)
print("P(X > 2*sigma) =", p4)
print("P(|X| <= u) =", p5)
Problem 2 Part 3 Output:
P(X > 0) = 0.5
P(X > sigma) = 0.15865525393145707
P(|X| > sigma) = 0.31731050786291426
P(X > 2*sigma) = 0.02275013194817921
P(|X| <= u) = 0.0
Problem 3: (Bayes' Rule)
One of the most fundamental and useful concepts in probability theory (and which serves as the foundation of Bayesian inference) is the so-called Bayes' Rule. In order to discuss Bayes' Rule, we need to introduce the concept of conditional probability.
Conditional probability, in essence, describes scenarios where probabilities change because of some additional knowledge you have. Consider, for example, the difference between the probability that you will live to be 110 years old (probably quite low!) vs. the probability tht you will live to be 110 years old given that you are already 109 years and 364 days old (this conditional probability is presumably much higher!). In this case, the "additional knowledge" comes in the form of some information about your life status.
Formally, conditional probabilities can be defined as follows. Let ${\cal A}$ and ${\cal B}$ be two different events, with $\mbox{Pr}({\cal A}) > 0$ and $\mbox{Pr}({\cal B}) > 0$. The conditional probability that ${\cal A}$ happens given that ${\cal B}$ is known to have happened is denoted by $\mbox{Pr}({\cal A} | {\cal B})$ and given by $$\mbox{Pr}({\cal A} | {\cal B}) = \frac{\mbox{Pr}({\cal A} \mbox{ and } {\cal B})}{\mbox{Pr}({\cal B})} = \frac{\mbox{Pr}({\cal A} \cap {\cal B})}{\mbox{Pr}({\cal B})}.$$
Now, Bayes' Rule follows (rather simply) from this idea. Bayes' Rule allows us to essentialy "flip around" the conditioning. It states $$\mbox{Pr}({\cal B} | {\cal A}) = \frac{\mbox{Pr}({\cal A} | {\cal B})\mbox{Pr}({\cal B})}{\mbox{Pr}({\cal A})}.$$ It's worth mentioning that the denominator here can often be calculated using what's called the Law of Total Probability. Specifically, we have $$\mbox{Pr}({\cal A}) = \mbox{Pr}({\cal A} | {\cal B})\mbox{Pr}({\cal B}) + \mbox{Pr}({\cal A} | {\cal B}^c)\mbox{Pr}({\cal B}^c).$$ Here, ${\cal B}^c$ is the complement of ${\cal B}$, which is essentially everything besides ${\cal B}$. In fact, we have $$\mbox{Pr}({\cal B}^c) = 1-\mbox{Pr}({\cal B}).$$
In this problem, we'll look at a numerical example that illustrates an interesting and often-misconstrued phenomenon (the numbers here are just ones I made up - not tied to any specific knowledge or reality!).
Suppose that the prevalence of a particular illness is known to be $1/1000$ of the population. In other words, 0.1% of the population has the illness (whether they know it or not!). A testing company has devised a test for the illness that has the following characteristics:
Now, suppose that a person receives a positive test for the illness. You should do the following:
# Problem 3
# Probability of having the illness
p_illness = 1 / 1000
# Probability of not having the illness
p_no_illness = 1 - p_illness
# Probability of testing positive given person has the illness
p_positive_given_illness = 1.0
# False positive rate
p_positive_given_no_illness = 0.05
# Law of Total Probability
p_positive = (p_positive_given_illness * p_illness + p_positive_given_no_illness * p_no_illness)
# Bayes' Rule
p_illness_given_positive = (p_positive_given_illness * p_illness / p_positive)
print("Probability of having the illness given the person tests positive:", p_illness_given_positive)
print("Percentage:", p_illness_given_positive * 100, "%")
Problem 3 Output:
Probability of having the illness given the person tests positive: 0.019627085377821395
Percentage: 1.9627085377821394 %
Was this result surprising?
Yes, at first this result was surprising to me, since it seems like a very low accuracy rate. However, once you realize that only 0.1% of the people actually get the illness and 5% of people who don't have it test positive, you can see that there will end up being a very large quantity of people who do not have it that tested positive for the illness.
Problem 4: (Exponential Random Variables)
One widely used continuous random variable is the exponential random variable. There exist several (equivalent) parameterizations of the exponential model. Here, we'll follow the one in the Numpy Random Number Generator; $X$ is an exponential random variable with scale paramter $\beta > 0$ if its PDF is given by $$f_X(x) = \left\{ \begin{array}{cc} \frac{1}{\beta}e^{-x/\beta} & x\geq 0 \\ 0 & x < 0 \end{array}\right. .$$ For this problem you are to
Problem 4 Part 1:
Let $X$ be exponential with scale parameter $\beta > 0$, so that
$$f_X(x) = \frac{1}{\beta} e^{-x/\beta}, \qquad x \geq 0.$$Then
$$E[X] = \int_0^\infty x \, \frac{1}{\beta} e^{-x/\beta} \, dx.$$We integrate by parts with $u = x$ and $dv = \frac{1}{\beta} e^{-x/\beta}\,dx$, so that $du = dx$ and $v = -e^{-x/\beta}$. This gives
$$E[X] = \Big[ -x e^{-x/\beta} \Big]_0^\infty + \int_0^\infty e^{-x/\beta} \, dx.$$The boundary term vanishes: it equals $0$ at $x = 0$, and $x e^{-x/\beta} \to 0$ as $x \to \infty$. Therefore
$$E[X] = \int_0^\infty e^{-x/\beta} \, dx = \Big[ -\beta e^{-x/\beta} \Big]_0^\infty = \beta.$$$$\boxed{E[X] = \beta}$$Used Claude to help write Latex, as this is my first time writing Latex.
# Problem 4 Part 2 and 3
import numpy as np
rng = np.random.default_rng(42) # Random number generator object with seed value of 42
beta = 1/3
x = rng.exponential(scale=beta, size=100)
print("Sample mean:", x.mean())
print("Sample variance:", x.var(ddof=1)) # Computing variance using ddof
Problem 4 Part 2 and 3 Output:
Sample mean: 0.2996846311139698
Sample variance: 0.08790231085864463
This is close to 1/3 and 1/9, the expected values from the derivation.
# Problem 4 Part 4
from scipy.stats import expon
beta = 1/3
p = expon.sf(4, scale=beta) / expon.sf(3, scale=beta)
print(p)
Problem 4 Part 4 Output:
0.04978706836786394
Probability that $X>4$ given that $X>3$.
Problem 5: (Counting-based Probabilities)
Suppose that you have a cookie jar that initially has only full-sized cookies in it. Because you want your cookie stash to last longer, you only eat them one half at a time. Specifically,
This problem is designed to explore probabilities associated with this model.
Suppose that you initially begin with $n$ full cookies. The probability that you pick a half-cookie in the first pick is, of course, zero. But, it is possible that you pick half cookies in each of the following selections, up to and including the $2n$-th (last) pick.
(Notice that for a jar with $n$ cookies, you will always need $2n$ picks, and the last one will always be a half cookie.)
Write a Python script that estimates the probability of picking a half cookie on each of the picks (you already know the results that you should get for the first and last picks!). Here, by estimate, what we mean is that your code should simulate the outcome of this overall experiment (all $2n$ picks) and keep track of how many $1$st picks, $2$nd picks, etc. are half cookies. Dividing those counts by the total number of trials (different runs of the experiment) gives you an empirical probability for each.
Note: You should not expect these probabilities to form a PMF! These events are not "mutually exclusive," so there's no reason to expect them to sum to $1$. Stated another way, you could easily draw out half-cookies, say, on many other picks, as well as the $2n$-th pick.
Your code should be parameterized by the number $n$ of cookies, and should use enough trials to have your results be representative (I would say 10000 or more). Notice, also, that you should shuffle your "jar" before each pick to simulate the random selection process. This is especially important because you'll sometimes be replacing a full cookie with a half cookie in a given draw, so the composition of the jar will change.
Once your code is written, you should:
Hint: The list objects and associated operations we discussed earlier this semester might be useful here, though there are (of course) many ways you could choose to address this problem. Lists are a convenient, and especially "Pythonic" choice.
Note: The quantity you are computing here has a formal description. For each value of $i\in\{1,\dots,2n\}$, let $H_i$ denote the event that you pick a half-cookie on the $i$-th draw. In the experiment outlined above, you're basically estimating ${\rm Pr}(H_i)$ for each $i$. There are ways to address this problem analytically (using, e.g., tree diagrams) but it's a little bit tedious and requires some careful bookkeeping. Fortunately, you're not required to solve the problem analytically here!
# Problem 5
import random
import matplotlib.pyplot as plt
def cookiejar(n, trials=20000): # Define function for cookie jar experiment with multiple trials
# Estimate Pr(H on pick i), the probability of drawing a half cookie on pick i for i = 1, ..., 2n starting from a jar of n full (F) cookies.
counts = [0] * (2 * n)
for _ in range(trials):
jar = ['F'] * n # F = full cookie, H = half cookie, initializing jar full of full cookies
for pick in range(2 * n):
random.shuffle(jar) # Shuffle jar before each pick
cookie = jar.pop() # Take one cookie from jar
if cookie == 'H':
counts[pick] += 1 # Ate half cookie, cookie gone since it was aleady a half
else:
jar.append('H') # Ate half, put the other half back since cookie was full cookie
return [c / trials for c in counts] # counts -> probabilities
# Part 1 Results
random.seed(1)
p5 = cookiejar(5) # Run function with n=5
print([round(p, 4) for p in p5]) # Rounds probabilities to four decimal places and prints the array
# Part 2 Results
n = 25
p25 = cookiejar(n) # Run function with n=25
plt.plot(range(1, 2*n + 1), p25, marker='o')
plt.xlabel("Pick number i")
plt.ylabel("Estimated Probability")
plt.title("Probability of drawing a half cookie on pick i (n = 25)")
plt.grid(True)
plt.show()
Used Claude to write the Python function for problem 5, and commented lines myself to learn the code.