473,804 Members | 3,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

32 New Member
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 24458
bvdet
2,851 Recognized Expert Moderator Specialist
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
fordie1000
32 New Member
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 Recognized Expert Contributor
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
smearedink
1 New Member
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
eggmilk
1 New Member
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
6906
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
5485
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, venkat.
6
5790
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 modified that is to upgrade a big, more than 100,000 lines DOS based program developed by FoxPro 2.x to .NET platform. The design is as previous and the implementation will be reprogrammed. As a duty in my job I should find an answer about the
11
3150
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 representing data type (int , int32, int64, double, string, etc....) ? Thanks,
13
2541
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 one. I'm an advanced beginner, but in all of the books and class material I have, I haven't found something that gets to the point about this... lot's of high level theoretical talk, but nothing that says things in simple terms. I'm building...
2
1872
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 simply set the target schema of your new XML file (which contains nothing besides the xml version line) and switch to data view. This simple way of data entry seems to be gone in VS2005, or is it hidden somewhere else?
5
7631
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 need the slope (representing growth) of monthly sales. the data is setup in a crosstab with the months in different collums, say column 1 to 12 for last year. I want to calculate the slope for every row (on the same query or another if needed)
1
4020
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
5233
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 Scientific Python. I know of other least squares routines, such as the one in scipy.optimize and I believe there is also one in numpy. Now here is my question: in my search for a good algorithm, I was not able to find any non-linear least squares fitting...
0
9585
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.