Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Curve fitting to data

Reply
Thread Tools

Curve fitting to data

 
 
Phil Rhoades
Guest
Posts: n/a
 
      12-16-2007
People,

Does anyone know of a Ruby app that will fit a curve to data eg fitting
a curve to:

-10 0
-9 19
-8 36
-7 51
-6 64
-5 75
-4 84
-3 91
-2 96
-1 99
0 100
1 99
2 96
3 91
4 84
5 75
6 64
7 51
8 36
9 19
10 0

Should give the formula for a parabola.

Thanks,

Phil.

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Fax: +610)2-8221-9599
E-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Thomas Adam
Guest
Posts: n/a
 
      12-16-2007
On 16/12/2007, Phil Rhoades <(E-Mail Removed)> wrote:
> People,
>
> Does anyone know of a Ruby app that will fit a curve to data eg fitting
> a curve to:
>
> -10 0
> -9 19


This looks like a prime candidate for GNUPlot -- which ruby has bindings for.

-- Thomas Adam

 
Reply With Quote
 
 
 
 
Phil Rhoades
Guest
Posts: n/a
 
      12-17-2007
Thomas,


On Sun, 2007-12-16 at 23:55 +0000, Thomas Adam wrote:
> On 16/12/2007, Phil Rhoades <(E-Mail Removed)> wrote:
> > People,
> >
> > Does anyone know of a Ruby app that will fit a curve to data eg fitting
> > a curve to:
> >
> > -10 0
> > -9 19

>
> This looks like a prime candidate for GNUPlot -- which ruby has bindings for.



I think GNUPlot requires the knowledge of the type of fn you are trying
to fit - I want the software to TELL me what sort of fn it is eg for the
data:

-10 0
-9 19
-8 36
-7 51
-6 64
-5 75
-4 84
-3 91
-2 96
-1 99
0 100
1 99
2 96
3 91
4 84
5 75
6 64
7 51
8 36
9 19
10 0

http://www.zunzun.com tells me that the formula for this data is:

y = a( atan(x) ) + b( x2 ) + c( sinh(x) ) + offset

I would like to be able to do this myself with my own (preferably Ruby)
code.

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Fax: +610)2-8221-9599
E-mail: (E-Mail Removed)


 
Reply With Quote
 
Marc Heiler
Guest
Posts: n/a
 
      12-17-2007
Whoa, that looks interesting... cant say I know anything to help you,
but I will "bookmark" this thread to see if someone interesting emerges
here
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Alex Fenton
Guest
Posts: n/a
 
      12-17-2007
Phil Rhoades wrote:

> On Sun, 2007-12-16 at 23:55 +0000, Thomas Adam wrote:
>> On 16/12/2007, Phil Rhoades <(E-Mail Removed)> wrote:
>>> People,
>>>
>>> Does anyone know of a Ruby app that will fit a curve to data eg fitting
>>> a curve to:
>>>
>>> -10 0
>>> -9 19

>> This looks like a prime candidate for GNUPlot -- which ruby has bindings for.

>
>
> I think GNUPlot requires the knowledge of the type of fn you are trying
> to fit - I want the software to TELL me what sort of fn it is eg for the
> data:


You can access the "R" statistical package via Ruby, which seems to have
curve fitting capabilities. But this would involve learning R, which
I've found to have a steeper curve than I cared to climb.

More generally, I think for most scientific purposes, it's a good idea
to have an idea of the type of curve (power, quadratic, cubic etc) that
might underlie the observed data. Most applications enforce this. If you
know the general form of the equation that's being fitted ( eg ax^2 + bx
+ c for a quadratic), it would be possible to get estimates for a, b and
c by using eg ordinary least squares to find a solution that minimises
the difference between observed and predicted values. How you get to
that with acceptable time is an algorithmic question...

But nothing I know of exactly in Ruby. You might take a look at the
source code for the website you linked to:
http://pythonequations.cvs.sourcefor....2&view=markup

Maybe a fun one for a Ruby Quiz sometime?

alex
 
Reply With Quote
 
