Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Polynomial Trendline / Extrapolation

by lwicks (Friar)
on May 29, 2008 at 16:34 UTC ( [id://689048]=perlquestion: print w/replies, xml ) Need Help??

lwicks has asked for the wisdom of the Perl Monks concerning the following question:

Hello Dear Monks,
I have been cobbling together some Perl that reads in disk use stats from a CSV file and generates charts using GD::Graph. Works great and I created a linear extrapolation too. And then the boss asked for a chart like the polynomial trend line in Excel! :-(

I have searched and searched and not found a CPAN module or similar. Can my fellow monks succeed where my Googling has failed?

Basically, is there a way I can take a simple array of numbers and then extrapolate from the last one by say 6 months. (The data is per month you see).

Something like this pseudo-code:

@monthly_data = ( 2,3,2,4,3,5,4,6,5,7,6,8,7,9,8 ); @next_six = extrapolate(@monthly_data, "polynomial");

Or something like that, @next_six would end up equalling something like ( 10,11,13,15,18,20 ) <- Not real results!

I am sure if I actually understood the math behind this I would A)find it easy and B)know better than to extrapolate like this.

Any suggestions on how to proceed would be much appreciated.

Lance

Kia Kaha, Kia Toa, Kia Manawanui!
Be Strong, Be Brave, Be perservering!

Replies are listed 'Best First'.
Re: Polynomial Trendline / Extrapolation
by dwm042 (Priest) on May 29, 2008 at 17:17 UTC
    Just as a rule of thumb, extrapolating the results of a polynomial fit (of higher order than a quadratic) much further than the "delta x" of one data point beyond your data is not a good idea. That means if your data are spaced a month apart, you don't want to extrapolate more than a month past your data set. The reason is that a polynomial of the form:
    y = ax + bx**2 + cx**3 + dx**4 and so on
    will be dominated by the high order terms outside the range of your data, and rapidly go to infinity (positive or negative). These kinds of functions suck at extrapolation.

    There are other functions that are better suited to extrapolation. Without knowing how your data are *supposed* to behave, I'd be hesitant to recommend any offhand.

    Update: typo
      I agree completely, after Googling extensively and spending too much time on Mathematics sites trying to get my head around it I came away with the general wisdom that extrapolating this way is bad.
      But it makes the pretty trendlines desired by "TheBoss" .

      At present my math-fu is too weak to solve it, so I suspect manually making charts in Excel will be the solution. :-(

      Kia Kaha, Kia Toa, Kia Manawanui!
      Be Strong, Be Brave, Be perservering!

      Hi, Can you suggest some better fuctions for polynomial trendline and extrapolation of a stock price data ? Regards, CH
        Here's a discovery i made about polynomial extrapolation (it probably can be derived from Newton's series but i haven't done it nor have i seen it done).

        First, some preliminaries: 3 points can be fit by a unique quadratic polynomial, i.e., parabola. 4 points can be fit by a unique cubic polynomial. n points can be fit by a unique (n+1)th degree polynomial.

        Suppose we have the 3 points x(0)==1, x(1)==4, x(2)==9. These are obviously fit by the quadratic y(x)=x**2. Now the question is: how do you extrapolate x(3)?

        Answer: Form the binomial expansion of (a-b)**3=0 and note the coefficients: Notice that I expand a CUBIC. For a cubic I would expand (a-b)**4, etc

        a**3-3(a**2)b+3ab**2-b**3=0

        The coefficients are 1, -3, +3, -1

        The discovery I made is that these terms correspond in order to the coefficients of x(3), x(2), x(1), and x(0).
        We are trying to determine x(3).

        We then have: x(3) = 3x(2) -3x(1) +x(0), or: x(3) = 3(9)-3(4)+1 = 16

        This method extrapolates the next term of any polynomial where the x intervals are the same size.
Re: Polynomial Trendline / Extrapolation
by FunkyMonk (Chancellor) on May 29, 2008 at 16:44 UTC
    There's a polyfit function in PDL::Slatec that does polynomial regression.

    I've never used PDL, but it looks promising.


    Unless I state otherwise, all my code runs with strict and warnings
Re: Polynomial Trendline / Extrapolation
by pc88mxer (Vicar) on May 29, 2008 at 17:13 UTC
    When using Excel do you have to specify the degree of the polynomial?

    Have a look at the module Algorithm::CurveFit - it'll fit your curve to whatever kind of expression you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://689048]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-25 01:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found