help on a function to compute quantiles

Hi!

I have two errors from the code below at the lines starting with int idx:

"invalid use of member (did you forget the &?)"

and on the line starting with out=/

"idx was not declared in this scope"

Can anyone kindly help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
double QCalc(std::vector<double> x, std::string method) {
    double alpha = 0.99;
    double alpha_tilde = 0.974234518211730;
    double out = 0.;
    std::sort(x.begin(), x.end());
    if (method == "quantile") {
        int idx = std::ceil((1. - alpha) * x.size());
        out = x[idx];
    } else if (method == "ETL") {
        int idx = std::ceil((1. - alpha_tilde) * x.size());
        for(int i=0; i<=idx; ++i) {
            out += x[i];
        }
        out /= idx + 1;
    }
    return out;
}
Last edited on
You wrote: double out = 0.;
Remove the . or make it 0.0 and your code should compile fine.
What other #include files are you using?

Your snippet appears to be missing several required #includes.


Hi, I did but still get the same errors
Last edited on
Take jlb's advice. Your code will compile just fine if you add the three missing #include directives. Hint: you need the #includes that define std::vector, std::string and std::ceil
Thank you!!!
Topic archived. No new replies allowed.