Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to plot 2 fitting lines in 1 dataset?
Date: Sun, 7 Jun 2009 13:01:02 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 71
Message-ID: <h0gdme$lhi$1@fred.mathworks.com>
References: <h0bk5u$cha$1@fred.mathworks.com> <h0buhq$832$1@fred.mathworks.com> <h0epld$lkf$1@fred.mathworks.com> <h0gbu5$ssj$1@fred.mathworks.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1244379662 22066 172.30.248.35 (7 Jun 2009 13:01:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 7 Jun 2009 13:01:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:545323


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0gbu5$ssj$1@fred.mathworks.com>...
> "Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0epld$lkf$1@fred.mathworks.com>...
> > Hi Lorenzo,
> > What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.  
> 
> Do you know where the break occurs? 
> 
> If you do, then split this into two problems. In
> fact they can be solved in one estimation step.
> 
> If not then you need to use a tool that can find the
> location of the break. This is sometimes known as a
> free knot spline, here a piecewise linear spline. Either
> problem can be solved easily enough, but the solution
> depends on your answer.
> 
> So, do you know the location of the break or must
> it be estimated?
> 
> John

Let me be more explicit, since I know you will ask
for more information.

x = rand(100,1);
y = (1 + x).*(x<0.4) + (-2*x + 2.2).*(x>=0.4) + randn(size(x))/20;
plot(x,y,'o')

Here, you and I know the break occurs at 0.4.
Can we estimate the coefficients of the lines?

IFF we knew the break location, then we would do this:

k = (x <= 0.4); % A logical varable
coef = [k,x.*k,~k,x.*~k]\y

coef = [k,x.*k,~k,x.*~k]\y
coef =
         0.981257860141531
          1.07669216114181
          2.20227048054644
         -2.01520597681586

With no error, the coefficients should have been
[1; 1; 2.2; -2], so we did rather well here.

Can I do as well if I do not assume the location of
the break is known? In fact, I cannot do quite as
well in general, since we must estimate an extra
parameter. We can do well enough though.

coefest = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]\y;
preds = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]*coefest(B);

BreakPoint = fminbnd(@(B) sum((y - preds(B)).^2),0,1)
BreakPoint =
         0.397098484667164

coefest(BreakPoint)
ans =
          0.98340320488404
          1.06085977689174
          2.20816686087013
          -2.0226000312523

See that I've used the limits of the data itself to
define the range over which fminbnd will be allowed
to search for the break point.

HTH,
John