Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
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
Combinatorial Analysis
•
•
•
•
•
Counting
Permutations: the order does matter
Combinations: the order doesn't matter
The Pigeonhole Principle
Examples
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.
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.
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
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!
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 2permutations from 4 items as:
• factorial(4)/factorial(4-2)
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!
Order
• P(n,1) = n
matters
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 < HDA >
and < HAD> are considered distinct and both
are included in the permutation.
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 rpermutation 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 nr. Note, repetition
is allowed in the permutation but does not occur
in the original set.
r-Permutation with repetition of a
set
• (16.6) The number of r permutations with
repetition of a set of size n is:
• nr
• repetition is allowed in the permutation but
not in the original set, because it is a set.
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
Permutation of a bag:
General Rule
• (16.7) The number of permutations of
a bag of size n with k distinct
elements occurring n1, n2, n3,.. nk
times is:
n!
n1! n2! n3! ... nk!
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
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)
Permutation of a bag
• O a single permutation
• M1,O, M2 , label the two copies of M.
• If we choose to distinguish the Ms we get.
M1M2O,M2M1O,M1OM2,M2OM1,OM1M2,OM2M1
• If we do not distinguish the Ms we get
• M,O,M = MOM,MMO,OMM
Permutation of a bag
• Calculate the number of distinguishable
permutations of the word LITTLE.
• Basic formula is
• N = total number of letters
• ni is frequency of each different letter
• Note double counting of letters T and L,
• 6!/2!*2!*1!*1!= 720/4 = 180
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))
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
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.
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:
<SO>,<SH>,<SN>,<OH>,<ON>,<OS>,<HN>,<HS>,<HO>,<NS>,<NO>,<NH>
• 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}
Combinations of a Set
• The binomial coefficient, “n choose r is written”:
n
n!
r r!(n r )!
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
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"
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
chose(6,3) . – gives 20
At the tip of Pascal's Triangle is the number 1, which makes up the 0th 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 2nd 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 0th element, 3 is element number 1, the next three is
the 2nd element, and the last 1 is the 3rd element.
Special Combinations of a Set
n
1
0
n
1
n
n
n
1
How would you verify these laws in R?
n
n
n 1
Calculating combinations using
factorial and division
8! 8 7 6!
8 7 56
6!
6!
Calculating
n
k
"n choose k".
8 8 7
28
2 1 2
Cancellation
already done for
these
9 98 7 6
126
4 1 2 3 4
12 12 1110 9 8
792
1 2 3 4 5
5
Combinations of a Set
• (16.10) The number of r-combinations of n
elements is n
r
• A student has to answer 6 out of 9 questions on
an exam. How many ways can this be done?
9 9!
9 87
84
6 6!3! 3 2 1
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 rcombination with repetition of a set is a
bag, since its elements need not be
distinct.
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:
• <S,S>,<S,O>,<S,N>,<O,S>,<O,O>,<O,N>,<N,S>,<N,O>,<N,N>
Note SO and OS are
distinct permutations
Combinations with repetitions of a
Set
• (16.12) The number of r-combinations with
repetition of a set of size n is:
Repetitions
size
n r 1
r
Combination
size
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.
3 7 1
9!
36
7 7!2!
The Equivalence of three
statements
• (16.13) The following numbers are equal:
• The number of integer solutions of the
equation x1+x2+x3+...+xn=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.
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
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?
Permutations of a bag
How many possible outcomes are there?
Rule of product giving 25=32 possible
outcomes.
Permutations of a bag
How many outcomes have one Head?
Permutation of a bag with 1 Head and four Tails.
5!
5
1!4!
Permutations of a bag
• How many outcomes contain at most one
Head?
5
!
• One Head
5
1!4!
• No Heads
5!
1
0!5!
• At most one Head 1 + 5 = 6 (rule of sums)
Combinations of Set
• A chairman has to select a committee of 5 from
a facility of 25. How many possibilities are
there?
25
25!
53130
5 5!20!
• How many possibilities are there if the chair
should be on the committee?
24
24!
10626
4 4!20!
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
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
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”.
The Pigeonhole Principle