Using simple arithmetic average to calculate the mean value for noisy data is not robust. Using Iteratively reweighted least squares instead, prove to be very good choice. Similar discussions can be also found in 2D, 3D regressions, for instance, in the book by William S. Cleveland, “Visualizaing Data”, Chapter 3, pp. 113-119.

Weiszfeld’s algorithm is one of the iteratively reweighted least square approach to tackle this. Below is my implementation to calculate the mean value of a 1D vector.

And a testing example:

So the expected average should be 5.2, with the outliers {-70, -20, 15.5,44} properly “filtered”.

Debugging the method shows that after a few iterations:

0.53636363636363626

5.1580805230206117

5.1687358118107358

5.1853981349120017

5.1983610387763433

5.1999977912528657

5.200000000000335

5.2

The expected average is there. Happy coding!

### Like this:

Like Loading...

*Related*

robert

August 26, 2011 at 9:30 pm

I am studying Weiszfeld algorithm . I want a c++ implementation.

Can you give me your c# code? Thanks a lot!

My email:robert.greenapple@gmail.com

xinyustudio

August 27, 2011 at 8:10 am

Go to Download page of this blog, and you will find the link. Or alternatively click this link to directly download the C# implementation: http://www.box.net/shared/ootm5cpp5h

robert

August 27, 2011 at 8:49 pm

I have download it, Thanks very much!

Shital Shah

November 15, 2011 at 6:57 pm

I think the variable temp should be Math.Abs(X[i]-m0) because it’s a norm. You will get fast convergence to a value very close in set but the wrong result if you use squares instead.

Manoj

September 1, 2012 at 1:38 am

how do u do it for a 2D set of points to obtain the geometric median? Thank you.