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 443603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:11:15+00:00 2026-05-12T21:11:15+00:00

I haven’t really used variance calculation that much, and I don’t know quite what

  • 0

I haven’t really used variance calculation that much, and I don’t know quite what to expect. Actually I’m not too good with math at all.

I have a an array of 1000000 random numeric values in the range 0-10000.

The array could grow even larger, so I use 64 bit int for sum.

I have tried to find code on how to calc variance, but I don’t know if I get correct output.

The mean is 4692 and median is 4533. I get variance 1483780.469308 using the following code:

// size is the element count, in this case 1000000
// value_sum is __int64

double p2 = pow( (double)(value_sum - (value_sum/size)), (double)2.0 );
double variance = sqrt( (double)(p2 / (size-1)) );

Am I getting a reasonable value?

Is anything wrong with the calculation?

  • 1 1 Answer
  • 0 Views
  • 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-12T21:11:15+00:00Added an answer on May 12, 2026 at 9:11 pm

    Note: It doesn’t look like you’re calculating the variance.

    Variance is calculated by subtracting the mean from every element and calculating the weighted sum of these differences.

    So what you need to do is:

    // Get mean
    double mean = static_cast<double>(value_sum)/size;
    
    // Calculate variance
    double variance = 0;
    for(int i = 0;i<size;++i) 
    {
      variance += (MyArray[i]-mean)*(MyArray[i]-mean)/size;
    }
    
    // Display
    cout<<variance;
    

    Note that this is the sample variance, and is used when the underlying distribution is unknown (so we assume a uniform distribution).

    Also, after some digging around, I found that this is not an unbiased estimator. Wolfram Alpha has something to say about this, but as an example, when MATLAB computes the variance, it returns the “bias-corrected sample variance”.

    The bias-corrected variance can be obtained by dividing by each element by size-1, or:

    //Please check that size > 1
    variance += (MyArray[i]-mean)*(MyArray[i]-mean)/(size-1); 
    

    Also note that, the value of mean remains the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

haven't used regex replaces much and am not sure if how I have done
Haven't seen this feature anywhere else. I know that the 32nd bit is used
I haven't used the STL much before, but I started to on this huffman
I haven't used ARC yet other than to deal with it when it forces
I haven't used multithreading in Clojure at all so am unsure where to start.
I haven't actually built an app yet, but I'm confused by documentation on bind-attr.
I haven't designed a website for about 3 years now, so I am quite
I haven't worked with SQL Reporting much, however I have been trying to get
Haven't seen anything, but I have seen that you can create albums in the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.