-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept-reject_algorithm_exercise.m
123 lines (94 loc) · 2.98 KB
/
accept-reject_algorithm_exercise.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
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
% Imlement an acceptance-rejection algorithm to generate samples from a
% beta distribution.
% Jacob Bado, 3-11-24
% Define Beta distribution parameters
a = 2;
b = 5;
% Define ranges of x values
x_vec = 0:0.001:1;
% Generate beta distribution f(x)
f = betapdf(x_vec, a, b);
% Find max density x location for f(x), this will be the mean of g(x)
g_mean = x_vec(f == max(f));
% Define g(x) standard deviation
g_std = 0.22;
% Generate gaussian distribution g(x)
g = normpdf(x_vec, g_mean, g_std);
% Programmatically determine the appropriate scaling constant M
M = max(f)/max(g);
% Show that f(x) <= M*g(x)
valid_check = min(M*g - f);
% Define sample size exponent
n = 1:6;
% Preallocate memory
accepted_samps = cell(max(n), 1);
accept_rate = zeros(1, sum([10^1, 10^2, 10^3, 10^4, 10^5, 10^6]));
counter = 0;
for i = n
% Define sample size
N = 10^i;
% Define variables to track acceptance
accepted_samps{i} = zeros(1, N);
n_accepted = 0;
% Iterate until N samples have been accepted
while n_accepted < N
counter = counter + 1;
% Generate random sample from g(x)
x = normrnd(g_mean, g_std, 1);
% Generate uniform random number from [0, 1]
u = unifrnd(0, 1, 1);
% Compute acceptance probability
fx = betapdf(x, a, b);
gx = normpdf(x, g_mean, g_std);
rho = fx/(M*gx);
% Accept or reject sample
if u <= rho
n_accepted = n_accepted + 1;
accepted_samps{i}(n_accepted) = x;
accept_rate(counter) = 1;
end
end
end
% Compute acceptance rate
acceptance_rate = 100*sum(accept_rate)/length(accept_rate);
%% Plot target and proposed distributions
gx_vec = linspace(-4*g_std+g_mean, 4*g_std+g_mean, length(x_vec));
fig = figure;
fig.Color = [1,1,1];
plot(x_vec, f, ...
'Color', [0.6, 0, 0.2], 'LineWidth', 2)
xlim('tight')
hold on
plot(gx_vec, M*normpdf(gx_vec, g_mean, g_std), '--', ...
'Color', [0, 0.6, 0.4], 'LineWidth', 2)
ax = gca;
ax.LineWidth = 2;
ax.FontWeight = 'bold';
xlabel('x')
ylabel('Probability Density')
title('Target and Proposed Distributions')
legend('f(x)', 'M*g(x)')
%% Plot target distribution and histograms of accepted samples
bin_edges = linspace(0, 1, 30);
fig = figure;
fig.Color = [1,1,1];
sgtitle('Comparison of Sample Histograms and Target Distribution')
for i = n
subplot(3,2,i)
hi = histogram(accepted_samps{i}, bin_edges, 'Normalization', 'pdf', ...
'FaceColor', [0, 0.6, 0.4], 'LineWidth', 1);
hold on
plot(x_vec, f, ...
'Color', [0.6, 0, 0.2], 'LineWidth', 2)
xlim('tight')
ax = gca;
ax.LineWidth = 2;
ax.FontWeight = 'bold';
xlabel('x')
ylabel('Probability Density')
title(['N Samples: ', num2str(10^i)])
if i == 6
legend('Sample Histogram', 'Target Distribution', ...
'Location', 'northeast')
end
end