# Fix the precision or let it vary?

Hey guys,

I want to add a new function that deals with infinite sums. What is the Stan procedure in dealing with the precision?

Shall I fix it to a specific value? Is there a “Stan-precision-value” commonly used? Or should I give the precision to the parameters such that the user of my function can set an individual precision?

Best,
Franzi

1 Like

Hi, @Franzi. Although we don’t have a standard per se, we’ve leaned toward high precision over high speed in all of our implementations.

For iterative algorithms like ODE solvers, we have versions that allow user-defined control of precision through tolerance arguments.

So I guess the question is whether the max precision you can get is going to be a lot slower than more modest precision, and if so, are there situations where the reduced precision is worth the tradeoff? If so, I’d suggest adding a precision parameter to the function if that’s possible and overloading it with a second implementation that just choose a relatively high precision.

2 Likes

Hi @Bob, now I tried for a few days to adapt my function in the way that I can allow a precision parameter with a default value for the case the user does not provide an own precision value. But I have not yet found a way that works.

The head of my function looks similar to this:

`template <bool propto, typename T_y, typename T_alpha, typename T_prec> return_type_t<T_y, T_alpha,T_prec> my_function(const T_y& y, const T_alpha& a, const T_prec& prec) {`

In this case, the function expects 3 arguments: my_function(y, alpha, precision).

I’d like to do something like this:

`template <bool propto, typename T_y, typename T_alpha, typename T_prec> return_type_t<T_y, T_alpha,T_prec> my_function(const T_y& y, const T_alpha& a, const T_prec& prec = 0) {`

Such that, when the user calls my_function(y, alpha), prec is set to 0. But this throws the error: “candidate expects 3 arguments, 2 provided”

How can I implement a function in Stan-style that allows default values for parameters?

Is `prec`, which i assume to be precision, intended to be of a `var` type? In other words how is `prec`'s gradient intended to be used? The signature should work if `prec` is just a simple `double`, otherwise the type `T_prec` needs to be specified when the function is called.

Also related, to avoid loss of precision in sum, use something like Kahan’s algorithm instead of naive implementation.

1 Like

It’s worth noting that if you’re interested in playing with the details of ODE solvers to get a specified precision, it might be worth it to try out Turing.jl, a PPL very similar to Stan that’s embedded in Julia. Not sure which will work better for your particular use case, but Turing gives you more flexibility since it lets you include any Julia package when you’re running your model.

Ok. Thanks for all the input! From all the comments, I conclude for my case to set the precision internally and don’t give the user the chance to vary it on his own wish.