-
Notifications
You must be signed in to change notification settings - Fork 0
/
Acceptance_Rejection_for_Normal_beta_distribution.qmd
263 lines (188 loc) · 5.34 KB
/
Acceptance_Rejection_for_Normal_beta_distribution.qmd
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---
title: Acceptance-Rejection Algorithm
format: gfm
editor: visual
---
## Problem i
From a normal distribution with mean 4 and standard deviation 2, run a simulation of size 10,000.
(Consider the candidate density, the density of an exponential distribution with a mean of 10).
$$
\begin{aligned}
& \text{target density:}~~ Y \sim \mathcal{N}(\mu = 4, \sigma = 2), \\
& \text{Candidate density:} ~~ \sim \mathcal{E}\text{xp}\left(\text{rate} = \frac{1}{10}\right)
\end{aligned}
$$
```{r}
#| warning: false
#| fig-height: 9
#| fig-width: 9
fv <- function(x) dexp(x, rate = 1/10)
fy <- function(x) dnorm(x, mean = 0, sd = 2)
ratio <- function(x) fy(x) / fv(x)
temp1 <- optimize(ratio, interval = c(0, 25), maximum = TRUE)
M1 <- temp1$objective
x_max1 <- temp1$maximum
# library(httpgd); hgd(); hgd_browse()
curve(ratio, 0, 5, lwd = 2, col = "red")
abline(h = M1, lwd = 2, col = "blue")
n <- 1e+4
ratio_2 <- function(x) ratio(x) / M1
sim_fun_1 <- function(n) {
i <- 0
sim_norm <- numeric(n)
while(i < n) {
u <- runif(3)
v <- -10 * log(u[1])
temp <- ratio_2(v)
if (u[2] < temp) {
i <- i + 1
sim_norm[i] <- ifelse(u[3] < 0.5, -1, 1) * v
}
}
return(sim_norm + 4)
}
start_time_1 <- Sys.time()
ress_1 <- sim_fun_1(n = n)
End_time_1 <- Sys.time()
time_1 <- difftime(End_time_1, start_time_1, unit = "sec")
ress_1 |> mean()
ress_1 |> sd()
hist(ress_1, probability = TRUE, col = "red")
curve(dnorm(x, 4, 2), -2, 10, lwd = 2, col = "blue", add = TRUE)
```
***
***
```{r}
#| warning: false
#| message: false
#| fig-height: 9
#| fig-width: 9
fv <- function(x) dexp(x, rate = 1)
fy <- function(x) dnorm(x, mean = 0, sd = 2)
ratio <- function(x) fy(x) / fv(x)
temp2 <- optimize(ratio, interval = c(0, 25), maximum = TRUE)
M2 <- temp2$objective
x_max2 <- temp2$maximum
curve(ratio, 0, 25, lwd = 2, col = "red")
abline(h = M2, lwd = 2, col = "blue")
n <- 1e+4
ratio_2 <- function(x) ratio(x) / M2
sim_fun_2 <- function(n) {
i <- 0
sim_norm <- numeric(n)
while(i < n) {
u <- runif(3)
v <- -log(u[1])
temp <- ratio_2(v)
if (u[2] < temp) {
i <- i + 1
sim_norm[i] <- ifelse(u[3] < 0.5, -1, 1) * v
}
}
return(sim_norm + 4)
}
start_time_2 <- Sys.time()
ress_2 <- sim_fun_2(n = n)
End_time_2 <- Sys.time()
time_2 <- difftime(End_time_2, start_time_2, unit = "sec")
ress_2 |> mean()
ress_2 |> sd()
hist(ress_2, probability = TRUE, col = "red")
curve(dnorm(x, 4, 2), -2, 10, lwd = 2, col = "blue", add = TRUE)
c(M1 = M1, M2 = M2)
c(Time_1 = time_1, Time_2 = time_2)
```
***
***
## Problem ii
From the beta distribution with the parameter of shape~(1)~ equal to 3.5 and the parameter of shape~(2)~ equal to 9.5
simulate The candidate density is beta distribution with parameter shape~(1)~ equal to 3 and parameter
shape~(2)~ is equal to one. Consider the number of data to be 100,000 and
Plot the histogram of the data and plot the target density on the histogram
Fit the data.
$$
\begin{aligned}
& \text{target variable:}~~~~Y \sim \text{Beta}(\text{shape}_1: 3.5, ~\text{shape}_2: 9.5),\\
& \text{Candidate Variable:}~~~V\sim \text{Beta}(\text{shape}_1: 3, ~\text{shape}_2: 1).\\
& F_V(v):~~\int_0^v 3x^2 dx = x^3|_0^v = v^3 \implies \\
u = v^3 \implies v = u^{\frac{1}{3}}
\end{aligned}
$$
```{r}
#| warning: false
#| fig-height: 9
#| fig-width: 9
fv <- function(x) dbeta(x, shape1 = 3, shape2 = 1)
fy <- function(x) dbeta(x, shape1 = 3.5, shape2 = 9.5)
ratio <- function(x) fy(x) / fv(x)
temp3 <- optimize(ratio, interval = c(0, 1), maximum = TRUE)
M3 <- temp3$objective
x_max3 <- temp3$maximum
curve(ratio, 0, 1, lwd = 2, col = "red")
abline(h = M3, lwd = 2, col = "blue")
n <- 1e+5
ratio_3 <- function(x) ratio(x) / M3
sim_fun_3 <- function(n) {
i <- 0
sim_beta <- numeric(n)
while(i < n) {
u <- runif(3)
v <- u[1]^(1/3)
temp <- ratio_3(v)
if (u[2] < temp) {
i <- i + 1
sim_beta[i] <- v
}
}
return(sim_beta)
}
start_time_3 <- Sys.time()
ress_3 <- sim_fun_3(n = n)
End_time_3 <- Sys.time()
time_3 <- difftime(End_time_3, start_time_3, unit = "sec")
ress_3 |> mean()
ress_3 |> sd()
hist(ress_3, probability = TRUE, col = "red")
curve(dbeta(x, shape1 = 3.5, shape2 = 9.5), 0, 1, lwd = 2, col = "blue", add = TRUE)
```
***
***
```{r}
#| warning: false
#| fig-height: 9
#| fig-width: 9
fv <- function(x) 1
fy <- function(x) dbeta(x, shape1 = 3.5, shape2 = 9.5)
ratio <- function(x) fy(x) / fv(x)
temp4 <- optimize(ratio, interval = c(0, 1), maximum = TRUE)
M4 <- temp4$objective
x_max4 <- temp4$maximum
curve(ratio, 0, 1, lwd = 2, col = "red")
abline(h = M4, lwd = 2, col = "blue")
n <- 1e+5
ratio_4 <- function(x) ratio(x) / M4
sim_fun_4 <- function(n) {
i <- 0
sim_beta <- numeric(n)
while(i < n) {
u <- runif(3)
v <- u[1]
temp <- ratio_4(v)
if (u[2] < temp) {
i <- i + 1
sim_beta[i] <- v
}
}
return(sim_beta)
}
start_time_4 <- Sys.time()
ress_4 <- sim_fun_4(n = n)
End_time_4 <- Sys.time()
time_4 <- difftime(End_time_4, start_time_4, unit = "sec")
ress_4 |> mean()
ress_4 |> sd()
hist(ress_4, probability = TRUE, col = "red")
curve(dbeta(x, shape1 = 3.5, shape2 = 9.5), 0, 1, lwd = 2, col = "blue", add = TRUE)
c(M3 = M3, M4 = M4)
c(Time_3 = time_3, Time_4 = time_4)
```