## Seminar 2. Probabilities and quantiles ##
### Normal distribution
# P(Z < 1.96)
pnorm(1.96)
# P(X < 1.96) X ~ N(2, sigma = 1)
pnorm(1.96, mean = 2, sd = 1)
# P(-8 < X < 7) X ~ N(6, sigma = 3)
pnorm(7, mean = 6, sd = 3) - pnorm(-8, mean = 6, sd = 3)
### Quantiles
# quantile of level 0.2 for Z ~ N(0, 1)
qnorm(p = 0.2)
# quantile of level 0.36 for X ~ N(5, sigma = 2)
qnorm(p = 0.36, mean = 5, sd = 2)
# Problem 3, part 2 (from seminar09, Moivre-Laplace Theorem)
n <- 6
p <- 0.5
q <- 1 - p
# P(2 <= S <= 4)
pnorm(4, mean = n*p, sd = sqrt(n*p*q)) -
pnorm(2, mean = n*p, sd = sqrt(n*p*q))
### Binomial distribution
# P(X <= 6) X ~ Binom(n = 6, p = 0.5)
pbinom(q=6, size=6, prob=0.5)
# P(X = 6)
dbinom(6, size=6, prob=0.5)
# Problem 3, part 1 (from seminar09, Exact solution)
# P(X = 2) + P(X = 3) + P(X = 4)
dbinom(2, size=6, prob=0.5) +
dbinom(3, size=6, prob=0.5) +
dbinom(4, size=6, prob=0.5)
# Binomial coefficient C_n^k
# C_10^2, n = 10, k = 2
choose(10, 2)
### Random samples and histograms
# random sample of n = 20 from Z ~ N(0, 1)
v <- rnorm(20)
v
# histogram - does not look like normal as n = 20 is very small
hist(v)
# random sample of n = 1000 from Z ~ N(0, 1)
v <- rnorm(1000)
# histogram - looks like normal as we increase n to 1000
hist(v)
# add color
hist(v, col = "hotpink")
# add title
hist(v, col = "hotpink", main = "Моя первая розовая гистограмма")