Calculating Standard Deviation & Variance in C++












4














so i've posted a few times and previously my problems were pretty vague



i started C++ this week and have been doing a little project



so i'm trying to calc standard deviation & variance



my code loads a file of 100 integers and put them into an array, counts them, calcs mean, sum, var and sd



but i'm having a little trouble with variance



i keep getting a huge number - i have a feeling it's to do with its calculation



my mean and sum are ok



any help or tips?



NB:



sd & mean calcs



Cheers,



Jack



 using namespace std;
int main()

{

int n = 0;
int Array[100];
float mean;
float var;
float sd;
string line;
float numPoints;

ifstream myfile(“numbers.txt");

if (myfile.is_open())

{
while (!myfile.eof())

{
getline(myfile, line);

stringstream convert(line);

if (!(convert >> Array[n]))

{
Array[n] = 0;
}
cout << Array[n] << endl;

n++;

}

myfile.close();

numPoints = n;

}
else cout<< "Error loading file" <<endl;

int sum = accumulate(begin(Array), end(Array), 0, plus<int>());

cout << "The sum of all integers: " << sum << endl;

mean = sum/numPoints;

cout << "The mean of all integers: " << mean <<endl;

var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;

sd = sqrt(var);

cout << "The standard deviation is: " << sd <<endl;

return 0;

}









share|improve this question
























  • In (Array[n] - mean) isn't n one more than the number of elements you have read? Also, while (!myfile.eof()) is almost always wrong
    – Bo Persson
    Oct 21 '15 at 20:35










  • You should use double instead of float
    – FredK
    Oct 21 '15 at 20:47










  • should be "
    – M.M
    Oct 23 '15 at 7:33
















4














so i've posted a few times and previously my problems were pretty vague



i started C++ this week and have been doing a little project



so i'm trying to calc standard deviation & variance



my code loads a file of 100 integers and put them into an array, counts them, calcs mean, sum, var and sd



but i'm having a little trouble with variance



i keep getting a huge number - i have a feeling it's to do with its calculation



my mean and sum are ok



any help or tips?



NB:



sd & mean calcs



Cheers,



Jack



 using namespace std;
int main()

{

int n = 0;
int Array[100];
float mean;
float var;
float sd;
string line;
float numPoints;

ifstream myfile(“numbers.txt");

if (myfile.is_open())

{
while (!myfile.eof())

{
getline(myfile, line);

stringstream convert(line);

if (!(convert >> Array[n]))

{
Array[n] = 0;
}
cout << Array[n] << endl;

n++;

}

myfile.close();

numPoints = n;

}
else cout<< "Error loading file" <<endl;

int sum = accumulate(begin(Array), end(Array), 0, plus<int>());

cout << "The sum of all integers: " << sum << endl;

mean = sum/numPoints;

cout << "The mean of all integers: " << mean <<endl;

var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;

sd = sqrt(var);

cout << "The standard deviation is: " << sd <<endl;

return 0;

}









share|improve this question
























  • In (Array[n] - mean) isn't n one more than the number of elements you have read? Also, while (!myfile.eof()) is almost always wrong
    – Bo Persson
    Oct 21 '15 at 20:35










  • You should use double instead of float
    – FredK
    Oct 21 '15 at 20:47










  • should be "
    – M.M
    Oct 23 '15 at 7:33














4












4








4







so i've posted a few times and previously my problems were pretty vague



i started C++ this week and have been doing a little project



so i'm trying to calc standard deviation & variance



my code loads a file of 100 integers and put them into an array, counts them, calcs mean, sum, var and sd



but i'm having a little trouble with variance



i keep getting a huge number - i have a feeling it's to do with its calculation



my mean and sum are ok



any help or tips?



NB:



sd & mean calcs



Cheers,



Jack



 using namespace std;
int main()

{

int n = 0;
int Array[100];
float mean;
float var;
float sd;
string line;
float numPoints;

ifstream myfile(“numbers.txt");

if (myfile.is_open())

{
while (!myfile.eof())

{
getline(myfile, line);

stringstream convert(line);

if (!(convert >> Array[n]))

{
Array[n] = 0;
}
cout << Array[n] << endl;

n++;

}

myfile.close();

numPoints = n;

}
else cout<< "Error loading file" <<endl;

int sum = accumulate(begin(Array), end(Array), 0, plus<int>());

cout << "The sum of all integers: " << sum << endl;

mean = sum/numPoints;

cout << "The mean of all integers: " << mean <<endl;

var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;

sd = sqrt(var);

cout << "The standard deviation is: " << sd <<endl;

return 0;

}









share|improve this question















so i've posted a few times and previously my problems were pretty vague



i started C++ this week and have been doing a little project



so i'm trying to calc standard deviation & variance



my code loads a file of 100 integers and put them into an array, counts them, calcs mean, sum, var and sd



but i'm having a little trouble with variance



i keep getting a huge number - i have a feeling it's to do with its calculation



my mean and sum are ok



any help or tips?



NB:



sd & mean calcs



Cheers,



Jack



 using namespace std;
int main()

{

int n = 0;
int Array[100];
float mean;
float var;
float sd;
string line;
float numPoints;

ifstream myfile(“numbers.txt");

if (myfile.is_open())

{
while (!myfile.eof())

{
getline(myfile, line);

stringstream convert(line);

if (!(convert >> Array[n]))

{
Array[n] = 0;
}
cout << Array[n] << endl;

n++;

}

myfile.close();

numPoints = n;

}
else cout<< "Error loading file" <<endl;

int sum = accumulate(begin(Array), end(Array), 0, plus<int>());

cout << "The sum of all integers: " << sum << endl;

mean = sum/numPoints;

cout << "The mean of all integers: " << mean <<endl;

var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;

sd = sqrt(var);

cout << "The standard deviation is: " << sd <<endl;

return 0;

}






c++ arrays average variance standard-deviation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 23 '15 at 6:52









Konamiman

42.5k1596126




42.5k1596126










asked Oct 21 '15 at 20:20









Jack

76128




76128












  • In (Array[n] - mean) isn't n one more than the number of elements you have read? Also, while (!myfile.eof()) is almost always wrong
    – Bo Persson
    Oct 21 '15 at 20:35










  • You should use double instead of float
    – FredK
    Oct 21 '15 at 20:47










  • should be "
    – M.M
    Oct 23 '15 at 7:33


















  • In (Array[n] - mean) isn't n one more than the number of elements you have read? Also, while (!myfile.eof()) is almost always wrong
    – Bo Persson
    Oct 21 '15 at 20:35










  • You should use double instead of float
    – FredK
    Oct 21 '15 at 20:47










  • should be "
    – M.M
    Oct 23 '15 at 7:33
















In (Array[n] - mean) isn't n one more than the number of elements you have read? Also, while (!myfile.eof()) is almost always wrong
– Bo Persson
Oct 21 '15 at 20:35




In (Array[n] - mean) isn't n one more than the number of elements you have read? Also, while (!myfile.eof()) is almost always wrong
– Bo Persson
Oct 21 '15 at 20:35












You should use double instead of float
– FredK
Oct 21 '15 at 20:47




You should use double instead of float
– FredK
Oct 21 '15 at 20:47












should be "
– M.M
Oct 23 '15 at 7:33




should be "
– M.M
Oct 23 '15 at 7:33












5 Answers
5






active

oldest

votes


















10














As the other answer by horseshoe correctly suggests, you will have to use a loop to calculate variance otherwise the statement




var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;




will just consider a single element from the array.



Just improved horseshoe's suggested code:



var = 0;
for( n = 0; n < numPoints; n++ )
{
var += (Array[n] - mean) * (Array[n] - mean);
}
var /= numPoints;
sd = sqrt(var);


Your sum works fine even without using loop because you are using accumulate function which already has a loop inside it, but which is not evident in the code, take a look at the equivalent behavior of accumulate for a clear understanding of what it is doing.



Note: X ?= Y is short for X = X ? Y where ? can be any operator.
Also you can use pow(Array[n] - mean, 2) to take the square instead of multiplying it by itself making it more tidy.






share|improve this answer



















  • 1




    thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
    – Jack
    Oct 22 '15 at 11:32






  • 2




    @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
    – Ahmed Akhtar
    Oct 23 '15 at 3:56





















1














Your variance calculation is outside the loop and thus it is only based on the n== 100 value. You need an additional loop.



You need:



var = 0;
n=0;
while (n<numPoints){
var = var + ((Array[n] - mean) * (Array[n] - mean));
n++;
}
var /= numPoints;
sd = sqrt(var);





share|improve this answer























  • I think you have a typo on the last line.
    – Jason
    Oct 21 '15 at 22:55






  • 1




    @ Jason: yes, true, but Ahmeds solutions now solves it very well
    – horseshoe
    Oct 21 '15 at 23:15






  • 1




    It's still good to fix your typos for anyone else that reads the code.
    – Jason
    Oct 21 '15 at 23:20










  • @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
    – Ahmed Akhtar
    Oct 23 '15 at 4:14












  • @Ahmed Akhtar. Changed it, thx
    – horseshoe
    Oct 23 '15 at 6:49



















1














Two simple methods to calculate Standard Deviation & Variance in C++.



#include <math.h>
#include <vector>

double StandardDeviation(std::vector<double>);
double Variance(std::vector<double>);

int main()
{
std::vector<double> samples;
samples.push_back(2.0);
samples.push_back(3.0);
samples.push_back(4.0);
samples.push_back(5.0);
samples.push_back(6.0);
samples.push_back(7.0);

double std = StandardDeviation(samples);
return 0;
}

double StandardDeviation(std::vector<double> samples)
{
return sqrt(Variance(samples));
}

double Variance(std::vector<double> samples)
{
int size = samples.size();

double variance = 0;
double t = samples[0];
for (int i = 1; i < size; i++)
{
t += samples[i];
double diff = ((i + 1) * samples[i]) - t;
variance += (diff * diff) / ((i + 1.0) *i);
}

return variance / (size - 1);
}





share|improve this answer





























    0














    Rather than writing out more loops, you can create a function object to pass to std::accumulate to calculate the mean.



    template <typename T>
    struct normalize {
    T operator()(T initial, T value) {
    return initial + pow(value - mean, 2);
    }
    T mean;
    }


    While we are at it, we can use std::istream_iterator to do the file loading, and std::vector because we don't know how many values there are at compile time. This gives us:



    int main()
    {
    std::vector<int> values(100); // initial capacity, no contents yet

    ifstream myfile(“numbers.txt");
    if (myfile.is_open())
    {
    std::copy(std::istream_iterator<int>(myfile), std::istream_iterator<int>(), std::back_inserter(values));
    myfile.close();
    }
    else { cout<< "Error loading file" <<endl; }

    float sum = std::accumulate(values.begin(), values.end(), 0, plus<int>()); // plus is the default for accumulate, can be omitted
    std::cout << "The sum of all integers: " << sum << std::endl;
    float mean = sum / values.size();
    std::cout << "The mean of all integers: " << mean << std::endl;
    float var = std::accumulate(values.begin(), values.end(), 0, normalize<float>{ mean });
    float sd = sqrt(var);
    std::cout << "The standard deviation is: " << sd << std::endl;
    return 0;
    }





    share|improve this answer





























      0














      Here's another approach using std::accumulate but without using pow. In addition, we can use an anonymous function to define how to calculate the variance after we calculate the mean. Note that this computes the unbiased sample variance.



      #include <vector>
      #include <algorithm>
      #include <numeric>

      template<typename T>
      T variance(const std::vector<T> &vec)
      {
      size_t sz = vec.size();
      if (sz == 1)
      return 0.0;

      // Calculate the mean
      T mean = std::accumulate(vec.begin(), vec.end(), 0.0) / sz;

      // Now calculate the variance
      auto variance_func = [&mean, &sz](T accumulator, const T& val)
      {
      return accumulator + ((val - mean)*(val - mean) / (sz - 1));
      };

      return std::accumulate(vec.begin(), vec.end(), 0.0, variance_func);
      }


      A sample of how to use this function:



      int main()
      {
      std::vector<double> vec = {1.0, 5.0, 6.0, 3.0, 4.5};
      std::cout << variance(vec) << std::endl;
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33268513%2fcalculating-standard-deviation-variance-in-c%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10














        As the other answer by horseshoe correctly suggests, you will have to use a loop to calculate variance otherwise the statement




        var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;




        will just consider a single element from the array.



        Just improved horseshoe's suggested code:



        var = 0;
        for( n = 0; n < numPoints; n++ )
        {
        var += (Array[n] - mean) * (Array[n] - mean);
        }
        var /= numPoints;
        sd = sqrt(var);


        Your sum works fine even without using loop because you are using accumulate function which already has a loop inside it, but which is not evident in the code, take a look at the equivalent behavior of accumulate for a clear understanding of what it is doing.



        Note: X ?= Y is short for X = X ? Y where ? can be any operator.
        Also you can use pow(Array[n] - mean, 2) to take the square instead of multiplying it by itself making it more tidy.






        share|improve this answer



















        • 1




          thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
          – Jack
          Oct 22 '15 at 11:32






        • 2




          @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
          – Ahmed Akhtar
          Oct 23 '15 at 3:56


















        10














        As the other answer by horseshoe correctly suggests, you will have to use a loop to calculate variance otherwise the statement




        var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;




        will just consider a single element from the array.



        Just improved horseshoe's suggested code:



        var = 0;
        for( n = 0; n < numPoints; n++ )
        {
        var += (Array[n] - mean) * (Array[n] - mean);
        }
        var /= numPoints;
        sd = sqrt(var);


        Your sum works fine even without using loop because you are using accumulate function which already has a loop inside it, but which is not evident in the code, take a look at the equivalent behavior of accumulate for a clear understanding of what it is doing.



        Note: X ?= Y is short for X = X ? Y where ? can be any operator.
        Also you can use pow(Array[n] - mean, 2) to take the square instead of multiplying it by itself making it more tidy.






        share|improve this answer



















        • 1




          thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
          – Jack
          Oct 22 '15 at 11:32






        • 2




          @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
          – Ahmed Akhtar
          Oct 23 '15 at 3:56
















        10












        10








        10






        As the other answer by horseshoe correctly suggests, you will have to use a loop to calculate variance otherwise the statement




        var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;




        will just consider a single element from the array.



        Just improved horseshoe's suggested code:



        var = 0;
        for( n = 0; n < numPoints; n++ )
        {
        var += (Array[n] - mean) * (Array[n] - mean);
        }
        var /= numPoints;
        sd = sqrt(var);


        Your sum works fine even without using loop because you are using accumulate function which already has a loop inside it, but which is not evident in the code, take a look at the equivalent behavior of accumulate for a clear understanding of what it is doing.



        Note: X ?= Y is short for X = X ? Y where ? can be any operator.
        Also you can use pow(Array[n] - mean, 2) to take the square instead of multiplying it by itself making it more tidy.






        share|improve this answer














        As the other answer by horseshoe correctly suggests, you will have to use a loop to calculate variance otherwise the statement




        var = ((Array[n] - mean) * (Array[n] - mean)) / numPoints;




        will just consider a single element from the array.



        Just improved horseshoe's suggested code:



        var = 0;
        for( n = 0; n < numPoints; n++ )
        {
        var += (Array[n] - mean) * (Array[n] - mean);
        }
        var /= numPoints;
        sd = sqrt(var);


        Your sum works fine even without using loop because you are using accumulate function which already has a loop inside it, but which is not evident in the code, take a look at the equivalent behavior of accumulate for a clear understanding of what it is doing.



        Note: X ?= Y is short for X = X ? Y where ? can be any operator.
        Also you can use pow(Array[n] - mean, 2) to take the square instead of multiplying it by itself making it more tidy.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 6:21

























        answered Oct 21 '15 at 22:38









        Ahmed Akhtar

        1,22411021




        1,22411021








        • 1




          thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
          – Jack
          Oct 22 '15 at 11:32






        • 2




          @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
          – Ahmed Akhtar
          Oct 23 '15 at 3:56
















        • 1




          thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
          – Jack
          Oct 22 '15 at 11:32






        • 2




          @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
          – Ahmed Akhtar
          Oct 23 '15 at 3:56










        1




        1




        thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
        – Jack
        Oct 22 '15 at 11:32




        thanks for the 'Note' it was useful. compare your code to horseshoe why is the for statement better than the while? or is there no real difference?
        – Jack
        Oct 22 '15 at 11:32




        2




        2




        @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
        – Ahmed Akhtar
        Oct 23 '15 at 3:56






        @jack technically there is no difference between the for and the while loops (except syntax), but usually when you need: (1) initialization of a variable before starting the loop, (2) an increment in the variable at the end of the loop and then (3) want to check for a condition to reiterate; then for makes the code much more readable and also ensures that you don't forget any of the three.
        – Ahmed Akhtar
        Oct 23 '15 at 3:56















        1














        Your variance calculation is outside the loop and thus it is only based on the n== 100 value. You need an additional loop.



        You need:



        var = 0;
        n=0;
        while (n<numPoints){
        var = var + ((Array[n] - mean) * (Array[n] - mean));
        n++;
        }
        var /= numPoints;
        sd = sqrt(var);





        share|improve this answer























        • I think you have a typo on the last line.
          – Jason
          Oct 21 '15 at 22:55






        • 1




          @ Jason: yes, true, but Ahmeds solutions now solves it very well
          – horseshoe
          Oct 21 '15 at 23:15






        • 1




          It's still good to fix your typos for anyone else that reads the code.
          – Jason
          Oct 21 '15 at 23:20










        • @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
          – Ahmed Akhtar
          Oct 23 '15 at 4:14












        • @Ahmed Akhtar. Changed it, thx
          – horseshoe
          Oct 23 '15 at 6:49
















        1














        Your variance calculation is outside the loop and thus it is only based on the n== 100 value. You need an additional loop.



        You need:



        var = 0;
        n=0;
        while (n<numPoints){
        var = var + ((Array[n] - mean) * (Array[n] - mean));
        n++;
        }
        var /= numPoints;
        sd = sqrt(var);





        share|improve this answer























        • I think you have a typo on the last line.
          – Jason
          Oct 21 '15 at 22:55






        • 1




          @ Jason: yes, true, but Ahmeds solutions now solves it very well
          – horseshoe
          Oct 21 '15 at 23:15






        • 1




          It's still good to fix your typos for anyone else that reads the code.
          – Jason
          Oct 21 '15 at 23:20










        • @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
          – Ahmed Akhtar
          Oct 23 '15 at 4:14












        • @Ahmed Akhtar. Changed it, thx
          – horseshoe
          Oct 23 '15 at 6:49














        1












        1








        1






        Your variance calculation is outside the loop and thus it is only based on the n== 100 value. You need an additional loop.



        You need:



        var = 0;
        n=0;
        while (n<numPoints){
        var = var + ((Array[n] - mean) * (Array[n] - mean));
        n++;
        }
        var /= numPoints;
        sd = sqrt(var);





        share|improve this answer














        Your variance calculation is outside the loop and thus it is only based on the n== 100 value. You need an additional loop.



        You need:



        var = 0;
        n=0;
        while (n<numPoints){
        var = var + ((Array[n] - mean) * (Array[n] - mean));
        n++;
        }
        var /= numPoints;
        sd = sqrt(var);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 23 '15 at 6:48

























        answered Oct 21 '15 at 20:42









        horseshoe

        569417




        569417












        • I think you have a typo on the last line.
          – Jason
          Oct 21 '15 at 22:55






        • 1




          @ Jason: yes, true, but Ahmeds solutions now solves it very well
          – horseshoe
          Oct 21 '15 at 23:15






        • 1




          It's still good to fix your typos for anyone else that reads the code.
          – Jason
          Oct 21 '15 at 23:20










        • @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
          – Ahmed Akhtar
          Oct 23 '15 at 4:14












        • @Ahmed Akhtar. Changed it, thx
          – horseshoe
          Oct 23 '15 at 6:49


















        • I think you have a typo on the last line.
          – Jason
          Oct 21 '15 at 22:55






        • 1




          @ Jason: yes, true, but Ahmeds solutions now solves it very well
          – horseshoe
          Oct 21 '15 at 23:15






        • 1




          It's still good to fix your typos for anyone else that reads the code.
          – Jason
          Oct 21 '15 at 23:20










        • @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
          – Ahmed Akhtar
          Oct 23 '15 at 4:14












        • @Ahmed Akhtar. Changed it, thx
          – horseshoe
          Oct 23 '15 at 6:49
















        I think you have a typo on the last line.
        – Jason
        Oct 21 '15 at 22:55




        I think you have a typo on the last line.
        – Jason
        Oct 21 '15 at 22:55




        1




        1




        @ Jason: yes, true, but Ahmeds solutions now solves it very well
        – horseshoe
        Oct 21 '15 at 23:15




        @ Jason: yes, true, but Ahmeds solutions now solves it very well
        – horseshoe
        Oct 21 '15 at 23:15




        1




        1




        It's still good to fix your typos for anyone else that reads the code.
        – Jason
        Oct 21 '15 at 23:20




        It's still good to fix your typos for anyone else that reads the code.
        – Jason
        Oct 21 '15 at 23:20












        @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
        – Ahmed Akhtar
        Oct 23 '15 at 4:14






        @horseshoe the loop should start with n=0; to cater for the first index of the array. "It's still good to fix your typos for anyone else that reads the code. – Jason"
        – Ahmed Akhtar
        Oct 23 '15 at 4:14














        @Ahmed Akhtar. Changed it, thx
        – horseshoe
        Oct 23 '15 at 6:49




        @Ahmed Akhtar. Changed it, thx
        – horseshoe
        Oct 23 '15 at 6:49











        1














        Two simple methods to calculate Standard Deviation & Variance in C++.



        #include <math.h>
        #include <vector>

        double StandardDeviation(std::vector<double>);
        double Variance(std::vector<double>);

        int main()
        {
        std::vector<double> samples;
        samples.push_back(2.0);
        samples.push_back(3.0);
        samples.push_back(4.0);
        samples.push_back(5.0);
        samples.push_back(6.0);
        samples.push_back(7.0);

        double std = StandardDeviation(samples);
        return 0;
        }

        double StandardDeviation(std::vector<double> samples)
        {
        return sqrt(Variance(samples));
        }

        double Variance(std::vector<double> samples)
        {
        int size = samples.size();

        double variance = 0;
        double t = samples[0];
        for (int i = 1; i < size; i++)
        {
        t += samples[i];
        double diff = ((i + 1) * samples[i]) - t;
        variance += (diff * diff) / ((i + 1.0) *i);
        }

        return variance / (size - 1);
        }





        share|improve this answer


























          1














          Two simple methods to calculate Standard Deviation & Variance in C++.



          #include <math.h>
          #include <vector>

          double StandardDeviation(std::vector<double>);
          double Variance(std::vector<double>);

          int main()
          {
          std::vector<double> samples;
          samples.push_back(2.0);
          samples.push_back(3.0);
          samples.push_back(4.0);
          samples.push_back(5.0);
          samples.push_back(6.0);
          samples.push_back(7.0);

          double std = StandardDeviation(samples);
          return 0;
          }

          double StandardDeviation(std::vector<double> samples)
          {
          return sqrt(Variance(samples));
          }

          double Variance(std::vector<double> samples)
          {
          int size = samples.size();

          double variance = 0;
          double t = samples[0];
          for (int i = 1; i < size; i++)
          {
          t += samples[i];
          double diff = ((i + 1) * samples[i]) - t;
          variance += (diff * diff) / ((i + 1.0) *i);
          }

          return variance / (size - 1);
          }





          share|improve this answer
























            1












            1








            1






            Two simple methods to calculate Standard Deviation & Variance in C++.



            #include <math.h>
            #include <vector>

            double StandardDeviation(std::vector<double>);
            double Variance(std::vector<double>);

            int main()
            {
            std::vector<double> samples;
            samples.push_back(2.0);
            samples.push_back(3.0);
            samples.push_back(4.0);
            samples.push_back(5.0);
            samples.push_back(6.0);
            samples.push_back(7.0);

            double std = StandardDeviation(samples);
            return 0;
            }

            double StandardDeviation(std::vector<double> samples)
            {
            return sqrt(Variance(samples));
            }

            double Variance(std::vector<double> samples)
            {
            int size = samples.size();

            double variance = 0;
            double t = samples[0];
            for (int i = 1; i < size; i++)
            {
            t += samples[i];
            double diff = ((i + 1) * samples[i]) - t;
            variance += (diff * diff) / ((i + 1.0) *i);
            }

            return variance / (size - 1);
            }





            share|improve this answer












            Two simple methods to calculate Standard Deviation & Variance in C++.



            #include <math.h>
            #include <vector>

            double StandardDeviation(std::vector<double>);
            double Variance(std::vector<double>);

            int main()
            {
            std::vector<double> samples;
            samples.push_back(2.0);
            samples.push_back(3.0);
            samples.push_back(4.0);
            samples.push_back(5.0);
            samples.push_back(6.0);
            samples.push_back(7.0);

            double std = StandardDeviation(samples);
            return 0;
            }

            double StandardDeviation(std::vector<double> samples)
            {
            return sqrt(Variance(samples));
            }

            double Variance(std::vector<double> samples)
            {
            int size = samples.size();

            double variance = 0;
            double t = samples[0];
            for (int i = 1; i < size; i++)
            {
            t += samples[i];
            double diff = ((i + 1) * samples[i]) - t;
            variance += (diff * diff) / ((i + 1.0) *i);
            }

            return variance / (size - 1);
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 5 '17 at 22:54









            D.Zadravec

            58427




            58427























                0














                Rather than writing out more loops, you can create a function object to pass to std::accumulate to calculate the mean.



                template <typename T>
                struct normalize {
                T operator()(T initial, T value) {
                return initial + pow(value - mean, 2);
                }
                T mean;
                }


                While we are at it, we can use std::istream_iterator to do the file loading, and std::vector because we don't know how many values there are at compile time. This gives us:



                int main()
                {
                std::vector<int> values(100); // initial capacity, no contents yet

                ifstream myfile(“numbers.txt");
                if (myfile.is_open())
                {
                std::copy(std::istream_iterator<int>(myfile), std::istream_iterator<int>(), std::back_inserter(values));
                myfile.close();
                }
                else { cout<< "Error loading file" <<endl; }

                float sum = std::accumulate(values.begin(), values.end(), 0, plus<int>()); // plus is the default for accumulate, can be omitted
                std::cout << "The sum of all integers: " << sum << std::endl;
                float mean = sum / values.size();
                std::cout << "The mean of all integers: " << mean << std::endl;
                float var = std::accumulate(values.begin(), values.end(), 0, normalize<float>{ mean });
                float sd = sqrt(var);
                std::cout << "The standard deviation is: " << sd << std::endl;
                return 0;
                }





                share|improve this answer


























                  0














                  Rather than writing out more loops, you can create a function object to pass to std::accumulate to calculate the mean.



                  template <typename T>
                  struct normalize {
                  T operator()(T initial, T value) {
                  return initial + pow(value - mean, 2);
                  }
                  T mean;
                  }


                  While we are at it, we can use std::istream_iterator to do the file loading, and std::vector because we don't know how many values there are at compile time. This gives us:



                  int main()
                  {
                  std::vector<int> values(100); // initial capacity, no contents yet

                  ifstream myfile(“numbers.txt");
                  if (myfile.is_open())
                  {
                  std::copy(std::istream_iterator<int>(myfile), std::istream_iterator<int>(), std::back_inserter(values));
                  myfile.close();
                  }
                  else { cout<< "Error loading file" <<endl; }

                  float sum = std::accumulate(values.begin(), values.end(), 0, plus<int>()); // plus is the default for accumulate, can be omitted
                  std::cout << "The sum of all integers: " << sum << std::endl;
                  float mean = sum / values.size();
                  std::cout << "The mean of all integers: " << mean << std::endl;
                  float var = std::accumulate(values.begin(), values.end(), 0, normalize<float>{ mean });
                  float sd = sqrt(var);
                  std::cout << "The standard deviation is: " << sd << std::endl;
                  return 0;
                  }





                  share|improve this answer
























                    0












                    0








                    0






                    Rather than writing out more loops, you can create a function object to pass to std::accumulate to calculate the mean.



                    template <typename T>
                    struct normalize {
                    T operator()(T initial, T value) {
                    return initial + pow(value - mean, 2);
                    }
                    T mean;
                    }


                    While we are at it, we can use std::istream_iterator to do the file loading, and std::vector because we don't know how many values there are at compile time. This gives us:



                    int main()
                    {
                    std::vector<int> values(100); // initial capacity, no contents yet

                    ifstream myfile(“numbers.txt");
                    if (myfile.is_open())
                    {
                    std::copy(std::istream_iterator<int>(myfile), std::istream_iterator<int>(), std::back_inserter(values));
                    myfile.close();
                    }
                    else { cout<< "Error loading file" <<endl; }

                    float sum = std::accumulate(values.begin(), values.end(), 0, plus<int>()); // plus is the default for accumulate, can be omitted
                    std::cout << "The sum of all integers: " << sum << std::endl;
                    float mean = sum / values.size();
                    std::cout << "The mean of all integers: " << mean << std::endl;
                    float var = std::accumulate(values.begin(), values.end(), 0, normalize<float>{ mean });
                    float sd = sqrt(var);
                    std::cout << "The standard deviation is: " << sd << std::endl;
                    return 0;
                    }





                    share|improve this answer












                    Rather than writing out more loops, you can create a function object to pass to std::accumulate to calculate the mean.



                    template <typename T>
                    struct normalize {
                    T operator()(T initial, T value) {
                    return initial + pow(value - mean, 2);
                    }
                    T mean;
                    }


                    While we are at it, we can use std::istream_iterator to do the file loading, and std::vector because we don't know how many values there are at compile time. This gives us:



                    int main()
                    {
                    std::vector<int> values(100); // initial capacity, no contents yet

                    ifstream myfile(“numbers.txt");
                    if (myfile.is_open())
                    {
                    std::copy(std::istream_iterator<int>(myfile), std::istream_iterator<int>(), std::back_inserter(values));
                    myfile.close();
                    }
                    else { cout<< "Error loading file" <<endl; }

                    float sum = std::accumulate(values.begin(), values.end(), 0, plus<int>()); // plus is the default for accumulate, can be omitted
                    std::cout << "The sum of all integers: " << sum << std::endl;
                    float mean = sum / values.size();
                    std::cout << "The mean of all integers: " << mean << std::endl;
                    float var = std::accumulate(values.begin(), values.end(), 0, normalize<float>{ mean });
                    float sd = sqrt(var);
                    std::cout << "The standard deviation is: " << sd << std::endl;
                    return 0;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 24 '17 at 11:36









                    Caleth

                    16.7k22138




                    16.7k22138























                        0














                        Here's another approach using std::accumulate but without using pow. In addition, we can use an anonymous function to define how to calculate the variance after we calculate the mean. Note that this computes the unbiased sample variance.



                        #include <vector>
                        #include <algorithm>
                        #include <numeric>

                        template<typename T>
                        T variance(const std::vector<T> &vec)
                        {
                        size_t sz = vec.size();
                        if (sz == 1)
                        return 0.0;

                        // Calculate the mean
                        T mean = std::accumulate(vec.begin(), vec.end(), 0.0) / sz;

                        // Now calculate the variance
                        auto variance_func = [&mean, &sz](T accumulator, const T& val)
                        {
                        return accumulator + ((val - mean)*(val - mean) / (sz - 1));
                        };

                        return std::accumulate(vec.begin(), vec.end(), 0.0, variance_func);
                        }


                        A sample of how to use this function:



                        int main()
                        {
                        std::vector<double> vec = {1.0, 5.0, 6.0, 3.0, 4.5};
                        std::cout << variance(vec) << std::endl;
                        }





                        share|improve this answer


























                          0














                          Here's another approach using std::accumulate but without using pow. In addition, we can use an anonymous function to define how to calculate the variance after we calculate the mean. Note that this computes the unbiased sample variance.



                          #include <vector>
                          #include <algorithm>
                          #include <numeric>

                          template<typename T>
                          T variance(const std::vector<T> &vec)
                          {
                          size_t sz = vec.size();
                          if (sz == 1)
                          return 0.0;

                          // Calculate the mean
                          T mean = std::accumulate(vec.begin(), vec.end(), 0.0) / sz;

                          // Now calculate the variance
                          auto variance_func = [&mean, &sz](T accumulator, const T& val)
                          {
                          return accumulator + ((val - mean)*(val - mean) / (sz - 1));
                          };

                          return std::accumulate(vec.begin(), vec.end(), 0.0, variance_func);
                          }


                          A sample of how to use this function:



                          int main()
                          {
                          std::vector<double> vec = {1.0, 5.0, 6.0, 3.0, 4.5};
                          std::cout << variance(vec) << std::endl;
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            Here's another approach using std::accumulate but without using pow. In addition, we can use an anonymous function to define how to calculate the variance after we calculate the mean. Note that this computes the unbiased sample variance.



                            #include <vector>
                            #include <algorithm>
                            #include <numeric>

                            template<typename T>
                            T variance(const std::vector<T> &vec)
                            {
                            size_t sz = vec.size();
                            if (sz == 1)
                            return 0.0;

                            // Calculate the mean
                            T mean = std::accumulate(vec.begin(), vec.end(), 0.0) / sz;

                            // Now calculate the variance
                            auto variance_func = [&mean, &sz](T accumulator, const T& val)
                            {
                            return accumulator + ((val - mean)*(val - mean) / (sz - 1));
                            };

                            return std::accumulate(vec.begin(), vec.end(), 0.0, variance_func);
                            }


                            A sample of how to use this function:



                            int main()
                            {
                            std::vector<double> vec = {1.0, 5.0, 6.0, 3.0, 4.5};
                            std::cout << variance(vec) << std::endl;
                            }





                            share|improve this answer












                            Here's another approach using std::accumulate but without using pow. In addition, we can use an anonymous function to define how to calculate the variance after we calculate the mean. Note that this computes the unbiased sample variance.



                            #include <vector>
                            #include <algorithm>
                            #include <numeric>

                            template<typename T>
                            T variance(const std::vector<T> &vec)
                            {
                            size_t sz = vec.size();
                            if (sz == 1)
                            return 0.0;

                            // Calculate the mean
                            T mean = std::accumulate(vec.begin(), vec.end(), 0.0) / sz;

                            // Now calculate the variance
                            auto variance_func = [&mean, &sz](T accumulator, const T& val)
                            {
                            return accumulator + ((val - mean)*(val - mean) / (sz - 1));
                            };

                            return std::accumulate(vec.begin(), vec.end(), 0.0, variance_func);
                            }


                            A sample of how to use this function:



                            int main()
                            {
                            std::vector<double> vec = {1.0, 5.0, 6.0, 3.0, 4.5};
                            std::cout << variance(vec) << std::endl;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 2 '18 at 8:44









                            rayryeng

                            82.6k17111139




                            82.6k17111139






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33268513%2fcalculating-standard-deviation-variance-in-c%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Xamarin.iOS Cant Deploy on Iphone

                                Glorious Revolution

                                Dulmage-Mendelsohn matrix decomposition in Python