mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-14 18:59:59 +01:00
Add exo 6
This commit is contained in:
29
M1/Monte Carlo Methods/Exercise6.rmd
Normal file
29
M1/Monte Carlo Methods/Exercise6.rmd
Normal file
@@ -0,0 +1,29 @@
|
||||
# Exercise 6 : Rejection - A First Example
|
||||
|
||||
```{r}
|
||||
f <- function(x) {
|
||||
return(
|
||||
(2 / pi * sqrt(1 - x^2)) * (x >= -1 & x <= 1)
|
||||
)
|
||||
}
|
||||
|
||||
n <- 10000
|
||||
|
||||
M <- 4 / pi
|
||||
g <- function(x) {
|
||||
return(
|
||||
1 / 2 * (x >= -1 & x <= 1)
|
||||
)
|
||||
}
|
||||
|
||||
x <- NULL
|
||||
while (length(x) < n) {
|
||||
U <- runif(1)
|
||||
X <- runif(1, -1, 1)
|
||||
x <- append(x, X[U <= (f(X) / (M * g(X)))])
|
||||
}
|
||||
|
||||
t <- seq(-1, 1, 0.01)
|
||||
hist(x, freq = FALSE, breaks = 50)
|
||||
lines(t, f(t), col = "red", lwd = 2)
|
||||
```
|
||||
Reference in New Issue
Block a user