Questions tagged [least-squares]

Refers to a general estimation technique that selects the parameter value to minimize the squared difference between two quantities, such as the observed value of a variable, and the expected value of that observation conditioned on the parameter value. Questions about the theory behind least-squares should utilize the Cross Validated (https://stats.stackexchange.com/questions) Stack Exchange site.

Overview

From the "Least squares" article on Wikipedia:

The method of least squares is a standard approach to the approximate solution of overdetermined systems, i.e., sets of equations in which there are more equations than unknowns. "Least squares" means that the overall solution minimizes the sum of the squares of the errors made in the results of every single equation.

Least squares problems fall into two categories: linear or ordinary least squares and non-linear least squares, depending on whether or not the residuals are linear in all unknowns. The linear least-squares problem occurs in statistical regression analysis; it has a closed-form solution. A closed-form solution (or closed-form expression) is any formula that can be evaluated in a finite number of standard operations. The non-linear problem has no closed-form solution and is usually solved by iterative refinement; at each iteration the system is approximated by a linear one, and thus the core calculation is similar in both cases.

Other References

Least squares methods are treated in many introductory statistics resources and textbooks, but there are also advanced resources dedicated only to the subject, for example:

Tag usage

Questions on should be about implementation and programming problems, not about the statistical or theoretical properties of the technique. Consider whether your question might be better suited to Cross Validated, the StackExchange site for statistics, machine learning and data analysis.

1013 questions
39
votes
2 answers

What is the difference between numpy.linalg.lstsq and scipy.linalg.lstsq?

lstsq tries to solve Ax=b minimizing |b - Ax|. Both scipy and numpy provide a linalg.lstsq function with a very similar interface. The documentation does not mention which kind of algorithm is used, neither for scipy.linalg.lstsq nor for…
lumbric
  • 7,644
  • 7
  • 42
  • 53
31
votes
8 answers

fast & efficient least squares fit algorithm in C?

I am trying to implement a linear least squares fit onto 2 arrays of data: time vs amplitude. The only technique I know so far is to test all of the possible m and b points in (y = m*x+b) and then find out which combination fits my data best so…
O_O
  • 4,397
  • 18
  • 54
  • 69
24
votes
4 answers

Lasso on sklearn does not converge

When I run something like import numpy from sklearn import linear_model A= #something b= #something clf=linear_model.Lasso(alpha=0.015, fit_intercept=False, tol=0.00000000000001, max_iter=10000000000000, positive=True) clf.fit(A,b) I get…
gota
  • 2,338
  • 4
  • 25
  • 45
23
votes
2 answers

Python statsmodels OLS: how to save learned model to file

I am trying to learn an ordinary least squares model using Python's statsmodels library, as described here. sm.OLS.fit() returns the learned model. Is there a way to save it to the file and reload it? My training data is huge and it takes around…
Nik
  • 5,515
  • 14
  • 49
  • 75
23
votes
8 answers

Finding translation and scale on two sets of points to get least square error in their distance?

I have two sets of 3D points (original and reconstructed) and correspondence information about pairs - which point from one set represents the second one. I need to find 3D translation and scaling factor which transforms reconstruct set so the sum…
user1476710
20
votes
1 answer

SciPy: leastsq vs least_squares

SciPy provides two functions for nonlinear least squares problems: optimize.leastsq() uses the Levenberg-Marquardt algorithm only. optimize.least_squares() allows us to choose the Levenberg-Marquardt, Trust Region Reflective, or Trust Region Dogleg…
visitor
  • 672
  • 6
  • 17
20
votes
4 answers

Partial Least Squares Library

There was already a question like this, but it was not answered, so I try to post it again. Does anyone know of an open-source implementation of a partial least squares algorithm in C++ (or C)? Or maybe a library that does it?
ISTB
  • 1,799
  • 3
  • 22
  • 31
19
votes
3 answers

Python: two-curve gaussian fitting with non-linear least-squares

My knowledge of maths is limited which is why I am probably stuck. I have a spectra to which I am trying to fit two Gaussian peaks. I can fit to the largest peak, but I cannot fit to the smallest peak. I understand that I need to sum the Gaussian…
Harpal
  • 12,057
  • 18
  • 61
  • 74
17
votes
4 answers

Non-linear Least Squares Optimization Library for C

I'm looking for a library in C that will do optimization of an objective function (preferrably Levenberg-Marquardt algorithm) and will support box constraints, linear inequality constraints and non-linear inequality constraints. I've tried several…
alkar
  • 5,459
  • 7
  • 27
  • 43
16
votes
1 answer

Graphing perpendicular offsets in a least squares regression plot in R

I'm interested in making a plot with a least squares regression line and line segments connecting the datapoints to the regression line as illustrated here in the graphic called perpendicular…
D W
  • 2,979
  • 4
  • 34
  • 45
15
votes
4 answers

Constrained least-squares estimation in Python

I'm trying to perform a constrained least-squares estimation using Scipy such that all of the coefficients are in the range (0,1) and sum to 1 (this functionality is implemented in Matlab's LSQLIN function). Does anybody have tips for setting up…
Nick
  • 655
  • 1
  • 5
  • 16
15
votes
2 answers

R draw (abline + lm) line-of-best-fit through arbitrary point

I am trying to draw a least squares regression line using abline(lm(...)) that is also forced to pass through a particular point. I see this question is related, but not quite what I want. Here's an example: test <- structure(list(x = c(0, 9, 27,…
thelatemail
  • 91,185
  • 12
  • 128
  • 188
14
votes
4 answers

Orthogonal regression fitting in scipy least squares method

The leastsq method in scipy lib fits a curve to some data. And this method implies that in this data Y values depends on some X argument. And calculates the minimal distance between curve and the data point in the Y axis (dy) But what if I need to…
Vladimir
  • 601
  • 2
  • 7
  • 13
14
votes
1 answer

Why Direct Linear Transformation (DLT) cannot give the optimal camera extrinsics?

I'm reading the source code of function solvePnP() in OpenCV, when the flags param uses default value SOLVEPNP_ITERATIVE, it's calling cvFindExtrinsicCameraParams2, in which it FIRST uses the DLT algorithm (if we have a non-planar set of 3D points)…
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
14
votes
4 answers

LASSO with $\lambda = 0$ and OLS produce different results in R glmnet

I expect LASSO with no penalization ($\lambda=0$) to yield the same (or very similar) coefficient estimates as an OLS fit. However, I get different coefficient estimates in R putting the same data (x,y) into glmnet(x, y , alpha=1, lambda=0) for…
Helpa
1
2 3
67 68