M. Edward (Ed) Borasky
Guest
Posts: n/a
 
      12-17-2007
Alex Fenton wrote:
> Phil Rhoades wrote:
>
>> On Sun, 2007-12-16 at 23:55 +0000, Thomas Adam wrote:
>>> On 16/12/2007, Phil Rhoades <(E-Mail Removed)> wrote:
>>>> People,
>>>>
>>>> Does anyone know of a Ruby app that will fit a curve to data eg fitting
>>>> a curve to:
>>>>
>>>> -10 0
>>>> -9 19
>>> This looks like a prime candidate for GNUPlot -- which ruby has
>>> bindings for.

>>
>>
>> I think GNUPlot requires the knowledge of the type of fn you are trying
>> to fit - I want the software to TELL me what sort of fn it is eg for the
>> data:

>
> You can access the "R" statistical package via Ruby, which seems to have
> curve fitting capabilities. But this would involve learning R, which
> I've found to have a steeper curve than I cared to climb.
>
> More generally, I think for most scientific purposes, it's a good idea
> to have an idea of the type of curve (power, quadratic, cubic etc) that
> might underlie the observed data. Most applications enforce this. If you
> know the general form of the equation that's being fitted ( eg ax^2 + bx
> + c for a quadratic), it would be possible to get estimates for a, b and
> c by using eg ordinary least squares to find a solution that minimises
> the difference between observed and predicted values. How you get to
> that with acceptable time is an algorithmic question...
>
> But nothing I know of exactly in Ruby. You might take a look at the
> source code for the website you linked to:
> http://pythonequations.cvs.sourcefor....2&view=markup
>
>
> Maybe a fun one for a Ruby Quiz sometime?
>
> alex


Well ... let's see:

1. There's no such thing as a "universal curve fitting algorithm", in
the sense that you give it a set of points and it spits out a formula.
The main reason is that given any *finite* set of points, there are an
*infinite* number of possible curves that can be drawn through them
exactly. Some of these are fairly well behaved outside the range of the
input points, and some of them exhibit bizarre behavior.

2. There's no such thing as a curve-fitting problem in a total vacuum,
without any context whatsoever. In other words, a request to fit a curve
to a set of points is meaningless without knowledge of how you will use
that fit.

3. Many of the "naive" techniques, like polynomial regression, behave
very badly. By "behave very badly", I mean "much worse than plotting the
points in Excel and making a wild guess."

Given all that, I don't see much hope of finding or writing a Ruby
program to do this. Conversely, if the original poster has a model that
the points should have come from, based on some theory, writing Ruby
code (or R or Python code) to fit the data to the model is easy. Just
about every reasonable fitting algorithm is already coded in R, so if
you just want an answer without all that pesky learning stuff, it's
probably easier to do it in R.





 
Reply With Quote
 
Phil Rhoades
Guest
Posts: n/a
 
      12-17-2007
Alex,


On Mon, 2007-12-17 at 11:40 +0900, Alex Fenton wrote:
> Phil Rhoades wrote:
>
> > On Sun, 2007-12-16 at 23:55 +0000, Thomas Adam wrote:
> >> On 16/12/2007, Phil Rhoades <(E-Mail Removed)> wrote:
> >>> People,
> >>>
> >>> Does anyone know of a Ruby app that will fit a curve to data eg fitting
> >>> a curve to:
> >>>
> >>> -10 0
> >>> -9 19
> >> This looks like a prime candidate for GNUPlot -- which ruby has bindings for.

> >
> >
> > I think GNUPlot requires the knowledge of the type of fn you are trying
> > to fit - I want the software to TELL me what sort of fn it is eg for the
> > data:

