# Thread: Calculating Skewness using s non iteratice aproach

1. ## Calculating Skewness using s non iteratice aproach

Hi guys,

A while back I was kindly helped with a non iterative algorithm to calculate a weighted average and its variance outlined here http://www.mathhelpforum.com/math-he...-variance.html I need to add Skew now.

From Skewness - Wikipedia, the free encyclopedia Skewness is given as the 3rd moment about the mean divided by standard deviation. Thanks to JakeD I have an algorithm to calculate the weighted average and variance (so SD too) that uses non iterative techniques (you can see that in the first thread). Keeping track of the mean as you go along is trivial. I feel there must be a small modification that could be made to accumulate the 3rd moment but to be frank I think its beyond me. (a simple code head!) I was hoping one of you maths guys might be able to help again. This needs to work on a 'stream' of time series data so I need to be able to calculate as a new piece of data rather than process the whole series each time. Hope someone can help.

Nick.

2. Am I right in thinking that the variance is the second moment and that the skewness is the third moment?

Here is the algorithm I used for variance and I wonder if simply summing ^3's rather than ^2's will give me the 3rd moment? I feel it should be quite easy but it's still rather beyond me.

mean_old = 0;
mean_new = 0;
sum = 0;
wgt_old = 0;
wgt_new = 0;

foreach x in data and w in weights:
wgt_new = wgt_old + w;
mean_new = (wgt_old*mean_old + w*x)/wgt_new;
sum = sum + wgt_old*mean_old^2 + w*x^2 - wgt_new*mean_new^2;
wgt_old = wgt_new;
mean_old = mean_new;
end for;
variance = sum/wgt_new;

3. Originally Posted by BlowFish
Am I right in thinking that the variance is the second moment and that the skewness is the third moment?
No the variance is the CENTRAL second moment, that is:

var=E[(x-mean)^2)]

Skewdness is the normalised third CENTRAL moment:

skew= E[(x-mean)^3)]/sigma^3

RonL

4. Thanks Ron - My knowledge of this stuff is weak, its hard for me to decipher not completely understanding the terms. I am still thinking that this should be quit doable by modifying the original algorithm.

5. Well still struggling on with this but could use some help if any one is up to it? assuming everything is initialised to zero. I have

foreach x in data and w in weights:
wgt_new = wgt_old + w;
mean_new = (wgt_old*mean_old + w*x)/wgt_new;
sum = sum + wgt_old*mean_old^2 + w*x^2 - wgt_new*mean_new^2;
sumcm = sumcm + wgt_old*mean_old^3 + w*x^3 - wgt_new*mean_new^2;
wgt_old = wgt_new;
mean_old = mean_new;
end for;
variance = sum/wgt_new;
SD = sqrt(variance);
sumSD3 = SumSD3 + SD^3;

CM3 = sumcm/sumSD3/wgt_new

I am assuming the formula for weighted skew found here 8.2.8 Weighted Samples is correct? I am assuming xbar is the mean though am unsure why they refer to it that way?