math icon indicating copy to clipboard operation
math copied to clipboard

Request for vector return from normal_cdf given vector arguments

Open adkinsty opened this issue 3 years ago • 7 comments

I wish to execute the normal CDF on vector inputs to obtain a vector of cumulative probabilities. However, I am getting a dimension mismatch error. The error says the return on the right-hand side is of type “real”. The Stan documentation says that the cdf function accepts “reals” arguments and has “reals” returns. I thought this psuedotype “reals” included vectors yet it appears to be returning a scalar.

Example code:

vector[N] mu;
vector[N] sigma;
vector[N] x;
vector[N] phi;

phi = normal_cdf(x, mu, sigma);

Example error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Dimension mismatch in assignment; variable name = phi, type = vector; right-hand side type = real.
Illegal statement beginning with non-void expression parsed as
  phi

adkinsty avatar Apr 11 '21 22:04 adkinsty

Yes all the lpdf and cdf functions return back a scalar. A real in the stan language is a scalar

It would be nice to have a vlpdf style function that returned a vector but we haven't had time to implement it

SteveBronder avatar Apr 11 '21 22:04 SteveBronder

This would be nice to have for all the copula stuff as well. As the copulas use the marginal cdfs evaluated at each data point.

spinkney avatar Apr 11 '21 22:04 spinkney

But I think there would not really be a performance gain..so these functions can live in Stan language is what I would think.

wds15 avatar Apr 12 '21 06:04 wds15

But I think there would not really be a performance gain..so these functions can live in Stan language is what I would think.

+1 ^

also, the row/col-wise framework proposed by @andrjohns would solve this issue.

spinkney avatar Apr 12 '21 13:04 spinkney

This feature would really help when we need access to individual observation-level log likelihoods but still want to take advantage of the vectorization in the _lpdf functions. Example: https://arxiv.org/abs/2209.09190

wlandau avatar Jan 25 '23 21:01 wlandau