# Univariate Distribution (Density) functions 
# Written for Advanced Statistics, Econ. NTU
# Jin-Lung Lin, March 2007
set.seed(12345678);
nobs=500;
n0=10;
p0=.25


#Binomial distribution

y=rbinom(nobs, size=n0, prob=p0)
x0=seq(from=0,to=n0,by=1)
hist(y,prob=T,breaks=x0)
curve(dbinom(x,size=n0,prob=p0),0,10,n=11,add=TRUE)
mtext(paste("Histogram and theoretical distribution of Bin(",n0,",",p0,")"),side=3) 



#Gamma distribution
a=1
b=1
x0=seq(0,50,by=0.1)
y=rgamma(nobs,shape=a,scale=b)
op =par(mfrow=c(2,1))
hist(y,prob=T)
mtext(paste("Histogram for Gamma(",n0,",",p0,")"),side=3) 
plot(dgamma(x0,shape=a,scale=b),,xlim=c(0,50),type="l")
mtext(paste("Theoretical distribution of Gamma(",n0,",",p0,")"),side=3) 
par(op)

#Gamma distribution
a=1
b=1
x0=seq(0,50,by=0.1)
y=rgamma(nobs,shape=a,scale=b)
hist(y,prob=T)
curve(dgamma(x,shape=a,scale=b),0,50,add=TRUE,col="magenta")
mtext(paste("Histogram and theoretical distribution for Gamma(",a,",",b,")"),side=3) 

#Gamma distribution
a=3
b=5
x0=seq(0,50,by=0.1)
y=rgamma(nobs,shape=a,scale=b)
hist(y,prob=T)
curve(dgamma(x,shape=a,scale=b),0,50,add=TRUE,col="magenta")
mtext(paste("Histogram and theoretical distribution for Gamma(",a,",",b,")"),side=3) 
