I try to write a custom pdf that in works like a histogram. Here is a code I have so far:
functions {
real mydist_lpdf(real x, vector[] w, real x_lower, real x_upper) {
int M = num_elements(w)-1;
int idx = round((x - x_lower)/(x_upper - x_lower) * M);
return w[idx];
}
}
data {
int<lower=1> N; // number of data points
real x_lower; // lower data boundary
real<lower=x_lower> x_upper; // upper data boundary
real<lower=x_lower, upper=x_upper> x[N]; // observations
int<lower=2> Ngrid; // number of items in the grid
}
parameters {
simplex[Ngrid] w; // fixed points of the interpolated distribution estimate
}
model {
x ~ mydist(x | w);
}
The code does not compile, because round
returns real
, not int
(because I cannot test it, there may be other, unrelated problems in my code).
The manual says:
The rounding functions cannot be used as indices to arrays because they return real values. Stan may introduce integer-valued versions of these in the future, but as of now, there is no good workaround.
Is there any, even mediocre, walkaround to my problem? Lack of such basic function is a real issue for me.