473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Scipy - How do you do a 'weighted' least squares fit to data?

Hi,

I have performed a fit to data using scipy's 'leastsq' function. However,
I wanted to be able to weight the fit by individual errors on the data points.

Here is an example of what I'm doing at the moment :

Expand|Select|Wrap|Line Numbers
  1. # fitting functions for the data
  2. fitfunc = lambda p,x: p[0] + p[1]*x
  3. errfunc = lambda p,x,y : y-fitfunc(p,x)
  4.  
  5.  
  6. x = [8365 7584 6956 6471 6149 6471 6956 7586]
  7. y = [-0.70728186 -0.48169299  0.          0.42631948  0.9592345   0.66865681 0.24757267 -0.28228264]
  8. errors = [ 0.10181198  0.24058398  0.10275219  0.2954378   0.09397324 0.21624879 0.10871897  0.34591412]
  9.  
  10. p0 = [5.0,-6.30]
  11. out = optimize.leastsq(errfunc, p0,args=(xfit_rot_data,yfit_rot_data,yfit_err),full_output=1)
  12.  
  13. bestparams= out[0]
  14.  
  15.  
Thanks ...
Nov 2 '09 #1
5 24387
bvdet
2,851 Expert Mod 2GB
You assignments to x, y and errors are not valid Python lists. I do not understand what "weight the fit" is.

BV
Nov 2 '09 #2
yes ... sorry the x,y, and error lists should have commas between them ... I just copied and pasted them from a text file as an example.

What I mean by weighting the fit is that there is an error associated with
each x,y data point and when scipy does the fitting I would like it to take into
account that error so that points with huge errors don't have the same influence on the fit as a point with a very small error.

Thanks
Nov 2 '09 #3
Glenton
391 Expert 256MB
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.
Nov 5 '09 #4
In your error function, rather than simply having y-fitfunc(p,x), have (y-fitfunc(p,x))/errors, where errors is your array of 1-sigma error bars. When leastsq squares these terms and sums them, it's effectively including the 1/sigma^2 weights you want.
Oct 31 '11 #5
In your err fun:

P = 1/ERRORS
errfun = VTPV ( errfun = min )
Jun 4 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: sdhyok | last post by:
Hi, I am trying to build up a system handling time series data a lot. Do you know any well-designed python class specially for time series data? Thanks in advance. Shin, Daehyok
3
by: venkat | last post by:
Hi, I want to solve linear least sqaure problem( min||c-Ax||2 subject to Bx=d ). How do I do it in python. lapack has a routine for doing this (DGGLSE). Can I access this from python? TIA,...
6
by: Hamed | last post by:
Hello I have employed as a developer in a software company that its team uses FoxPro / VB 6.0 / VC++ 6.0 as the developing tools and newly is going to migrate to VS.NET. There is a project...
11
by: Charles T. | last post by:
Hi, I currently writing a serialize/unserialize architecture. The read/write function will read/write from a binary file. My question is is there some sort on defined standart to use when...
13
by: Michelle | last post by:
Hi all... I could use a little TLC here for understanding and not for solving a specific problem... sorry if I've got the wrong group, but I'm using VB so I figured this was the most appropriate...
2
by: Stephan (Germany) | last post by:
Hello, maybe this qustion sounds silly but in VS2005, I'm looking for the same possibilty to entry data for a predefined XML schema in grid view. In VS2003, you were able to do it if you...
5
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I...
1
by: J.SanTanA | last post by:
Hello All! Any one know where can I find some php code (or "php-able" code) implementing least squares method? Thx in advance and pls excuse my poor eng
4
by: Evelien | last post by:
Dear python-users, I am trying to do a non-linear least squares fitting. Maybe trying is not the best word, as I already succeeded in that. At the moment I am using leastSquaresFit from...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.