Zack Morris
Zack Morris

Reputation: 4823

Weighted Variance and Weighted Standard Deviation in C++

Hi I'm trying to calculate the weighted variance and weighted standard deviation of a series of ints or floats. I found these links:

http://math.tutorvista.com/statistics/standard-deviation.html#weighted-standard-deviation

http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf (warning pdf)

Here are my template functions so far. Variance and standard deviation work fine but for the life of me I can't get the weighted versions to match the test case at the bottom of the pdf:

template <class T>
inline float    Mean( T samples[], int count )
{
    float   mean = 0.0f;

    if( count >= 1 )
    {
        for( int i = 0; i < count; i++ )
            mean += samples[i];

        mean /= (float) count;
    }

    return mean;
}

template <class T>
inline float    Variance( T samples[], int count )
{
    float   variance = 0.0f;

    if( count > 1 )
    {
        float   mean = 0.0f;

        for( int i = 0; i < count; i++ )
            mean += samples[i];

        mean /= (float) count;

        for( int i = 0; i < count; i++ )
        {
            float   sum = (float) samples[i] - mean;

            variance += sum*sum;
        }

        variance /= (float) count - 1.0f;
    }

    return variance;
}

template <class T>
inline float    StdDev( T samples[], int count )
{
    return sqrtf( Variance( samples, count ) );
}

template <class T>
inline float    VarianceWeighted( T samples[], T weights[], int count )
{
    float   varianceWeighted = 0.0f;

    if( count > 1 )
    {
        float   sumWeights = 0.0f, meanWeighted = 0.0f;
        int     numNonzero = 0;

        for( int i = 0; i < count; i++ )
        {
            meanWeighted += samples[i]*weights[i];
            sumWeights += weights[i];

            if( ((float) weights[i]) != 0.0f ) numNonzero++;
        }

        if( sumWeights != 0.0f && numNonzero > 1 )
        {
            meanWeighted /= sumWeights;

            for( int i = 0; i < count; i++ )
            {
                float   sum = samples[i] - meanWeighted;

                varianceWeighted += weights[i]*sum*sum;
            }

            varianceWeighted *= ((float) numNonzero)/((float) count*(numNonzero - 1.0f)*sumWeights);    // this should be right but isn't?!
        }
    }

    return varianceWeighted;
}

template <class T>
inline float    StdDevWeighted( T samples[], T weights[], int count )
{
    return sqrtf( VarianceWeighted( samples, weights, count ) );
}

Test case:

int     samples[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 };

printf( "%.2f\n", StdDev( samples, 9 ) );

int     weights[] = { 1, 1, 0, 0, 4, 1, 2, 1, 0 };

printf( "%.2f\n", StdDevWeighted( samples, weights, 9 ) );

Result:

7.46
1.94

Should be:

7.46
5.82

I think the problem is that weighted variance has a few different interpretations and I don't know which one is standard. I found this variation:

http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Weighted_incremental_algorithm

template <class T>
inline float    VarianceWeighted( T samples[], T weights[], int count )
{
    float   varianceWeighted = 0.0f;

    if( count > 1 )
    {
        float   sumWeights = 0.0f, meanWeighted = 0.0f, m2 = 0.0f;

        for( int i = 0; i < count; i++ )
        {
            float   temp = weights[i] + sumWeights,
                    delta = samples[i] - meanWeighted,
                    r = delta*weights[i]/temp;

            meanWeighted += r;
            m2 += sumWeights*delta*r;   // Alternatively, m2 += weights[i] * delta * (samples[i]−meanWeighted)
            sumWeights = temp;
        }

        varianceWeighted = (m2/sumWeights)*((float) count/(count - 1));
    }

    return varianceWeighted;
}

Result:

7.46
5.64

I also tried looking at boost and esutil but they didn't help much:

http://www.boost.org/doc/libs/1_48_0/boost/accumulators/statistics/weighted_variance.hpp http://esutil.googlecode.com/svn-history/r269/trunk/esutil/stat/util.py

I don't need an entire statistics library, and more importantly, I want to understand the implementation.

Can someone please post functions to calculate these correctly?

Bonus points if your functions can do it in a single pass.

Also, does anyone know if weighted variance gives the same result as ordinary variance with repeated values? For example, would the variance of samples[] = { 1, 2, 3, 3 } be the same as weighted variance of samples[] = { 1, 2, 3 }, weights[] = { 1, 1, 2 }?

Update: here is a google docs spreadsheet I have set up to explore the problem. Unfortunately my answers are nowhere close to the NIST pdf. I think the problem is in the unbias step, but I can't see how to fix it.

https://docs.google.com/spreadsheet/ccc?key=0ApzPh5nRin0ldGNNYjhCUTlWTks2TGJrZW4wQUcyZnc&usp=sharing

The result is a weighted variance of 3.77, which is the square of the weighted standard deviation of 1.94 I got in my c++ code.

I am attempting to install octave on my Mac OS X setup so that I can run their var() function with weights, but it is taking forever to install it with brew. I am deeply into yak shaving now.

Upvotes: 1

Views: 2411

Answers (3)

Janek_Kozicki
Janek_Kozicki

Reputation: 826

