chips<-round(1/540,4)
chips
## [1] 0.0019
toppings<-c('A','C')
meats<-c('E','T')
dressings<-c('F','V')
combinations<-function(a,b,c){
for(i in a){
for(j in b) {
for(k in c) {
vect<-paste0(c(i,j,k), collapse=",")
print(vect)
}
}
}
}
combinations(toppings,meats,dressings)
## [1] "A,E,F"
## [1] "A,E,V"
## [1] "A,T,F"
## [1] "A,T,V"
## [1] "C,E,F"
## [1] "C,E,V"
## [1] "C,T,F"
## [1] "C,T,V"
There are 10 heart not face cards out of 52.
round(10/52,4)
## [1] 0.1923
i<-0
j<-0
for(dice1 in 1:6){
for(dice2 in 1:6){
if(sum(dice1,dice2)<6){
i<-i+1
}
j<-j+1
}
}
probability<-round(i/j,4)
probability
## [1] 0.2778
P(Male|TotalGender)=P(Male)/P(TotalGender)
probability<-round(sum(233,159,102,220,250)/2001,4)
probability
## [1] 0.4818
the first card will be a club = (13/52)
the second card will be a black card = (26/52)
the third card will be a face card = (12/52)
probability<-round((13/52)*(26/52)*(12/52),4)
probability
## [1] 0.0288
P1 = the probability of choosing a heart for the first card drawn = (13/52)
P2 = the probability of choosing a spade for the second card drawn = (13/51)
without replacement conditional probability rule: P=(P1∗P2)/P1
probability<-round(((13/52)*(13/51))/(13/52),4)
probability
## [1] 0.2549
the probability of choosing a heart = (13/52)
the probability of choosing a red card = (25/51)
probability<-round((13/52)*(25/51),4)
probability
## [1] 0.1225
probability<-round((4/85)*(12/84),4)
probability
## [1] 0.0067
Step 1 - P(MaleGrad|Male)=P(MaleGrad)/P(Male)=(52/300)/(141/300)=52/141
Step 2 - P(Male|TotalGrad)=P(Male)/P(TotalGrad)=(52/300)/(102/300)=52/102
# step 1
probability1<-round(52/141,4)
probability1
## [1] 0.3688
# step 2
probability2<-round(52/102,4)
probability2
## [1] 0.5098
drink<-c(6)
sandwich<-c(5)
chips<-c(3)
count<-0
combinations<-function(a,b,c){
for(i in 1:a){
for(j in 1:b) {
for(k in 1:c) {
count<-count+1
}
}
}
print(paste("Possible different value meal = ",count))
}
combinations(drink,sandwich,chips)
## [1] "Possible different value meal = 90"
Formula: P(n)=n!
patients<-5
print(paste("Possible ways can the doctor visit patients = ",factorial(patients)))
## [1] "Possible ways can the doctor visit patients = 120"
Permutation Formula: P(n,k)=n!/(n−k)!
songs<-5
total_songs<-8
print(paste("Lineups for ",songs," out of ",total_songs," songs = ",factorial(total_songs)/factorial(total_songs - songs)))
## [1] "Lineups for 5 out of 8 songs = 6720"
fours<-3
sixes<-5
two<-1
dice_rolled_9_times<-9
print(paste("Ways to get ",fours," fours, ",sixes," sixes and ",two," two = ",factorial(dice_rolled_9_times)/(factorial(fours)*factorial(sixes)*factorial(two))))
## [1] "Ways to get 3 fours, 5 sixes and 1 two = 504"
cards<-3
total_cards<-52
print(paste("Possible ways to draw",cards,"cards from",total_cards,"cards (without replacement) = ",round(factorial(total_cards)/(factorial(cards)*factorial(total_cards - cards)))))
## [1] "Possible ways to draw 3 cards from 52 cards (without replacement) = 22100"
TVs<-12
surround_sound_systems<-9
DVD_players<-5
count<-0
combinations<-function(a,b,c){
for(i in 1:a){
for(j in 1:b) {
for(k in 1:c) {
count<-count+1
}
}
}
print(paste(count, "home theater systems can be built with",TVs,"TVs,",surround_sound_systems,"surround_sound_systems and",DVD_players,"DVD_players"))
}
combinations(TVs,surround_sound_systems,DVD_players)
## [1] "540 home theater systems can be built with 12 TVs, 9 surround_sound_systems and 5 DVD_players"
letters<-5
total_letters<-26
digits<-3
total_digits<-10
print(paste("Password with",letters,"letters with",digits,"digits = ",(factorial(total_letters)/factorial(total_letters - letters))*(factorial(total_digits)/factorial(total_digits - digits))))
## [1] "Password with 5 letters with 3 digits = 5683392000"
9P4 9P4 = Permutation(9,4)
permutation<-factorial(9)/factorial(9-4)
permutation
## [1] 3024
11C8 11C8 = Combination(11,8)
combination<-factorial(11)/(factorial(8)*factorial(11-8))
combination
## [1] 165
12P8/12C4�812𝐶𝐶4
permutation=function(a,b){factorial(a)/factorial(a-b)}
combination=function(a,b){factorial(a)/(factorial(b)*factorial(a-b))}
eval<-permutation(12,8)/combination(12,4)
eval
## [1] 40320
cabinets<-7
candidates<-13
permutation=function(a,b){factorial(a)/factorial(a-b)}
permutation(candidates,cabinets)
## [1] 8648640
word<-c('Population')
total_letters<-nchar(word)
repeated_p<-2
repeated_o<-2
print(paste("Ways to arrange the word '",word,"' =",factorial(total_letters)/(factorial(repeated_p)*factorial(repeated_o))))
## [1] "Ways to arrange the word ' Population ' = 907200"
Step 1 - E(x) = sum(x*p(x))
Step 2 - Var(x) = sum((x^2 - E(x))^2 * px)
Step 3 - SD(x) = sqrt(Var(x))
Step 4 - E(x) = sum((x>=9)*p(x))
Step 5 - E(x) = sum((x<=7)*p(x))
x<-c(5,6,7,8,9)
px<-c(0.1,0.2,0.3,0.2,0.2)
# step 1
print(paste("E(x) = ",round(sum(x*px),1)))
## [1] "E(x) = 7.2"
# step 2
print(paste("Var(x) = ",round(sum((x - sum(x*px))^2 * px),1)))
## [1] "Var(x) = 1.6"
# step 3
print(paste("SD(x) = ",round(sqrt(sum((x - sum(x*px))^2 * px)),1)))
## [1] "SD(x) = 1.2"
# step 4
print(paste("E(x) = ",round(sum((x>=9)*px),1)))
## [1] "E(x) = 0.2"
# step 5
print(paste("E(x) = ",round(sum((x<=7)*px),1)))
## [1] "E(x) = 0.6"
Step 1 - E(x) = sum(xp(x)^3 + x(1-p(x)^3))
Step 2 - E1(x) = 994 * E(x)
x1<-23
x2<--4
px<-188/376
# step 1
print(paste("E(x) = ",round(sum(x1*px^3 + x2*(1-px^3)),2)))
## [1] "E(x) = -0.62"
# step 2
print(paste("994 * E(x) = ",994 * round(sum(x1*px^3 + x2*(1-px^3)),2)))
## [1] "994 * E(x) = -616.28"
Step 1 - E(x) = sum(xp(x)^3 + x(1-p(x)^3))
Step 2 - E1(x) = 615 * E(x)
x1<-1
x2<--7
n<-11
k<-8
val<-0
for(i in 1:k){
val<-val+(factorial(n)/(factorial(i)*factorial(n-i)))
}
px<-val/2^11
# step 1
print(paste("E(x) = ",round(sum(x1*px + x2*(1-px)),2)))
## [1] "E(x) = 0.73"
# step 2
print(paste("E(x) = ",round(615*sum(x1*px + x2*(1-px)),2)))
## [1] "E(x) = 451.64"
Step 1 - E(x) = sum(xp(x) + x(1-p(x)))
Step 2 - E1(x) = 632 * E(x)
x1<-583
x2<--35
px<-(13/52)*(12/51)
# step 1
print(paste("E(x) = ",round(sum(x1*px + x2*(1-px)),2)))
## [1] "E(x) = 1.35"
# step 2
print(paste("E(x) = ",round(632*1.35,2)))
## [1] "E(x) = 853.2"
n<-10
k<-2
print(paste("P(x) = ",round(pbinom(k, size = n, prob = 0.3),3)))
## [1] "P(x) = 0.383"
n<-5
print(paste("P(x) = ", n*0.3))
## [1] "P(x) = 1.5"
Poisson’s distribution: for k=5: p(k)=(λ^k⋅e^(−λ))/k!
for k>5: ppois(k, λ, lower.tail = FALSE)
# for k=5: round(((5.5)^5*exp(-5.5))/factorial(5),4)
# for k>5 (this will answer the question above):
round(ppois(5, 5.5, lower.tail = FALSE),4)
## [1] 0.4711
round(ppois(4, 5.7, lower.tail = FALSE),4)
## [1] 0.6728
# λ = 0.4 times/day * 1 day * 7 times
round(ppois(1, 0.4*1*7, lower.tail = TRUE),4)
## [1] 0.2311
Hypergeometric distribution: phyper(q, m, n, k, lower.tail = TRUE, log.p = FALSE)
employees_over_50<-6
employees_under_50<-19
dismissed<-8
df<-data.frame(employees_over_50, employees_under_50,dismissed)
q<-1
prob_more_than_1_over_50<-round(phyper(q, df$employees_over_50, df$employees_under_50, df$dismissed, lower.tail=FALSE),3)
cbind(df, prob_more_than_1_over_50)
## employees_over_50 employees_under_50 dismissed prob_more_than_1_over_50
## 1 6 19 8 0.651
patients_with_heart_problem<-10
patients_with_no_heart_problem<-25-10
selection<-8
df<-data.frame(patients_with_heart_problem, patients_with_no_heart_problem, selection)
q<-6 # < 7
prob_less_than_7_patients<-round(phyper(q, df$patients_with_heart_problem, df$patients_with_no_heart_problem, df$selection, lower.tail=TRUE),3)
cbind(df, prob_less_than_7_patients)
## patients_with_heart_problem patients_with_no_heart_problem selection
## 1 10 15 8
## prob_less_than_7_patients
## 1 0.998