forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox_muller.jl
156 lines (122 loc) · 4.98 KB
/
box_muller.jl
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
using DelimitedFiles, LinearAlgebra
using Test
function create_grid(n, endpoints)
grid_extents = endpoints[2] - endpoints[1]
# number of points along any given axis
# For 2D, we take the sqrt(n) and then round up
axis_num = ceil(Int, sqrt(n))
# we are now rounding n up to the nearest square if it was not already one
if sqrt(n) != axis_num
n = axis_num^2
end
# Distance between each point
dx = grid_extents / (axis_num)
# Initializing the array, particles along the column, dimensions along rows
a = zeros(n, 2)
# This loops over the relevant dimensions
for i = 1:axis_num
for j = 1:axis_num
a[(i - 1) * axis_num + j, :] .=
[(i - 0.5) * dx + endpoints[1],
(j - 0.5) * dx + endpoints[1]]
end
end
return a
end
function create_rand_dist(n, endpoints)
grid_extents = endpoints[2] - endpoints[1]
return(rand(n,2) * grid_extents .+ endpoints[1])
end
# This function reads in a pair of input points and performs the Cartesian
# Box--Muller transform
function cartesian_box_muller(input_pts, sigma, mu)
r = sqrt(-2 * log(input_pts[1]))
theta = 2 * pi * input_pts[2]
return [sigma * r * cos(theta) + mu[1],
sigma * r * sin(theta) + mu[2]]
end
# This function reads in a pair of input points and performs the Cartesian
# Box--Muller transform
function polar_box_muller(input_pts, sigma, mu)
r_0 = input_pts[1]^2 + input_pts[2]^2
# this method is only valid for points within the unit circle
if r_0 == 0 || r_0 > 1
return [NaN, NaN]
end
return [sigma * input_pts[1] * sqrt(-2 * log(r_0) / r_0) + mu[1],
sigma * input_pts[2] * sqrt(-2 * log(r_0) / r_0) + mu[2]]
end
function is_gaussian(input_pts; bounds = [-1 1; -1 1], dx = 0.1,
sigma = 1, mu = [0,0], threshold = 0.1)
histogram = zeros(ceil(Int,(bounds[1,2]-bounds[1,1])/dx),
ceil(Int,(bounds[2,2]-bounds[2,1])/dx))
for i = 1:size(input_pts)[1]
input_x = input_pts[i, 1]
input_y = input_pts[i, 2]
if !(isnan(input_x) || isnan(input_y))
bin = CartesianIndex(ceil(Int, (input_x - bounds[1,1]) / dx),
ceil(Int, (input_y - bounds[2,1]) / dx))
if bin[1] <= size(histogram)[1] && bin[1] > 0 &&
bin[2] <= size(histogram)[2] && bin[2] > 0
histogram[bin] += 1
end
end
end
n = sum(histogram)
normalize!(histogram)
rms = 0
for i = 1:size(histogram)[1]
x = bounds[1,1] + i*dx
for j = 1:size(histogram)[2]
y = bounds[2,1] + j*dx
gaussian_value = exp(-(((x+mu[1])^2)/(2*sigma^2) +
((y+mu[2])^2)/(2*sigma^2)))
rms += (gaussian_value - histogram[i,j])^2
end
end
return sqrt(rms/n) < threshold
end
function main(n)
# This casts the input onto the nearest square for the cartesian grids
n = Int(ceil(sqrt(n))^2)
cartesian_grid = create_grid(n, [0,1])
polar_grid = create_grid(n, [-1,1])
cartesian_rand = create_rand_dist(n, [0,1])
polar_rand = create_rand_dist(n, [-1,1])
cartesian_grid_output = similar(cartesian_grid)
polar_grid_output = similar(polar_grid)
cartesian_rand_output = similar(cartesian_rand)
polar_rand_output = similar(polar_rand)
# going through each pair of points and using the x,y coordinates in
# their respective functions
for i = 1:size(cartesian_grid)[1]
cartesian_grid_output[i,:] .=
cartesian_box_muller(cartesian_grid[i,:], 1, [0,0])
polar_grid_output[i,:] .= polar_box_muller(polar_grid[i,:], 1, [0,0])
cartesian_rand_output[i,:] .=
cartesian_box_muller(cartesian_rand[i,:], 1, [0,0])
polar_rand_output[i,:] .= polar_box_muller(polar_rand[i,:], 1, [0,0])
end
@testset "histogram tests of Box--Muller Gaussianness" begin
@test is_gaussian(cartesian_grid_output;
bounds = [-3 3; -3 3], dx = 0.3,
sigma = 1, mu = [0,0])
@test is_gaussian(cartesian_rand_output;
bounds = [-3 3; -3 3], dx = 0.3,
sigma = 1, mu = [0,0])
@test is_gaussian(polar_grid_output;
bounds = [-3 3; -3 3], dx = 0.3,
sigma = 1, mu = [0,0])
@test is_gaussian(polar_rand_output;
bounds = [-3 3; -3 3], dx = 0.3,
sigma = 1, mu = [0,0])
end
writedlm("cartesian_grid_output.dat", cartesian_grid_output)
writedlm("polar_grid_output.dat", polar_grid_output)
writedlm("cartesian_rand_output.dat", cartesian_rand_output)
writedlm("polar_rand_output.dat", polar_rand_output)
writedlm("cartesian_grid.dat", cartesian_grid)
writedlm("polar_grid.dat", polar_grid)
writedlm("cartesian_rand.dat", cartesian_rand)
writedlm("polar_rand.dat", polar_rand)
end