--- title: "Lab3" author: "Zhou" date: "2/3/2021" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## when sample size goes to infinity ```{r} # Expectation = Mean when sample size goes to infinity. par(mfrow=c(3,1),mar = c(3,4,1,1)) # The expectation of chi square here equals the df x=rchisq(n=10,df=5) hist(x) abline(v=mean(x), col = "red") x=rchisq(n=100,df=5) hist(x) abline(v=mean(x), col = "red") x=rchisq(n=10000,df=5) hist(x) abline(v=mean(x), col = "red") # Variance ## change the number size n=100 x=rnorm(n,100,5) hist(x) c(min(x),max(x)) sum(x-mean(x))/(n-1) sum((x-mean(x))^2)/(n-1) sqrt(sum((x-mean(x))^2)/(n-1)) # var() ``` ## Covariance ```{r} # define 3 random variable n=10000 a=rnorm(n,100,5) x=a+rpois(n, 100) y=a+rchisq(n,5) z=a+rt(n,100) par(mfrow=c(3,1),mar = c(3,4,1,1)) plot(x,y) plot(x,z) plot(y,z) var(x) var(y) var(z) cov(x,y) cov(x,z) cov(y,z) W=cbind(x,y,z) dim(W) cov(W) var(W) cor(W) ``` ## Matrix manipulations ```{r} a=matrix(seq(10,60,10),2,3) b=matrix(seq(1,6),2,3) a b # add a+b # subtraction a-b # (dot)product a*b # (dot)division a/b # matrix multiplication c=matrix(c(1,1,1,1,3,4,2,1,30,50,40,25),4,3) b=matrix(c(20000,10000,1000,1000,300,20),3,2) t=c%*%b # inverse t = t[1:2,1:2] ti=solve(t) ti ti %*% t t%*%ti # transpose c=matrix(c(1,1,1,4,30,50),2,3) c t(c) # Properties of transpose A=matrix(c(1,1,1,4,30,50),2,3) B=matrix(c(1000,300,20,20000,10000,1000),3,2) t(A%*%B) t(B)%*%t(A) ```