beta distribution define !
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!