-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem 12.2.Rmd
59 lines (55 loc) · 1.06 KB
/
Problem 12.2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
title: "12.2 Independent sampling"
output: html_notebook
---
#12.2.1
```{r}
fPDF <- function(X){
y <- ifelse(X < 0.9735, (1 / 1.33485) * (1 / sqrt(2 * pi)) * exp(-X ^ 2 / 2), ifelse(X <= 5, 0.186056, 0))
return(y)
}
integrate(fPDF,0,8)
```
```{r}
fReject <- function(N){
count <- 1
lSamples <- vector(length=N)
while(count <= N){
X <- runif(1, 0, 5)
Y <- runif(1, 0, (1 / 1.335) * (1 / sqrt(2 * pi)))
if(Y < fPDF(X)){
lSamples[count] <- X
count <- count + 1
}
}
return(lSamples)
}
mean(fReject(10000))
var(fReject(10000))
```
```{r}
fIntegrator <- function(X){
return(integrate(fPDF, 0, X)[[1]])
}
lCDF <- sapply(seq(0, 5, 0.1), fIntegrator)
fICDF <- approxfun(lCDF, seq(0, 5, 0.1))
fInverseTransform <- function(N){
lCDF <- runif(N, 0, 1)
return(sapply(lCDF, fICDF))
}
mean(fInverseTransform(100000))
var(fInverseTransform(100000))
```
```{r}
fImportance <- function(N){
lX <- runif(N, 0, 5)
lF <- sapply(lX, fPDF)
lG <- rep(1 / 5, N)
lRatio <- lX * lF / lG
mean(lRatio)
}
fImportance(10000)
```
```{r}
rep(1 / 5, 20)
```