R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # chex45.r J F Monahan, May 2010 > # > # demonstration of Levinson-Durbin algorithm > # likelihood computation for ARMA(1,1) model > # > # set parameter values > phi <- .9 > th <- .5 > n <- 4 > # table covariance function -- gamma(0) stored in gam[1] > gam <- rep(0,n+1) > gam[1] <- (1+th*th-2*phi*th)/(1-phi*phi) > gam[2] <- (1-phi*th)*(phi-th)/(1-phi*phi) > for(j in 3:(n+1)) gam[j] <- phi*gam[j-1] > "covariance function" [1] "covariance function" > gam [1] 1.8421053 1.1578947 1.0421053 0.9378947 0.8441053 > # Toeplitz covariance matrix > d <- 1 + abs(matrix(1:n,n,n) - t(matrix(1:n,n,n))) > A <- matrix(gam[d],n,n) > "covariance matrix" [1] "covariance matrix" > A [,1] [,2] [,3] [,4] [1,] 1.8421053 1.157895 1.042105 0.9378947 [2,] 1.1578947 1.842105 1.157895 1.0421053 [3,] 1.0421053 1.157895 1.842105 1.1578947 [4,] 0.9378947 1.042105 1.157895 1.8421053 > # make correlations > r <- gam[1+(1:n)]/gam[1] > "correlations" [1] "correlations" > r [1] 0.6285714 0.5657143 0.5091429 0.4582286 > "right hand side" [1] "right hand side" > y <- c(-2,1,0,1) > y [1] -2 1 0 1 > # start Levinson-Durbin > d <- 1 > b <- -r[1] > x <- y[1] > det <- 1 > # set up for Cholesky factor > LAinv <- diag(rep(1,n)) > for(k in(2:n)) { + # first the scalars + d <- 1 + sum(r[1:(k-1)]*b[1:(k-1)]) + LAinv[((n-k+1):n),n-k+1] <- c(1,b)/sqrt(d) + e <- r[k] + sum(r[rev(1:(k-1))]*b[1:(k-1)]) + f <- y[k] - sum(r[rev(1:(k-1))]*x[1:(k-1)]) + print("d,e,f") + print(c(d,e,f)) + # then the vectors + x <- c(x+(f/d)*b[rev(1:(k-1))],f/d) + print(x) + b <- c(b-(e/d)*b[rev(1:(k-1))],-e/d) + print(b) + det <- det*d + } [1] "d,e,f" [1] 0.6048980 0.1706122 2.2571429 [1] -4.345479 3.731444 [1] -0.4512821 -0.2820513 [1] "d,e,f" [1] 0.55677656 0.07655678 0.11282051 [1] -4.4026316 3.6400000 0.2026316 [1] -0.4125 -0.2200 -0.1375 [1] "d,e,f" [1] 0.54625000 0.03732143 1.05500000 [1] -4.6681922 3.2151030 -0.5940503 1.9313501 [1] -0.40310559 -0.20496894 -0.10931677 -0.06832298 > "solution" [1] "solution" > x/gam[1] [1] -2.5341615 1.7453416 -0.3224845 1.0484472 > "determinant" [1] "determinant" > det*(gam[1]^n) [1] 2.118421 > "Cholesky factor of inverse" [1] "Cholesky factor of inverse" > LAinv [,1] [,2] [,3] [,4] [1,] 1.3530202 0.0000000 0.0000000 0 [2,] -0.5581208 1.3401689 0.0000000 0 [3,] -0.2976644 -0.6047942 1.2857571 0 [4,] -0.1860403 -0.3779964 -0.8081902 1 > "Is it identity? (except for gamma(0))" [1] "Is it identity? (except for gamma(0))" > t(LAinv) %*% A %*% LAinv [,1] [,2] [,3] [,4] [1,] 1.842105e+00 1.980033e-16 -1.254141e-16 1.110223e-16 [2,] 2.633538e-16 1.842105e+00 1.794543e-16 -2.220446e-16 [3,] -2.078427e-16 2.491349e-16 1.842105e+00 2.220446e-16 [4,] 1.110223e-16 -2.220446e-16 2.220446e-16 1.842105e+00 > "Cholesky factor" [1] "Cholesky factor" > Lt <- chol(A) > Lt [,1] [,2] [,3] [,4] [1,] 1.357242 0.8531234 0.7678111 0.6910300 [2,] 0.000000 1.0555973 0.4763721 0.4287349 [3,] 0.000000 0.0000000 1.0127394 0.4177550 [4,] 0.000000 0.0000000 0.0000000 1.0031201 > det <-prod(diag(Lt)) > "determinant" [1] "determinant" > det*det [1] 2.118421 > z <- c(-2, 1, 0, 1) > one <- rep(1,4) > LiBz <- forwardsolve(t(Lt),z) > "solution" [1] "solution" > backsolve(Lt,LiBz) [1] -2.5341615 1.7453416 -0.3224845 1.0484472 > LiBone <- forwardsolve(t(Lt),one) > zAiz <- sum( LiBz * LiBz ) > zAione <- sum( LiBz * LiBone ) > oneAione <- sum( LiBone * LiBone ) > muhat <- zAione / oneAione > "muhat" [1] "muhat" > muhat [1] -0.07971014 > "quadratic form" [1] "quadratic form" > qf <- zAiz - zAione*muhat > qf [1] 7.857101 > # clean up > rm(list=ls()) > q()