Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3850870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:04:34+00:00 2026-05-19T17:04:34+00:00

I need to calculate the mean-squared error of a 16-bit operation for an arbitrary

  • 0

I need to calculate the mean-squared error of a 16-bit operation for an arbitrary number of data points (upwards of 100 million). I decided to go with a running average so I wouldn’t have to worry about overflow from adding a large number of squared errors. At 100 million samples I had problems with floating point precision (inaccurate results) so I moved to double.

Here is my code

int iDifference = getIdeal() - getValue();

m_iCycles++;


// calculate the running MSE as

// http://en.wikipedia.org/wiki/Moving_average

// MSE(i + 1) = MSE(i) + (E^2 - MSE(i))/(i + 1)

m_dMSE = m_dMSE + ((pow((double)iDifference,2) - m_dMSE) / (double)m_iCycles);

Is there a better way to implement this to maintain accuracy? I considered normalizing the MSE to one and simply keeping a sum with a final division on completion to calculate the average.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:04:34+00:00Added an answer on May 19, 2026 at 5:04 pm

    Floating point numbers don’t overflow in this kind of situation, they only lose precision. So there are no advantages of a running average over a running total here. The consequence is the same whether the running total or the denominator grows.

    To maintain precision in a running total, keep subtotals instead of a single total. Just keep adding to a subtotal until adding one more would cause overflow. Then move on to the next subtotal. Since they are all the same order of magnitude (in base 2), optimal precision may be achieved by converting to floating point and using a pairwise accumulation into one final total.

    // first = errors, second = counter
    typedef pair< vector< uint32_t >, uint32_t > running_subtotals;
    
    void accumulate_error( uint32_t error, running_subtotals &acc ) {
        ( numeric_limits< uint32_t >::max() - error < acc.first.back()?
            * acc.first.insert( acc.first.end(), 0 ) : acc.first.back() )
            += error; // add error to current subtotal, or new one if needed
        ++ acc.second; // increment counter
    }
    
    double get_average_error( running_subtotals const &total ) {
        vector< double > acc( total.first.begin(), total.first.end() );
        while ( acc.size() != 1 ) {
            if ( acc.size() % 2 ) acc.push_back( 0 );
            for ( size_t index = 0; index < acc.size() / 2; ++ index ) {
                acc[ index ] = acc[ index * 2 ] + acc[ index * 2 + 1 ];
            }
            acc.resize( acc.size() / 2 );
        }
        return acc.front() / total.second;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the mean, standard deviation, and other values for a number
I have a set a data that I need to calculate their consecutive mean
For 100 companies, I have collected data for 102 days. I need to calculate
I need to calculate (in R) the number of positive and negative runs in
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I need to calculate the within and between run variances from some data as
I have a data.frame in panel format (country-year) and I need to calculate the
I need to calculate the time till Christmas in milliseconds. I'm using joda time
I need to calculate the total of an invoice. This invoice is created with
I need to calculate the net total of a column-- sounds simple. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.