help-octave
[Top][All Lists]
Advanced

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

Re: Moving polynomial fit


From: macy
Subject: Re: Moving polynomial fit
Date: Mon, 7 Sep 2009 08:00:40 -0700 (PDT)
User-agent: SquirrelMail/1.4.13

NOW I REMEMBER!

I had to rewrite the function polyval into a custom type function so that
I could use c variable as an array, not just a vector.

Then, just write the whole for-loop as an array sequence.  As I said I
went from 20 minutes down to 12 seconds for mine.

Robert

> 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
>
>




reply via email to

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