# ============================================================================== # --------------------------------- SEMINAR 5 ---------------------------------- # ============================================================================== # ----------------------------------- TASK 1 ----------------------------------- # ...................................... A ..................................... # Create density plot, distribution function plot, and quantile function plot # of random variable X with binomial distribution Bi(n = 100, p = 0.5). # HINT: apply these functions on the values from the x-axis: ?pbinom ?dbinom ?qbinom ### DISTRIBUTION FUNCTION: xx <- 0:100 dist.b <- pbinom(xx, size = 100, prob = 0.5) # computes the values of distribution function for each value of vector 0:100 # of all possible outcomes (number of successes) of binomial distribution plot(xx, dist.b, type = 's', col = "red", main = "Distribution", xlab = "", ylab = "") ### DENSITY FUNCTION: xx <- 0:100 density.b <- dbinom(xx, size = 100, prob = 0.5) plot(xx, density.b, type = 'h', col = "red", main = "Probability mass function", xlab = "", ylab = "") points(xx, density.b, pch = 19, col = "darkred", cex = 0.5) # computes the values of density function for each value of vector 0:100 # of all possible outcomes (number of successes) of binomial distribution ### QUANTILE FUNCTION: xx <- seq(0, 1, by = 0.01) quantile.b <- qbinom(xx, size = 100, prob = 0.5) plot(xx, quantile.b, type = 's', col = "red", main = "Quantile function", xlab = "", ylab = "") # computes the values of quantile function for each value of given vector of values # between 0 and 1 of different probabilities # ...................................... B ..................................... # Generate random vector of length 1000 with independent ele- # ments, where each element X is following a binomial distribution # Bi(100; 0.5). Produce a histogram and compare it to the # desinty plot of the same distribution. ?rbinom n <- rbinom(1000, size = 100, prob = 0.5) # vector of 1000 random numbers hist(n, xlim = c(0, 100), breaks = seq(0, 1000, by = 3), freq = F, col = "salmon2") d.b <- dbinom(0:100, size = 100, prob = 0.5) # density of binomial distribution points(0:100, d.b, pch = 19, type = "b") # ----------------------------------- TASK 2 ----------------------------------- # ...................................... A ..................................... # The same for Poisson distribution Po(lambda = 10) # HINT: apply these functions on the values from the x-axis: ?ppois ?dpois ?qpois ### DISTRIBUTION FUNCTION: dist.p <- ppois(0:30, lambda = 10) plot(0:30, dist.p, type = 's', col = "red", main = "Distribution", xlab = "", ylab = "") ### DENSITY FUNCTION: density.p <- dpois(0:30, lambda = 10) plot(0:30, density.p, type = 'h', col = "red", main = "Probability mass function", xlab = "", ylab = "") points(0:30, density.p, pch = 19, col = "darkred", cex = 0.5) ### QUANTILE FUNCTION: xx <- seq(0, 1, by = 0.01) quantile.p <- qpois(xx, lambda = 10) plot(xx, quantile.p, type = 's', col = "red", main = "Quantile function", xlab = "", ylab = "") # ...................................... B ..................................... # Generate random vector of length 1000 with independent ele- # ments, where each element X is following a poisson distribution # Po(lambda = 10). Produce a histogram and compare it to the # desinty plot of the same distribution. ?rpois n <- rpois(1000, lambda = 10) # vector of 1000 random numbers d.p <- dpois(0:30, lambda = 10) # density of binomial distribution hist(n, ylim = c(0, 0.15), freq = F, col = "red") points(0:30, d.p, pch = 19, type = "b") box() # ----------------------------------- TASK 3 ----------------------------------- # ...................................... A ..................................... # Compute the probability that some realization of X from N(20, 16) lies among # values 12 and 28 (use the cumulative distribution function). pnorm(28, mean = 20, sd = sqrt(16)) - pnorm(12, mean = 20, sd = 4) # F(b) - F(a) # ...................................... B ..................................... # Find the value of the random variable X from N(20, 16) for which the # probability that X is lesser or equal to it is exactly 95%. qnorm(0.95, mean = 20, sd = 4) # ----------------------------------- TASK 4 ----------------------------------- # ...................................... A ..................................... n <- 50000 p <- 0.5 # 1000 random numbers from the binomial distribution: X <- rbinom(1000, n, p) # standardization of the previous vector (from the lecture slaid): U <- (X - n * p) / sqrt(n * p * (1 - p)) xx <- seq(-3, 3, by = 0.01) density.n <- dnorm(xx, 0, 1) # density of normal distribution hist(U, freq = F, col = "red", ylim = c(0, 0.5)) lines(xx, density.n, lwd = 2) # histogram of the transformed data from the binomial distribution looks quite # similar to the density function of the normal distribution (thanks to the # central limit theorem) # ...................................... B ..................................... # EXTRA TASK n <- 500 p <- 0.5 x <- dbinom(0:n, n, p) # probability function of binom. distr. for all possible number of successes 0:n # use formulas from the lecture for computing # standart deviation and expactation value of X: exp.b <- n * p sd.b <- sqrt(n * p * (1 - p)) # plot the result: xx <- seq(0, n, by = 0.01) d.norm <- dnorm(xx, mean = exp.b, sd = sd.b) plot(0:n, x, xlab = "number of successes", col = "red", pch = 16, ylab = "value in Pascal triangle", main = paste("n=", n)) lines(xx, d.norm) # the probability function of the binomial distribution for large n looks # similar to the denstiy function of normal distribution, which is the consequence # of the central limit theorem