Issue #1985 highlighted how changes improving numerical accuracy do not always propagate their benefits to closely related distributions. In the case of standard transformations, the Stan community could capture some DRY benefits from connecting as many derived functions as possible to a reference function that keeps up with the latest numerical improvements (or at least tries to).
While I don't have the overview to make a super-issue of this – there are surely some places where it does make sense to duplicate even lengthy and complicated numerical code – I think there is some low-hanging fruit for the normal distribution. Along the lines of what I tried to do in pull request #3363, I would propose specifically:
- refactoring
normal_lcdf to rely on std_normal_lcdf (with some extra partials lines to avoid autodiffing, as discussed here);
- refactoring
lognormal_lcdf and lognormal_lccdf to rely on normal_lcdf (likewise with some extra partials lines);
- ditto for
exp_mod_normal_lcdf and exp_mod_normal_lccdf; and
- ditto for
skew_normal_lcdf and skew_normal_lccdf.
The wiener5_lpdf function already uses an approach somewhat like this.
On the OpenCL side, there is also std_normal_lcdf_scaled_impl, but I don't understand enough about OpenCL to know whether it would make sense to tie that to std_normal_lcdf also.
If there is agreement that this kind of structure would be useful, I am happy to take a stab at it.
Issue #1985 highlighted how changes improving numerical accuracy do not always propagate their benefits to closely related distributions. In the case of standard transformations, the Stan community could capture some DRY benefits from connecting as many derived functions as possible to a reference function that keeps up with the latest numerical improvements (or at least tries to).
While I don't have the overview to make a super-issue of this – there are surely some places where it does make sense to duplicate even lengthy and complicated numerical code – I think there is some low-hanging fruit for the normal distribution. Along the lines of what I tried to do in pull request #3363, I would propose specifically:
normal_lcdfto rely onstd_normal_lcdf(with some extra partials lines to avoid autodiffing, as discussed here);lognormal_lcdfandlognormal_lccdfto rely onnormal_lcdf(likewise with some extra partials lines);exp_mod_normal_lcdfandexp_mod_normal_lccdf; andskew_normal_lcdfandskew_normal_lccdf.The
wiener5_lpdffunction already uses an approach somewhat like this.On the OpenCL side, there is also
std_normal_lcdf_scaled_impl, but I don't understand enough about OpenCL to know whether it would make sense to tie that tostd_normal_lcdfalso.If there is agreement that this kind of structure would be useful, I am happy to take a stab at it.