## -----------------------------------------------------------------------------
n <- 10 # Number of trials
p <- 0.6 # Probability of success
k <- 4 # Number of successes

choose(n, k) * p^k * (1 - p)^(n - k)

## -----------------------------------------------------------------------------
dbinom(k, size = n, prob = p)

## -----------------------------------------------------------------------------
sum(dbinom(0:5, size = n, prob = p))
pbinom(5, size = n, prob = p) # Specific R function

## -----------------------------------------------------------------------------
1 - pbinom(5, size = n, prob = p)
pbinom(5, size = n, prob = p, lower.tail = FALSE)

## -----------------------------------------------------------------------------
n * p # Expected value obtained through analytical computations
sum(0:n * dbinom(0:n, size = n, prob = 0.6)) # Numerical computation

## -----------------------------------------------------------------------------
sum(sqrt(0:n) * dbinom(0:n, size = n, prob = 0.6))

## -----------------------------------------------------------------------------
qbinom(0.25, size = 10, prob = 0.6) # First quartile

## -----------------------------------------------------------------------------
pbinom(4, size = 10, prob = 0.6) # The value is smaller than 0.25
pbinom(5, size = 10, prob = 0.6) # The value is larger than 0.25

## -----------------------------------------------------------------------------
kk <- 0:n
prob <- dbinom(0:n, size = n, prob = p)
plot(kk, prob, type = "h", main = "binom(10, 0.6)", xlab = "k", ylab = "P(Y=k)")

## -----------------------------------------------------------------------------
x <- 1 # Point at which to compute the density
mu <- 3 # Mean
sigma2 <- 10 # Variance

1 / sqrt(2 * pi * sigma2) * exp(-1 / (2 * sigma2) * (x - mu)^2)
dnorm(x, mean = mu, sd = sqrt(sigma2))

## -----------------------------------------------------------------------------
integrate(dnorm, lower = -1, upper = 1)

## -----------------------------------------------------------------------------
pnorm(1) - pnorm(-1)

## -----------------------------------------------------------------------------
qnorm(0.75)

## -----------------------------------------------------------------------------
pnorm(qnorm(0.75))

## -----------------------------------------------------------------------------
curve(dnorm(x, mean = 0, sd = 1), from = -3, to = 3, ylab = "f(x)", main = "N(0,1)")

## -----------------------------------------------------------------------------
set.seed(123) # Fixes the "seed", see below
R <- 5
rnorm(R, mean = 0, sd = 1)

## -----------------------------------------------------------------------------
dice <- 1:6

## -----------------------------------------------------------------------------
set.seed(123) # R command to fix the "seed"
sample(x = dice, size = 1) # size = 1 implies that a single die is rolled

## -----------------------------------------------------------------------------
set.seed(140)
n <- 10
# replace = TRUE implies that the die is rolled 10 times
sim <- sample(dice, size = n, replace = TRUE)
sim

## -----------------------------------------------------------------------------
sample(1:90, size = 5, replace = FALSE)

## -----------------------------------------------------------------------------
sample(dice)
