Re-weighted average, Weiszfeld’s algorithm and its C# implementation

29 Jun

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.

Weiszfeld's algorithm.png

And a testing example:

Weiszfeld's algorithm_2.png

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:


The expected average is there. Happy coding!


Posted by on June 29, 2009 in Dotnet/C#


Tags: , , , ,

5 responses to “Re-weighted average, Weiszfeld’s algorithm and its C# implementation

  1. 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!

  2. 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:

  3. robert

    August 27, 2011 at 8:49 pm

    I have download it, Thanks very much!

  4. 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.

  5. 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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: