1. When you roll a fair die 3 times, how many possible outcomes are there?
Solution: 1
p_outcomes <- 6 * 6 * 6
p_outcomes
## [1] 216
2. What is the probability of getting a sum total of 3 when you roll a die two times?
Solution: 2
die1 <- c(rep(1,6),rep(2,6),rep(3,6),rep(4,6),rep(5,6),rep(6,6))
die2 <- rep(1:6,6)
Samplespace <- data.frame(die1 = die1, die2 = die2, sum = die1 + die2)
nrow(Samplespace[Samplespace$sum == 3,]) / nrow(Samplespace)
## [1] 0.05555556
3. Assume a room of 25 strangers. What is the probability that two of them have the same birthday? Assume that all birthdays are equally likely and equal to 1/365 each. What happens to this probability when there are 50 people in the room?
Solution: 3
#To repeat this for 25 people, we'll go down to 341/365.
pdiff <- prod(365:341)/(365^25)
#And now we'll need to subtract this from 1 to get the probability that two people have the same #birthday:
psame <- 1 - pdiff
#If there are 50 people in the room, we'll go down to 316/365 in our cumulative product, and do the same #thing:
pdiff <- prod(365:316)/(365^50)
psame <- 1-pdiff
psame
## [1] 0.9703736
Sometimes you cannot compute the probability of an outcome by measuring the sample space and examining the symmetries of the underlying physical phenomenon, as you could do when you rolled die or picked a card from a shuffled deck. You have to estimate probabilities by other means. For instance, when you have to compute the probability of various english words, it is not possible to do it by examination of the sample space as it is too large. You have to resort to empirical techniques to get a good enough estimate. One such approach would be to take a large corpus of documents and from those documents, count the number of occurrences of a particular character or word and then base your estimate on that. Write a program to take a document in English and print out the estimated probabilities for each of the words that occur in that document. Your program should take in a file containing a large document and write out the probabilities of each of the words that appear in that document. Please remove all punctuation (quotes, commas, hyphens etc) and convert the words to lower case before you perform your calculations. Extend your program to calculate the probability of two words occurring adjacent to each other. It should take in a document, and two words (say the and for) and compute the probability of each of the words occurring in the document and the joint probability of both of them occurring together. The order of the two words is not important. Use the accompanying document for your testing purposes. Compare your probabilities of various words with the Time Magazine corpus: http://corpus.byu.edu/time/
library(stringr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(sqldf)
## Loading required package: gsubfn
## Loading required package: proto
## Could not load tcltk. Will use slower R code instead.
## Loading required package: RSQLite
library(DT)
filename <- "time.txt"
textlistorig <- scan(filename, character(0), encoding = "UTF-8", quote=NULL)
#textlistorig<-gsub("\\.","",textlistorig)
# textlistorig<-gsub("\\,","",textlistorig)
textlist <- gsub(",", '', textlistorig)
textlist <- gsub(".", '', textlist)
textlist <- tolower(str_replace_all(textlistorig, "[^A-Za-z/']", ""))
textlist <- str_replace_all(textlist,"/'s$", "")
textlist <- textlist[textlist != ""]
textlist <- gsub("'", '', textlist)
textlist <- gsub("//", '', textlist)
textlist <- gsub("/", '', textlist)
#Calculate word frequencies
textlist.freq<-table(textlist)
newDF <- data.frame(cbind(names(textlist.freq),as.integer(textlist.freq)))
newDF = newDF[-1,]
Single word Text List:
head(newDF)
## X1 X2
## 2 a 19290
## 3 aa 5
## 4 aaa 5
## 5 aaaaaaaaaagh 1
## 6 aaaand 1
## 7 aaaargh 1
Lets test probability of a given word.
singleWord <- function(sWord) {
cWord <- as.numeric(fn$sqldf("select X2 from newDF where X1 = '$sWord'"))/sum(as.numeric(newDF$X2))
return(cWord)
}
sTest <- singleWord("from")
sTest
## [1] 4.587926e-05
Lets test probability of two given word, in this case “for”, “the”.
bigramtest <- function(filename, word1, word2){
textlistorig <- scan(filename, character(0), encoding = "UTF-8", quote=NULL)
textlist <- tolower(str_replace_all(textlistorig, "[^A-Za-z/']", ""))
textlist <- str_replace_all(textlist,"/'s$", "")
textlist <- textlist[textlist != ""]
textlistDF <- data.frame(Word = textlist)
textlistDFS <- textlistDF %>%
group_by(Word) %>%
summarize(Count = n())
textlistDFS <- textlistDFS %>%
mutate(Probability = Count / sum(textlistDFS$Count))
textlistDF$NextTo <- c(NA, head(textlist, -1))
textlistDF <- textlistDF[2:nrow(textlistDF),]
textlistDF$Flag <- ifelse((textlistDF$Word == word1 & textlistDF$NextTo == word2) |
(textlistDF$Word == word2 & textlistDF$NextTo == word1),1,0)
return(sum(textlistDF$Flag) / nrow(textlistDF))
}
lr <- bigramtest("time.txt", "for", "the")
lr
## [1] 0.001525302