You are here

Hessian wrong for R fit function

For an R fit function example, the Hessian comes back all zeros. I found this from thread https://openmx.ssri.psu.edu/thread/2230

#------------------------------------------------
set.seed(135)
nobs <- 13
adat <- data.frame(x=rnorm(nobs))
 
dmod <- mxModel(
    name='I will run fast on OpenMx',
    mxMatrix(name='A', nrow=nobs, ncol=1, free=T, values=0.1),
    mxMatrix(name='X', nrow=nobs, ncol=1, free=F, values=as.matrix(adat)),
    mxAlgebra((X-A) %^% 2, name='Row'),
    mxAlgebra(sum(Row), name='Red'),
    mxFitFunctionAlgebra('Red')
)
 
dmodRun <- mxRun(dmod) # runs super fast := 0.07 sec
omxCheckCloseEnough(mxEval(A, dmodRun), as.matrix(adat), epsilon=10^(-5))
 
 
#------------------------------------------------
robj1 <- function(model, state) {
    a <- model$A$values
    x <- model$X$values
    return(sum((x - a) ^ 2))
}
 
emod <- mxModel(
    name='I will run slow on OpenMx',
    mxMatrix(name='A', nrow=nobs, ncol=1, free=T, values=0.1),
    mxMatrix(name='X', nrow=nobs, ncol=1, free=F, values=as.matrix(adat)),
    mxFitFunctionR(robj1)
)
 
emodRun <- mxRun(emod) # runs super slow := 10.5 sec
omxCheckCloseEnough(mxEval(A, emodRun), as.matrix(adat), epsilon=10^(-5))
 
summary(emodRun)
#free parameters:
#                                name matrix row col    Estimate Std.Error
#1   I will run slow on OpenMx.A[1,1]      A   1   1 -0.44507074        NA
#2   I will run slow on OpenMx.A[2,1]      A   2   1 -0.46596519        NA
#3   I will run slow on OpenMx.A[3,1]      A   3   1 -0.10446305        NA
#4   I will run slow on OpenMx.A[4,1]      A   4   1  1.40833517        NA
#5   I will run slow on OpenMx.A[5,1]      A   5   1 -1.31608659        NA
#6   I will run slow on OpenMx.A[6,1]      A   6   1  0.24520877        NA
#7   I will run slow on OpenMx.A[7,1]      A   7   1  1.20474301        NA
#8   I will run slow on OpenMx.A[8,1]      A   8   1 -0.44070022        NA
#9   I will run slow on OpenMx.A[9,1]      A   9   1  0.42282637        NA
#10 I will run slow on OpenMx.A[10,1]      A  10   1  0.96486349        NA
#11 I will run slow on OpenMx.A[11,1]      A  11   1  1.00777299        NA
#12 I will run slow on OpenMx.A[12,1]      A  12   1  0.05038159        NA
#13 I will run slow on OpenMx.A[13,1]      A  13   1  0.61003287        NA
 
# R fit function Hessian is all zero
all(emodRun$output$calculatedHessian == matrix(0, 13, 13))
#[1] TRUE
all(emodRun$output$hessian == matrix(0, 13, 13))
#[1] TRUE
 
# Row fit function Hessian is diagonal: 2*I
omxCheckCloseEnough(dmodRun$output$calculatedHessian, diag(2, 13), 1e-10)
#dmodRun$output$calculatedHessian and diag(2, 13) are equal to within 1e-10.
Reporter: 
Created: 
Wed, 07/16/2014 - 21:50
Updated: 
Mon, 07/21/2014 - 12:59

Comments

I have a 1 line fix. The problem only affects mxFitFunctionR. I can check it in now or we can wait until after beta2.

Great! I'm glad there's a 1-line fix. I'd hold on committing it until after the next Beta, provided the Beta is actually being released soon. I have several changes, I've been holding until after the next Beta.

Joshua fixed this.