################ ### EXERCISE 1.1 ################ ### Simulation of flipping a coin. ### Change the probabilities for "head" and "tail", ### change the number of flips (argument size), ### and study the distribution of the outcome ### SEE LECTURE: Definitions 1 & 3 S = sample(c("head", "tail"), size=10, replace=TRUE, prob=c(0.5, 0.5)) S table(S) prop.table(table(S)) ################ ### EXERCISE 1.2 ################ ### Simulation of tossing the dice. ### Change the number of tosses (argument "size"), ### and study the distribution of the outcome ### SEE LECTURE: Definition 2 & 3 S = sample(1:6, size=10, replace=TRUE) S table(S) prop.table(table(S)) ### Use the operators ">", ">=", "<", "<==", and "==" ### to obtain frequencies for specified subsets S>2 table(S>2) prop.table(table(S>2)) ################ ### EXERCISE 1.3 ################ ### Calculate the number of possible combinations ### when drawing k elements out of n, using the binomial coefficient. ### Use different values of k and n. ### SEE LECTURE: Theorem 1 n = 3 k = 2 ### Combinations without replicates choose(n, k) ### Combinations with replicates choose(n+k-1, k) ### Calculate all possible combinations (without replicates) ### when taking k elements out of the set x. ### Choose other sets x (e.g. weekdays) and other values of k. x = c("apple", "banana", "orange") k = 2 combn(x, k) ### Alternative with package "gtools": library(gtools) combinations(3, 2, x, repeats=FALSE) ### Calculate all possible combinations (with replicates) ### when taking k elements out of the set x. ### Choose other sets x (e.g. weekdays) and other values of k. library(gtools) combinations(3, 2, x, repeats=TRUE) ################ ### EXERCISE 1.4 ################ ### Calculate the number of possible variations ### when drawing k elements out of n. ### Use different values of k and n. ### SEE LECTURE: Theorem 2 n = 3 k = 2 ### Variation without replicates y = n:(n-k+1) y prod(y) ### Variation with replicates n^k ### Calculate all possible variations (without replicates) ### when taking k elements out of the set x. ### Choose other sets x (e.g. weekdays) and other values of k. x = c("apple", "banana", "orange") k = 2 permutations(3, 2, x, repeats=FALSE) ### Calculate all posibble variations (with replicates) ### when taking k elements out of the set x. ### Choose other sets x (e.g. weekdays) and other values of k. permutations(3, 2, x, repeats=TRUE) ### Alternative way: expand.grid(x, x) ################ ### EXERCISE 1.5 ################ ### Let X be the number of food products that can be produced by a company, ### and let P be the probability that the components for each number of ### products are available on the market. ### SEE LECTURE: Example 4 X = c(0, 1, 2, 3, 4) P = c(0.2, 0.3, 0.2, 0.2, 0.1) ### Calculate the expectation of X, i.e. the average expected number ### of products that can be produced. ### Calculate the standard deviation of X, i.e. the average deviation from ### the expectation. ### SEE LECTURE: Definitions 8 and 9 sum(X * P) sqrt(sum(X^2 * P) - sum(X * P)) ### Draw the distribution of X using a bar chart. ### Change arguments, col, lwd, cex.axis, cex.lab, xlab, ylab, main, cex.main plot(X, P, type="h", col="blue", lwd=3, cex.axis=1.5, cex.lab=1.5, xlab="Number X of products", ylab="P(X)", main="Distribution", cex.main=1.5) ### Draw the cumulative distribution function of X. ### Modify graphic parameters. ### What is the probability that 3 or less products can be produced. cumsum(P) SF = stepfun(X, c(0, cumsum(P))) plot(SF, col="grey", lwd=1, lty=3, cex.axis=1.5, cex.lab=1.5, xlab="Number X of products", ylab="F(X)=P(x<=X)", main="Cumulative distribution function", cex.main=1.5, verticals=TRUE, do.points=FALSE) lines(SF, col="blue", lwd=3, verticals=FALSE, do.points=FALSE)