To check my understanding so far:
In short: template my functions and template my parameter arguments or, alternatively, “classify” my parameters according to the stan::agrad::var hierarchy. Other, non-parameter arguments (function data inputs or function outputs, even by reference) do not need to be generalized, correct? Parameters, other inputs and outputs should be done via std::vector that Stan creates when translating from Stan to C++, correct?
Without a firmer grasp of these details, I would prefer to be as Stan-independent as possible to allow for running of my growth model outside of Stan, so this makes me want to choose _not_ to use the stan::agrad::var hierarchy but to use instead the template mechanism.
Does this make sense?
Best,
Jarrett
This helps! Thanks.
Alas, my tree growth function calls other user-written C functions, which call other user-written C functions…most of which make heavy use of gsl C library functions. Instead of templafying “everything”, I am hoping to create a sort of wrapper for my “main” tree growth function, so that the wrapper assigns the values of stan::agrad::var typed parameters to standard types that subsequent functions can operate on without (much) further generalization. Does this sound reasonably doable to you?
Best,
Jarrett
Alas, my tree growth function calls other user-written C functions, which call other user-written C functions…most of which make heavy use of gsl C library functions. Instead of templafying “everything”, I am hoping to create a sort of wrapper for my “main” tree growth function, so that the wrapper assigns the values of stan::agrad::var typed parameters to standard types that subsequent functions can operate on without (much) further generalization. Does this sound reasonably doable to you?
You either have to be able to template everything
else out or you have to be able to calculate
derivatives with respect to the parameters. If you
can calculate derivatives, it's actually not that hard
to interface with the derivative code. But p
Differentiation (wrt parameters) for my implicitly defined tree radius is no problem on paper (Implicit Function Theorem) (I haven't actually done this yet, but I expect this to be no problem, on paper, because my equations are relatively simple.) But, my lack of experience in C++/Stan innards leaves me wondering: will I be able to exploit a relatively high-level solution using templates, as we have been discussing, or do I have to dig down and "calculate derivatives" and "interface with the derivative code" as Bob suggests?
BTW, have been using my own MH MCMC routine for sampling a small subset of parameters, but I anticipate problems in the future as I try to sample a larger number of parameters, use larger data sets, and make model extensions. I think HMC might be one way to help with anticipated problems, and I think Stan might save me from reinventing the wheel.
Continued thanks to Bob and Michael for kindly guiding me toward to a solution.
I’m trying to reconcile Michael’s comments, “It is possible to automatically differentiate these kinds of solutions, by either explicitly differentiating the root finder or appealing to the things like the Implicit Function Theorem” with, in an earlier post, “It is possible but would require adding a new var implementation which requires a much deeper understanding of the inner workings of the autodiff code.” (Perhaps I mistakenly conflate these 2 posts.)
Selfishly assuming that I’m only looking for a solution to my particular problem at the moment, I may use implicit differentiation to get the derivative of my implicit function. A more careful (considering existence, multiple implicit functions, etc.) approach for more a more general solution of cases with implicit functions may have to appeal to the Implicit Function Theorem (or related), along with a much more ambitious code effort. But I think I may get by for now with the derivative obtained by implicit differentiation for my particular case.
So, I have (“…explicitly…”, as I take Michael to mean) the analytic derivative in hand. Then, I must implement “…via a new var implementation…then implement the chainable method in the corresponding vari”. Once I do this, then, as Michael says, I can “…automatically differentiate…”. Yes?
Am I missing a possibly more “automatic”, high-level approach that doesn’t require entering the innards of var and vari and the chainable method?
And, yes, I do anticipate huge differences going forward (and Stan's cool), hence my persistence in spite of revealing my neophyte level of C++ know-how. (I can see the power of generalization, but I’m still devising my coup.)
I’ll keep in mind Bob’s suggestion about finite differences.
Somewhat gropingly,
Jarrett
So, I have (“…explicitly…”, as I take Michael to mean) the analytic derivative in hand. Then, I must implement “…via a new var implementation…then implement the chainable method in the corresponding vari”. Once I do this, then, as Michael says, I can “…automatically differentiate…”. Yes?