[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Moving polynomial fit
From: |
babelproofreader |
Subject: |
Re: Moving polynomial fit |
Date: |
Mon, 7 Sep 2009 07:27:55 -0700 (PDT) |
On Sep 5, 2009, at 11:09 AM, babelproofreader wrote:
> I would like to write a script/function for use on a time series
> that will
> least squares fit a polynomial of a given degree to a moving window
> across
> the time series in the same way as a moving average computes an
> average of a
> moving window.....
There might be a better way ... Assuming you'd like to center your
window about the data point of interest ...
---------------- begin: mwpolysmooth.m ---------------
function ym = mwpolysmooth (x, y, order, window)
% USAGE: ym = mwpolysmooth (x, y, order, window)
xc = num2cell(x);
idx = @(xc) find (abs (x - xc) < 0.5*window);
n = cellfun (idx, xc, 'uniformoutput', false);
mpfit = @(n) polyfit (x(n), y(n), order);
pm = cellfun (mpfit, n, 'uniformoutput', false);
nc = num2cell (1:numel(x));
mpval = @(n) polyval (pm{n}, x(n));
ym = cell2mat (cellfun (mpval, nc, 'uniformoutput', false));
end
---------------- end: mwpolysmooth.m ---------------
Ben,
Thanks for your function - I've learned some useful coding from it - but I'm
not sure if it really address my problem. I wish to fit real time streaming
data (market prices), but as I understand it your code "peaks into the
future" to smooth over the window i.e. the fitted value for the most recent
streaming data "n" is different from the value that will be calculated when
this data value is (n - some_value) in the past, hence invalidating any
historical testing over past prices. The simple for loop below is an example
of what I want to do, but it is slow.
x=(1:length(price))';
polysmooth=price;
for i=5:length(price)
y=price(i-4:i,1);
a=polyfit(x,y,2);
polysmooth(i,1)=polyval(a,5);
endfor
--
View this message in context:
http://www.nabble.com/Moving-polynomial-fit-tp25309400p25331404.html
Sent from the Octave - General mailing list archive at Nabble.com.