Академический Документы
Профессиональный Документы
Культура Документы
seq(), rep()
sample(), rnorm(), rbinom(), pbinom()
matrix(), t(), diag(), solve()
dim(), nrow(), ncol()
apply()
sd(), var(), cumsum()
plot() # 1-dimensional data
1. Warm Up
Generate the following vectors and matrices
> sample(10)
> sample(1:10)
(a) A scientist needs to experiment upon 4 conditions (A, B, C and D), 5 times
each. Generate a vector (1, 1, 1, 1, 1, 2, . . . , 4)T of length 20, representing these
conditions.
(b) The scientist wants to do the 20 experiments in a completely random order;
generate such an order for her.
(c) (If you didn’t already do this above) How would you return a character vector
with entries "A", "B", "C", "D" containing your random permutation?
3. Random Walks
1
A random walk on the integers is a sequence X0 , X1 , X2 , . . . with X0 = 0, and
Xi = Xi−1 + Di ,
where the Di are independent, equal to +1 with probability p, and otherwise −1.
The command apply() allows you to neatly perform an operation on each row (or
column) of a matrix. For example, if you want the row-by-row averages of the matrix
A, you could use
2
(c) Solve the system of linear equations Ax = b.
(d) Find a vector containing the sums of each row of A.
Can you think of (or find) any other ways of achieving this?
(e) Create a second matrix B, where the ith column of B is the sum of the first i
columns of A.
(f) Calculate the standard deviation of each column of B (the command you need
is sd()).
5. Diagonals
6. *Brownian Motion
A standard Brownian motion is a continuous stochastic process (Xt )t≥0 such that
X0 = 0, Xt − Xs ∼ N (0, (t − s)), and disjoint increments are independent.
Produce a vector realizing Xt for t = 0, 0.01, 0.02, . . . , 1, and plot it.
Given values of Xt and Xt+2d , can you find the conditional distribution of Xt+d ?
Use this to expand the resolution of your process to increments of size 0.0025. The
function diff() might be useful.