vendredi 25 janvier 2019

How to calculate standard deviation from range

There is a function to calculate standard deviation.

template<class T>
double stDev(const std::vector<T> &v)
{
    if (v.empty()) {
        return 0;
    }

    double sum = std::accumulate(v.begin(), v.end(), 0.0);
    double mean = sum / v.size();

    std::vector<double> diff(v.size());
    std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });
    double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);

    return std::sqrt(sq_sum / v.size());
}

I need a function to calculate standard deviation from range.

template<class T>
double stDev(const std::vector<T> &v, double minVal, double maxVal)
{
    // if v[i] in [minVal .. maxVal] assumed that deviation is 0.  
    // if v[i] < minVal assumed that deviation is minVal - v[i].  
    // if v[i] > maxVal assumed that deviation is v[i] - maxVal.
}

Aucun commentaire:

Enregistrer un commentaire