
# com302b of august 2023
# re kiosk 1.1 (e)
# some easy chi-squareds by histogram and exact densities

sim <- 10^6
x1 <- rnorm(sim)
x2 <- rnorm(sim)
x3 <- rnorm(sim)
x4 <- rnorm(sim)

z1 <- x1^2
z2 <- x1^2 + x2^2
z3 <- x1^2 + x2^2 + x3^2 
z4 <- x1^2 + x2^2 + x3^2 + x4^2

## first, very simple, just histograms: 

hist(z1)
hist(z2)
hist(z3)
hist(z4)

## then a bit more, plotting exact densities along
#  with histograms, here shown for z3; do the others yourself 

aux <- 0.75
eps <- 0.0001

z3val <- seq(eps,aux*max(z3),length=1000)

g3val <- dchisq(z3val,3)

max3 <- max(g3val)

par(bty="l")
par(cex.lab=1.4142) 
   
hist(z3,prob=T,main=" ",breaks=30,
   xlab="z3",ylab="histogram and density",ylim=c(0,max3))
matlines(z3val,g3val,type="l",lwd=3)
