# This program illustrate the pdf and pmf can be interpreted # as a limiting histogram # Created by Kno, 2011.3.22 # Discrete # Binomial Y_1, ..., Y_k ~ Bin(n,p) for k=k1, k2, k3 respectively k1<-10; k2<-100; k3<-1000; n<-10; p<-0.4 ; y1<-rbinom(k1, n, p); y2<-rbinom(k2, n, p);y3<-rbinom(k3, n, p); x<-2:n; par(mfrow=c(2,2)); hist(y1); hist(y2); hist(y3); plot(dbinom(x,n,p)~x); summary(y3); # Bernoulli Bernoulli(p) = Bin(1,p) k1<-10; k2<-100; k3<-1000; n<-1; p<-0.4 ; y1<-rbinom(k1, n, p); y2<-rbinom(k2, n, p);y3<-rbinom(k3, n, p); x<-0:n; par(mfrow=c(2,2)); hist(y1); hist(y2); hist(y3); plot(dbinom(x,n,p)~x); summary(y3); # When k -> \infinity, the distribution of Y does not change # Central limiting theorem: Distribution of \bar{Y} or sum of Y's # can be approximated by normal k1<-10; k2<-100; k3<-1000; n<-1000; p<-0.4 ; y1<-rbinom(k1, n, p); y2<-rbinom(k2, n, p);y3<-rbinom(k3, n, p); x<-0:n; par(mfrow=c(2,2)); hist(y1); hist(y2); hist(y3); nx<-(-n):n;nx<-nx/20 +n*p; plot(dnorm(nx,n*p,sqrt(n*p*(1-p)))~nx); stem(y3); # Continuous # normal k1<-10; k2<-100; k3<-1000; y1<-rnorm(k1); y2<-rnorm(k2);y3<-rnorm(k3); par(mfrow=c(2,2)); hist(y1); hist(y2); hist(y3); nx<-(-100):100;nx<-nx/20 plot(dnorm(nx)~nx); stem(y3); par(mfrow=c(2,2)); qqnorm(y1); qqnorm(y2); qqnorm(y3);