STATS 320: introduction to R
(Self Study)
March 3, 2015
What is R?
A statistical computation environment.
Implementation of statistical procedures.
Interactive use like a calculator or as a programming language.
Example: calculate the volume of a cylinder
Statistics 320
Assignment 2
Due: 4pm, April 5, 2012
1 Consider the mixed congruential generator
xi+1 = 32xi + 2
mod 31
a What is the largest possible period this generator may have?
b Start with x0 = 16 and produce x1 , x2 , . . . , x31 . You should expla
Statistics 320
1
a
Assignment 3 Answers
Semester 1, 2012
i The following program was used for this simulation
# Question 1
#
# Part a i
#
# Simulation of the M/M/1 queue
#
# Set parameters for arrival and service times
lambda <- 1
mu <- 2
# Specify time f
Statistics 320
1
Assignment 2 Answers
Semester 1, 2012
a 31.
b We have x0 = 16,
x1 = (32x0 +2) mod 31 = 514 mod 31 = (3116+18) mod 31 =
18
x2 = (32x1 +2) mod 31 = 578 mod 31 = (3118+20) mod 31 =
20
The full sequence is given in the printout from R.
> x<-r
Statistics 320
1
Assignment 1 Answers
Semester 1, 2012
1
0 c sin(x) dx
a
= (2c/)
= [(c/) cos(x)]1 = (c/)( cos() + cos(0)
0
c = /2.
b For 0 < x < 1,
x
F (x) = 0 (/2) sin(y) dy = [(1/2) cos(y)]x = (1/2)( cos(x)+
0
1)
Thus
for x 0
0
1
1 cos(x) for 0 < x <
Statistics 320
Assignment 4
Due: 4pm, May 31, 2012
Note: This is the compulsory computing
assignment
1 On Cecil under Programs you will nd programs for simulating an
M/M/1 queue and using batched means to estimate average queue
length. The R version is ca
Statistics 320
Assignment 4 Answers
Semester 1, 2012
1 The program used for this question was
#
# Question 1
#
# Simulate an M/M/1 queue and use batched means to estimate
# mean queue length
#
# Arrivals form a Poisson process, rate lambda
# Service times
STATS 320
Tutorial, Week 2
March 13, 2015
1 Let X and Y be discrete random variables taking values in cfw_1, 0, 1, 2
and cfw_2, 0, 2 respectively. Suppose that their joint probability function pXY (x, y) is given by
pXY (x, y)
-1
x
0
1
2
y
0
2
0
1
4
1
12
STATS 320
Tutorial, Week 1
March 6, 2014
1 In an election, candidate A receives n votes while candidate B receives
just 1. What is the probability that A was always ahead in the vote
count assuming that every ordering in the vote is equally likely.
First:
STATS 320
Tutorial, Week 4
March 27, 2014
1 Show that E(X Y |Y = k) = E(X|Y = k) k and Var(X Y |Y =
k) = Var(X|Y = k) (assuming X is a continuous random variable)
Using the denition of the expectation, we have:
E(X Y |Y = k) =
(x k)Pr(X Y = x k|Y = k)dx
=
STATS 320
Tutorial, Week 3
March 20, 2015
Consider a stochastic process cfw_Xt , t cfw_0, 1, 2, . . . with the one-step transition probabilities given below:
j = 0, i = 1
1
1
j = N, i = N 1
Pr(Xt+1 = i|Xt = j) =
i=j1
p/2
p/2
i=j+1
1 For N = 10, use R an
STATS 320
Tutorial, Week 3
March 20, 2015
Consider a stochastic process cfw_Xt , t cfw_0, 1, 2, . . . with the one-step transition probabilities given below:
j = 0, i = 1
1
1
j = N, i = N 1
Pr(Xt+1 = i|Xt = j) =
i=j1
p/2
p/2
i=j+1
1 For N = 10, use R an
STATS 320
Tutorial, Week 7
1 May 2015
1. Consider the following probabilities of being sunny, overcast and raining the next day given the weather of the present day:
Today
Sunny
Overcast
Raining
Sunny
0.65
0.30
0.20
Tomorrow
Overcast Raining
0.25
0.10
0.4
STATS 320
Tutorial, Week 7
1 May 2015
1. Consider the following probabilities of being sunny, overcast and raining the next day given the weather of the present day:
Today
Sunny
Overcast
Raining
Sunny
0.65
0.30
0.20
Tomorrow
Overcast Raining
0.25
0.10
0.4
Statistics 320
Assignment 2
Due: 2pm, 23 April 2015.
1 Show that
i = 2N
N
i
for all i in [0, N ], satises the equality = P, where P is the 1step transition probability matrix for the Erhenfest process with N
particles.
2 What is the variance of the time t
STATS 320
Assignment 3
Due: 2pm, Thu 7 May 2015
Question 1 in this assignment is about materials taught in the rst half of
the semester, and Questions 2 and 3 are about those taught in the second
half.
1 [20 marks] Write a computer program that calculates
Statistics 320
Assignment 3
Due: 4pm, May 10, 2012
1 In this question you are required to simulate a single server queue.
There are basic programs to do this in both R and Matlab available
on Cecil under Programs. (The R version is mm1queueR.txt and the
M
Statistics 320
Assignment 1
Due: 4pm, March 22, 2012
1 Suppose X is a random variable with probability density function
f (x) =
c sin(x) for 0 < x < 1
0
otherwise
a Determine the value of c.
b Determine the distribution function of X.
c Sketch the density
STATS 320 General Queues
Department of Statistics
The University of Auckland
Semester 1, 2015
1 / 31
Outline
1
Queues
2
Littles Laws
3
Simulating M/M/1: Second Method
4
Non-Markovian Queues
5
Busy and Idle Periods
6
PASTA
2 / 31
1. Queues
1. Queues
Arriva
STATS 320 Random Variables and Distributions
Department of Statistics
The University of Auckland
1
Outline
Random variables
A RV is a variable whose value is subject to variation due to
randomness.
A RV X is a real-valued function dened on the sample spac
STATS 320: Probabilities
Department of Statistics
The University of Auckland
1
Outline
1 Monty Hall problem
2 Set Theory
3 Probability
4 Conditional Probability, Independence & Bayes theorem
5 Exercises
2
1 Monty Hall problem
2 Set Theory
3 Probability
4
STATS 320: Markov chains (introduction)
Department of Statistics
The University of Auckland
1
Outline
1 The Erhenfest model of diusion
Outline
1 The Erhenfest model of diusion
2 Markov chains
2
1 The Erhenfest model of diusion
2 Markov chains
3
The Erhenf
STATS 320 Markov chains (ctnd)
Department of Statistics
The University of Auckland
March 19, 2015
1
Stationary Markov chain
Denition
A (discrete-time) stochastic process cfw_Xt : t 0 is stationary if,
for any k 0 and any m t, the joint distribution of (Xt
STATS 320 Random Variables and Distributions
Department of Statistics
The University of Auckland
1
Outline
1 Conditional expectation
2 More than one Random Variable: Joint Distributions
3 Dependence
4 Linear Functions
5 Distributions
2
1 Conditional expec
STATS 320 More on stationarity
Department of Statistics
The University of Auckland
March 23, 2015
1
1 Return time to state i
2 The Erhenfest process
Stationary distribution so far
Assume a well-behaved Markov chain.
i is the long-run frequency at which st
STATS 320 Simulating Markov chains
Department of Statistics
The University of Auckland
March 25, 2015
1
Outline
1 Sampling from (.)
2 Simulating the Erhenfest process
2
Simulating a discrete-time Markov chain
0 : vector of initial state frequencies. P: 1-
STATS 320 Googles PageRank algorithm
Department of Statistics
The University of Auckland
April 20, 2015
1
Pre-history of internet search engines
Type in your query.
Search engine keeps an index of all web pages. Browses
through it and counts the occurence
STATS 320 The Metropolis-Hastings algorithm
Department of Statistics
The University of Auckland
April 22, 2015
1
Introduction
Metropolis proposed a rst algorithm in 1953, generalized by
Hastings in 1970.
Huge impact on statistics, in particular statistica
STATS 320 Population genetics
Department of Statistics
The University of Auckland
March 26, 2015
1
Denitions
The genome of an organism is its whole hereditary
information. It is encoded in the DNA.
A given fraction of a genome is called a gene. Genes are