Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obj$fn() depends on starting values? #112

Closed
colemonnahan opened this issue Aug 18, 2015 · 3 comments
Closed

obj$fn() depends on starting values? #112

colemonnahan opened this issue Aug 18, 2015 · 3 comments

Comments

@colemonnahan
Copy link
Contributor

I have a simple logistc model (r and K are fixed effects) where I used the equivalent of ADMB's posfun to keep the population from going negative while adding a penalty in a way s.t. differentiability is maintained. I do this to avoid getting NaN values from fn and gr in the MCMC procedure. I'm trying to evaluate fn across a grid of parameters to visualize contours.

It seems to work but only for some starting values. If I build the model with the true parameters it fails:

> logistic.pars <- list(logr=logr.true, logK=logK.true)
> logistic.obj <- MakeADFun(data=logistic.data, parameters=logistic.pars, DLL='logistic')
> logistic.obj$fn(c(log(.1), log(1000)))
[1] NaN

However if I change the input parameters to what I want to evalulate it works:

> logistic.pars <- list(logr=log(.1), logK=log(1000))
> logistic.obj <- MakeADFun(data=logistic.data, parameters=logistic.pars, DLL='logistic')
> logistic.obj$fn(c(log(.1), log(1000)))
[1] 176776.5

Naive question here, but why does fn depend on the the values for parameters in MakeADFun. It seems to me that would only be used as the starting value for the optimizer.

@colemonnahan
Copy link
Contributor Author

@James-Thorson pointed out a thread that discusses posfun: #7. I tried this function and it helped alleviate this issue. I'd still like to understand why fn would depend on the values in parameters, so if someone has any ideas please share.

@kaskr
Copy link
Owner

kaskr commented Aug 31, 2015

I suspect your cpp file contains parameter dependent branching and that is what causes confusion...

Here is an example to demonstrate:

PARAMETER(x);
if (x>0) x = exp(x);
return x; 

It's important to know that the branching operation is not being taped. So, if you run from R

obj1 <- MakeADFun(parameters=list(x=1))

you will get a 'recording' of the mapping f(x)=exp(x) without branching.
Likewise,

obj0 <- MakeADFun(parameters=list(x=0))

will result in the mapping f(x)=x without branching.

If you wish to include the branching in the recording you need to use CppAD's conditional expressions (as in the 'posfun' example).

@colemonnahan
Copy link
Contributor Author

@kaskr That makes a lot of sense and is very interesting. My C++ knowledge doesn't go very deep so I was not aware of that. Thanks for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants