Sampling from inverse gamma distribution by specifing mean and standard deviation
Ältere Kommentare anzeigen
I am writing a function to draw samples from an inverse gamma distribution by specifing mean and standard deviation.
Here is the code.
function x = inv_gamma_rnd_mean_std(mu, sig)
% mu is the mean, sig is the standard deviation.
% shape (a) and scale (b) parameters
a = mu^2/sig^2+2;
b = mu*(a-1);
x = 1/gamrnd(a,1/b);
end
The code looks problematic because my Monte Carlo test returns a standard deviation far from the specified one. For instance, I expected std(tmp) becomes arount 2.0 in the code below, but it is far from 2.0
tmp=zeros(10000,1);
for idx = 1:10000
tmp(idx) = inv_gamma_rnd_mean_std(0.1,2.0);
end
mean(tmp)
std(tmp)
I could not find what was going wrong in my function. Thank you for your support in advance.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
