Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


I'm performing research that will help determine the size of observed space and the time elapsed since the big bang. Hopefully you can help!

I have data conforming to a piecewise linear function on which I want to perform two linear regressions. There is a point at which the slope and intercept change, and I need to (write a program to) find this point.


share|cite|improve this question
see cross posted question:… – Jeromy Anglim Dec 23 '10 at 12:22

A simple answer would be to consider the break point a variable, call it x. Collect the points below x and perform a linear fit, returning the error (say sum of squared errors over the points). Collect the points above x and do the same. Then you can define f(x) as the total error over the two fits. Consider this a function in one variable and minimize it.

share|cite|improve this answer
The point is how to find the optimal break point. – Studnet T Oct 30 '15 at 6:14
@StudnetT: that is exactly what I was describing. Julián Aguirre gave a process to make sure it is continuous, which I did not consider. In my case, the abscissa of the break point is the independent variable and the error of the fit is the result. We then minimize the error as a function of the breakpoint. – Ross Millikan Oct 30 '15 at 8:39

I am assuming that you want a continuous piecewise linear fit of the form $$ a(x-\xi)+c\hbox{ if }x\le\xi,\quad b(x-\xi)+c\hbox{ if }x>\xi. $$ There are 4 variables to determine: the slopes $a$, $b$, the point $\xi$ and $c$, the ordinate at $\xi$. Let $\{(x_i,y_i)\}$, $1\le i\le N$, be your data. For simplicity I will assume that $x_i<x_{i+1}$. For each $i=1,\dots,N-1$ minimize the expression $$ O_i=\sum_{j=1}^i(y_j-a(x_j-\xi)-c)^2+\sum_{j=i+1}^N(y_j-b(x_j-\xi)-c)^2. $$ This can be done by solving the system of 4 equations with 4 unknowns $$ \frac{\partial O_i}{\partial a}=\frac{\partial O_i}{\partial b}=\frac{\partial O_i}{\partial c}=\frac{\partial O_i}{\partial \xi}=0. $$ (This will be nonlinear, and will have more than one solution)

Finally, choose the index $i$ that gives the minimum $O_i$.

I tried this in a mock example, and it came out quite right.

share|cite|improve this answer

(This was supposed to be a comment, but it got too long.)

The problem of piecewise linear regression has been looked into many times before; I do not currently have access to these papers (and thus cannot say more about them), but you might want to look into these:

This paper (published in a physiology journal, of all places) discusses how to fit a piecewise linear function to certain data sets encountered in neurological research. A FORTRAN routine is included.

This paper also includes a FORTRAN routine for fitting piecewise linear functions to data.

This paper relies on maximum likelihood to find the best fit piecewise linear function.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.