library(ggplot2)
Example 2.4-5 p. 67
Try the code
# the pmf
dbinom(x=2,size = 8,prob = 0.2)
Example 2.4-7 p. 67
Try the code
# the cdf
pbinom(q = 8,size = 10,prob = 0.8) # P(X<=8)
# see if it is the same as
1-dbinom(9,10,0.8)-dbinom(10,10,0.8)
Example 2.4-8 p. 68
Try the code
pbinom(q = 5,size = 10,prob = 0.5, lower.tail = TRUE)
pbinom(q=5, size=10, prob=0.5, lower.tail = FALSE) # P(X>=6)
Problem 2.4-7 p. 72
Try the code
# part (d)
# draw two random variables from uniform distribution U(0,1) (to be discussed in Chapter 3)
x<-runif(n = 2000,min=0,max=1)
y<-runif(n = 2000,min=0,max=1)
# Count to number of point in the unit circle that is in 1st quadrant only
pQ1 <-sum(x^2+y^2<1)
# probability of W
pw<-pQ1/2000
pw
# how does it compare to pi/4
pi/4
Problem 2.4-9 p. 73
Try the code
# (b) \
# mean
20*0.8
# variance
var<- 20*0.8*0.2
var
sdev <-sqrt(var)
sdev
# (c)
# P(X=15)
dbinom(x = 15,size = 20,prob = 0.8)
# P(X>15)
1-pbinom(q = 15,size = 20,prob = 0.8, lower.tail = TRUE)
# pbinom(q = 15, size = 20, prob = 0.8, lower.tail = FALSE) # or using lower.tail = FALSE
# P(X<=15)
pbinom(q = 15,size = 20,prob = 0.8, lower.tail = TRUE)
Problem 2.4-4 p. 72
# (b)
# P(X>=2)
1-dbinom(0,7,0.15)-dbinom(1,7,0.15)
# P(X>=2)=1-P(X<=1)
1-pbinom(1,7,0.15)
pbinom(1,7,0.15,lower.tail = FALSE)
Example 2.5-1 p. 75
# P(X>=4)=P(X>3)
1-pgeom(q = 2,prob = 1/4, lower.tail = TRUE)
# or equivalently
pgeom(q = 2,prob = 1/4, lower.tail = FALSE)
Example 2.6-1 p. 82
# P(X<=6)
ppois(q = 6,lambda = 5)
# P(X>5)
ppois(q = 5,lambda = 5,lower.tail = FALSE)
# P(X=6)
dpois(x = 6,lambda = 5)
Example 2.6-4 p. 83
# P(X>=5)=P(X>4)
ppois(4,6,lower.tail = FALSE)
Problem 2.6-2 p. 85
# P(X=2) for Poisson(3)
dpois(2,3)
Problem 2.6-3 p. 85
# P(X>10) for Poisson(11)
ppois(q = 10,lambda = 11,lower.tail = FALSE)
# or
1-ppois(q = 10,lambda = 11)
Problem 2.6-5 p. 85
# P(X<=1) for Poisson(225/150)
ppois(q = 1,lambda = 225/150)
Problem 2.6-8 p. 85
# Using b(n=1000,p=0.005)
pbinom(q = 1,size = 1000,prob = 0.005)
# Using Poisson(lambda=n*p)
ppois(q = 1,lambda = 5)
# Using Poisson(5) to find P(4<=X <=6)
ppois(q = 6,lambda = 5)-ppois(q = 3,lambda = 5)
Exercise 1 If \(X \sim b(10,0.2)\). Find the \(P(2 < X < 4)\).
# Parameters
n <- 10 # Number of trials
p <- 0.2 # Probability of success
# Calculate P(2 < X < 4)
prob <- pbinom(4, size = n, prob = p) - pbinom(2, size = n, prob = p)
# Print the result
print(prob)
Exercise 2 If \(X \sim b(10,0.2)\). Find the \(P(2 \le X \le 4)\).
# Parameters
n <- 10 # Number of trials
p <- 0.2 # Probability of success
# Calculate P(2 <= X <= 4)
prob <- pbinom(4, size = n, prob = p) - pbinom(1, size = n, prob = p)
# Print the result
print(prob)
Exercise 3 If \(X \sim Poisson(4)\). Find the \(P(X \le 2)\).
# Parameter
lambda <- 4
# Calculate P(X <= 2)
prob <- ppois(2, lambda)
# Print the result
print(prob)
Exercise 4 If \(X \sim Poisson(3)\). Find the \(P(2 \le X \le 7)\).
# Parameter
lambda <- 3
# Calculate P(2 <= X <= 7)
prob <- ppois(7, lambda) - ppois(1, lambda)
# Print the result
print(prob)
Exercise 5 Draw a 1000 random
observations from uniform distribution U(0,1) store them in
a vector x. Do it over and store the values in a vector
y. Compare the sum(x) and sum(y).
Which one is bigger? Y is bigger
Try the code