Tutorial 2

.pdf
MATH423 (Fall2023) Tutorial Time and Location : Every Wednesday 10:35-11:25AM at WONG 1020 Office Hour : Every Friday from 10:00-11:00AM at Burnside Hall 1134 Email : [email protected] Tutorial 2 Generate random variables Example: normal random variables set.seed( 123 ) r.norm <- rnorm( 500 , 0 , 1 ) hist(r.norm, main= "histogram of generated Normal random variables" , xlab= "N(0, 1) random variables" ) histogram of generated Normal random variables N(0, 1) random variables Frequency -3 -2 -1 0 1 2 3 0 20 40 60 80 100 hist(r.norm, main= "histogram of generated Normal random variables" , xlab= "N(0, 1) random variables" , freq = FALSE, ylim = c( 0 , 0.5 )) x <- seq(- 3 , 3 , 0.1 ) curve(dnorm(x, 0 , 1 ), col= "red" , add= TRUE) 1
histogram of generated Normal random variables N(0, 1) random variables Density -3 -2 -1 0 1 2 3 0.0 0.1 0.2 0.3 0.4 0.5 Similarly, you can generate uniform random variables and Cauchy random variables: set.seed( 123 ) r.unif <- runif( 500 , - 1 , 1 ) hist(r.unif, main= "histogram of generated Uniform random variables" , xlab= "UNIF(-1. 1) random variables" ) 2
histogram of generated Uniform random variables UNIF(-1. 1) random variables Frequency -1.0 -0.5 0.0 0.5 1.0 0 10 20 30 40 50 60 set.seed( 123 ) r.cauchy <- rcauchy( 500 , 0 , 1 ) hist(r.cauchy, main= "histogram of generated Cauchy random variables" , xlab= "Cauchy(0, 1) random variables" ) histogram of generated Cauchy random variables Cauchy(0, 1) random variables Frequency -150 -100 -50 0 50 100 150 200 0 50 100 150 200 250 3
Page1of 9
Uploaded by wvictoria1122 on coursehero.com