Dot Product: * Command vs. Loop gives different results.

I have two vectors in Matlab, z and beta . Vector z is 1x17:

1 0.430742139435890 0.257372971229541 0.0965909090909091 0.694329541928697 0 0.394960106863064 0 0.100000000000000 1 0.264704325268675 0.387774594078319 0.269207605609567 0.472226643323253 0.750000000000000 0.513121013402805 0.697062571025173

... and beta is 17x1:

6.55269487769363e+26 0 0 -56.3867588816768 -2.21310778926413 0 57.0726052009847 0 3.47223691057151e+27 -1.00249317882651e+27 3.38202232046686 1.16425987969027 0.229504956512063 -0.314243264212449 -0.257394312588330 0.498644243389556 -0.852510642195370

I am dealing with some feature problems, and I noticed that if I want to calculate the z*beta dot product, I can get 2 different solutions. If I use the command * , z*beta = 18.5045 . If I write a loop to calculate the point product (below), I get a solution of 0.7287.

 summation=0; for i=1:17 addition=z(1,i)*beta(i); summation=summation+addition; end 

Any idea what is going on here?

Here's a link to the data: https://dl.dropboxusercontent.com/u/16594701/data.zip

+4
source share
1 answer

The problem is that adding floating point numbers is not associative . Summing up a sequence of numbers of comparable magnitude, this is usually not a problem. However, in your sequence, most numbers are about 1 or 10, and several entries are 10 ^ 26 or 10 ^ 27. In this situation, numerical problems are almost inevitable.

The wikipedia page http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems shows a processed example where (a + b) + c not equal to a + (b + c) , that is, it demonstrates that the order in which you add numbers floating point matters.

I would suggest that this is homework designed to illustrate these exact issues. If not, I would ask what the data represent in order to judge the appropriate approach. It would probably be much more productive to find out why such large numbers are produced in the first place, than to try to understand that this is a point product that includes them.

+7
source

All Articles