I just came across Welford's method for calculating standard deviations,
and I realized I didn't know how to implement it in J.

See

  - http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  - 
http://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of-computing-standard-deviation/
  - http://www.johndcook.com/standard_deviation.html

for the various articles that got me wondering about this.

Here's the algorithm, as stated in the above links.

  1. Initialize m_1 = x_1 and s_1 = 0.
  2. For subsequent xs,
     m_k = m_{k-1} + (x_k - m_{k-1})/k
     s_k = s_{k-1} + (x_k - m_{k-1})*(x_k - m_k)
  3. Then the kth running standard deviation is:
     stddev_k = s_k / (k - 1)

I've not tried to switch it from 1-based indexing to 0-based indexing,
or to use J-like phrasing in the problem, for fear I'd make a stupid
mistake.

How would I do this in J?

Cheers,
Johann

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to