| re: Scipy - How do you do a 'weighted' least squares fit to data?
Hi
It seems that the problem is mathematical, rather than coding! I won't help with the python, cos you clearly know what you're doing, but I'm sure people would appreciate posting back your solution.
I can think of two methods, one crude and quick, the other more rigorous.
Method 1:
- Create an integer weighting, but inverting the errors (1/error), multiplying by some suitable constant, and rounding to the nearest integer.
- Create a new data set by adding multiple copies of each data point, corresponding to the above integer.
- Do a least square fit on this new data set.
Obviously by picking the constant suitably large you can get the weighting quite accurate. The big advantage is that it's a small tweak on your code.
METHOD 2:
- Create the weighted least square function yourself (Sum ((data-f(x))^2)/error).
- Use optimise to minimise this error based on your parameters (ie don't use least squares, but the other scipy optimize function).
Let me know if you don't have any joy or need some clarification and I'll make a bit more effort.
|