-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxMuller.R
More file actions
53 lines (42 loc) · 1.18 KB
/
BoxMuller.R
File metadata and controls
53 lines (42 loc) · 1.18 KB
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
#-----------------------
# Box-Muller transform
#-----------------------
# 1. generate uniform vectors
n = 10000
set.seed(2023)
u1 = runif(n)
u2 = runif(n)
# 2. define Box-Müller transform function
x1 = sqrt(-2*log(u1))*cos(2*pi*u2)
x2 = sqrt(-2*log(u1))*sin(2*pi*u2)
x = data.frame(x1, x2)
# 3. print first lines of dataset
data = x
data[1:6,]
# x1 x2
# 1 1.139567 -0.4752820
# 2 -1.449969 0.2893102
# 3 -1.791041 0.6499395
# 4 -1.255448 0.5252876
# 5 2.625178 -0.3092095
# 6 1.735641 -1.1015367
# 4. plotting
library(ggplot2)
p1<- ggplot(data, aes(x = x1)) +
geom_histogram(aes(y = ..density..),
colour = 1, fill = "white") +
geom_density(lwd = 1, colour = "#a50101") + theme_minimal()
p2<- ggplot(data, aes(x = x2)) +
geom_histogram(aes(y = ..density..),
colour = 1, fill = "white") +
geom_density(lwd = 1, colour = "#a50101") + theme_minimal()
p3<- ggplot(data, mapping = aes(x = x1, y = x2)) +
geom_point(size = 0.6) +
geom_jitter(alpha = 0.75, size = 0.6) + theme_minimal()
library(gridExtra)
grid.arrange(arrangeGrob(p1, p2),
arrangeGrob(p3, ncol=1),
ncol=2, widths=c(1,1))
#----
# end
#----