Hello Pablo,
I understand that you want to generate 10,000 samples from the beta distribution using the acceptance-rejection method.
You can use a while loop to keep generating pairs until you have enough successful ones. I suggest you to refer to the following code for reference:
alpha = 2; beta = 3; a = 0; b = 1; c = 2.5;
successful_samples = 0;
total_pairs = 0;
samples = zeros(1, 10000);
while successful_samples < 10000
U = rand;
V = rand;
X = a + (b - a) * U;
Y = c * V;
% Check if the generated point (X, Y) is under the beta PDF curve
if Y <= (gamma(alpha + beta) / (gamma(alpha) * gamma(beta))) * X^(alpha - 1) * (1 - X)^(beta - 1)
successful_samples = successful_samples + 1;
samples(successful_samples) = X;
end
total_pairs = total_pairs + 1;
end
% Now you have 10,000 samples in 'samples' and the number of pairs generated in 'total_pairs'
% Plot the density curve
x_values = linspace(0, 1, 100);
pdf_values = betapdf(x_values, alpha, beta);
figure;
plot(x_values, pdf_values, 'LineWidth', 2);
hold on;
% Plot the histogram of the samples
histogram(samples, 'Normalization', 'pdf');
legend('Beta PDF', 'Histogram of Samples');
title('Density Curve and Histogram of Beta Distributed Samples');
xlabel('Value');
ylabel('Density');
Hope this helps,
Balaji