BM = rep(0,T)

n = 100

count = 0

for (i in 1:n){

X = rnorm(T,mu,sigma)

BM[1] = X[1]

for (j in 2:T){

BM[j] = BM[j-1]+X[j]

}

if (BM[T]>3) {count = count+1}

}count/n

count = 0

for (i in 1:n){

X[1] = 5

for (j in 2:(T+1)){

inc = sample(posinc,1)

X[j] = X[j-1]+inc

X[j] = max(X[j],0)

}

if (X[T+1]<10){count = count + 1}

}

count/n

install.packages("expm")

library(expm)

vect = c(0.6,0.3,0.4,0.7)

P = matrix(vect,2,2)

P%^%5

P%^%10

P%^%20

# ~~~~~~~~~~~~~~~~~~~~~~~~~ b ~~~~~~~~~~~~~~~~~~~~~~~~~

P5 = P%^%5

P5[1,2]

t = 24

lambda = 20

n = 1000

Y = rep(0,n)

for (i in 1:n){

Nt = rpois(1,lambda*t)

X = rexp(Nt,1)

Y[i] = sum(X)

}

mean(Y)

var(Y)

entries = c(0.3,0.7,0,0.4,0.6,0,1,0,0)

P = matrix(entries,3,3)

P = t(P)

P%^%2

P%^%3

P%^%5

P%^%20

P%^%100

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

P = matrix(0,6,6)

P[1,] = c(0.3,0.7,0,0,0,0)

P[2,] = c(0.5,0.5,0,0,0,0)

P[3,] = c(0.3,0.2,0,0,0,0.5)

P[4,] = c(0,0,0.8,0,0.2,0)

P[5,] = c(0,0,0,0.6,0,0.4)

P[6,] = c(0,0,0,0,0,1)

P

rowSums(P)

P%^%2

P%^%3

P%^%5

P%^%20

P%^%100

P%^%1000

P%^%10000

n = 1e5

T = 90

X = matrix(0,T,n)

for (k in 1:n){

posinc = c(2,1,1,1,0,0,0,0,-1,-1)

X[1,k] = 20+sample(posinc,1)

}

for (k in 1:n){

for (j in 2:T){

posinc = c(2,1,1,1,0,0,0,0,-1,-1)

X[j,k] = X[j-1,k]+sample(posinc,1)

X[j,k] = max(X[j,k],0)

}

}

mean((X[T,]>5)*(X[T,]<15))

mean(X[60,])

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~ Question 2 ~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

t = 12

lambda = 10

n = 1e5

Y = rep(0,n)

for (i in 1:n){

Nt = rpois(1,lambda*t)

X = rgamma(Nt,1,1)

Y[i] = sum(X)

}

mean(Y>100)