Random Variables

STAT 20: Introduction to Probability and Statistics

Adapted by Gaston Sanchez

Agenda

  • Quiz-3 reminder
  • WSP-5 reminder
  • Brief lecture on random variables
  • WS: Random Variables

Announcements: Quiz 3

Quiz-3 next Monday, Oct 13th in class.

  • Section 1, 8am in Barker 101
  • Section 8, 10am in SOCS 60
  • Topics:
    • Intro to Probability
    • Computing Probabilities
    • Probability Distributions
    • Random Variables
    • Expected Value and Variance

Announcements: WSP 5, Oct-14th

  • WS Computing probs
  • WS Prob distributions
  • WS Random variables
  • WS EV and Variance

Lecture

Random Variables 1

Let \(X\) be the number of heads in three tosses of a fair coin. What is the distribution of \(X\)? That is, what is \(f(x) = P(X = x)\)? What values will \(x\) take?

Random Variables 2

What about if \(X\) is the number of heads in 3 tosses of a biased coin, where the chance of heads is \(\frac{2}{3}\)? Now what is \(f(x) = P(X = x)\)?

Demo: dbinom()

dbinom() gives you the pmf

# Binomial: parameters
n = 3   # number of tosses
p = 2/3 # chance of heads

Demo: dbinom()

dbinom() gives you the pmf

# Binomial: parameters
n = 3   # number of tosses
p = 2/3 # chance of heads

# probability distribution (pmf)
pmf = dbinom(x = 0:n, size = n, prob = p)
pmf
[1] 0.03703704 0.22222222 0.44444444 0.29629630

Demo: pbinom()

pbinom() gives you the CDF

# Binomial: parameters
n = 3   # number of tosses
p = 2/3 # chance of heads

Demo: pbinom()

pbinom() gives you the CDF

# Binomial: parameters
n = 3   # number of tosses
p = 2/3 # chance of heads

# cumulative distribution function
cdf = pbinom(q = 0:3, size = n, prob = p)
cdf
[1] 0.03703704 0.25925926 0.70370370 1.00000000


# equivalent
cumsum(pmf)
[1] 0.03703704 0.25925926 0.70370370 1.00000000

Demo rbinom()

rbinom() simulates drawing from a binomial distribution; we can use it to get an empirical histogram

# Binomial: parameters
n = 3   # number of tosses
p = 2/3 # chance of heads

# simulation (100 tosses of biased coin)
tosses = rbinom(n = 100, size = n, prob = p)
tosses
  [1] 1 2 1 3 1 2 2 2 3 3 1 3 2 3 3 3 1 1 3 2 1 1 1 2 2 3 3 2 3 3 1 1 2 2 1 1 1
 [38] 3 3 2 1 2 2 2 2 2 2 3 1 1 1 2 3 2 3 2 3 2 0 2 1 0 2 2 2 2 3 1 1 1 2 3 2 3
 [75] 2 3 3 2 3 3 2 3 3 2 3 2 2 2 2 1 0 1 3 3 2 3 3 2 1 2

Demo: Empirical Probability Distribution

data.frame(tosses) |> 
  count(tosses)
  tosses  n
1      0  3
2      1 25
3      2 40
4      3 32


data.frame(tosses) |> 
  count(tosses) |> 
  mutate(prop = n / sum(n))
  tosses  n prop
1      0  3 0.03
2      1 25 0.25
3      2 40 0.40
4      3 32 0.32

Demo: Empirical histogram

Code
data.frame(tosses) |> 
  count(tosses) |> 
  mutate(prop = n / sum(n)) |> 
  ggplot(aes(x = tosses, y = prop)) +
  geom_col() +
  labs(title = "Probability Histogram", 
       subtitle = "Number of heads in 3 tosses of biased coin", 
       x = "heads") +
  theme_bw()

Random Variables 3

Now suppose we toss a fair coin until the first time it lands heads, and let \(X\) be the number of tosses (including the last one, which is the first time the coin lands heads). What is the pmf of \(X\)? Is it binomial?

