#This is a script to demonstrate some aspects
# of topic 11, probability.
################################################
## Important Note: Students are not expected ##
## to be able to come up with the ideas and ##
## programming examples demonstrated on this ##
## script. ##
################################################
#
#
# First, we want to install gnrnd4(), just in case
# we need it.
source("../gnrnd4.R")
#
# to demonstrate a deterministic task just look at
# your algorithm (the set of steps you follow) for doing
# long division. Try doing 507605 / 13
# 39046 R 7
# +--------
# 13 | 507605
# 39
# --
# 117
# 117
# ---
# 06
# 0
# --
# 60
# 52
# --
# 85
# 78
# --
# 7
# You follow those prescribed steps and each time
# you get the same answer
#
# of course we could do this in R
as.integer( 507605/13)
507605 - 39046*13
# or better for finding the remainder
507605 %% 13
#
# Here is another deterministic task
# ( remember that we already loaded gnrnd4)
gnrnd4(873461401, 400001)
L1
# You can run those two last lines any number of
# times and you always get the same 15 values:
# three 1's, six 2's, two 3's, and four 4's,
# and you get them in the same order:
# 4 4 4 2 2 2 4 2 3 1 3 1 2 2 1
# here is a non-deterministic task
L2 <- as.integer( runif(15,1,5) )
L2
# Every time you run those last two lines you
# get a different listing of 15 values, each
# of which is between 1 and 4, inclusive.
###################################
## The following is beyond the ##
## expectations for what you ##
## should be able to do for this ##
## class. ##
###################################
# As described in the web page,
# here is a Risk "battle" between
# 3 attacking red armies and 2 defending
# blue armies. Run the next line
# each time there is a battle
{
blue_die<- as.integer(runif(2,1,7))
red_die <- as.integer(runif(3,1,7))
blue_die <-sort( blue_die, decreasing=TRUE)
red_die <- sort( red_die, decreasing=TRUE)
print( c(" red=",red_die))
print( c("blue=",blue_die))
R<-0
B<-0
if( red_die[1] > blue_die[1] )
{ B<- -1} else { R <- -1}
if( red_die[2] > blue_die[2] )
{ B<- B - 1} else { R <- R -1}
print( c("red=",R,"blue=",B))
} # end of block
###################################
## The following is beyond the ##
## expectations for what you ##
## should be able to do for this ##
## class. ##
###################################
# Demo of a sample space
#
# make a table of outcomes from two
# lists of possible values
{
m_list <- 4:19
t_list <- sort(sample( m_list, 6))
s_list <- sort(sample( m_list, 6))
t_list
s_list
demo_table <- matrix(0,nrow=6, ncol=6)
rownames( demo_table ) <- s_list
colnames( demo_table ) <- t_list
demo_table
for( i in 1:6 )
{ k <- as.integer( t_list[i]/3 )
for( j in 1:6)
{ m <- as.integer(s_list[j]/2)
demo_table[j,i] = k*m
}
}
print("top/3 * s/2 with integer division")
demo_table
}
# now find the frequency of each different
# table value
table( demo_table )
# using that we can express the
# probability of getting any particular
# result.
## Look ways of counting
#
# straight multiplication
#
# example, we have 4 things in group 1
# we have 3 things in group 2
# we have 5 things in group 3
# how many arrangements are there of
# a thing from group 1, a thing from
# group 2, and a thing from group 3?
#
# conceptually, that will be
4*3*5
###################################
## The following is beyond the ##
## expectations for what you ##
## should be able to do for this ##
## class. ##
###################################
#
# we could construct such a situation
# and then create a listing of all such
# threesomes
group_1 <- c("brown", "black", "gray",
"tan")
group_2 <- c("big","little", "tiny")
group_3 <- c("mouse","cat","dog",
"hamster","gerbil")
#
{ L4 <- NULL
for( i in 1:4)
{ g_1 <- group_1[i]
for ( j in 1:3)
{ g_2 <- group_2[j]
for ( k in 1:5 )
{ new_item <- paste(g_1,g_2,
group_3[k])
L4 <- c(L4,new_item)
}
}
}
L4
}
#################
# permutations
#
# Here we start with a fixed number of
# different things and we ask for the
# number of ways to arrange some
# selection of those things.
#
# for example, we had group 1 above
group_1
# how many different ways are there
# to arrange those four things
# to do this we want to use the R command
# permutations, but that is not a built-in
# function. We will need to load it
# from the web. This may take a moment or
# two ...
install.packages("gtools")
library(gtools)
# now we can use the function.
# Note that in this approach we do not
# see the function listed in our environment.
permutations( 4, 4, group_1)
# a slightly different question
# could be raised if we do not want to
# use all of the items in our arrangement:
#
# Show all of the arrangements of the
# values in group 1 if we only take 2
# at a time
permutations(4, 2, group_1)
# What if we look at group_3.
#
# Show all of the arrangements of the
# values in group 3 if we only take 2
# at a time
permutations(5, 2, group_3)
# it was interesting to use permutations()
# to produce all of the possible
# arrangements. From the list we can see
# the actual number of such arrangements.
# However, we want to get that number
# of arrangements without getting the list.
#
# Look at the web pages for the math behind
# this, but we will just use the function
# nPr() or num_perm() to do this.
# These are functions that I have provided.
source("../permutations.R")
# we could find the number of permutations
# of 5 things taken two at a time via
nPr(5,2)
# or by using the alternative function
num_perm(5,2)
######################################
## the two functions merely arrive ##
## at the answer using different ##
## approaches (i.e., algorithms) ##
######################################
# How many permutations are there for
# 15 things taken 7 at a time?
nPr(15,7)
# As you can see we really would not want
# to produce the list of all those
# 32,432,400 things just to find out
# how may permutations there are.
########################################
## You are expected, in this course, ##
## to be able to find the number of ##
## permutations of n things taken r ##
## at a time. I suggest using nPr() ##
## or num_perm() to do this. ##
########################################
# find the number of permutations of 24
# things take 5 at a time
num_perm( 24, 5 )
############################
# combinations
#
###################################
## The following is beyond the ##
## expectations for what you ##
## should be able to do for this ##
## class. ##
###################################
# When we look at combinations we
# disregard the order of the items.
# so in group 1 we had 4 things:
group_1
# if we ask for the permutations of those
# 4 things taken 2 at a time we can get
permutations(4,2,group_1)
# but notice that we have both
# [1,] "black" "brown"
# and
# [4,] "brown" "black"
# These have the same items, just in
# a different order
# For combinations we do not want to
# see these as different results.
# We can use the function combinations()
# to get the list of such combinations.
#
# Note that combinations was loaded at
# the same time we got permutations.
combinations(4,2,group_1)
#
# remember finding permutations of
# the 5 things in group 3 taken 3 at a time
permutations( 5, 3, group_3)
# but the combination of cat dog gerbil
# appears 6 times as
# [1,] "cat" "dog" "gerbil"
# [4,] "cat" "gerbil" "dog"
#[13,] "dog" "cat" "gerbil"
#[16,] "dog" "gerbil" "cat"
#[25,] "gerbil" "cat" "dog"
#[28,] "gerbil" "dog" "cat"
#
# In fact each item in the permutation
# list appears 6 times in the list if
# we disregard order.
# Look at the combinations of 5 things
# taken 3 at a time from group 3
combinations( 5, 3, group_3)
#
# Again, we rarely want to see all of
# the combinations. Usually, we just
# want to find the number of combinations
# of n things taken r at a time.
#
# See the web pages for the math behind
# this, but we have two functions, nCr()
# and num_comb() that just find the
# desired number, although they arrive
# at the answer in slightly different
# ways.
#
# We need to load these functions
#
source("../combinations.R")
# Then, the number of combinations of
# 5 things taken 3 at a time is
nCr(5,3)
# or we could do
num_comb( 5, 3 )
# How many combinations are there of 27
# things taken 5 at a time?
nCr( 27,5 )
# How many combinations are there of 27
# things taken 22 at a time?
nCr( 27,22 )
# For discussion: why did we get the
# same answer for the two cases?
#
# How many combinations are there of 31
# things taken 6 at a time?
nCr( 31, 6 )
# How many combinations are there of 31
# things taken 25 at a time?
nCr( 31, 25 )
########################################
## You are expected, in this course, ##
## to be able to find the number of ##
## combinations of n things taken r ##
## at a time. I suggest using nCr() ##
## or num_comb() to do this. ##
########################################
########################################
## Just for completeness, you might ##
## note that doing the ##
## source("../combinations.R") ##
## actually loads all 4 functions ##
## nPr(), nCr(), num_perm() and ##
## num_comb(). ##
########################################
#########################################
# Contingency tables
#
###################################
## The following is beyond the ##
## expectations for what you ##
## should be able to do for this ##
## class. ##
###################################
#
# Let us make up one
gnrnd4(187341901, 63500300)
L1
c_table <- matrix( L1, nrow=4)
c_table
rs <- rowSums( c_table )
c_table <- cbind( c_table, rs)
c_table
cs <- colSums( c_table )
c_table <- rbind( c_table, cs)
c_table
rownames( c_table) <- c("AA","AAA","C","D","Total")
colnames( c_table) <- c("Ever","Rayo","Maxw",
"ProC","Amaz","total")
c_table
##########################################
## Now that we have the table you are ##
## expected to be able to use the ##
## values in the table to answer some ##
## probability questions. ##
##########################################
# if an item is selected at random,
# what is the probability that
# it as a AA package?
# P(AA)=2670/12587
# it is a ProC package?
# P(Proc)=2242/12587
# it is a Rayo C package?
# P( Rayo and C)=546/12587
# it is a Rayo or a C Package?
# P( Rayo or C)=(2643+3331-546)/12587
# it is a Rayo or ProC package?
# P( Rayo or ProC)=(2643+2242)/12587
# it is Not a D package?
# P( D' )=(12587-3438)/12587
# it is Not a Amaz package?
# P( Amaz' ) = 1 - 2704/12587
# it is neither a
# C nor Ever package?
# P( C' or Ever')=(12587-2695-3331+662)/12587
# given it is a D it is a Maxw
# package?
# P( Maxw | D ) =671/3438
# given that it is a ProC it is a AAA
# package?
# P( AAA | Proc ) = 468/2242