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.
I'm trying to calculate standard deviation & variance. My code loads a file of 100 integers and puts them into an array, counts them, calculates the mean, sum, variance and SD. But I'm having a little trouble with the variance.
I keep getting a huge number - I have a feeling it's to do with its calculation.
My mean and sum are ok.
NB:
using namespace std;
int main() {
int n = 0;
int Array[100];
float mean;
float var, 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;
}
Step 1: Find the mean. Step 2: For each data point, find the square of its distance to the mean. Step 3: Sum the values from Step 2. Step 4: Divide by the number of data points.
What is the standard deviation example? Consider the data set: 2, 1, 3, 2, 4. The mean and the sum of squares of deviations of the observations from the mean will be 2.4 and 5.2, respectively. Thus, the standard deviation will be √(5.2/5) = 1.01.
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) {
const size_t sz = vec.size();
if (sz == 1) {
return 0.0;
}
// Calculate the mean
const 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:
#include <iostream>
int main() {
const std::vector<double> vec = {1.0, 5.0, 6.0, 3.0, 4.5};
std::cout << variance(vec) << std::endl;
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With