[R] a question about gradient in optim

Mehmet Balcilar mbalcilar at gmail.com
Tue May 23 11:03:33 CEST 2006


I guess I just need to sum over observations.

Here are my functions:

--------------------------------------------------------------------------------------------
#
#  function to compute gradient vector of the two-regime LSTAR model
#

dlogistic <- function(theta,x,z,g.scale)
{
  n = nrow(x)
  k = length(theta)
  k1 = (k-2)/2 + 2
  G = logistic(z,g.scale,exp(theta[1]),theta[2])
  dgam = exp(theta[1]) * (x %*% theta[3:k1] - x %*% theta[(k1+1):k]) * (G*(1-G)) * (z-theta[2])/g.scale
  dc = (x %*% theta[(k1+1):k] - x %*% theta[3:k1]) * (G*(1-G)) * (exp(theta[1])/g.scale)*rep(1,T)
  return(cbind(dgam,dc));
}

#
# function to compute the value of the logistic transition function
#

logistic <- function(z,g.scale,gam,c)
{
  return(1./(1+exp(-(gam/g.scale)*(z-c))))
}



#  gradlstar
#
#  function to compute gradient matrix of parameter estimates for
#  LSTAR model
#
#

gradlstar = function(theta,x,z,g.scale)
{
  dgamdc=dlogistic(theta,x,z,g.scale)
  G = logistic(z,g.scale,exp(theta[1]),theta[2])
  dphi_1 = x*(1-G)
  dphi_2 = x*G
  grad = cbind(dgamdc,dphi_1,dphi_2)
  return(grad)
}

---------------------------------------------------------------------------------


I guess

  apply(grad,2,sum)

will do what I want.

Thanks.

Prof Brian Ripley wrote:
> On Tue, 23 May 2006, Mehmet Balcilar wrote:
>
>> Hi,
>>
>> I am using optim to estimate the parameters of a smooth transition
>> autoregressive model.  I  wrote a function to return the  gradient  for
>> each observation. So, for k parameters and n observations my function
>> returns a (n x k) matrix. The gr argument of optim expects a (k x 1)
>> vector.  Now,  what is the correct way of passing my gradient matrix to
>> optim?
>
> gr is documented to be a function, not a vector!
>
> Since you are optimizing a numeric function such as the
> log-likelihood, the derivative is a vector, not a matrix, and your gr
> function should return a vector.  We have far too few details, but for
> example did you forget to sum over observations?
>



More information about the R-help mailing list