Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spatial Statistics and Spatial Knowledge Discovery First law of geography [Tobler]: Everything is related to everything, but nearby things are more related.

Similar presentations


Presentation on theme: "Spatial Statistics and Spatial Knowledge Discovery First law of geography [Tobler]: Everything is related to everything, but nearby things are more related."— Presentation transcript:

1 Spatial Statistics and Spatial Knowledge Discovery First law of geography [Tobler]: Everything is related to everything, but nearby things are more related than distant things. Drowning in Data yet Starving for Knowledge [Naisbitt -Rogers] Lecture 2 : Basic Statistics with R Pat Browne

2 Combinatorial Analysis Counting Permutations: the order does matter Combinations: the order doesn't matter The Pigeonhole Principle Examples

3 Combinatorial Analysis Combinatorial analysis deals with permutations of a set or bag and combinations of a set, which lead to binomial coefficients and the Binomial Theorem.

4 Rules of Counting Rule of sums: The size of the union on n finite pair wise disjoint sets is the sum of their sizes. Rule of product (a.k.a. the fundamental principle of counting): The size of the cross product of n sets is the product of their sizes. Rule of difference: The size of a set with a subset removed is the size of the set minus the size of the subset.

5 Product Rule Example If each license plate contains 3 letters and 2 digits. How many unique licenses could there be? Using the rule of products. 26  26  26  10  10 = 1,757,600

6 All Permutation of a set A permutation of a set of elements is a linear ordering (or sequence) of the elements e.g. {1,4,5} Example Permutation 1 : 1, 4, 5 Example Permutation 2 : 1, 5, 4 An anagram is a permutation of words. There are n  (n – 1)  (n - 2).. 1 permutations of a set of n elements. This is factorial n, written n!

7 All Permutation of a set There are: n  (n – 1)  (n - 2).. 1 permutations of a set of n elements. Calculated using factorial n, written n! In R this is written factorial(3) In R we can calculate the number of 2- permutations from 4 items as: factorial(4)/factorial(4-2)

8 Some Permutation of a set More generally permutation of size r from a set of size n. (16.4) P(n,r) = n!/(n-r)! The number of 2 permutations of BYTE is P(4,2) = 4!/(4-2)! = 4  3 = 12 BY,BT,BE,YB,YT,YE,TB,TY,TE,EB,EY,ET P(n,0) = 1 P(n,n-1) = P(n,n) = n! P(n,1) = n Order matters

9 3-permuations of set Calculate the number of 3-permutations of the word HAND. There are 4 objects and taking 3 at a time. 4Pick3 = 4! / (4-3)! = 4! / 1! = 24 / 1 = 24. Would you consider HDA and HAD as equivalent permutations? They are not equivalent, because each permutation is a sequence, hence and are considered distinct and both are included in the permutation.

10 r-Permutation with repetition of a set An r-permutations is a permutation that allows repetition. Here are all the 2-permutation of the letters in SON: SS,SO,SN,OS,OO,ON,NS,NO,NN. Given a set of size n, in constructing an r- permutation with repetition, for each element we have n choices. (16.6) The number of r permutations with repetition of a set of size n is n r. Note, repetition is allowed in the permutation but does not occur in the original set.

11 r-Permutation with repetition of a set (16.6) The number of r permutations with repetition of a set of size n is: n r repetition is allowed in the permutation but not in the original set, because it is a set.

12 Permutation of a bag A bag may have duplicate elements. Transposition of equal (or duplicate) elements in a permutation does not yield a different permutation e.g. AA=AA. Hence, there will be fewer permutations of a bag than a set of the same size. The permutations on the set {S,O,N} and the bag  M,O,M  are: {S,O,N} = SON,SNO,OSN,ONS,NSO,NOS  M,O,M  = MOM,MMO,OMM

13 Permutation of a bag: General Rule (16.7) The number of permutations of a bag of size n with k distinct elements occurring n 1, n 2, n 3,.. n k times is: n! n 1 !  n 2 !  n 3 !...  n k !

