# Solved – MLE of Cauchy distribution in R

I am trying to compute the following "approximate Maximum Likelihood Estimate" in R. I am a little lost as to how to do this though: any hints would be appreciated.

Contents

You want to use the function `nlm()` to calculate the argmax, which requires a function to be optimized as the first argument and the an initial guess at the parameters as the second argument, like so:

``# define g(x;gamma) g  <-  function(x,gamma) {     (1/pi) * ( gamma / (x^2 + gamma^2) ) }  # define y here so it can be used inside fun() via lexical scoping y <- blah blah blah  # the function to be optimized is optimized over the first argument. fun  <- function(x){     # separate the first argument into the variables needed      # for opimization     phi = x[1:2]     psi = x[3:4]     gamma = x      # PERFORM THE BIG SUM     out  <-  0     for(t in seq(r + 1,T-s)){         # calculate A         A  <-  (y[t]   * (psi*phi + 1)                 + (psi*phi + phi) * y[t-1]                 - phi* y[t-2]                 - psi * y[t+1]                 )         # increment the sum         out  <-  out + log(g(A,gamma))     }     #nlm performs the argmin, so we'll return -out     -out }  # do the optimization out  <-  nlm(fun,         # initial guess at the parameters,              c(0,0,# phi                0,0,# psi                0))# gamma  phi_hat   <- out\$estimate[1:2] 	psi_hat   <- out\$estimate[3:4] gamma_hat <- out\$estimate ``

Note that `nlm()` does not make restrictions on the arguments so, for example, if you need the arguments to be positive, you need to make a transformation like this:

``phi = exp(x[1:2]) ``

in `fun()` and also here:

``phi_hat   <- exp(out\$estimate[1:2]) ``

Rate this post