title: "exercise 3"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
#b)
library(rmarkdown)
x <- c(0:20)
d <- ppois(1:22, 1, sort) #we use a discrete distribution otherwise we'd have p=0
d_2 <- ppois(1:22, 5.5, sort)
steps <- stepfun(x,d)
steps_2 <- stepfun(x,d_2)
plot(steps, main = "cdf for a Poisson distributed random variable ", col.points = "red")
plot(steps_2, add=TRUE, col.points = "blue")
axis(side=1, 1:20)
m <- dpois(0:20, 1, sort)
m_2 <- dpois(0:20, 5.5, sort)
plot(x, m, type = "l", col="red", main = "pmf for a Poisson distributed random variable", ylab = "f(x)")
par(new=TRUE)
plot(x, m_2, type = "l", col="blue", ylab = "f(x)" )
axis(side=2)
#When lambda increases, the shape of the pmf gets flatter, and the cdf moves right.
#c)
random_pois<-rpois(1000, 1)
hist(random_pois)
lines(density(random_pois), adjust = 2, col="red")
random_p<-rpois(1000, 5.5)
hist(random_p, prob=TRUE)
lines(density(random_p), adjust = 2, col="red")
#When m increases, the shape of the cdf and pmf get more similar to the one's in the case of Normal distribution. Infact, when m takes large values, the Poisson distribution aproximates the Gaussian distribution.
R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
Including Plots
You can also embed plots, for example:
plot(pressure)
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.