[Video]
# Generate 10 separate random flips with probability .3
rbinom(10, 1, .3)
## [1] 1 1 1 1 1 1 0 0 0 1
# 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
[Video]
# 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
# 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
# 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
[Video]
# 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
# 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
[Video]
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?
# 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
# 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
[Video]
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?
# 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
# 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
[Video]
If X is a binomial with size 50 and p = .4, what is the expected value of 3*X?