Random Variables 4

Finally, let’s consider a deck of cards, and we are interested in the number of hearts dealt in a hand of five. Call this number \(X\). What is the pmf of \(X\)?

\(f(x)\) and \(F(x)\)

  • \(f(x)\) is the probability mass function of \(X\).

    • What does that mean?
    • What is the connection to the distribution table?
    • What is the connection to the probability histogram?


  • \(F(x)\) is the cumulative distribution function.


  • What is the connection between \(f\) and \(F\)?

Example

Box with tickets: { 0, 0, 0, 1, 1, 2, 2, 2 }.

Draw one ticket, and let \(X\) be the value of the ticket.

Example

Box with tickets: { 0, 0, 0, 1, 1, 2, 2, 2 }.

Draw one ticket, and let \(X\) be the value of the ticket.


pmf: \(\quad f(X) = P(X=x)\)

  • \(P(X = 0) = 3/8\)
  • \(P(X = 1) = 2/8\)
  • \(P(X = 2) = 3/8\)

cdf: \(\quad F(x) = P(X \leq x)\)

  • \(P(X \leq x) = 0 \quad \quad \text{for} \ x < 0\)
  • \(P(X \leq x) = 3/8 \quad \text{for} \ 0 \leq x < 1\)
  • \(P(X \leq x) = 5/8 \quad \text{for} \ 1 \leq x < 2\)
  • \(P(X \leq x) = 1 \quad \quad \text{for} \ x \geq 2\)

Example

Box with tickets: { 0, 0, 0, 1, 1, 2, 2, 2 }.

Draw one ticket, and let \(X\) be the value of the ticket.

\[ f(X) \rightarrow P(X=x) \]

  • \(P(X = 0) = 3/8\)
  • \(P(X = 1) = 2/8\)
  • \(P(X = 2) = 3/8\)

\[ F(X) \rightarrow P(X \leq x) \]

\[ F(X) = \begin{cases} 0, \quad \quad x < 0 \\ 3/8, \quad 0 \leq x < 1 \\ 5/8, \quad 1 \leq x < 2 \\ 8/8, \quad 2 \leq x \end{cases} \]

Sketching \(F(X)\)

Box with tickets: { 0, 0, 0, 1, 1, 2, 2, 2 }.

Draw one ticket, and let \(X\) be the value of the ticket.

Concept Question 1

Roll a pair of fair six-sided dice and let \(X = 1\) if the dice land showing the same number of spots, and \(0\) otherwise. For example, if both dice land \(2\), then \(X = 1\), but if one lands \(2\) and the other lands \(3\), then \(X = 0\).


What is \(P(X=1)\)?

Roll a pair of fair six-sided dice and let \(X = 1\) if the dice land showing the same number of spots, and \(0\) otherwise. For example, if both dice land \(2\), then \(X = 1\), but if one lands \(2\) and the other lands \(3\), then \(X = 0\).


What is \(P(X=1)\)?

01:00

Roll a pair of fair six-sided dice and let \(X = 1\) if the dice land showing the same number of spots, and \(0\) otherwise.

What is the cdf \(F(x)\)?

Concept Question 2

01:30

The graph of the cdf of a random variable \(X\) is shown below. What is \(F(2)\)? What about \(f(2)\)?

02:00

You have \(10\) people with a cold and you have a remedy with a \(20\%\) chance of success. What is the chance that your remedy will cure at least one sufferer?

What is the chance that at least one person is cured?

01:30

There are 4 histograms of different Poisson distributions below. Match each distribution to its parameter \(\lambda\). Recall that \(\lambda\) is how many occurrences we think will happen in a given period of time.

\[ (1)\: \lambda = 0.5 \hspace{2cm} (2)\: \lambda = 1 \hspace{2cm} (3) \: \lambda = 2 \hspace{2cm} (4) \: \lambda = 4\hspace{2cm} \]

Practice Problems

20:00

WS: Random Variables

45:00