dlfivefifty / InverseTransformSampling

Matlab implementation of inverse transform sampling in 1D and 2D
10 stars 2 forks source link

Incorrect variance for Beta distribution? #1

Open timueh opened 5 years ago

timueh commented 5 years ago

Hey there,

I drew samples for the beta distribution and noticed that the variance is off. It seems to be a structural issue. Any ideas?

a = 2; b = 2;
X = sample(@(x) (x.^(a-1).*(1-x).^(b-1) / beta(a,b)), [0 1], 100000);
mean(X), var(X) % = 0.5005, 0.0201

The analytical values for the mean and variance are

mu = a / (a + b) % 0.5
var = a*b/( (a+b)^2*(a+b+1) ) % 0.05

thanks!

timueh commented 5 years ago

Interesting: If I use ApproxFun.sample in Julia, it works. So there is some bug in the Matlab implementation I suppose?

julia> using SpecialFunctions, ApproxFun

julia> α, β = 2., 2.
(2.0, 2.0)

julia> f = Fun(t->t^(α-1)*(1-t)^(β-1)/beta(α,β), 0..1)
Fun(Chebyshev(0..1),[0.75, 6.08434e-17, -0.75])

julia> s = ApproxFun.sample(f,10000);

julia> mean(s), var(s)
(0.4985766951022462, 0.050219874600315156)

Is there a standalone Julia implementation of your inverse CDF sampling that does not depend on types from ApproxFun such as Fun or Chebyshev?

dlfivefifty commented 5 years ago

Is there a standalone Julia implementation

No. @ajt60gaibb will have to jump in here as I don't have a working version of Matlab.

timueh commented 5 years ago

Any idea @ajt60gaibb?

timueh commented 5 years ago

I'm sorry to bother you again, but is there any advice you can give @ajt60gaibb?

Thanks :)