0% found this document useful (0 votes)
8 views39 pages

R Lab5

This document discusses discrete and continuous probability distributions, including the binomial and Poisson distributions. For the binomial distribution, it provides the formulas and explains how to calculate probabilities using R code. It gives examples calculating probabilities for binomial distributions. For the Poisson distribution, it similarly provides the formula and explains R code to calculate probabilities, and gives examples. It also compares the binomial and Poisson distributions through an example problem.

Uploaded by

AMRIT RANJAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views39 pages

R Lab5

This document discusses discrete and continuous probability distributions, including the binomial and Poisson distributions. For the binomial distribution, it provides the formulas and explains how to calculate probabilities using R code. It gives examples calculating probabilities for binomial distributions. For the Poisson distribution, it similarly provides the formula and explains R code to calculate probabilities, and gives examples. It also compares the binomial and Poisson distributions through an example problem.

Uploaded by

AMRIT RANJAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Discrete and Continuous Probability distributions

Lab - 6

S. Devi Yamini

August 3, 2020

August 3, 2020 1 / 22
LAB 6

AIM
Computing, plotting, and visualizing discrete and
continuous distributions and visualizing it.

August 3, 2020 2 / 22
Probability distributions

Discrete
Binomial distribution

August 3, 2020 3 / 22
Probability distributions

Discrete
Binomial distribution
Poisson distribution

August 3, 2020 3 / 22
Probability distributions

Discrete
Binomial distribution
Poisson distribution

Continuous
Normal distribution

August 3, 2020 3 / 22
BINOMIAL DISTRIBUTION

August 3, 2020 4 / 22
Binomial Distribution

Binomial probability
P(X = x) = xn p x q n−x , x = 0, 1, . . . , n


n - number of trials
x - number of successes
p - prob of success
q - prob of failure

August 3, 2020 5 / 22
Binomial Distribution

Binomial probability
P(X = x) = xn p x q n−x , x = 0, 1, . . . , n


n - number of trials
x - number of successes
p - prob of success
q - prob of failure

Mean= np
Variance= npq

August 3, 2020 5 / 22
Binomial distribution

To find P(X = x), the R-code is dbinom(x,n,p)

August 3, 2020 6 / 22
Binomial distribution

To find P(X = x), the R-code is dbinom(x,n,p)


To display the probabilities for all possible values of X , the R-code is
x =0:n
dbinom(x,n,p)

August 3, 2020 6 / 22
Binomial distribution

To find P(X = x), the R-code is dbinom(x,n,p)


To display the probabilities for all possible values of X , the R-code is
x =0:n
dbinom(x,n,p)
The cumulative distribution P(X ≤ x) is got by
pbinom(x,n,p,lower.tail=T)
Here lower.tail takes true because X covers the values from 0 to x

August 3, 2020 6 / 22
Binomial distribution

To find P(X = x), the R-code is dbinom(x,n,p)


To display the probabilities for all possible values of X , the R-code is
x =0:n
dbinom(x,n,p)
The cumulative distribution P(X ≤ x) is got by
pbinom(x,n,p,lower.tail=T)
Here lower.tail takes true because X covers the values from 0 to x
P(X > x) is got by pbinom(x,n,p,lower.tail=F)

August 3, 2020 6 / 22
Binomial distribution

To find P(X = x), the R-code is dbinom(x,n,p)


To display the probabilities for all possible values of X , the R-code is
x =0:n
dbinom(x,n,p)
The cumulative distribution P(X ≤ x) is got by
pbinom(x,n,p,lower.tail=T)
Here lower.tail takes true because X covers the values from 0 to x
P(X > x) is got by pbinom(x,n,p,lower.tail=F)
Plotting the binomial distribution:
x =0:n
y =dbinom(x,n,p)
plot(x,y ,type= “h”)

August 3, 2020 6 / 22
Binomial Distribution

Problem 1
For a random variable X following B(7, 41 ), (a) Compute the probability of
two successes, (b) Compute the binomial probabilities for the whole space
(c) Display those probabilities in a table (d) Plot the distribution.

August 3, 2020 7 / 22
Binomial Distribution

Problem 1
For a random variable X following B(7, 41 ), (a) Compute the probability of
two successes, (b) Compute the binomial probabilities for the whole space
(c) Display those probabilities in a table (d) Plot the distribution.
(a) dbinom(2, size= 7,prob= 41 )

August 3, 2020 7 / 22
Binomial Distribution

