Regression of an absolute value function I have a series of data (21 points), that resemble a lot like an absolute value function (V-shape).
I am trying to find the parameters which approximate my datapoints as best as possible with the help of Matlab. I am only looking for the minimum value of the function (the breakpoint).
My function needs to be of the form : $sign(x-m)*a*(x-m) + b$.
Which means that my  needs to be symmetrical (same $a$ for the left and right arm of the function).
I have already tried a method :


*

*I was making 2 polyfit of 11 data points (each arm of the V-shape + the middle)

*Finding the intersection between the 2 curves

*Mirroring the data points with the y value of the minimum

*Making another 2 polyfit with the full set of 21 datapoints

*Finding the new intersection between the 2 polyfit

*Reflecting again, etc.


This method works well, but it doesn't give me the same result as the formula above.
I have tried multiple ways to implement the function, but nothing seems to work.
 A: Note that $a\cdot\text{sign}(x-m)\cdot (x-m) $ is more often written as $a\cdot \text{abs}(x-m)$ or just $a|x-m|$.
In the case where $m$ is unknown and you want to estimate it, you can use nonlinear least squares on this.
[In Matlab, see lsqcurvefit, for example]
Alternatively, since given a value for $m$ you can write it as a linear regression problem, you can write the problem as partially linear model*, where for example, given some value of $m$ you can estimate $a$ and $b$ by least squares, so you can just optimize the MSE over $m$). Any univariate optimizer should work nicely for that. While $a$ and $b$ can actually be eliminated, you can avoid the effort of doing that: within the function that calculates the sum of squares of residuals for a given $m$, you just compute the least squares fit of $a$ and $b$ in a regression of $y$ on $|x-m|$ and the sum of squares of that fit is the function value.
[In Matlab, see fminsearch as an example of an optimizer, but since it's sum of squares, you should be able to take advantage of lsqnonlin]
* not to be confused with partial least squares which is quite a different thing.
I don't have Matlab at present, but I can illustrate some of these ideas in R easily enough; it's not hard to do the same kind of thing in Matlab.
First I generated some data with $m=11$,$a=0.75$ and $b=5$:
set.seed(329783)
m=11; a=0.75; b=5
x = runif(100,3,21)
y = a*abs(x-m)+b + rnorm(100,0,.3)


a) If you have moderately good guesses at the parameters, this is simple with a nonlinear least squares program:
> Vfit0 = nls(y~a*abs(x-m)+b,start=list(m=10,a=1,b=4))
>  summary(Vfit0)

Formula: y ~ a * abs(x - m) + b

Parameters:
  Estimate Std. Error t value Pr(>|t|)    
m 11.03212    0.03891   283.5   <2e-16 ***
a  0.74747    0.01122    66.6   <2e-16 ***
b  5.01031    0.05792    86.5   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2827 on 97 degrees of freedom

Number of iterations to convergence: 4 
Achieved convergence tolerance: 2.878e-09


If you don't have good guesses at the parameters (or you want to completely automate it), good estimates are easy enough to construct for this problem.
If you subtract off the minimum $y$ (to almost get rid of $b$), the square of $y-\min(y)$ is approximately quadratic in $x$ (it's not all that sensitive to accuracy in $b$). The x-value for the minimum of that quadratic should still be near $m$.
The minimum of a quadratic ($a_2x^2+a_1x+a_0$) will be at $x=-\frac{a_1}{2a_2}$.
So calculating that for a quadratic fit (in $x$) to $y^*=(y-\min(y))^2$, should give a good estimate of $m$. Then given that $m$, a linear regression of $y$ on $|x-m|$ gives a good $a$ and $b$. So those should be nice starting values --
Here's how I did that in R:
qmin = function(v) -v[2]/v[3]/2

y0 = y^2-min(y^2)
m0 = qmin( lm( y0~x+I(x^2) )$coefficients ) # start estimate of m
stcoef = lm(y~abs(x-m0))$coefficients # to get start estimates of a and b

Vfit = nls(y~sign(x-m)*a*(x-m)+b,start=list(m=m0,a=stcoef[2],b=stcoef[1]))

the final fit was the same as above so I won't give that, but the starting values were
         m         a        b
  11.14097 0.7536526 4.984033

which are much closer to the final estimates than my (quite sufficient) original "guess"
