MATLAB: Command skewness(x) vs calculating sample skewness by hand

MATLABskewness

Task is to calculate sample skewness(x) by hand in Matlab to show that you understand the mathematical concept aka:
My solution was: skewness_x=((1/numel(x))*(sum((x-mean(x)).*(x-mean(x)).*(x-mean(x))))/(std(x)^3)) except this value is 3*10^-4 off the value which i get from skewness(x).
(x being a (1,300) vektor with 300 random values)
Did i make a mistake in my formula? Or does Matlab round somewhere?

Best Answer

This is not a rounding issue.
The answer is almost always to check the docs for the function in question.
doc skewness
If you do read that doc, in the algorithms section, you will quickly see that skewness does something slightly different. That difference probably comes about because of a degrees of freedom thing. Think of it like this: when you compute the SAMPLE standard deviation, it needs to use the mean, as if the mean were known. So a sample standard deviation (what you get from std(x,0), or just the default std(x)) has a factor if 1/sqrt(n-1) in there, instead of 1/sqrt(n). This is the classically seen difference between a population estimator versus a bias corrected sample standard deviation.
But when you compute skewness, it also uses the sample standard deviation. So skewness(x,0) needs to tweak those factors of n in the formula to give a bias corrected estimator.
Related Question