>
> You can access the "R" statistical package via Ruby, which seems to have
> curve fitting capabilities. But this would involve learning R, which
> I've found to have a steeper curve than I cared to climb.
>
> More generally, I think for most scientific purposes, it's a good idea
> to have an idea of the type of curve (power, quadratic, cubic etc) that
> might underlie the observed data. Most applications enforce this. If you
> know the general form of the equation that's being fitted ( eg ax^2 + bx
> + c for a quadratic), it would be possible to get estimates for a, b and
> c by using eg ordinary least squares to find a solution that minimises
> the difference between observed and predicted values. How you get to
> that with acceptable time is an algorithmic question...
>
> But nothing I know of exactly in Ruby. You might take a look at the
> source code for the website you linked to:
> http://pythonequations.cvs.sourcefor....2&view=markup
>
> Maybe a fun one for a Ruby Quiz sometime?



I installed grace and that does pretty much what you said, which is not
quite what I want but interesting . .

Yeh, I did have a look at the PythonEquations stuff but it looks too
tough for me to translate/make use of - I would certainly be happy if
someone wanted to make it a Ruby Quiz!

Regards,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Fax: +610)2-8221-9599
E-mail: (E-Mail Removed)


 
Reply With Quote
 
jwmerrill@gmail.com
Guest
Posts: n/a
 
      12-17-2007
On Dec 16, 7:07 pm, Phil Rhoades <(E-Mail Removed)> wrote:

> I think GNUPlot requires the knowledge of the type of fn you are trying
> to fit - I want the software to TELL me what sort of fn it is eg for the
> data:
>
> -10 0
> -9 19
> -8 36
> -7 51
> -6 64
> -5 75
> -4 84
> -3 91
> -2 96
> -1 99
> 0 100
> 1 99
> 2 96
> 3 91
> 4 84
> 5 75
> 6 64
> 7 51
> 8 36
> 9 19
> 10 0
>
> http://www.zunzun.comtells me that the formula for this data is:
>
> y = a( atan(x) ) + b( x2 ) + c( sinh(x) ) + offset
>
> I would like to be able to do this myself with my own (preferably Ruby)
> code.
>
> Thanks,
>
> Phil.


Isn't the result zunzun spit out "wrong"? The data is most simply
described as an inverted parabola:

y = a x^2 + c

This points exactly to the problem others have mentioned. Given a
large enough space of functions, you can fit practically anything, but
what does it mean? Generally speaking, no one has any business
fitting 21 data points with 4 parameters.

JM
 
Reply With Quote
 
Phil Rhoades
Guest
Posts: n/a
 
      12-17-2007
JM,


On Mon, 2007-12-17 at 15:10 +0900, (E-Mail Removed) wrote:
> On Dec 16, 7:07 pm, Phil Rhoades <(E-Mail Removed)> wrote:
>
> > I think GNUPlot requires the knowledge of the type of fn you are trying
> > to fit - I want the software to TELL me what sort of fn it is eg for the
> > data:
> >
> > -10 0
> > -9 19
> > -8 36
> > -7 51
> > -6 64
> > -5 75
> > -4 84
> > -3 91
> > -2 96
> > -1 99
> > 0 100
> > 1 99
> > 2 96
> > 3 91
> > 4 84
> > 5 75
> > 6 64
> > 7 51
> > 8 36
> > 9 19
> > 10 0
> >
> > http://www.zunzun.comtells me that the formula for this data is:
> >
> > y = a( atan(x) ) + b( x2 ) + c( sinh(x) ) + offset
> >
> > I would like to be able to do this myself with my own (preferably Ruby)
> > code.
> >
> > Thanks,
> >
> > Phil.

>
> Isn't the result zunzun spit out "wrong"? The data is most simply
> described as an inverted parabola:
>
> y = a x^2 + c
>
> This points exactly to the problem others have mentioned. Given a
> large enough space of functions, you can fit practically anything, but
> what does it mean? Generally speaking, no one has any business
> fitting 21 data points with 4 parameters.



Sorry, my cut and paste had a typo - I left out the "^", the formula
should have been:

y = a( atan(x) ) + b( x^2 ) + c( sinh(x) ) + offset

which is closer to the parabola (and the zunzun display on the screen
seemed perfect) but yes, you are correct.

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Fax: +610)2-8221-9599
E-mail: (E-Mail Removed)


 
Reply With Quote
 
