Skip to content
All library documents

Debugging Constrained GARCH Likelihood Optimization in R

Article Quant Q&A · Author: Nils

Summary

The question concerns finding valid starting values for constrained maximum likelihood estimation of a GARCH volatility model in R. The response identifies a more immediate issue in the likelihood function: it overwrites the likelihood on each loop iteration and does not return a value, so the optimizer cannot evaluate even the supplied starting point. The suggested first step is to evaluate the function directly and correct its output before investigating parameter constraints or alternative algorithms.

The answer offers no GARCH estimation results or detailed guidance on choosing a feasible parameter region. It also notes that the posted code is difficult to interpret, limiting further diagnosis. The exchange therefore teaches a basic but important optimization check, rather than a complete method for estimating volatility or selecting valid initial parameters.

Key ideas

  • Evaluate the objective function directly at the proposed starting values to check that it returns a scalar.
  • The likelihood loop overwrites its value on each iteration instead of accumulating contributions.
  • The function must return a usable value before a constrained optimizer can proceed.
  • The response does not derive a feasible GARCH parameter region or recommend an alternative estimation algorithm.

Tags

Full text
# Starting values for constrOptim() in R


# Starting values for constrOptim() in R












I want to perform a constraint optimization for Maximum Likelihood Estimation in R to forecast volatility of returns. The probleme is that my initial values aren't in the permitted region. Is there something like an EM algorithm which I can use. Somebody an idea how to find the value region?

my code:

```
library(quantmod)
getSymbols('^GDAXI', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
GDAXI.DE=GDAXI[ , "GDAXI.Close"]
log_r1=diff(log(GDAXI.DE[39:2575]))

sigma=matrix(nrow=length(log_r1)-1, ncol=1)
for(i in 2:length(log_r1))
{
sigma[i-1]=var(log_r1[1:i])
}

error=matrix(nrow=length(log_r1), ncol=1)
mu=mean(log_r1)
for(i in 1:length(log_r1))
{
error[i]=log_r1[i]-mu
}

sig=var(log_r1)
er=error
Sigma_garch=matrix(nrow=length(sigma), ncol=1)
garch=function(th)
{
omega.0=th[1]
alpha.0=th[2]
beta.0=th[3]
for(i in 1:length(sigma))
{
if(i==1)
{
  Sigma_ga=sig
}else
{
  Sigma_ga=Sigma_garch[i-1]
}
Sigma_garch[i]=omega.0+alpha.0*er[i]^2+beta.0*Sigma_ga 
}
return(Sigma_garch)
}

LogLik=function(th)
{
input=garch(th)
sig=input
for(i in 1:length(sigma))
{
LL=(-1/2)*log(2*pi)-(1/2)*log(sig[i])-(1/2)*er[i]/sig[i]
}
}

ui=matrix(c(diag(1,3),c(0,(-1),(-1))),4,3, byrow = T)
ci=c(0.01,0,0,(-0.99))
theta=c(0.02,0.01,0.95)
est=constrOptim(c(0.0000029,0.095,0.0888),LogLik, grad=NULL, ui,ci,hessian=TRUE)
```

## Answer by Bob Jansen (score 1)

https://quant.stackexchange.com/a/24850

Your Log Likelihood function `LogLik` doesn't work: 1) `LL` is reset every iteration and 2) the function doesn't return anything, you can try yourself:

```
> LogLik(c(0.0000029,0.095,0.0888))
```

This is the reason for the error given by `constrOptim`, it seems that the function doesn't give a value for the initial vector (or for any value).

I find your code a bit hard to follow and I'm not sure what you're trying to implement so it's hard to give further advice.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.