Path: news.mathworks.com!not-for-mail
From: "Richard Willey" <rwilley@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Partial curve fitting
Date: Tue, 26 May 2009 08:42:28 -0400
Organization: The MathWorks, Inc.
Lines: 130
Message-ID: <gvgo3k$6kg$1@fred.mathworks.com>
References: <gvcl7u$p85$1@fred.mathworks.com> <gvdkj1$gsv$1@fred.mathworks.com>
Reply-To: "Richard Willey" <rwilley@mathworks.com>
NNTP-Posting-Host: willeyr.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1243341748 6800 172.31.57.162 (26 May 2009 12:42:28 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 26 May 2009 12:42:28 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:542580


>It might be a bit stupid question, but I've started to use curve fitting 
>toolbox, and
>I wonder if there is a way to fit a specially formed curve to only a part 
>of the data points,
>without knowing beforehand wich part to fit.

Hi Grygory

From the sounds of things, you're interested in a change point detection 
algorithm.  (You need an algorithm that is able to detect the most likely 
point in time at which your data set is flipping from model 1 to model 2).

Unfortunately, neither Curve Fitting Toolbox nor Statistics Toolbox have any 
change point detection algorithms available by default.  With this said and 
done, MATLAB is a really great platform for implementing change point 
detection algorithms.

Here's a few pointers that you might find useful:

If you google change point + MATLAB + Bayesian you can find a lot of useful 
citations.

I personally found "Bayesian Econometric Methods" by Koop, Poirier, and 
Tobias pretty easy to follow.  The "Computational Statistics Handbook with 
MATLAB" by Martinez and Martinez is another useful source with pretty 
accessible source code.

The CUSUM algorithm is an oldie but goodie...  Base MATLAB includes an 
algorithm called CUMSUM which can be easily modified for this type of work. 
However, you might find the follow example easier to understand

%% Random data



clear all

clc



% Create A



A = randn(100,1);

A = A+100;



B = randn(100,1);

B = B + 101;



data = [A;B];



% Calculate the Cumulate Sum



%Calculate the average

 mu = mean(data);



% Initialize the cumulative sum

CUSUM = zeros(length(data)+1,1);



CUSUM(1) = 0 + data(1) - mu;



%CUMSUM Loop



for i = 2:length(data)

    CUSUM(i) = CUSUM(i-1) + (data(i) - mu);

end



plot(CUSUM)



%  Find the inflection point



X = linspace(1,length(CUSUM),length(CUSUM));

X = X';

Y = CUSUM;

cfun = fit(X,Y,'poly2');

upordown = differentiate(cfun,1);



if upordown < 0, index = find(CUSUM == min(CUSUM))

else

    index = find(CUSUM == max(CUSUM));

end;



index