Search code examples
c++statisticsnormal-distribution

How to Calculate the sample mean, standard deviation, and variance in C++ from random distributed data and compare with original mean and sigma


I have Python script for Gaussian Normal distribution:

import numpy as np

x_mu = 25
x_sigma = 5
size = 1000

x_distribution = np.random.normal(x_mu, x_sigma, size)

#i am looking for help ONLY FOR this line below
test_distribution = np.std(x_distribution)  
print (test_distribution)

Updated: For large data (could be 10000 or only 500, so i will test it too), the sample mean and standard deviation have to be close to the original input (sigma and mean).

I am referring to this link and need to write in c++.

Please, if you know any libs in C++ or ideas, leave in comments or answer. Thanks


Solution

  • There is no standard deviation function C++, so you'd need to do write all the necessary functions yourself -- Generate random numbers and calculate the standard deviation.

    double stDev(const vector<double>& data) {
        double mean = std::accumulate(data.begin(), data.end(), 0.0) / data.size();
        double sqSum = std::inner_product(data.begin(), data.end(), data.begin(), 0.0);
        return std::sqrt(sqSum / data.size() - mean * mean);
    }
    
    int main() {
    
        double x_mu = 25;
        double x_sigma = 5;
        size_t size = 1000;
        std::normal_distribution<double> x_distribution(x_mu, x_sigma);
    
        //generate random numbers and store them in a vector
        vector<double> data(size);
        std::random_device rd;
        std::mt19937 gen(rd());
        for(size_t i=0; i<size; i++) {
            data[i] = x_distribution(gen);
        }
    
        double test_distribution = stDev(data); 
        cout << test_distribution << endl;
        return 0;
    }
    

    Update: To get mean, variance and standard deviation, you may create separate functions to do the calculations. One possible implementation would be:

    double mean(const vector<double>& data) {
            return  std::accumulate(data.begin(), data.end(), 0.0) / data.size();
    }
    
    double variance(const vector<double>& data) {
            double xBar = mean(data);
            double sqSum = std::inner_product(data.begin(), data.end(), data.begin(), 0.0);
            return sqSum / data.size() - xBar * xBar;
    }
    
    double stDev(const vector<double>& data) {
         return std::sqrt(variance(data));       
    }