Pembangkitan Bilangan Acak dan Resampling τρ
Pembangkitan Bilangan Acak Resampling Jackknife Bootstrap Topics
Random Generators in R Distribution R name Additional Arguments beta beta shape1, shape2, ncp binomial binom size, prob Chauchy cauchy location, scale chi-squared chisq df, ncp exponential exp rate F f df1, df2, ncp gamma gamma shape, scale geometric geom prob hypergeometric hyper m, n, k log-normal lnorm meanlog, sdlog uniform unif min, max neg binomial nbinom size, prob normal norm mean, sd Poisson pois lambda Student s t t df, ncp
#pembangkitan bilangan acak x <- rnorm(10) #x~n(0,1) x1<- rnorm(10,3,2) #x1~n(3,sd=2) x2<- rbinom(10,1,0.4) #x2~bernoulli(0.4) #mencari nilai peluang suatu peubah acak p1 <- pnorm(1.645) #P(Z<1.645)=0.95 p2 <- pnorm(1.96) #P(Z<1.975)=0.975 p3 <- pnorm(-1.96) p4 <- pf(15,df1=10,df2=15) #mencari nilai peubah acak dgn diketahui peluang q1 <- qnorm(0.975) q2 <- qnorm(0.95,2,1) #X~N(2,1), P(X<x)=0.95 #mencari nilai density ##plot density normal a <- seq(-4,4,length=1000) da <- dnorm(a) plot(a,da)
Transformation Methods for Generating Random Numbers Direct Transformation ex : X~U 0,1 Y~U(a, b) Transformation Y = b 1 X + a Indirect Transformation The Inverse Transform Method The Acceptance-Rejection Method
Ilustrasi 1 Transformasi langsung Akan dibangkitkan 100 bilangan U(3,4) dari bilangan U(0,1) Misal X~U(0,1) akan diubah menjadi Y~U(3,4) Y = X+3 Maka X <- runif(100) #X~U(0,1) Y <- X+3
Indirect Transformation 1. The Inverse Transform Method If pdf r.v X f x and cdf r.v X F X X then F X X ~U(0,1) Algorithms for generating X with pdf f(x) : 1. Find F X X 2. U = F X X, U~U(0,1) 3. Find X = F X 1 (U) 4. So X have pdf f(x)
Ilustrasi 2 Simulate a random sample from the distribution with density f Y y = 3y 4 ; 1 y <
Jawaban 2 ##Y~ 3y^(-4)n --> F(y) = 1-y^(-3) ### Y = (1-U)^(-1/3) U <- runif(100) #U~U(0,1) Y1 <- (1-U)^(-1/3) #FUNGSI yy <- function(n,fyu) { u <- runif(n) y <- fyu(u) return(y) } fyu <- function(u) {y = (1-u)^(-1/3)} #1000 bil acak Y hasil <- yy(1000,fyu)
We need to generate (x, y) on this Accept x if (x, y) on the curve Reject x if (x, y) on the outside of curve Indirect Transformation (2) 2. The Acceptance-Rejection Method Basic idea : y = f(x) x criteria
Algorithms for generating X ~ pdf f(x) with x 0 < x < x 1 : 1. Generate x from U(x 0, x 1 ) 2. Find F so f x F for x 0 < x < x 1 3. Generate y 1 from U(0, F) 4. Find y 2 = f(x) 5. If y 1 y 2, accept x
Ilustrasi 3 Simulate a random sample from the distribution with density f Y y = 3y 2 ; 0 < y < 1 using Acceptance- Rejection Method
Jawaban 3 #ACCEPTANCE REJECTION METHOD #FUNGSI ar <- function(n,x0,x1,f) { xx <- seq(x0,x1,length=10000) F <- max(f(xx)) terima <- 0 iterasi <- 0 hasil <- numeric(n) while(terima<n) { x <- runif(1,x0,x1) y1<- runif(1,0,f) y2<- f(x) if(y1<=y2) { terima <- terima+1 hasil[terima]<-x } iterasi <- iterasi+1 } list(hasil,iterasi) } set.seed(10) f <- function(x) {3*x^2} yyy <- ar(10,0,1,f)
Resampling Alternative methods when we dealed small sample for taking inferences Jacknife Bootstrap Useful function in R Sample takes a sample of the specified size from the elements of x using either with or without replacement sample(x, size, replace = FALSE, prob = NULL)
Bootstrap Jacknife We have a sample y = (y 1,, y n ) to estimate θ with the estimator θ = f(y) The leave-one-out observation samples y (i) = (y 1,, y i 1, y i+1,, y n ) for i = 1,, n are called jacknife samples Jacknife statistics are θ (i) = f(y (i) ) Jacknife estimators θ jack = nθ n 1 θ. V jack θ = n 1 n n i=1 θ i θ. 2 θ (.) = 1 n n i=1 θ (i)
Ilustrasi 4 Using data BOD in R, find the jacknife estimator for the mean and SE of demand. Algorithm: 1. Compute θ = y 2. Take jacknife sample y i ; i = 1,, n 3. Compute jacknife estimator θ jack = nθ n 1 θ. = ny n 1 n V jack θ = n 1 n n i=1 y i 1 n n i=1 y i n i=1 2 y i
Jawaban 4 #JACKKNIFE data(bod) demand <- BOD$demand ##fungsi jackknife sjk <- function(data) { n <- length(data) sampel.j <- NULL for(i in 1:n) { sampel.j<-cbind(sampel.j,data[-i]) } return(sampel.j) } sj <- sjk(demand) ##jackknife statistics mean.sjk <- apply(sj,2,mean) ##jackknife estimator n <- length(demand) mean.jack <- n*mean(demand)-(n-1)*mean(mean.sjk) var.jack <- ((n-1)/n)*sum((mean.sjk-mean(mean.sjk))^2) se.jack <- sqrt(var.jack)
Jacknife Bootstrap We have a sample y = (y 1,, y n ) to estimate θ with the estimator θ = f(y) Steps Repeatedly simulate sample of size n from y y b (i) Compute statistic of interest θ b (i) = f(y b (i) ) Study behavior of statistic over N repetitions Bootstrap estimators N θ b = 1 N i=1 θ b (i) V b θ = 1 N 1 N i=1 θ b (i) θ b 2
Ilustrasi 5 Lakukan proses bootstrap pada data BOD untuk menduga rataan dan SE dari demand Algorithm: 1. Resample data with replacement, and the size of the resample must be equal to the size of the original data set. 2. Compute statistic μ 3. Repeat point 1 and 2, N times 4. Plot histogram of μ i ; i = 1,2,, N
Jawaban 5 #BOOTSTRAP bs <- function(data,ulangan) { n <- length(data) sb <- NULL for(i in 1:ulangan) { sb <- cbind(sb,sample(data,n,replace=t)) } msb <- apply(sb,2,mean) mean.b <- mean(msb) var.b <- var(msb) se.b <- sqrt(var.b) return(list(mean.b,var.b,se.b)) } hasil.b <- bs(demand,50)