Very simple example of debugging issue with Probabilistic syntax

38 views
Skip to first unread message

brandon...@gmail.com

unread,
Jan 15, 2025, 12:44:43 AM1/15/25
to TMB Users
Hello,
I'm new to using the syntax but familiar with TMB. I was having trouble debugging some code and located the issue. In the code below, any thoughts on why simply adding the probabilistic print statement seem to mess up the estimation but the other way doesn't.
Thanks to anyone who knows,
Brandon

set.seed(10)
data <- list(x = rbinom(100,1,prob=0.2))
parameters <- list(mu = 0)
f <- function(parms){
  RTMB::getAll(data,
               parms)
  x <- OBS(x)
  # print(x[1]%~%RTMB::dbinom(1,plogis(mu)))
  # print(RTMB::dbinom(1,1,plogis(mu),log=TRUE))
  nll <- -sum(dbinom(x,1,plogis(mu),log=TRUE))
  return(nll)
}
obj <- RTMB::MakeADFun(f,
                       parameters,
                       silent = FALSE)
opt <- nlminb(obj$par,
              obj$fn,
              obj$gr)
print(opt$par)

 

Saang-Yoon Hyun

unread,
Jan 15, 2025, 1:00:42 AM1/15/25
to brandon...@gmail.com, TMB Users
Hi, Brandon.
The line looks suspicious, which is "nll <- -sum(dbinom(x,1,plogis(mu),log=TRUE))."    Usually the folks do its calculation as follows:

Type nll = 0.0; 
for(int i=0; i<n; i++)  {
  ......
  nll -= dbinom(x(i),1,plogis(mu),log=TRUE));
};

Hope this helps.  Cheers,
Saang-Yoon 

--
To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
---
You received this message because you are subscribed to the Google Groups "TMB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/tmb-users/cb2c6ebe-ff79-447f-9e82-1958cab12ddbn%40googlegroups.com.

Brandon Chasco

unread,
Jan 15, 2025, 1:06:11 AM1/15/25
to Saang-Yoon Hyun, TMB Users
Hi Saang-Yoon,
Thanks for the quick response. The likelihood is fine: it's simply summing instead of doing the incrementing. The problem, I think, stems from the probabilistic syntax that was added to the RTMB package. For some reason, simply adding print(1%~%binom(1,par)) seems to blow up the estimation, even though it's not part of the likelihood. I'm sure it's just my unfamiliarity with the new syntax.
Brandon
--


Brandon Chasco

Kasper Kristensen

unread,
Jan 15, 2025, 6:45:01 AM1/15/25
to TMB Users
I'm not entirely sure I understand the problem, but it seems you expect this line (???):
x[1] %~% RTMB::dbinom(1,plogis(mu))
to be equivalent with
nll <- -sum(dbinom(x,1,plogis(mu),log=TRUE))

However, that looks like a typo. It should have been:
x %~% RTMB::dbinom(1,plogis(mu))

Reply all
Reply to author
Forward
0 new messages