## This is the hint program for game 5. # Essential the history dump from class R codes. Enjoy! # by Kno Tsao x<-runif(20) summary(x) plot(x) plot(x,dunif(x),main="pdf of U(0,1)") plot(x,punif(x),main="cdf of U(0,1)") # Reassign x x<-seq(from=0,to=1,length.out=1000) # Setup/Plots for U(0,1) windows() par(mfrow=c(2,2)); plot(x,dunif(x),main="pdf of U(0,1)") # Standardized histograms x10<-runif(10,min=0,max=1); x100<-runif(100,min=0,max=1); x1000<-runif(1000,min=0,max=1); hist(x10,main="10 samples from U(0,1)",prob=TRUE); hist(x100,main="100 samples from U(0,1)",prob=TRUE); hist(x1000,main="1000 samples from U(0,1)",prob=TRUE); # Closer examination of the plot for 10 samples from U(0,1) hist(x10,main="10 samples from U(0,1)"); x10<-sort(x10) x10[0<= x10 & x10 <=0.2] x10[0.6<= x10 & x10 <=0.8] sum((0.6<= x10 & x10 <=0.8)*1) # no. of x10 lie between [0.6,0.8] # Even larger sample sizes windows() par(mfrow=c(2,2)); x<-seq(from=0,to=1,length.out=1000) plot(x,dunif(x),main="pdf of U(0,1)") xe3<-runif(10^3,min=0,max=1); xe6<-runif(10^6,min=0,max=1); xe7<-runif(10^7,min=0,max=1); # Standardized histograms hist(xe3,main="10^3 samples from U(0,1)",prob=TRUE); hist(xe6,main="10^6 samples from U(0,1)",prob=TRUE); hist(xe9,main="10^7 samples from U(0,1)",prob=TRUE); # U(0,0.5) windows() par(mfrow=c(2,2)); x<-seq(from=0,to=1,length.out=1000) plot(x,dunif(x,min=0,max=.5),main="pdf of U(0,.5)") # Standardized histograms xe3<-runif(10^3,min=0,max=.5); xe6<-runif(10^6,min=0,max=.5); xe7<-runif(10^7,min=0,max=.5); hist(xe3,main="10^3 samples from U(0,.5)",prob=TRUE); hist(xe6,main="10^6 samples from U(0,.5)",prob=TRUE); hist(xe7,main="10^7 samples from U(0,.5)",prob=TRUE); # Discrete Uniform from stackexchange # http://stats.stackexchange.com/questions/3930/are-there-default-functions-for-discrete-uniform-distributions-in-r rdu<-function(n,k) sample(1:k,n,replace=T) ddu<-function(x,k) ifelse(x>=1 & x<=k & round(x)==x,1/k,0) pdu<-function(x,k) ifelse(x<1,0,ifelse(x<=k,floor(x)/k,1)) # You may use these user-defined functions to construct similar plots/figures