Problem 1
For a random variable X following B(7, 41 ), (a) Compute the probability of
two successes, (b) Compute the binomial probabilities for the whole space
(c) Display those probabilities in a table (d) Plot the distribution.
(a) dbinom(2, size= 7,prob= 41 )
[1] 0.3114624

August 3, 2020 7 / 22
Binomial Distribution

Problem 1
For a random variable X following B(7, 41 ), (a) Compute the probability of
two successes, (b) Compute the binomial probabilities for the whole space
(c) Display those probabilities in a table (d) Plot the distribution.
(a) dbinom(2, size= 7,prob= 41 )
[1] 0.3114624
(b) dbinom(0 : 7, 7, 14 )

August 3, 2020 7 / 22
Binomial Distribution

Problem 1
For a random variable X following B(7, 41 ), (a) Compute the probability of
two successes, (b) Compute the binomial probabilities for the whole space
(c) Display those probabilities in a table (d) Plot the distribution.
(a) dbinom(2, size= 7,prob= 41 )
[1] 0.3114624
(b) dbinom(0 : 7, 7, 14 )
(c) p = data.frame(0 : 7, dbinom(0 : 7, 7, 14 )
round(p, 4)

August 3, 2020 7 / 22
Binomial Distribution

Problem 1
For a random variable X following B(7, 41 ), (a) Compute the probability of
two successes, (b) Compute the binomial probabilities for the whole space
(c) Display those probabilities in a table (d) Plot the distribution.
(a) dbinom(2, size= 7,prob= 41 )
[1] 0.3114624
(b) dbinom(0 : 7, 7, 14 )
(c) p = data.frame(0 : 7, dbinom(0 : 7, 7, 14 )
round(p, 4)
(d) plot(0 : 7, dbinom(0 : 7, 7, 41 ), type = ’o’)

August 3, 2020 7 / 22
Binomial Distribution

Problem 2
If 10 % of the screws produced by an automatic machine are defective,
find the probability that out of 20 screws selected at random, there are (a)
exactly 2 defectives (b) At most 2 defectives (c) between 1 and 3
defectives ( inclusive)

August 3, 2020 8 / 22
Binomial Distribution

Problem 2
If 10 % of the screws produced by an automatic machine are defective,
find the probability that out of 20 screws selected at random, there are (a)
exactly 2 defectives (b) At most 2 defectives (c) between 1 and 3
defectives ( inclusive)
(a) dbinom(2, 20, 0.10)

August 3, 2020 8 / 22
Binomial Distribution

Problem 2
If 10 % of the screws produced by an automatic machine are defective,
find the probability that out of 20 screws selected at random, there are (a)
exactly 2 defectives (b) At most 2 defectives (c) between 1 and 3
defectives ( inclusive)
(a) dbinom(2, 20, 0.10)
(b) pbinom(2, 20, 0.10, lower .tail = T )

August 3, 2020 8 / 22
Binomial Distribution

Problem 2
If 10 % of the screws produced by an automatic machine are defective,
find the probability that out of 20 screws selected at random, there are (a)
exactly 2 defectives (b) At most 2 defectives (c) between 1 and 3
defectives ( inclusive)
(a) dbinom(2, 20, 0.10)
(b) pbinom(2, 20, 0.10, lower .tail = T )
(c) sum(dbinom(1 : 3, 20, 0.10))

August 3, 2020 8 / 22
Binomial Distribution

Problem 2
If 10 % of the screws produced by an automatic machine are defective,
find the probability that out of 20 screws selected at random, there are (a)
exactly 2 defectives (b) At most 2 defectives (c) between 1 and 3
defectives ( inclusive)
(a) dbinom(2, 20, 0.10)
(b) pbinom(2, 20, 0.10, lower .tail = T )
(c) sum(dbinom(1 : 3, 20, 0.10))
Compute the probability for at least 2 defectives

August 3, 2020 8 / 22
POISSON DISTRIBUTION

August 3, 2020 9 / 22
Problem 1:

1. Compute P(X=5) with Poisson parameter λ = 7


dpois(x=5,lambda=7)
OR
dpois(5,7)

2. Compute P(X=0), P(X=1),. . .,P(X=5)


dpois(0:5, 7)

3. Compute P(X≤ 5)
sum(dpois(0:5, 7))
OR
ppois(q=5, lambda=7, lower.tail=T)

4. Compute P(X¿5)
ppois(q=5, lambda=7, lower.tail=F)

August 3, 2020 10 / 22
Problem 2

Check the relationship between mean and variance in Poisson distribution


with λ = 4 and n = 100

R code
x = 0 : 100
p = dpois(x, 4)
mean = sum(x ∗ p)
var = sum(x 2 ∗ p) − (mean)2
mean
var

August 3, 2020 11 / 22
Problem 3

Compute the poisson probabilities and cumulative probabilities of the


values between 0 and 10 for the parameter 2 in a table (rounded upto 4
decimal places).

p=data.frame(0:10, dpois(0:10,2), ppois(0:10,2))


round(p,4)
p

August 3, 2020 12 / 22
Problem 4

If there are twelve cars crossing a bridge per minute on an average, find
the probability of having sixteen or more cars crossing the bridge in a
particular minute.

August 3, 2020 13 / 22
Problem 4

If there are twelve cars crossing a bridge per minute on an average, find
the probability of having sixteen or more cars crossing the bridge in a
particular minute.

ppois(15, lambda=12, lower.tail=F)

August 3, 2020 13 / 22
Problem 5

Compute the Binomial and Poisson probabilities using the data n = 8 and
λ = 2.4. Also plot the probabilities
Binomial
x =0:8
p=dbinom(x,8,0.3)
plot(x, p, type=”h”, main=”Pmf for binomial distribution”,col=2)

Poisson
x =0:8
p=dpois(x,2.4)
plot(x,p,type=”h”, main=”Pmf for Poisson distribution”,col=4)

August 3, 2020 14 / 22
Try these

Poisson distribution with parameter 2


How to obtain a sequence from 0 to 10
Calculate P(0), P(1),. . .,P(10) and make the output prettier
Find P(X≤ 6)
Sum all the probabilities
Find P(X¿6)
Make a table of first 11 poisson probs and make the output prettier
Plot the probabilities. Put some labels on both the axes and a title

August 3, 2020 15 / 22
NORMAL DISTRIBUTION

August 3, 2020 16 / 22
Problem 1

Create a sequence of numbers from -10 to 10 incremented


by 0.1. Let the mean=2.5, standard deviation=0.5.
Visualize the normal curve for the above sequence. Also
find the cummulative distribution function.
Code:
x=seq(-10,10,by=0.1)
y=dnorm(x, mean=2.5,sd=0.5)
plot(x,y,type=”l”)
y1=pnorm(x,mean=2.5,sd=0.5)
plot(x,y1)

August 3, 2020 17 / 22
Problem 2

Create a sequence of 200 numbers with x=-3 to 3.


Visualize the standard normal curve with mean=0 and
sd=1
Code:
x=seq(-3,3,length=200)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type=”l”)

August 3, 2020 18 / 22
Problem 3

Find the area to the left of 0, given a sequence from -3 to


3 follows Normal distribution wtih mean=0 and sd=1
Code:
x=seq(-3,3,length=200)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type=”l”)
x=seq(-3,0,length=100)
y=dnorm(x,0,1)
polygon(c(-3,x,0),c(0,y,0),col=”red”)
pnorm(0,mean=0,sd=1)

