Ch. 1 - The binomial distribution

Flipping coins in R

[Video]

Simulating coin flips

# Generate 10 separate random flips with probability .3
rbinom(10, 1, .3)
##  [1] 1 1 1 1 1 1 0 0 0 1

Simulating draws from a binomial

# Generate 100 occurrences of flipping 10 coins, each with 30% probability
rbinom(100, 10, .3)
##   [1] 2 3 1 2 3 1 0 2 0 2 2 3 2 0 2 1 4 2 5 1 5 2 5 4 2 5 2 1 2 3 1 3 5 4 0 4 3
##  [38] 3 3 2 5 3 4 3 3 3 3 4 4 7 6 1 1 3 2 2 2 2 7 3 3 3 5 2 3 4 4 4 2 2 4 4 5 6
##  [75] 4 4 3 2 4 1 2 3 5 3 3 5 1 2 3 3 4 1 4 4 2 2 1 4 5 1

Density and cumulative density

[Video]

Calculating density of a binomial

# Calculate the probability that 2 are heads using dbinom
dbinom(2, 10, .3)
## [1] 0.2334744
# Confirm your answer with a simulation using rbinom
mean(rbinom(10000, 10, .3) == 2)
## [1] 0.2358

Calculating cumulative density of a binomial

# Calculate the probability that at least five coins are heads
1 - pbinom(4, 10, .3)
## [1] 0.1502683
# Confirm your answer with a simulation of 10,000 trials
mean(rbinom(10000, 10, .3) >= 5)
## [1] 0.1484

Varying the number of trials

# Here is how you computed the answer in the last problem
mean(rbinom(10000, 10, .3) >= 5)
## [1] 0.1464
# Try now with 100, 1000, 10,000, and 100,000 trials
mean(rbinom(100, 10, .3) >= 5)
## [1] 0.09
mean(rbinom(1000, 10, .3) >= 5)
## [1] 0.158
mean(rbinom(10000, 10, .3) >= 5)
## [1] 0.1518
mean(rbinom(100000, 10, .3) >= 5)
## [1] 0.15106

Expected value and variance

[Video]

Calculating the expected value

# Calculate the expected value using the exact formula
25 * .3
## [1] 7.5
# Confirm with a simulation using rbinom
mean(rbinom(10000, 25, .3))
## [1] 7.5068

Calculating the variance

# Calculate the variance using the exact formula
25 * .3 * (1 - .3)
## [1] 5.25
# Confirm with a simulation using rbinom
var(rbinom(10000, 25, .3))
## [1] 5.282772

Ch. 2 - Laws of probability

Probability of event A and event B

[Video]

Solving for probability of A and B

If events A and B are independent, and A has a 40% chance of happening, and event B has a 20% chance of happening, what is the probability they will both happen?

  • [*] 8%
  • 12%
  • 20%
  • 60%

Simulating the probability of A and B

# Simulate 100,000 flips of a coin with a 40% chance of heads
A <- rbinom(100000, 1, .4)

# Simulate 100,000 flips of a coin with a 20% chance of heads
B <- rbinom(100000, 1, .2)

# Estimate the probability both A and B are heads
mean(A & B)
## [1] 0.08065

Simulating the probability of A, B, and C

# You've already simulated 100,000 flips of coins A and B
A <- rbinom(100000, 1, .4)
B <- rbinom(100000, 1, .2)

# Simulate 100,000 flips of coin C (70% chance of heads)
C <- rbinom(100000, 1, .7)

# Estimate the probability A, B, and C are all heads
mean(A & B & C)
## [1] 0.0556

Probability of A or B

[Video]

Solving for probability of A or B

If coins A and B are independent, and A has a 60% chance of coming up heads, and event B has a 10% chance of coming up heads, what is the probability either A or B will come up heads?

  • 6%
  • 50%
  • [*] 64%
  • 70%

Simulating probability of A or B

# Simulate 100,000 flips of a coin with a 60% chance of heads
A <- rbinom(100000, 1, .6)

# Simulate 100,000 flips of a coin with a 10% chance of heads
B <- rbinom(100000, 1, .1)

# Estimate the probability either A or B is heads
mean(A | B)
## [1] 0.6435

Probability either variable is less than or equal to 4

# Use rbinom to simulate 100,000 draws from each of X and Y
X <- rbinom(100000, 10, .6)
Y <- rbinom(100000, 10, .7)

# Estimate the probability either X or Y is <= to 4
mean(X <= 4 | Y <= 4)
## [1] 0.20702
# Use pbinom to calculate the probabilities separately
prob_X_less <- pbinom(4, 10, .6)
prob_Y_less <- pbinom(4, 10, .7)

# Combine these to calculate the exact probability either <= 4
prob_X_less + prob_Y_less - prob_X_less * prob_Y_less
## [1] 0.2057164

Multiplying random variables

[Video]

Expected value of multiplying a random variable

If X is a binomial with size 50 and p = .4, what is the expected value of 3*X?

  • 1.2
  • 20
  • [*] 60
  • 150

Simulating multiplying a random variable

Variance of a multiplied random variable

Adding two random variables

Solving for the sum of two binomial variables

Simulating adding two binomial variables

Simulating variance of sum of two binomial variables


Ch. 3 - Bayesian statistics

Updating with evidence

Updating

Updating with simulation

Updating after 16 heads

Updating with simulation after 16 heads

Prior probability

Updating with priors

Updating with three coins

Bayes’ theorem

Updating with Bayes theorem

Updating for other outcomes

More updating with priors