Axel Etzold
Guest
Posts: n/a
 
      12-17-2007

-------- Original-Nachricht --------
> Datum: Mon, 17 Dec 2007 15:31:03 +0900
> Von: Phil Rhoades <(E-Mail Removed)>
> An: (E-Mail Removed)
> Betreff: Re: Curve fitting to data


> JM,
>
>
> On Mon, 2007-12-17 at 15:10 +0900, (E-Mail Removed) wrote:
> > On Dec 16, 7:07 pm, Phil Rhoades <(E-Mail Removed)> wrote:
> >
> > > I think GNUPlot requires the knowledge of the type of fn you are

> trying
> > > to fit - I want the software to TELL me what sort of fn it is eg for

> the
> > > data:
> > >
> > > -10 0
> > > -9 19
> > > -8 36
> > > -7 51
> > > -6 64
> > > -5 75
> > > -4 84
> > > -3 91
> > > -2 96
> > > -1 99
> > > 0 100
> > > 1 99
> > > 2 96
> > > 3 91
> > > 4 84
> > > 5 75
> > > 6 64
> > > 7 51
> > > 8 36
> > > 9 19
> > > 10 0
> > >
> > > http://www.zunzun.comtells me that the formula for this data is:
> > >
> > > y = a( atan(x) ) + b( x2 ) + c( sinh(x) ) + offset
> > >
> > > I would like to be able to do this myself with my own (preferably

> Ruby)
> > > code.
> > >
> > > Thanks,
> > >
> > > Phil.

> >
> > Isn't the result zunzun spit out "wrong"? The data is most simply
> > described as an inverted parabola:
> >
> > y = a x^2 + c
> >
> > This points exactly to the problem others have mentioned. Given a
> > large enough space of functions, you can fit practically anything, but
> > what does it mean? Generally speaking, no one has any business
> > fitting 21 data points with 4 parameters.


Dear Phil,

it is of course preferable to have some idea about the underlying
relationship between data graphed, such as

y = a*x^2 + b*x + c,

and then fit that model (this can be done by solving a linear
equation,

Matrix([x_0^2,x_0,1],...,[x_n^2,x_n,1])*([a,b,c]^transpose)=[y_0,..,y_n]^transpose

(numbering data points as ((x_0,y_0),....(x_n,y_)) and
[..] indicating rows in the matrix or row vectors)),

as this is a linear equation in the parameters a,b,c .
You can do that with any software that solves linear or matrix
equations, i.e., rsruby or rb-gsl .

It is of course also true that one can basically draw arbitrary
curves to connect data points, if you don't know that a model
like the above is "true".

Now, one additional line of thought is pursued in the discipline
of "approximation theory" (see eg., Wikipedia, or for a deeper
insight, http://books.google.de/books?id=ODZ1...hl=de#PPA93,M1).

Here, one starts with a points, as yours, and asks,

Given a distance measure between the data and the curve ("norm") and a set of admissible model curves (e.g., all continuous curves on an interval),
which curves will minimize that norm ?

There are indeed some results available, such as Chebyshev or Remez(Remes) approximation procedures.

This kind of procedure can be recommended when the functional relationship of your data is rather complicated/not enormously interesting/you distrust
simple models, you know something about the general wiggliness of the underlying curve (see the Jackson theorems in Powell's book), and you need to have information about what you would have measured at some point you didn't actually measure and the result should be not too far off ...


Best regards,

Axel


--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
native ruby Levenberg–Marquardt (or other curve fitting) Fearless Fool Ruby 2 05-05-2011 11:20 PM
Quadratic curve fitting! Vinodh Kumar C++ 6 12-01-2010 12:30 PM
curve and curve with error objects : sorting , friend Hicham Mouline C++ 2 09-14-2009 02:11 PM
Fitting lots of fields on one web page =?Utf-8?B?R2VvZmYgQyBNZWxib3VybmU=?= ASP .Net 3 05-30-2005 03:54 PM
Curve fitting Tom Anderson Python 2 05-08-2005 10:24 PM



Advertisments