Solved – Optim result highly dependent on starting value

fittinglikelihoodoptimizationr

I want to fit a standardized Student's-t distribution. The log-likelihood is given by:

\begin{align*}
log \mathcal{L}(\nu | l_1,…,l_n)=\sum_{i=1}^n \left( log \left( (\pi (\nu-2))^{-\frac{1}{2}}\Gamma \left(\frac{\nu}{2} \right)^{-1} \Gamma \left(\frac{\nu+1}{2} \right) \left(1+\frac{l^2}{\nu-2} \right)^{\text{$-\frac{1+\nu}{2}$}}\right)\right)
\end{align*}

I try do to this with the optim command. My R code is (data):

# log likelihood
pinumber<-3.141592653589793
startvalue<-55

loglikstandardizedt <-function(par){
if(par>0) return(-sum(log((pinumber*(par-2))^(-1/2)*gamma(par/2)^(-1)*gamma((par+1)/2)*(1+standresidalvewma^2/(par-2))^(-(1+par)/2))))
else return(Inf)
}

optim(startvalue,loglikstandardizedt, method="BFGS")
param = optim(startvalue,loglikstandardizedt, method="BFGS")$par

I can control it via looking at the plot:

  denstiystandtresid<-function (x) (pinumber*(param-2))^(-1/2)*gamma(param/2)^(-1)*gamma((param+1)/2)*(1+x^2/(param-2))^(-(1+param)/2)

    plot(density(standresidalvewma),ylim=c(0,0.8))
    curve(denstiystandtresid,col="red",add=TRUE)

I have the problem, that the output of optim, that means the value for "par" is highly dependend on the starting value. What is the right starting value? And what is the best value for my $\nu$?

The variation is very large, I get values from 27 up to 215, so this is a huge difference?

Best Answer

In general, when using optim for one dimensional problem, you should set the method argument to Brent otherwise optim is indeed unreliable.

There are several problems with your code as written:

  • pinumber you can use pi instead (it's a reserved constant in R)
  • par is a reserved keyword in R so it's better to use another name.
  • loglikstandardizedt assumes a vector standresidalvewma which is not declared in the call to loglikstandardizedt