Some of the models I fit quite regularly have fairly straightforward analytical gradients but are very expensive to evaluate via autodiff. I know it’s in theory possible to hack together my own version of Stan with these analytical gradients defined on the back end, but I’d probably mess that up. Something like this would be extremely handy:
data {
...
}
parameters {
real par1;
real par2;
}
gradients {
par1 = some expression;
// gradient wrt par2 to be left to autodiff
}
model {
...
}
This isn’t much of a question, but would something like this be possible in theory?
I needed to calculate \frac x {1 - x} or \exp(\mbox{logit}(x)) in a hot path, but could not find it in the Stan math library. It has derivative \frac 1 {(1-x)^2}
User defined gradients are in Stan Road Map https://github.com/stan-dev/stan/wiki/Stan-Road-Map,
but while waiting for them, what @aaronjg did doesn’t seem that complicated (given that the process of including additional .hpp is not too complicated for you)
We have to trust our users. There’s so many ways to screw up a Stan program that we can’t really provide much protection.
I’d trust a fair bit more than 0% because some users are careful with software and test as they go.
And as others have noted, we wouldn’t do this without finite diff or autodiff tests.
What we’re likely to add is a way to define a function with gradients, not just define gradients w.r.t. the log density for parameters.
Then users will only define gradients w.r.t. constrained parameters, so we’ll need to chain the transforms and Jacobians for any constrained parameters.
I really look forward to be able to define something like a _grad function for my custom _lpdf/lpmf function. And not have to deal with c++.
Is there an open issue about this that I can track?
I can’t code something like this, but I volunteer to help with the testing and documentation :)
The research project I am working on will require custom gradients. Is there a guide or some instructions on how to modify source and recompile for use with PyStan 3? Thanks in advance for any answers