int tail_repeat(int x, int top, int period) {
int z;
if (x > top)
z <- top - period + 1 + fmod(x-top-1, period);
else
z <- x;
return z;
}
--- Translating Stan model to C++ code ---
bin/stanc /home/krzysiek/projekty/westbrook_ats_survival/analysis/cjs-finalize-2/cjs.stan --o=/home/krzysiek/projekty/westbrook_ats_survival/analysis/cjs-finalize-2/cjs.cpp --no_main
Model name=cjs_model
Input file=/home/krzysiek/projekty/westbrook_ats_survival/analysis/cjs-finalize-2/cjs.stan
Output file=/home/krzysiek/projekty/westbrook_ats_survival/analysis/cjs-finalize-2/cjs.cpp
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
base type mismatch in assignment; variable name = z, type = int; right-hand side type=real
ERROR at line 6
4: int z;
5: if (x > top)
6: z <- top - period + 1 + fmod(x-top-1, period);
^
7: else
> tail_repeat(1:20, 9, 4)
[1] 1 2 3 4 5 6 7 8 9 6 7 8 9 6 7 8 9 6 7 8
> On Jan 14, 2015, at 2:26 PM, Krzysztof Sakrejda <krzysztof...@gmail.com> wrote:
>
> Ok, that does leave the absurd while-loop/test implementation... oh well, it's only
> a small number of iterations. My case is a data transformation so it doesn't really
> loose the derivative information.
>
> Is this just to keep users from doing silly things? I realize that can be a headache
> for you guys but... you're already letting users write near-arbitrary likelihood functions...
>
Yes, and I have to say the dev team's split on whether to
allow this kind of thing or not.
The perceived problem is that our users understand stats
and probability, but not necessarily HMC-based computation.
So what can make sense mathematically can kill the performance
of the sampler.
int floor_real_to_int(real x) {
real y;
int z;
y <- floor(x);
if (y == 0.0) {
z <- 0;
if (y > 0) {
z <- 0;
while (z < y) {
z <- z + 1;
}
}
if (y < 0) {
z <- 0;
while (z > y) {
z <- z - 1;
}
}
return z;
}
This is simpler, but still inefficient:
int int_floor(real x) {
int i;
i <- 0;
if (x >= 0) while (x > i + 1) i <- i + 1;
else while (x < i) i <- i - 1;
return i;
}
But it may still not terminate if x > max integer value.
To write a really robust and efficient version, you'd need
to test the outside values and throw an exception, and
then use a binary search on the inside to make the whole
thing logarithmic time complexity.
int int_floor(real x) {
int i;
i <- 0;
if (x >= 0) while (x > i + 1) i <- i + 1;
else while (x < i) i <- i - 1;
return i;
}