help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Moving polynomial fit


From: Ben Abbott
Subject: Re: Moving polynomial fit
Date: Mon, 07 Sep 2009 11:18:06 -0400


On Sep 7, 2009, at 10:27 AM, babelproofreader wrote:



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.


You can modify the window to represent only past values by ...

  idx = @(xc) find ((xc - x) < window & (xc - x) > 0);

Ben





reply via email to

[Prev in Thread] Current Thread [Next in Thread]