Tutorial 2 - Questions.
Tutorial 2 - Questions.
7 Mar 2023
Scenarios
Many instances of binomial distributions can be found in real life.
- For example, if a new drug is introduced to cure a disease, it either cures the disease
(it’s successful) or it doesn’t cure the disease (it’s a failure).
- If you purchase a lottery ticket, you’re either going to win money, or you aren’t.
Basically, anything you can think of that can only be a success or a failure can be represented by
a binomial distribution.
A binomial distribution can be thought of as simply the probability of a SUCCESS or FAILURE
outcome in an experiment or survey that is repeated multiple times.
Maths
X ~ Binomial(n, p)
- For binomial distribution of the outcomes, n = number of observations, p = probability
Question: it is a discrete probability distribution or continues one? – discrete
Coding
In these exercises, you will practice using rbinom() function to generate random “flips” that are
either “heads” (1) or “tails” (0), to simulate random data
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Binomial.html
rbinom(n, size, prob)
generates required number of random values of given probability from a given sample.
n - number of observations
size - number of trials
prob - probability of success of each trial.
1
ICT583
7 Mar 2023
how about increasing the number of observations, as well as the number of trials in each
observation?
f (event) = prob
rbinom(10000, 12, .5) %>% hist(freq = F)
2
ICT583
7 Mar 2023
Coding
dbinom(x, size, prob)
gives the probability density distribution at each point.
x - vector of numbers (specify where you want to evaluate the binomial density)
If you flip 10 coins each with a 30% probability of coming up heads, what is the probability
exactly 2 of them are heads?
# Calculate the probability that 2 are heads using dbinom
dbinom(2, 10, .3)
plot(1:10, dbinom(1:10, size=10, prob=.3), type='h')
3
ICT583
7 Mar 2023
For example, you will observe 100 times, the random deviates are
r = rbinom(100, 10, .3)
# we know the chance of head is 0.5, so, what is the probability of getting 2 heads given 10
trials per observation?
dbinom(2, 10, .5)
4
ICT583
7 Mar 2023
Scenario
If you flip ten coins that each have a 30% probability of heads, what is the probability at least
five are heads?
# Confirm your answer with a simulation using rbinom, with 10000 observations
mean( rbinom(10000, 10, .3) >= 5 )
5
ICT583
7 Mar 2023
Variance
What is the variance of a binomial distribution where n coins are flipped, each having a p chance
of heads?
Var= n * p * (1-p)
When n = 1, p = .5, var = 0.25, SD = sqrt(.25)
When n = 25, p = .3, var = 5.25 , SD = 2.291288
# Confirm with a simulation using rbinom
r = rbinom(10000, 25, 0.3)
var(r)
sd(r)
6
ICT583
7 Mar 2023
by hands
what is the probability they will both happen?
Joint probability: P(A ⋂ B) = P(A) * P(B) # intersection
= .4 * .2
what is the probability either A or B will come up heads?
Union probability: P(A ⋃ B) # union
= P(A) + P(B) – P(A ⋂ B)
= .4+.2 - .4 * .2
Coding
Assuming 100000 observations done, one trial,
A <- rbinom(100000, 1, .4)
B <- rbinom(100000, 1, .2)
a = mean(A)
b = mean(B)
j = a * b
u = a + b – j
# or
mean(A & B)
mean(A | B)
7
ICT583
7 Mar 2023
When a random variable X is normally distributed with mean mu and standard deviation sigma.
# Draw a random sample of 100,000 from the Binomial(1000, .2) distribution
b <- rbinom(100000, 1000, .2)
plot(hist(b, breaks=30))
hist(b, breaks=50, main = "my binomial dist")
8
ICT583
7 Mar 2023
# probability density
g <- rnorm(100000, 0, sqrt(1))
plot(hist(g, breaks=30))
hist(g, breaks=50, main = "Gaussian dist", freq = F)
9
ICT583
7 Mar 2023
10
ICT583
7 Mar 2023
# dice
dice = 1:6
p = 1/6
EV = sum(dice)*p
var = map(dice, function(x) p * ( x - EV)^2 ) %>% unlist %>% sum
sd = sqrt(var)
# blood type
A couple has a 25% (p) chance of a having a child with type O
blood. What is the chance that three (X) of their five (n) kids
have type O blood?
dbinom(3, 5, .25)
p= map(0:5, function(x) dbinom(x, 5, .25))
EV = map2(0:5, p, function(x, y) x*y ) %>% unlist %>% sum
var= pmap(
list(
as.list(0:5),
EV,
p
) ,
\(x,y,z) z * (x-y)^2
) %>% unlist %>%sum
n = 5
p = .25
1-p = .75
5*.25
5*.25*.75
11