14 Calculating permutation of a set and a bag In R calc. number of 3-perms of set {S,O,N} factorial(3) R calc. number of 3-perms of bag  M,O,M  factorial(3) / (factorial(1) * factorial(2)) O occurs once, M twice, gives 3

15 Permutation of a bag Consider the permutation of the 11 letters of MISSISSIPPI. M occurs 1 time, I occurs 4 times, S occurs 4 times, and P occurs 2 times. Calculate this in R as follows: factorial(11)/(factorial(4)*factorial(4)*factorial(2)) Test the following in R: factorial(0) == factorial(1)

16 Permutation of a bag  O  a single permutation  M 1,O, M 2 , label the two copies of M. If we choose to distinguish the Ms we get. M 1 M 2 O,M 2 M 1 O,M 1 OM 2,M 2 OM 1,OM 1 M 2,OM 2 M 1 If we do not distinguish the Ms we get  M,O,M  = MOM,MMO,OMM

17 Permutation of a bag Calculate the number of distinguishable permutations of the word LITTLE. Basic formula is N = total number of letters n i is frequency of each different letter Note double counting of letters T and L, 6!/2!*2!*1!*1!= 720/4 = 180

18 Permutations in R On previous slides we calculated the number of permutauions. Below we calculate the actual permuations themseves. > install.packages('combinat') > library(combinat) > m <- t(array(unlist(permn(3)), dim = c(3, 6))) # we get 3!=6 permutations > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 3 2 [3,] 3 1 2 [4,] 3 2 1 [5,] 2 3 1 [6,] 2 1 3 What does this function do? sample(1:3, 3) Evaluate it 6 times and see. Evaluate permn(3), Evaluate unlist(permn(3))

