Suppose I have a vector of length N, how can I map each element in the vector with a given function and get a vector of length N as output?
Let f(x, a, b) = gamma_cdf(x, a, b)
vector[N] z;
Desired output:
output_vector : [f(z[1], a, b), f(z[2], a, b),......, f(z[N], a, b)]
@hhau’s code above, even though it’s a loop, can be run inside a map_rect function, but there is no currently exposed signature for gamma_cdf(or, as @hhau points out, any *_cdf) that returns a vector. Generally, you can roll your own cdf that returns a vector, but in the particular case, calculating a gamma cdf requires calculating the lower incomplete gamma function, and Stan’s exposed gamma_p does not operate on vectors. I don’t know if there would be another way.
I find it valuable to start with an example in the Stan manual and create small experiments. For example, try to compile the map_rect example in the Stan manual as is:
and then add your hypothetical Hill function. You will see that you can call the Hill function within the example lr function:
functions {
real Hill(real t, real ec, real slope) {
return 1 / (1 + (t / ec)^(-slope));
}
vector lr(vector beta, vector theta, real[] x, int[] y) {
real t = beta[1];
real ec = theta[1];
real slope = 1;
real myHill;
real lp;
myHill = Hill(t, ec, slope);
lp = bernoulli_logit_lpmf(y | beta[1]
+ to_vector(x) * beta[2]);
return [lp]';
}
}
The above, of course, doesn’t do anything with your function because I don’t know your goals. But small experiments can be valuable.