
## com42a of august 2024,
#  a little simulation & histograms thing for stk4011,
#  exercise 1.1(e)

sim <- 10^5 
x1 <- rnorm(sim)
x2 <- rnorm(sim)
x3 <- rnorm(sim)

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

wella <- c(mean(z1),mean(z2),mean(z3))
wellb <- c(var(z1),var(z2),var(z3))

print(wella) # close to 1, 2, 3 
print(wellb) # close to 2, 4, 6

hist(z3)

## histogram and density for z1: 

z1val <- seq(0.001,15,length=1000)
g1val <- (2*pi)^(-1/2)*(1/sqrt(z1val))*exp(-z1val/2) 

hist(z1,prob=T,breaks=30,
   xlab="z1",ylab="density and histogram",main=" ")
matlines(z1val,g1val,type="l",lwd=3)

## histogram and density for z2: 

z2val <- seq(0.001,15,length=1000)
g2val <- 0.50*exp(-z2val/2) 

hist(z2,prob=T,breaks=30,
   xlab="z2",ylab="density and histogram",main=" ")
matlines(z2val,g2val,type="l",lwd=3)
