Least Squares Polynomial Fit

Does anyone know of an AppleScript for doing a least squares polynomial fit to a list of equispaced data?

Hello Adam.

No. (And it depends on what you are going to use it for, maybe chi-squared will help you out if you are doing statistics.

On the other hand, if you are going to get coordinates for smooth curves, then you better look at splines and B-Splines of various classes (C1-C3). The clue is I think, is that you must have polynomials to derivate (dfferentiate).

My old friend La Grange, once made up some formulas for expressing segments between points as polynomials, those can be found in Sedgewick: Algorithms. I made that work pretty well in Java a long time ago, should you be interested. But you’ll have to convert it to AppleScript yourself.

But: He describes a method of least squares, pretty adaptible to AppleScript in Algorithms Second Editon pages 551-553.

The algorithms, is described in a Pascal-like syntax, and may help you out.

Thanks, McUsrII. I was just looking for an easy way out before I undertook one myself. No point in reinventing the wheel.

In that case, I’d undertake some serious googling. :slight_smile:

I am sure somebody somewhere has converted such algorithms to Applescript.


Actually, Satimage has the polynomial fit function, as demonstrated here.

You have interesting problems Adam. :slight_smile:

Thanks, MacusrII, that looks like exactly what I need – all I’ll have to do is massage my data into the form the OSAX needs to fit a polynomial having first extracted it all from the text list it’s in. Great stuff.

Just to sate your inevitable curiosity, it’s a long list of values of a stock portfolio recorded daily over several years that is acquired and stored in a text document by another AppleScript. The daily stock values are found by an app called Soulver which is not scriptable, so I read and parse the XML in which Soulver saves the daily values given number of shares owned, add them for a total value, and store them as text with acquisition dates. Now I want to study the trends having first decided what to do about weekends and what the X values will be so I can get back to dates. I realize that there is probably software to do this, but I want to massage it myself.

Hello Adam.

I am glad you found a way to do that. I was looking for a way to graph datasets and tried with Grapher that ships with OS X. Unfortunately, there isn’t any sound way to script it. Then I discovered plot, which I at least can “send” Datasets to plot, by filling out a propertylist file.

Hopefully plot gets to be updated soon, it is quite of age.

Just mentioning this, as an alternative, I think you can use plot too for doing your own regression analysis. :slight_smile: