http://www.yorku.ca/nuri/econ3500/ch7-binomial-combinations-pg215.219.pdf (relevant text pages) Ch7 install.packages("combinat") library(combinat) install.packages("gtools") library(gtools) install.packages("prob") library(prob) library(combinat) library(gtools) library(prob) # combinations {gtools} combinations(3,2,letters[1:3]) combinations(3,2,letters[1:3],repeats=TRUE) permutations(3,2,letters[1:3]) permutations(3,2,letters[1:3],repeats=TRUE) permutations permutations(3,2,repeats=TRUE) permutations(3,2) permutations(3,2,4:6) x=4:6 permutations(3,2,x) combinations combinations(3,2) combinations(3,2,1:3) combinations(3,2,1:3,repeats=TRUE) 7.3 # combinations and permutations urnsamples(x, size = ?, replace = ?, ordered = ?) # x a vector or data frame from which sampling should take place. # size number indicating the sample size. # replace logical (TRUE/FALSE) whether sampling done with replacement. # ordered logical (TRUE/FALSE) whether order is important. # “In how many distinct ways can we permute the order of our n distinct # objects?” p. 215 ===================================================================== Case1: with replacement (repetitions allowed) and order matters x=1:3 urnsamples(x, size = 2, replace = TRUE, ordered = TRUE) # n^2 expand.grid(x,x) urnsamples(x, size = 3, replace = TRUE, ordered = TRUE) # n^n expand.grid(x,x,x) expand.grid(LETTERS[1:3],LETTERS[1:3]) # expand.grid(LETTERS[1:3],LETTERS[1:3],LETTERS[1:3]) ======================================================================= Case2 # without replacement (no repetitions) and order matters (PERMUTATIONS) # # # # # n ways of picking the first in line (n - 1) ways of picking the second in line (n - 2) ways of picking the third in line . . . 1 way of picking the last object # n(n - 1)(n - 2)(n - 3) . . . 1 # This product is called “n factorial” and is written “n!”; that is # n! = n(n - 1)(n - 2)(n - 3) . . . 1 # In R # factorial(n) factorial(3) # =6 factorial(4) # = 24 x=c("Orange","Banana","Pear","Apple") # pages 215-216 urnsamples(x, size = 4, replace = FALSE, ordered = TRUE) # 24 permutations 4! # if with replacement (replace=TRUE) 4x4x4x4=256 permutations(4,4,x) # Consider a clearer case: permutations of A, B, C x=c("A","B","C") # ch7 page 216 urnsamples(x, size = 3, replace = FALSE, ordered = TRUE) # permutations(3,3,x) # x=LETTERS[1:3] # permutations(3,3,LETTERS[1:3]) Page 217 permutation formula => n!/(n-r)! Page 218: x=c("A","B","C","D") # ch7 page 218 urnsamples(x, size = 2, replace = FALSE, ordered = TRUE) # permutations(4,2,x) # # # # “This list shows the permutations in which we are interested. List the permutations in which we are not interested, the ones left out, and count them. You should have (4! - 12) = 24 - 12 = 12 The 24 permutations where there are 2 containing AB (for example): unique(urnsamples(x, size = 4, replace = FALSE, ordered = TRUE)) ======================================================================= Case3: without replacement and order does not matter (COMBINATIONS) page 218 indistinguishable objects # n=5 r=2 (n-3)=3 n!/(n-r)!=20 x=c("A","A","A","B","C") # indistinguishable ch7 page 218 urnsamples(x, size = 5, replace = FALSE, ordered = TRUE) unique(urnsamples(x, size = 5, replace = FALSE, ordered = TRUE)) # to see that there are 6 of each permutation # y=urnsamples(x, size = 5, replace = FALSE, ordered = TRUE) See: 7.3-binomial-combinations-pg218-duplicates.pdf # permutations(5, 5, x, set=FALSE, repeats.allowed=FALSE) # unique(permutations(5, 5, x, set=FALSE, repeats.allowed=FALSE)) # combinations => n!/r!(n-r)! # page 219 n!/r!(n-r)! => 35 n=7, r=4 n-r=3 x=c("A","A","A","A","B","B","B") # indistinguishable urnsamples(x, size = 4, replace = FALSE, ordered = FALSE) urnsamples(1:7, size = 4, replace = FALSE, ordered = FALSE) # combn(7,4) # combinations(7,4) # combinations(7,4,8:14) The Binomial Expansions “bi” means two. means two terms. Definition: The factorial of an integer n ≥ 0, written n!, is n × n-1 × ... × 2 × 1. In particular, 0! = 1. “four factorial” “n factorial” 1!=1 The following is expression (7.1) on page 219 4!=1x2x3x4=24 In S-Plus the function factorial factorial(4) [1] 24 factorial(0:4) [1] 1 1 2 6 24 combinations’ expression (7.1) on pg 219 is the function choose choose(n,x) choose(4,2) [1] 6 choose(4,0:4) In S-Plus ‘ [1] 1 4 6 4 1 The ‘permutations’ expression on pg 218 [n!/(n-r)!] is the function Choose(n,r,order=T) choose(4,2,order=T) [1] 12 Pascal’s Triangle (pgs 227 and 591-592 of text) Expand Expression = Expression = Expression (a + b)4 = a4 + 4a3b + 6a2b2 + 4ab3 + b4 Expand Pascal’s Triangle (pg 591 of text) is a triangle of coefficients that is based on the binomial theorem. It is formed by adding the two numbers directly above and placing 1’s on the outer sides. 1 1 1 1 1 1 1 1 1 The binomial 7 1 8 9 36 2 3 4 5 6 21 84 1 3 6 10 15 28 1 1 4 10 20 35 56 126 5 15 35 70 1 1 6 21 56 126 7 28 84 coefficient expansion is as follows: Expand the following: 1 1 8 1 36 9 1 Expand Expression Result In general, Expression Result Expression Result (relevant text pages on combinations) http://www.yorku.ca/nuri/econ3500/ch7-binomial-combinations-pg215.219.pdf