Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot Product: * Command vs. Loop gives different results

I have two vectors in Matlab, z and beta. Vector z is a 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 a 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'm dealing with some singularity issues, and I noticed that if I want to compute the dot product of z*beta, I potentially get 2 different solutions. If I use the * command, z*beta = 18.5045. If I write a loop to compute the dot 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's going on here?

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

like image 541
Amy Avatar asked Mar 24 '23 04:03

Amy


1 Answers

The problem here is that addition of floating point numbers is not associative. When summing a sequence of numbers of comparable magnitude, this is not usually a problem. However, in your sequence, most numbers are around 1 or 10, while several entries have magnitude 10^26 or 10^27. Numerical problems are almost unavoidable in this situation.

The wikipedia page http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems shows a worked example where (a + b) + c is not equal to a + (b + c), i.e. demonstrating that the order in which you add up floating point numbers does matter.

I would guess that this is a homework assignment designed to illustrate these exact issues. If not, I'd ask what the data represents to suss out the appropriate approach. It would probably be much more productive to find out why such large numbers are being produced in the first place than trying to make sense of the dot product that includes them.

like image 159
nibot Avatar answered Apr 05 '23 06:04

nibot