In R code Name CSC315 Lab 4 Probability Add R code to
In R code:
########################################################
# Name:
# CSC-315
# Lab #4: Probability
########################################################
##########################################################################
# Add R code to the script below and create a Notebook to complete
# the steps and explicitly answer the following questions
##########################################################################
# Basic Probability Questions. You do not need to use R for these,
# but you should put the answers in your R script.
# A standard deck of cards contains 52 cards, with 13 cards from each
# suit (hearts, clubs, diamonds, and spades).
#1. If one card is selected at random, what is the probability that it
# is the ace of spades?
#2. What is the probability that it is NOT the ace of spades?
#3. What is the probability that it is an ace?
#4. What is the probability that it is an ace or a 4?
# You should use R to answer the remaining questions.
# A probability distribution of a discrete random variable gives
# the probability for each value of the variable.
# Use the \'count.heads\' function below to answer the first question
################################################################
# this function counts the number of heads in \'n\' coin tosses
################################################################
count.heads <-function(n) {
s = sample(c(\"H\", \"T\"), n, replace = TRUE)
num = sum(s==\"H\")
return(num)
}
#5. Find the empirical distribution of X = the number of heads in 3
# coin tosses by first using the replicate function to create a
# vector of X values in 1000 experiments. Then create a relative
# frequency table (which approximates the probability distribution table) and
# also create a bar graph of the relative frequencies, making sure to
# appropriately label the x-axis, y-axis, and title of the graph. Also
# set range of the y-axis by specifying the argument ylim = c(0,1).
# Note: your results should roughly match the theoretical probabilities
# which are P(X=0) = 0.125, P(X=1) = 0.375, P(X=2) = 0.375, and
# P(X = 3) = 0.125
#6. Repeat the previous exercise but use the \'permutations\' function from
# the package \'gtools\' to calculate the classical probability distribution
# table for X, which will give you the theoretical probabilities above
# (Note: you do not need to construct a graph for this one).
#7. If you roll two dice (each with the values 1-6), what is the
# probability that the sum of the numbers is 7? Answer this question
# by first writing a function that randomly rolls two dice and then
# calculates the sum. Then use the \'replicate\' function to find the
# empirical probability based on 5000 experiments.
#8. Repeat the previous exercise but use the \'permutations\' function to
# first enumerate the sample space obtained by rolling two dice.
# Poker Time! The commands below generate all possible poker hands.
# Here we ignore the suit since this is not important for our
# calculations. We also use combinations instead of
# permutations to create the sample space. With combinations the order
# (e.g., of cards in a hand) does not matter. The cards are
# also sampled without replacement (repeats.allowed = FALSE by default).
# We also specify \'set = FALSE\' to allow for duplicate values in the deck
# vector. Finally, each combination is equally likely, so classical
# probability can be used.
deck = rep(1:13,4)
hands = combinations(52, 5, deck, set = FALSE)
#9. How many possible poker hands are there?
##############################################################
# this function returns true if a hand contains a 4-of-a-kind
##############################################################
four.of.a.kind <- function(x) {
t = table(x)
m = max(t)
if (m == 4) return (TRUE)
return (FALSE)
}
#10. Show that the probability of being dealt a four-of-a-kind is
# approximately 0.00024 (or 1/4165). Note: You MUST use the
# apply function and the four.of.a.kind function above to find this.
# Because the hands matrix contains more than
# 2.5 million rows, even with the apply function this
# calculation will take time (5-10 minutes). You may therefore want to test
# on a subset of the hands matrix first. There are two 4-of-a-kinds
# if you look at the first 20,000 rows
#11. Create a function that determines whether a vector \'x\' contains
# a full house (i.e., 3 of a kind and 1 pair). You can assume
# that \'x\' represents 5 cards.
#12. Show that the probability of being dealt a full house is
# approximately 0.00144 (roughly 1/694). In the first
# 20,000 rows of the hands matrix, there are 18 full houses.
Solution
The pumping lemma for context-free languages (called simply \"the pumping lemma\" for the remainder of this article) describes a property that every one context-free languages ar sure to have.
The property could be a property of all strings within the language that ar of length a minimum of p, wherever p could be a constant—called the pumping length—that varies between context-free languages.
Say s could be a string of length a minimum of p that\'s within the language.
The pumping lemma states that s is split into 5 substrings, s = uvwxy, wherever vx is non-empty and also the length of vwx is at the most p, specified continuation v and x any (and the same) range of times in s produces a string that\'s still within the language (it is feasible and sometimes helpful to repeat zero times, that removes v and x from the string). This method of \"pumping up\" extra copies of v and x is what provides the pumping lemma its name.
Finite languages (which ar regular and thence context-free) adjust the pumping lemma trivially by having p adequate to the utmost string length in L and one. As there are not any strings of this length the pumping lemma isn\'t profaned.
Usage of the lemma[edit]
The pumping lemma is usually wont to prove that a given language L is non-context-free, by showing that willy-nilly long strings s ar in L that can\'t be \"pumped\" while not manufacturing strings outside L.
For example, the language L = zero } is shown to be non-context-free by victimization the pumping lemma in an exceedingly proof by contradiction. First, assume that L is context free. By the pumping lemma, there exists associate degree whole number p that is that the pumping length of language L. contemplate the string s = apbpcp in L. The pumping lemma tells US that s is written within the kind s = uvwxy, where u, v, w, x, and y ar substrings, specified |vwx| p, |vx| 1, and uviwxiy is in L for each whole number i zero. By the selection of s and also the undeniable fact that |vwx| p, it\'s simply seen that the substring vwx will contain no over 2 distinct symbols. That is, we\'ve got one amongst 5 prospects for vwx:
vwx = aj for a few j p.
vwx = ajbk for a few j and k with j+k p.
vwx = bj for a few j p.
vwx = bjck for a few j and k with j+k p.
vwx = cj for a few j p.
For each case, it\'s simply verified that uviwxiy doesn\'t contain equal numbers of every letter for any i one. Thus, uv2wx2y doesn\'t have the shape aibici. This contradicts the definition of L. Therefore, our initial assumption that L is context free should be false.
While the pumping lemma is usually a great tool to prove that a given language isn\'t context-free, it doesn\'t provides a complete characterization of the context-free languages. If a language doesn\'t satisfy the condition given by the pumping lemma, we\'ve got established that it\'s not context-free.
On the opposite hand, there ar languages that aren\'t context-free, however still satisfy the condition given by the pumping lemma, for instance L = j, k, l i, j , i1 : for s=bjckdl with e.g. j1 opt for vwx to consist solely of b’s, for s=aibjcjdj opt for vwx to consist solely of a’s; in each cases all tense strings ar still in L.[2] There ar a lot of powerful proof techniques accessible, like Ogden\'s lemma, however conjointly these techniques don\'t provides a complete characterization of the context-free languages.
The pumping lemma for context-free languages (called simply \"the pumping lemma\" for the remainder of this article) describes a property that every one context-free languages ar sure to have.
The property could be a property of all strings within the language that ar of length a minimum of p, wherever p could be a constant—called the pumping length—that varies between context-free languages.
Say s could be a string of length a minimum of p that\'s within the language.
The pumping lemma states that s is split into 5 substrings, s = uvwxy, wherever vx is non-empty and also the length of vwx is at the most p, specified continuation v and x any (and the same) range of times in s produces a string that\'s still within the language (it is feasible and sometimes helpful to repeat zero times, that removes v and x from the string). This method of \"pumping up\" extra copies of v and x is what provides the pumping lemma its name.
Finite languages (which ar regular and thence context-free) adjust the pumping lemma trivially by having p adequate to the utmost string length in L and one. As there are not any strings of this length the pumping lemma isn\'t profaned.
Usage of the lemma[edit]
The pumping lemma is usually wont to prove that a given language L is non-context-free, by showing that willy-nilly long strings s ar in L that can\'t be \"pumped\" while not manufacturing strings outside L.
For example, the language L = zero } is shown to be non-context-free by victimization the pumping lemma in an exceedingly proof by contradiction. First, assume that L is context free. By the pumping lemma, there exists associate degree whole number p that is that the pumping length of language L. contemplate the string s = apbpcp in L. The pumping lemma tells US that s is written within the kind s = uvwxy, where u, v, w, x, and y ar substrings, specified |vwx| p, |vx| 1, and uviwxiy is in L for each whole number i zero. By the selection of s and also the undeniable fact that |vwx| p, it\'s simply seen that the substring vwx will contain no over 2 distinct symbols. That is, we\'ve got one amongst 5 prospects for vwx:
vwx = aj for a few j p.
vwx = ajbk for a few j and k with j+k p.
vwx = bj for a few j p.
vwx = bjck for a few j and k with j+k p.
vwx = cj for a few j p.
For each case, it\'s simply verified that uviwxiy doesn\'t contain equal numbers of every letter for any i one. Thus, uv2wx2y doesn\'t have the shape aibici. This contradicts the definition of L. Therefore, our initial assumption that L is context free should be false.
While the pumping lemma is usually a great tool to prove that a given language isn\'t context-free, it doesn\'t provides a complete characterization of the context-free languages. If a language doesn\'t satisfy the condition given by the pumping lemma, we\'ve got established that it\'s not context-free.
On the opposite hand, there ar languages that aren\'t context-free, however still satisfy the condition given by the pumping lemma, for instance L = j, k, l i, j , i1 : for s=bjckdl with e.g. j1 opt for vwx to consist solely of b’s, for s=aibjcjdj opt for vwx to consist solely of a’s; in each cases all tense strings ar still in L.[2] There ar a lot of powerful proof techniques accessible, like Ogden\'s lemma, however conjointly these techniques don\'t provides a complete characterization of the context-free languages.



