Performing Rcpp Quantization

I have an Rcpp double containing a NumericVector x. I would like to get the .95 quantum of such an x ​​in the Rcpp code stream. I don't know how to get it. Is there an implementation of RcppArmadillo?

+3


source to share


1 answer


I'm not an expert Rcpp

and my function probably needs a lot of improvement, but it seems that you can easily create your own Rcpp quantization function (which will not be as accurate on small vectors due to the high skew probability and indexing problem on non-integer indices , but improves as the vector grows)

library(Rcpp) # You can use sourceCpp() instead of cppFunction if you wish
cppFunction('NumericVector Cquantile(NumericVector x, NumericVector q) {
  NumericVector y = clone(x);
  std::sort(y.begin(), y.end());
  return y[x.size()*(q - 0.000000001)];
}')

      

1e+3

Vector testing

set.seed(123)
y <- rnorm(1000)
qs <- seq(0, 100, 25)/100

quantile(y, qs)
#           0%          25%          50%          75%         100% 
# -2.809774679 -0.628324243  0.009209639  0.664601867  3.241039935 


setNames(Cquantile(y, qs), paste0(qs * 100, "%"))
#          0%         25%         50%         75%        100% 
# -2.80977468 -0.62957874  0.00729009  0.66441586  3.24103993 

      



Looks close enough. So the 95% quantile for our vector y

will be

Cquantile(y, .95)
## [1] 1.675697

      


There are also some sugar quantization functions for known distributions like normal, gamma, etc. that can be used instead, see here for some Examples.

+4


source







All Articles