I'm trying to calculate the variance of a vector of long doubles. I've tried implementing other code I've seen, but it doesn't return the correct value.
long double variance = 0;
for (int x = 0; x < (v.size() - 1); x++) {
variance += (v.at(x) - mean) * (v.at(x) - mean);
}
variance /= v.size();
For example, if my vector is {1,2,3,4,5}, the above code gives me 2.25. To my understanding the correct answer is 2.
Any help is appreciated, I'm not sure what I'm missing.