On 9/21/12 6:07 PM, Andrew Gelman wrote:
> I think we should report 0 for variance and sd when n=0 or n=1.
> In any reasonable weighting scheme, variance estimates based on 0 or 1
> point would be zero-weighted anyway,
This is all coming back to me, and I'm wondering
if we're talking about the same situation.
What do you mean by "report"? I'm not talking
about ESS or R-hat here, but the functions that
get called in Stan models.
And what's a "weighting scheme"? I'm used to
seeing weighting by precision (inverse variance).
I'm worried about catching cases where programmers
make an error and don't know it.
My limited imagination can't dream up a situation where
a user would want a return of 0 for size-0 or size-1
inputs.
I can just do what I'm told without understaning,
so if you really think it's important, I'll put it
in and document it without understanding why we
do it the way you're suggesting.
The main use I've seen so far is data normalization.
vector[K] y_raw;
...
vector[K] y;
...
real y_sd;
y_sd <- sd(y_raw);
y_mean <- mean(y_raw);
y <- (y_raw - y_mean) / y_sd; // I think we vectorized - scalar
In this case, if K = 1, then setting sd(y_raw)
to 0 sets the value in y[0] to 0 / 0, which
yields NaN.
In general, it's better to fail early as soon
as the problem comes up (here computing sd()),
because it's easier to debug.
This also brings up what to do with min, max, and
mean in the face of zero-length inputs. We currently
are raising exceptions if any of these is fed
a length-zero input.
> but I don't see a reason for crashing
> the program or sending out NA's
> just because we happen to compute a variance of a vector of length 1.
Nobody is suggesting either crashing or returning
NA. At most, we'd raise a domain error in the
same way we would if someone tries to supply a negative
scale parameter to the normal distribution.
NA values are an R thing for representing undefined
values. There's no NA in C++. We do use
NaN values depending on the error policy
configuration.
- Bob