19 Permutations in R Alternative way to calculate the actual permuations. > install.packages(“gregmisc”) > library(gregmisc) > length(permutations(n=3,r=2))/2 [1] 6 > permutations(n=3,r=2) [,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 2 1 [4,] 2 3 [5,] 3 1 [6,] 3 2 n Size of the source vector r Size of the target vectors

20 Combinations of a set An r-combination of a set is a subset of size r. Note the while a permutation is a sequence a combination is a set.

21 Example: Combinations of a Set An r -combination of a set is a subset of size r. A permutation is a sequence while a combination is a set. The 2-permutations (seq.) of SOHN is:,,,,,,,,,,, The 2-combinations (set) of SOHN is: {S,O},{S,H},{S,N},{O,H},{O,N},{H,N} Note in combinations order is ignored in sets {a,b}={b,a}

22 Combinations of a Set The binomial coefficient, “n choose r is written”:

23 Combinations in R install.packages(“combinat”) if not already done There are 15 ways to choose 2 items from 6 items. > choose(6,2) [1] 15 There are zero ways to pick 6 items from 2 items. > choose(2,6) [1] 0

24 Combinations in R # install.packages('combinat') if not already done # Generate all combinations of # the letters a,b,c,d taking 2 at a time. combn(letters[1:4], 2) #Gives [,1] [,2] [,3] [,4] [,5] [,6] [1,] "a" "a" "a" "b" "b" "c" [2,] "b" "c" "d" "c" "d" "d"

25 Pascal’s Triangle Beginning with row 0 and place (or column) 0, the number 20 appears in row 6, place 3. Using R we can check this. > choose(6,3) [1] 20 Check these on the triangle. > choose(7,4) [1] 35 > choose(7,2) [1] 21

26 At the tip of Pascal's Triangle is the number 1, which makes up the 0 th row. The first row (1 & 1) contains two 1's, both formed by adding the two numbers above them to the left and the right, in this case 1 and 0 (all numbers outside the Triangle are 0's). Do the same to create the 2 nd row: 0+1=1; 1+1=2; 1+0=1. And the third: 0+1=1; 1+2=3; 2+1=3; 1+0=1. In this way, the rows of the triangle go on infinitely. A number in the triangle can also be found by nCr (n Choose r) where n is the number of the row and r is the element (column) in that row. For example, in row 3, 1 is the 0 th element, 3 is element number 1, the next three is the 2 nd element, and the last 1 is the 3 rd element. chose(6,3). – gives 20

27 Special Combinations of a Set How would you verify these laws in R?

28 Calculating combinations using factorial and division

29 Calculating "n choose k". Cancellation already done for these

30 Combinations of a Set (16.10) The number of r-combinations of n elements is A student has to answer 6 out of 9 questions on an exam. How many ways can this be done?

31 Combinations with repetitions of a Set An r-combination with repetitions of a set S of size n is a bag of size r all of whose elements are in S. An r-combination of a set is a subset of that set; an r- combination with repetition of a set is a bag, since its elements need not be distinct.

32 Combinations with repetitions of a Set For example, the 2-combinations with repetition of SON are the bags:  S,O ,  S,N ,  O,N ,  S,S ,  O, O ,  N,N  On the other hand, the 2-permutations with repetition are the sequences:,,,,,,,, Note SO and OS are distinct permutations

33 Combinations with repetitions of a Set (16.12) The number of r -combinations with repetition of a set of size n is: Repetitions size Combination size

34 Combinations with repetitions of a Set Out of 7 people each gets either a burger, a cheese burger, or fish (3 choices). How many different orders are possible? The answer is the number of 7-combinations with repetition of a set of 3 elements.

35 The Equivalence of three statements (16.13) The following numbers are equal: The number of integer solutions of the equation x 1 +x 2 +x 3 +...+x n =r. The number of r -combinations with repetition of a set of size n. The number of identical ways r identical objects can be distributed among n different containers.

36 Permutations How many permutations of the letters are there in the following words: LIE: n=3, 3! = 6 BRUIT: n=5, 5! = 120 CALUMMNY: n=7, 7!=5040

37 Permutations of a bag A coin is tossed 5 times, landing Head or Tails to form an outcome. One possible outcome is HHTTT. How many possible outcomes are there? How many outcomes have one Head? How many outcomes contain at most one Head?

38 Permutations of a bag How many possible outcomes are there? Rule of product giving 2 5 =32 possible outcomes.

39 Permutations of a bag How many outcomes have one Head? Permutation of a bag with 1 Head and four Tails.

40 Permutations of a bag How many outcomes contain at most one Head? One Head No Heads At most one Head 1 + 5 = 6 (rule of sums)

41 Combinations of Set A chairman has to select a committee of 5 from a facility of 25. How many possibilities are there? How many possibilities are there if the chair should be on the committee?

42 3-permuations of set Calculate the number of 3-combinations of the string ABCDE. Combinations: In each of the 3 letters groups the position does not matter (e.g. ABC, ABD, ABE, ACD, ACE, ADE are distinct but CBA is not because it is considered equivalent to ABC) C(n,r) = n!/(r! * (n-r)!) Formula: C(5,3) = 5!/(3!*(5-3)!) = 5*4*3*2*1/(3*2*1*2*1)) = 5*2 = 10

43 Calculate the number of combinations of a set of 6 elements Formula: C(n,r) = n!/(r! * (n-r)!) Example: C(6,2) = 6!/(2!*(6-2)!) = 15 In R > choose(6,1) + choose(6,2) + choose(6,3) + choose(6,4) + choose(6,5) + choose(6,6) Gives 63

44 The Pigeonhole Principle (16.43) If more than n pigeons are placed in n holes, at least one hole will contain more than one pigeon. With more than n pigeons in n holes the average number of pigeons per hole is greater than one. The statement “at least one hole will contain more than one pigeon” is equivalent to “the maximum number of pigeons in any whole is greater than one”.

45 The Pigeonhole Principle


Download ppt "Spatial Statistics and Spatial Knowledge Discovery First law of geography [Tobler]: Everything is related to everything, but nearby things are more related."

Similar presentations


Ads by Google