beta distribution define !

1 次查看(过去 30 天)
Pablo Brando
Pablo Brando 2021-11-29
编辑: Balaji 2024-2-22
sample 10000 values from the beta distribution
In each case, count the number of pairs you need to generate until you find 10000 succesful ones.
for alpha = 2 beta = 3 and . Plot the density curve using the plot function and the histogram derived from your samples.
This is the matlab code to find 1 succesful one, but i need to find 10000 and count the pairs needed to get them,
alpha=2; beta=3; a=0; b=1; c=2.5;
X=0; Y=c; % Initial values
while Y > gamma(alpha+beta)/gamma(alpha)/gamma(beta)*X.^(alpha-1).*(1-X).^(beta-1))
U=rand; V=rand; X=a+(b-a)*U; Y=c*V;
end; X
I cant use any other random number generator, the problem is i dont know how to generate 10000 samples with a loop

回答(1 个)

Balaji
Balaji 2024-2-22
编辑:Balaji 2024-2-22
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

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by