Beruflich Dokumente
Kultur Dokumente
# CHAPTER 1 #
#############
# sample command
n <- 10; k <- 5
sample(n,k)
sample(n,k,replace=TRUE)
sample(n,n)
# birthday problem
k <- 23
1-prod((365-k+1):365)/365^k
pbirthday(23)
qbirthday(0.5)
pbirthday(23,coincident=3)
#############
# CHAPTER 2 #
#############
# elder is a girl
n.ab <- sum(child1==1 & child2==1)
n.b <- sum(child1==1)
n.ab/n.b
# prompt player
print("Monty Hall says `Pick a door, any door!'")
# receive the player's choice of door (should be 1,2, or 3)
chosen <- scan(what = integer(), nlines = 1, quiet = TRUE)
# pick Monty's door (can't be the player's door or the car door)
if (chosen != cardoor) montydoor <- doors[-c(chosen, cardoor)]
else montydoor <- sample(doors[-chosen],1)
#############
# CHAPTER 3 #
#############
# can also do
dbinom(0:5,5,1/2)
dbinom(5,5,c(1/4,1/2,3/4))
dbinom(5,5:8,1/2)
# plotting PMFs
xs <- 0:5
ys <- dbinom(xs,5,0.2)
plot(xs,ys,type='h')
points(xs,ys,pch=16)
#############
# CHAPTER 4 #
#############
dgeom(5,0.2)
pgeom(5,0.2)
rgeom(1,0.2)
dnbinom(5,5,0.2)
dpois(1,5)
# number of matches
n <- 100
r <- replicate(10^4,sum(sample(n)==(1:n)))
mean(r) # this is equivalent to sum(r) / n
#############
# CHAPTER 5 #
#############
# options in plotting:
# axis labels and title: xlab, ylab, main
# axis limits: xlim, ylim
# type of plot: 'l', 'h', 'p', 's'
# (optional) if 'l', can decide line width with lwd, line type with lty
# (optional) if 'p', can decide plotting character with pch
# col: 'red', 'green', 'blue', etc.
# also, histograms, as we discuss next.
x <- seq(-3,3,0.01)
y <- dnorm(x)
plot(x,y,type='l')
plot(x,y,type="l",xlab="x",ylab="dnorm(x)",main="Standard Normal PDF",col="orange")
#############
# CHAPTER 6 #
#############
# defining a function
M <- function(t) {exp(t^2/2)}
# sample moments
x <- rnorm(100)
mean(x^6)
# sample skewness
skew <- function(x) {
centralmoment <- mean((x-mean(x))^3)
centralmoment/(sd(x)^3)
}
# sample kurtosis
kurt <- function(x) {
centralmoment <- mean((x-mean(x))^4)
centralmoment/(sd(x)^4) - 3
}
# median of Bin(50,0.2)
n <- 50; p <- 0.2
which.max(pbinom(0:n,n,p)>=0.5)
# sample mode
datamode <- function(x) {
t <- table(x)
m <- max(t)
as.numeric(names(t[t==m]))
}
# dice simulation
r <- replicate(10^6,sum(sample(6,6,replace=TRUE)))
sum(r==18)/10^6
#############
# CHAPTER 7 #
#############
# dmultinom
x <- c(2,0,3)
n <- sum(x)
p <- c(1/3,1/3,1/3)
dmultinom(x,n,p)
dmultinom(x,7,p) # throws an error
dmultinom(x,prob=p) # also okay
rmultinom(10,n,p)
# dcauchy
dcauchy(0)
pcauchy(3) # compare to pnorm(3)
hist(rcauchy(1000)) # hilarity!
hist(rcauchy(1000),breaks=100)
#############
# CHAPTER 8 #
#############
# convolution of Unifs
nsim <- 10^5
x <- runif(nsim)
y <- runif(nsim)
hist(x+y)
# marginal PMF of X
hist(x,breaks=seq(-1/2,n+1/2,1))
# order statistics
# generate order statistics of 10 i.i.d. N(0,1) r.v.s
sort(rnorm(10))
# generate 10^4 replications of the 9th order statistic of 10 i.i.d. N(0,1) r.v.s
order_stats <- replicate(10^4, sort(rnorm(10)))
x9 <- order_stats[9,]
hist(x9)
#############
# CHAPTER 9 #
#############
# linear regression
x <- rnorm(100)
y <- 3 + 5*x + rnorm(100)
b <- cov(x,y) / var(x)
a <- mean(y) - b*mean(x)
plot(x,y)
abline(a=a,b=b)
##############
# CHAPTER 10 #
##############
# Jensen's
x <- rexp(1000)
log(mean(x))
mean(log(x))
##############
# CHAPTER 11 #
##############
# matrix calculations
Q <- matrix(c(1/3,1/3,1/3,0,
0,0,1/2,1/2,
0,1,0,0,
1/2,0,0,1/2),nrow=4,ncol=4,byrow=TRUE)
Q2 <- Q %*% Q
Q3 <- Q2 %*% Q
Q4 <- Q2 %*% Q2
Q5 <- Q3 %*% Q2
Q5[3,4]
eigen(t(Q))
##############
# CHAPTER 12 #
##############
# Metropolis-Hastings
y <- 3
sigma <- 1
mu <- 0
tau <- 2
d <- 1
niter <- 10^4
theta <- rep(0,niter)
theta[1] <- y
for (i in 2:niter){
theta.p <- theta[i-1] + rnorm(1,0,d)
r <- dnorm(y,theta.p,sigma) * dnorm(theta.p,mu,tau) /
(dnorm(y,theta[i-1],sigma) * dnorm(theta[i-1],mu,tau))
flip <- rbinom(1,1,min(r,1))
theta[i] <- if(flip==1) theta.p else theta[i-1]
}
theta <- theta[-(1:(niter/2))]
hist(theta)
mean(theta)
var(theta)
# Gibbs sampler
x <- 7
lambda <- 10
a <- 1
b <- 1
niter <- 10^4
p <- rep(0,niter)
N <- rep(0,niter)
##############
# CHAPTER 13 #
##############
# 2D Poisson process
L <- 5
lambda <- 10
n <- rpois(1,lambda*L^2)
x <- runif(n,0,L)
y <- runif(n,0,L)
plot(x,y,pch=4)