weighted median
dhopkins-gweiss opened this issue · 0 comments
dhopkins-gweiss commented
I think the weight is placed on the ordered values, not on the original values passed in.
E.g.
> roll_medianr(5:3, 3, weight = c(1,0,0))
gives NA NA 3
1xsmallest, 0x4, 0x5
Not NA NA 5
as expected (1x5,0x4,0x3)
My guess is this stems from sorting first without respect to the weights...
` NumericVector copy(x.begin() + offset, x.begin() + offset + n);
std::sort(copy.begin(), copy.end());
double weights_sum = sum(weights);
int k = 0;
double sum = weights_sum - weights[0];
while (sum > weights_sum / 2) {
++k;
sum -= weights[k];
}
return copy[k]; `