python - Tiny numerical difference in sum-of-squares depending on numpy procedure call used -
i writing function computes sum of squares of errors. x , y vectors of same length; y observation data, x data computed model.
the code like:
>> res = y.ravel() - x.ravel() >> np.dot(res.t, res) >> 1026.7059479504269 >> np.sum(res**2) >> 1026.7059479504273 now @ last 2 digits. can tell me reason it? both calls should result in same operations. know difference comes from?
floating point addition not associative--the order in operations performed can affect result. presumably np.dot(res, res) , np.sum(res**2) perform operations in different orders.
here's example np.sum(x**2) != np.sum(x[:50]**2) + np.sum(x[50:]**2) (and neither equals np.dot(x, x)):
>>> np.random.seed(1234) >>> x = np.random.randn(100) >>> np.sum(x**2) 99.262119361371433 >>> np.sum(x[:50]**2) + np.sum(x[50:]**2) 99.262119361371461 >>> np.dot(x, x) 99.262119361371447
Comments
Post a Comment