2

I have a time-series which I need to fit onto an AR (auto-regression) model.

The AR model has the form:

x(t) = a0 + a1*x(t-1) + a2*x(t-2) + ... + aq*x(t-q) + noise.

I have two contraints:

  1. Find the best AR fit when lag.max = 50.
  2. Sum of all coefficients a0 + a1 + ... + aq = 1

I wrote the below code:

    require(FitAR)
    data(lynx) # my real data comes from the stock market.
    z <- -log(lynx)

    #find best model
    step <- SelectModel(z, ARModel = "AR" ,lag.max = 50, Criterion = "AIC",Best=10)
    summary(step) # display results

    # fit the model and get coefficients
    arfit <- ar(z,p=1, order.max=ceil(mean(step[,1])), aic=FALSE)

    #check if sum of coefficients are 1
    sum(arfit$ar)
    [1] 0.5784978

My question is, how to add the constraint: sum of all coefficients = 1?

I looked at this question, but I do not realize how to use it.

                      **UPDATE**

I think I manage to solve my question as follow.

  library(quadprog)

  coeff <- arfit$ar
  y <- 0
  for (i in 1:length(coeff)) {
    y <- y + coeff[i]*c(z[(i+1):length(z)],rep(0,i))
    ifelse (i==1, X <- c(z[2:length(z)],0), X <- cbind(X,c(z[(i+1):length(z)],rep(0,i))))
  }

  Dmat <- t(X) %*% X

  s <- solve.QP(Dmat , t(y) %*% X, matrix(1, nr=15, nc=1), 1, meq=1 )
  s$solution
  # The coefficients should sum up to 1
  sum(s$solution)
Community
  • 1
  • 1
Paul
  • 73
  • 6

0 Answers0