I am trying to write a custom lpdf, but am having trouble working out how to code it in Stan.
In words, the value of the log pdf f(x) depends on which of three cases applies, as shown, where x, y and z are all scalars:
(i) x < y < z: f(x) = a(x),
(ii) y < x < z: f(x) = b(x), or
(iii) y < z < x: f(x) = c(x)
where the functions a, b, c are valid log pdf ( normalised if necessary so the the resultant f(x) will be a valid log pdf).
I can write code for this in R, with which I am very familiar, but whatever I try in Stan raises errors that I do not know how to deal with.
For example
functions{
real a(real x){
return 0.1;
}
real b(real x){
return 0.1;
}
real c(real x){
return 0.1;
}
real my_lpdf(real[] x, real y, real z){
int N = size(x);
vector[N] f;
for (i in 1:N){
if (x[i] <= y) {
f[i] = a(x[i]);
} else if (x[i] <= z){
f[i] = b(x[i]);
} else {
f[i] = c(x[i]);
}
}
return f;
}
}
fails, with the message:
Ill-typed arguments supplied to infix operator <=. Available signatures:
(int, int) => int
(int, real) => int
(real, int) => int
(real, real) => int
Instead supplied arguments of incompatible type: real, array[] real.
Obviously my functions a, b,c are wrong but were needed to get code that could be checked. That code reflects how I would code it in R.
The underlying problem is that as a very occasional user of Stan I am not used to its much tighter discipline over types etc. than R.
How should I code this function in Stan?