0% found this document useful (0 votes)
4 views7 pages

Poisson Distribution Rlab

The document explains the Poisson distribution, characterized by a single parameter lambda, which represents the average number of occurrences in a given interval. It details key properties, core functions for generating random numbers and calculating probabilities, as well as data handling and ggplot2 functions for visualizing distributions. Several programs simulate Poisson distributions for different lambda values, demonstrating how the distribution changes from right-skewed to more symmetric as lambda increases, and validates the distribution through simulation.

Uploaded by

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

Poisson Distribution Rlab

The document explains the Poisson distribution, characterized by a single parameter lambda, which represents the average number of occurrences in a given interval. It details key properties, core functions for generating random numbers and calculating probabilities, as well as data handling and ggplot2 functions for visualizing distributions. Several programs simulate Poisson distributions for different lambda values, demonstrating how the distribution changes from right-skewed to more symmetric as lambda increases, and validates the distribution through simulation.

Uploaded by

sauryaprakash196
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

POISSON DISTRIBUTION

The Poisson distribution is defined by a single parameter, lambda which represents the
average number of occurrences in that interval.

Key Properties:

 Discrete: It only deals with whole numbers (you can't have 2.5 emails).

 Independent: One event happening doesn't make the next one more or less likely.

 Constant Rate: The average rate (lambda) is assumed to be constant over the interval.

The four core functions are:

 rpois(n,lambda) - generates random numbers from Poisson distribution.


 dpois(x,lambda) - gives exact probability P ( X=x )
 ppois(q,lambda) - gives cumulative probability P ( X ≤ q )
 qpois(p,lambda) - finds value of X for given probability.

The Data Handling functions are:

 [Link]() - creates a table-like structure.


 head() - shows first 6 rows.
 tail() - shows last 6 rows.
 melt() - from reshape2 package converts wide format to long format.
 [Link]() - converts to numeric type.
 [Link]()- converts to categorical variable , used for grouping in plots.
 str_extract()- from stringr package , extracts numbers or patterns from text.

ggplot2 functions are:

 ggplot(data, aes())- creates plot object.


 aes()- maps variables to x-axis, y-axis, colour and fill.
 geom_histogram()- draws histogram.
 geom_bar()- used for bar plots (when data already summarized).
 geom_line()- draws line graph.
 geom_point()- adds points to graph.
 geom_density()- draws smooth density curve.
 facet_wrap(~variable)- creates separate panels for each category.
 scale_fill_manual()- sets custom colors manually.
 ggtitle() - adds title.
 xlab() and ylab()- set axis labels.
 theme_minimal() / theme_light() – changes overall appearance.
Few other functions are:
 binwidth = 1 (since Poisson is discrete, bin width must be 1.)
 alpha = (controls transparency (0 to 1)).
 color = (sets border or line color).
 position = "dodge" (separates bars side by side.)

To generate random counts, the density, the distribution, use rpois, dpois, ppois, respectively
and as 𝜆 grows large the Poisson distribution begins to resemble the normal distribution.

PROGRAMS
1) A tech company observes that the number of system errors per day follows a Poisson
distribution.

Assume the average number of daily errors (λ) can vary depending on server load:

 λ = 1 (Very Low Traffic)

 λ = 2 (Low Traffic)

 λ = 5 (Moderate Traffic)

 λ = 10 (High Traffic)

 λ = 20 (Peak Load)

Simulate and compare Poisson distributions for different values of λ and study how the
shape of the distribution changes as λ increases.

library(ggplot2)

library(reshape2)

library(stringr)

pois1<-rpois(n=10000, lambda=1)

pois2<-rpois(n=10000, lambda=2)
pois5<-rpois(n=10000, lambda=5)

pois10<-rpois(n=10000, lambda=10)

pois20<-rpois(n=10000, lambda=20)

pois<- [Link](Lambda.1=pois1,

Lambda.2=pois2,

Lambda.5=pois5,

Lambda.10=pois10,

Lambda.20=pois20)

pois<- melt(data=pois, [Link]="Lambda", [Link]="x")

pois$Lambda <- [Link]([Link](

str_extract(string=pois$Lambda, pattern="\\d+)))

ggplot(pois, aes(x = x, fill = Lambda)) +

geom_histogram(binwidth = 1, color = "black") +

facet_wrap(~Lambda) + scale_fill_manual(values = c("red",


"blue", "green", "orange", "purple")) +

ggtitle("Probability Mass Function") + theme_minimal()


The Poisson distribution was simulated for different values of λ.
For small λ, the distribution is highly right-skewed.
As λ increases, the distribution becomes more symmetric and spread out.
Since mean and variance are both equal to λ, increasing λ increases dispersion.
For large λ, the Poisson distribution approaches the Normal distribution.

2) Assume number of rainy events per day in different cities follows Poisson
distribution:

 Small Town → λ = 1

 City → λ = 4

 Metro → λ = 8

 Coastal Region → λ = 12

Simulate Poisson distributions for different values of λ and compare their shapes using
histograms and density curves. Interpret how the distribution changes as λ increases.

library(ggplot2)

library(reshape2)

library(stringr)

rain1<-rpois(10000, lambda = 1)

rain4<-rpois(10000, lambda = 4)

rain8<-rpois(10000, lambda = 8)

rain12<-rpois(10000, lambda = 12)

rain_data <- [Link](

Lambda.1= rain1,

Lambda.4= rain4,

Lambda.8= rain8,

Lambda.12= rain12

)
rain_data <- melt(rain_data, [Link] = "Lambda",
[Link] = "Events")

rain_data$Lambda <- [Link]([Link](

str_extract(rain_data$Lambda, "\\d+")))

ggplot(rain_data, aes(x = Events, fill = Lambda)) +

geom_histogram(aes(y = ..density..),

binwidth = 1,

color = "black",

alpha = 0.6) +

geom_density(alpha = 0.3) +

facet_wrap(~Lambda, scales = "free") +

scale_fill_manual(values = c("red", "blue", "green",


"purple")) +

ggtitle("Comparison of Poisson Distributions (Rainfall


Events)") +

xlab("Number of Events per Day") +

ylab("Density") + theme_light() + theme([Link] =


element_text(hjust = 0.5, size =14))
The Poisson distribution was simulated for λ = 1, 4, 8, and 12.
For small λ, the distribution is highly right-skewed.
As λ increases, the distribution becomes more symmetric and spread out.
The density curves show that for large λ, the Poisson distribution approaches the Normal
distribution.
Both mean and variance increase with λ.

3) Generate 10,000 values from Poisson(λ = 6) and compare:

 Histogram of simulated data

 Theoretical probabilities using dpois()

library(ggplot2)

[Link](123)

x_sim <- rpois(10000, lambda = 6)

sim_data <- [Link](table(x_sim))

colnames(sim_data) <- c("x", "Freq")

sim_data$x <- [Link]([Link](sim_data$x))

sim_data$Prob_sim <- sim_data$Freq / sum(sim_data$Freq)

sim_data$Prob_theory <- dpois(sim_data$x, lambda = 6)


ggplot(sim_data, aes(x = x)) + geom_bar(aes(y = Prob_sim),
stat = "identity",fill = "skyblue", alpha = 0.6) +
geom_point(aes(y = Prob_theory),color = "red", size = 3) +
ggtitle("Simulated vs Theoretical Poisson (λ = 6)")
+ylab("Probability") + theme_minimal()

This demonstrates that simulation approximates the theoretical model when the sample size is
large. Hence, the Poisson distribution is validated through simulation.

You might also like