I have the following algorithm
- Generate U~(0,1)
- X=-beta*ln(u_1*u_2*...*u_alpha)
- repeat step 1 an step 2 N times
I think in r is as follows
Gam<-function(alpha,beta){
N<-1000
u<-runif(alpha)
x<-rep(0,alpha)
X<-rep(0,N)
for(j in 1:N){
for(i in 1:alpha){
x[i]<--beta*log(prod(u[i]))
X[j]<-x[i]
}
}
return(X)}
#Comparation with the predefined gamma function
y<-rgamma(alpha,beta)
par(mfrow=c(1,2))
hist(X,freq=F,nclass=10)
hist(y,freq=F,nclass=10)
The output is 1000 times the same number .2139196 when I call the function with alpha=3 and beta=4
At least there is no error but it should have not return the same number,
what am I doing wrong in the current code?