RSS

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:

0.53636363636363626
5.1580805230206117
5.1687358118107358
5.1853981349120017
5.1983610387763433
5.1999977912528657
5.200000000000335
5.2

The expected average is there. Happy coding!

About these ads
 
5 Comments

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!
    My email:robert.greenapple@gmail.com

     
  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: http://www.box.net/shared/ootm5cpp5h

     
  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 46 other followers

%d bloggers like this: