gamma function

64 views
Skip to first unread message

Jan-Ole Koslik

unread,
Jan 16, 2025, 3:05:33 PMJan 16
to TMB Users
Hi, 

I'm currently implementing some nonstandard distributions to be compatible with RTMB. 

I am really not sure if I'm just overlooking stuff, but it seems like there is no simple AD implementation of the gamma function, which I really need. 

I found this post by Ben, where you (Kasper) mention the rtmbXtras package that has logspace_gamma().

However, as I use the distributions within a package, I would like to avoid linking to rtmbXtras in addition to RTMB. Would it be possible to have an implementation in RTMB? Or is there one and I am just really stupid?

For now I found the kind of silly workaround

lgamma_ad <- function(x) {
  - RTMB::dgamma(1, shape = x, scale = 1, log = TRUE) - 1
}
lgamma_ad(exp(-100))
lgamma(exp(-100))

but this seems very rough to me :D

Thanks in advance :)

Kasper Kristensen

unread,
Jan 17, 2025, 2:16:13 AMJan 17
to TMB Users
It's in the 'Math group', that's why its hidden:

RTMB::MakeTape(lgamma,1)

Jan-Ole Koslik

unread,
Jul 31, 2025, 6:07:18 AMJul 31
to TMB Users
Hi Kasper, thanks for that. 
How can I use lgamma inside a package and make sure it's using the RTMB version?

Kasper Kristensen

unread,
Jul 31, 2025, 6:23:14 AMJul 31
to Jan-Ole Koslik, TMB Users
Does it work to add this to the NAMESPACE file?:

import("RTMB") ## All RTMB functions and classes

You might not want to import everything though. There's probably a way to import only the Math group, but I'm not sure how to do that.



--
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/741d2200-021e-4374-a84c-5d9dab7548c2n%40googlegroups.com.
Message has been deleted

Jan-Ole Koslik

unread,
Aug 1, 2025, 5:28:39 AMAug 1
to TMB Users
Yes, I'm doing that at the moment but it leads to some other problems :/
Reply all
Reply to author
Forward
0 new messages