1. r1 <- (1/8) / (1/20) r2 <- r1 * ((1/8) / (1/25+1/25)) pi0 <- 1 / (1 + r1+r2) pi1 <- pi0 * r1 pi2 <- pi0 * r2 pi <- c(pi0,pi1,pi2) pi # desired proportion is the rate of repair completions while in state 0, # divided by the overall rate of repair completions propRepsState0 <- pi0 * (1/8) / (pi0 * (1/8) + pi1 * (1/8) ) print(propRepsState0) 2. findProbsKLater <- function(hmmOut,k) { nstates <- hmmOut@nstates u <- getpars(hmmOut) p <- u[(nstates+1):(nstates + nstates^2)] p <- matrix(p,nrow=nstates,byrow=T) prodK <- diag(nstates) for (i in 1:k) { prodK <- prodK %*% p } samplePath <- hmmOut@posterior$state lastState <- samplePath[length(samplePath)] prodK[lastState,] } library(hmmr) data(sp500) cls <- sp500$Close z <- hmm(cls,3) print(findProbsKLater(z,2)) 3. From our book: f_Y(s) = s f_X(s) / EX F_Y(t) = integral f_Y(s) ds from 0 to t = integral s f_X(s) ds from 0 to t / EX Using integration by parts, with u = s and dv = f_X(s) ds, the above integral becomes uv - integral(v du) s F_X(s) eval from 0 to t - integral F_X(s) ds from 0 to t = tF_X(t) - integral F_X(s) ds from 0 to t FY <- function(FX,c,t,EX=NULL) { if (is.null(EX)) { F1 <- function(s) 1 - FX(s) EX <- integrate(F1,0,c)$value } uv <- t * FX(t) / EX ivdu <- integrate(FX,0,t)$value / EX uv - ivdu } FXexample <- function(s) s^2 # density is 2t on (0,1) print(FY(FXexample,1,0.5,2/3)) # should be 1/8 print(FY(FXexample,1,0.5)) # should be 1/8