-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarsaglia.m
71 lines (52 loc) · 1.56 KB
/
marsaglia.m
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
%% Marsaglia Variant for Normally Distributed Random Deviates
% This is a modification of the Box-Muller method in order to avoid using
% trigonometric functions. We take advantage of their polar form.
%
% This method computes pairs of normally distributed random numbers.
% --------------------------------------------------------------------
clear all
close all
% Parameters
a = -5 ;
b = 5 ;
deltax = 0.05 ;
x = a:deltax:b ; % Grid for the graphical checks
ndeviates = 10^6; % Number of deviates required
W = ones([1,ndeviates]) ;
V1 = zeros([1,ndeviates]) ;
V2 = zeros([1,ndeviates]) ;
for i = 1:ndeviates
while W(i) >= 1
u1 = rand([1,1]) ;
u2 = rand([1,1]) ;
V1(i) = 2*u1 - 1 ;
V2(i) = 2*u2 - 1 ;
W(i) = ( V1(i)^2 ) + ( V2(i)^2 ) ;
end
end
Z1 = V1 .* sqrt((-2*log(W) ./ W )) ;
Z2 = V2 .* sqrt((-2*log(W) ./ W )) ;
%% Graphical check for Z1 - Histogram with overplot
% This method (and the same for Z2) computes a histogram and then
% normalises it, followed by plotting the standard normal curve.
close all
figure(1)
h1 = histogram(Z1,x,'Normalization','pdf') ;
hold on
plot(x,pdf('Normal',x,0,1),'r','LineWidth',2)
xlim([a,b])
xlabel('x')
ylabel('f')
legend('Sampled', 'Theory')
title('Standard Normal Distribution X ~ N(0,1)')
%% Graphical check for Z2 - Histogram with overplot
close all
figure(1)
h1 = histogram(Z2,x,'Normalization','pdf') ;
hold on
plot(x,pdf('Normal',x,0,1),'r','LineWidth',2)
xlim([a,b])
xlabel('x')
ylabel('f')
legend('Sampled', 'Theory')
title('Standard Normal Distribution X ~ N(0,1)')