中位数计算

template <class T>
double VectorMedian(std::vector<T> &In) {
    std::sort(In.begin(), In.end());
    if(In.size() % 2 == 0) {
        return 0.5*(In.at(In.size()/2)+In.at(In.size()/2-1));
    }
    else
        return In.at((In.size()-1)/2);
}

Let {x(1), x(2), ..., x(m)} represent the values of x after they have been sorted in non-decreasing order.

x(1)=min(x) and x(m)=max(x).

median(x): x(r+1) if m is odd, i.e., m=2r+1

                 0.5*(x(r)+x(r+1)) if m is even, i.e., m=2r

Thus, for seven values, the median is x(4), while for ten values, the median is 0.5*(x(5)+x(6)).

The mean can be distorted by outliers, and since the variance is computed using the mean, it is also sensitive to outliers.

Indeed, the variance is particularly sensitive to outliers since it uses the squared difference between the mean and other values.

posted @ 2019-04-10 20:08  东宫得臣  阅读(140)  评论(0编辑  收藏  举报