using floor() and ceil() functions for linear interpolation

558 views
Skip to first unread message

Jon Zelner

unread,
Mar 12, 2014, 11:03:51 AM3/12/14
to stan-...@googlegroups.com
I'm hoping to do linear interpolation on a function in Stan (a continuous age-prevalence curve for a disease, with knot points at each year of age) and came across this example on page 252 of the Stan manual:

"One case where rounding is reasonable is linear interpolation of function values. For example, consider xm <- x[floor(c)]; xp <- x[ceil(c)]; x <- (c - floor(c))*xp + (ceil(c) - c)*xm;"

But since the documented versions of floor() and ceil() only return reals, I was wondering if there's some compile-time magic that coerces the output of these functions to an int in this context, or if the integer indices need to be obtained by incrementing an integer or some other similar method.

Thanks!

-Jon 

Bob Carpenter

unread,
Mar 12, 2014, 11:41:28 AM3/12/14
to stan-...@googlegroups.com
Sorry about that --- it never should've went into the doc. There's
really not a good way to do integer floor or ceiling right now in Stan
(or round). I just added to the next release doc issue to get rid
of that doc. (Or we'll add the functions and update the doc.)

They have the signatures the way they do because it matches the
usage in the C++ library, but in C++ you can just assign a double
to an integer and it'll cast --- we explicitly stop users from doing
that in Stan.

So we'll need a floor and ceil function for integers,
maybe ifloor and iceil (to align with fmax and fmin and fabs).
The closest we have now is int_step, which is not very helpful
here, though could be hacked if the integers weren't too large.
There's already an issue:

https://github.com/stan-dev/stan/issues/374

and Michael's already raised objections because it's not
theoretically kosher with HMC (though in some interpolation
cases it'd be OK, which is why it's in the doc as such).
But then the real-valued versions are just as incompatible
and we already have those.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marcus Brubaker

unread,
Mar 12, 2014, 12:56:05 PM3/12/14
to stan-...@googlegroups.com
This is a good justification for proper (int returning) versions of floor/ceil/round, probably with large warning labels to allay Michaels concerns.  Efficiently implementing interpolation requires them despite the fact that interpolation is generally pretty smooth and differentiable.

Cheers,
Marcus
Reply all
Reply to author
Forward
0 new messages