numpy sum of squares for matrix
I do have a matrix with observations in rows (measurements at differnt pH) with data points as columns (concentration over time). So one row consists of differnt data points for one pH.
I do want to fit an ODE to the data. So i defined a cost function and would like to calculate the sum of squares for all observatoins. Taking the sum of sqares for this matrix should work like:
res = y - yhat # calculate residuals ssq = np.diag(np.dot(res.T,res)) # sum over the diagonal
is that correct ?
If you would take the sum of the last array it would be correct. But it's also unnecessarily complex (because the off-diagonal elements are also calculated with np.dot) Faster is:
ssq = np.sum(res**2)
If you want the ssd for each experiment, you can do:
ssq = np.sum(res**2, axis=1)