August 3, 2020 19 / 22
Problem 4

For Problem 3, find the area to the left of 1. (First draw


the image and then compute)
Code:
x=seq(-3,3,length=200)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type=”l”)
x=seq(-3,1,length=100)
y=dnorm(x,0,1)
polygon(c(-3,x,1),c(0,y,0),col=”blue”)
pnorm(1,mean=0,sd=1)

August 3, 2020 20 / 22
Problem 5

Compute the area between x=1 and 2 for Problem 3.


Code:
x=seq(-3,3,length=200)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type=”l”)
x=seq(1,2,length=100)
y=dnorm(x,0,1)
polygon(c(1,x,2),c(0,y,0),col=”red”)
pnorm(2,mean=0,sd=1)-pnorm(1,mean=0,sd=1)
0.1359

August 3, 2020 21 / 22
Problem 6
Find the Quantile( or Percentile) when area is given. That
is find the value of x corresponding to 40%
x = seq(−3, 3, length = 200)
y = dnorm(x, mean = 0, sd = 1)
plot(x, y , type = ”l”)
qnorm(0.40, mean = 0, sd = 1)
−0.2533
x = seq(−3, −0.2533, length = 100)
y = dnorm(x, mean = 0, sd = 1)
polygon(c(−3, x, −0.2533), c(0, y , 0), col = 4)
text(−1, 0.2, “0.40”)
The last command prints 40% at the position
x = −1,y = 0.2. August 3, 2020 22 / 22

You might also like