From 5d242a381b877959f1ca64246045e875caaf0cee Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Wed, 18 Sep 2024 17:01:12 +0200 Subject: [PATCH] Add question 2 --- M1/Monte Carlo Methods/Exercise2.rmd | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/M1/Monte Carlo Methods/Exercise2.rmd b/M1/Monte Carlo Methods/Exercise2.rmd index f3a27d1..29f1ece 100644 --- a/M1/Monte Carlo Methods/Exercise2.rmd +++ b/M1/Monte Carlo Methods/Exercise2.rmd @@ -1,5 +1,6 @@ # Exercise 2 : Exponential distribution and related distributions +### Question 1 ```{r} n <- 10000 u <- runif(n) @@ -9,3 +10,18 @@ curve(dexp(x, rate = 2), add = TRUE, col = "red") qqplot(x, rexp(n, rate = 2)) ``` + +### Question 2 + +```{r} +lambda <- 1.5 +n <- 10000 +S <- numeric(n) +for (i in 1:n) { + u <- runif(10) + x <- -1/lambda * log(1-u) + S[i] <- sum(x) +} +hist(S, freq = FALSE, breaks = 50) +curve(dgamma(x, shape = 10, rate = lambda), add = TRUE, col = "red") +```