Here's a much shorter version with a working Demo :

 #include <iostream>
 #include <vector>
 #include <boost/accumulators/accumulators.hpp>
 #include <boost/accumulators/statistics/stats.hpp>
 #include <boost/accumulators/statistics/weighted_variance.hpp>
 #include <boost/accumulators/statistics/variance.hpp>

 namespace ba = boost::accumulators;

 int main() {
     std::vector<double> numbers{2, 3, 5, 7, 11, 13, 17, 19, 23};
     std::vector<double> weights{1, 1, 0, 0,  4,  1,  2,  1, 0 };

     ba::accumulator_set<double, ba::stats<ba::tag::variance          >          > acc;
     ba::accumulator_set<double, ba::stats<ba::tag::weighted_variance > , double > acc_weighted;

     double n = numbers.size();
     double N = n;

     for(size_t i = 0 ; i<numbers.size() ; i++ ) {
         acc         ( numbers[i] );
         acc_weighted( numbers[i] ,   ba::weight = weights[i] );
         if(weights[i] == 0) {
             n=n-1;
         }
     };

     std::cout << "Sample Standard Deviation, s: "          << std::sqrt(ba::variance(acc)                  *N/(N-1))        << std::endl;
     std::cout << "Weighted Sample Standard Deviation, s: " << std::sqrt(ba::weighted_variance(acc_weighted)*n/(n-1))        << std::endl;
 }

Make note that n must reflect the number of samples with nonzero weights, hence extra n=n-1; line.

Sample Standard Deviation, s: 7.45729
Weighted Sample Standard Deviation, s: 5.82237

Upvotes: 0

dpmcmlxxvi
dpmcmlxxvi

Reputation: 1302

Solution

You accidentally added an extra term "count" in the denominator of the variance update term.

When using the correction below I get your expected answer of

5.82

FYI, one way to pick up on things like this when you are doing a code review is to do a "dimensional analysis". The "units" of the equation were wrong. You were effectively dividing by an order N squared term when it should be an order N term.

Before

template <class T>
inline float    VarianceWeighted( T samples[], T weights[], int count )
{
    ...
            varianceWeighted *= ((float) numNonzero)/((float) count*(numNonzero - 1.0f)*sumWeights);    // this should be right but isn't?!
    ...
}

After

Removing "count" this line should be replaced by

template <class T>
inline float    VarianceWeighted( T samples[], T weights[], int count )
{
    ...
            varianceWeighted *= ((float) numNonzero)/((float) (numNonzero - 1.0f)*sumWeights);  // removed count term
    ...
}

Upvotes: 0

Travis
Travis

Reputation: 2026

float mean(uint16_t* x, uint16_t n) {
    uint16_t sum_xi = 0;
    int i;
    for (i = 0; i < n; i++) {
        sum_xi += x[i];
    }
    return (float) sum_xi / n;
}

/**
 * http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weigmean.pdf
 */
float weighted_mean(uint16_t* x, uint16_t* w, uint16_t n) {
    int sum_wixi = 0;
    int sum_wi = 0;
    int i;
    for (i = 0; i < n; i++) {
        sum_wixi += w[i] * x[i];
        sum_wi += w[i];
    }
    return (float) sum_wixi / (float) sum_wi;
}

float variance(uint16_t* x, uint16_t n) {
    float mean_x = mean(x, n);
    float dist, dist2;
    float sum_dist2 = 0;

    int i;
    for (i = 0; i < n; i++) {
        dist = x[i] - mean_x;
        dist2 = dist * dist;
        sum_dist2 += dist2;
    }

    return sum_dist2 / (n - 1);
}

/**
 * http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weighvar.pdf
 */
float weighted_variance(uint16_t* x, uint16_t* w, uint16_t n) {
    float xw = weighted_mean(x, w, n);
    float dist, dist2;
    float sum_wi_times_dist2 = 0;
    int sum_wi = 0;
    int n_prime = 0;

    int i;
    for (i = 0; i < n; i++) {
        dist = x[i] - xw;
        dist2 = dist * dist;
        sum_wi_times_dist2 += w[i] * dist2;
        sum_wi += w[i];

        if (w[i] > 0)
            n_prime++;
    }

    if (n_prime > 1) {
        return sum_wi_times_dist2 / ((float) ((n_prime - 1) * sum_wi) / n_prime);
    } else {
        return 0.0f;
    }
}

/**
 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Weighted_incremental_algorithm
 */
float weighted_incremental_variance(uint16_t* x, uint16_t* w, uint16_t n) {
    uint16_t sumweight = 0;
    float mean = 0;
    float M2 = 0;
    int n_prime = 0;

    uint16_t temp;
    float delta;
    float R;

    int i;
    for (i = 0; i < n; i++) {
        if (w[i] == 0)
            continue;

        temp = w[i] + sumweight;
        delta = x[i] - mean;
        R = delta * w[i] / temp;
        mean += R;
        M2 += sumweight * delta * R;
        sumweight = temp;

        n_prime++;
    }

    if (n_prime > 1) {
        float variance_n = M2 / sumweight;
        return variance_n * n_prime / (n_prime - 1);
    } else {
        return 0.0f;
    }
}

void main(void) {
    uint16_t n = 9;
    uint16_t x[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 };
    uint16_t w[] = { 1, 1, 0, 0,  4,  1,  2,  1,  0 };

    printf("%f\n", weighted_variance(x, w, n)); /* outputs: 33.900002 */
    printf("%f\n", weighted_incremental_variance(x, w, n)); /* outputs: 33.900005 */
}

Upvotes: 3

Related Questions