# Curve fitting to data

**URL:** <https://www.ruby-forum.com/t/curve-fitting-to-data/123356>\
**Category:** Ruby\
**Created:** [December 17, 2007, 12:43am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356 "2007-12-17T00:43:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 12:43am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/1 "2007-12-17T00:43:38Z")

</div>

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

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Fax: +61:(0)2-8221-9599  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 12:57am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/2 "2007-12-17T00:57:06Z")

</div>

On 16/12/2007, Phil R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 A.

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 1:08am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/3 "2007-12-17T01:08:14Z")

</div>

Thomas,

On Sun, 2007-12-16 at 23:55 +0000, Thomas A. wrote:

> On 16/12/2007, Phil R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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 R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Fax: +61:(0)2-8221-9599  
E-mail: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 2:31am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/4 "2007-12-17T02:31:24Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 4:48am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/5 "2007-12-17T04:48:03Z")

</div>

Alex F. wrote:

> > > > -9 19  
> > > > I’ve found to have a steeper curve than I cared to climb.  
> > > > 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.sourceforge.net/pythonequations/PythonEquations/DESolver/DESolver.py?revision=1.2&view=markup](http://pythonequations.cvs.sourceforge.net/pythonequations/PythonEquations/DESolver/DESolver.py?revision=1.2&view=markup)
> 
> Maybe a fun one for a Ruby Q. 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.

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 3:41am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/6 "2007-12-17T03:41:24Z")

</div>

Phil R. 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:

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:

> **[Python Equations](https://sourceforge.net/projects/pythonequations/)**
>
> Download Python Equations for free. Moved to Google Code, see http://code.google.com/p/pythonequations/downloads/list

Maybe a fun one for a Ruby Q. sometime?

alex

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 7:17am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/7 "2007-12-17T07:17:39Z")

</div>

On Dec 16, 7:07 pm, Phil R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -4 84  
> 7 51
> 
> 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

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 4:51am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/8 "2007-12-17T04:51:00Z")

</div>

Alex,

On Mon, 2007-12-17 at 11:40 +0900, Alex F. wrote:

> > > > -9 19
> 
> source code for the website you linked to:  
> [http://pythonequations.cvs.sourceforge.net/pythonequations/PythonEquations/DESolver/DESolver.py?revision=1.2&view=markup](http://pythonequations.cvs.sourceforge.net/pythonequations/PythonEquations/DESolver/DESolver.py?revision=1.2&view=markup)
> 
> Maybe a fun one for a Ruby Q. 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 Q.!

Regards,

## Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Fax: +61:(0)2-8221-9599  
E-mail: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/9 "2007-12-17T15:57:33Z")

</div>

-------- Original-Nachricht --------

> Datum: Mon, 17 Dec 2007 15:31:03 +0900  
> Von: Phil R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Betreff: Re: Curve fitting to data

> > > data:  
> > > -1 99  
> > > 10 0
> > 
> > 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,  
[Approximation Theory and Methods - M. J. D. Powell - Google Books](http://books.google.de/books?id=ODZ1OYR3w4cC&dq=powell+approximation+theory&pg=PP1&ots=EcBMamoYaB&sig=YUJjf_bhjy65LEFeC963CrjwPV4&prev=http://www.google.de/search?q=powell+approximation+theory&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&sa=X&oi=print&ct=title&cad=one-book-with-thumbnail&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

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 17, 2007, 7:32am UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/10 "2007-12-17T07:32:17Z")

</div>

JM,

On Mon, 2007-12-17 at 15:10 +0900, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > -6 64  
> > 5 75  
> > I would like to be able to do this myself with my own (preferably Ruby)
> 
> 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 R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Fax: +61:(0)2-8221-9599  
E-mail: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 18, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/curve-fitting-to-data/123356/11 "2007-12-18T12:26:26Z")

</div>

A quadratic indeed fits this function very well. The  
[zunzun.com](http://zunzun.com) function finder was run with the option  
to try every possible function, rather than limit  
to simple curves only. This can be done on the site  
in two ways:

1. Use the function finder “Smoothness Control” to  
only allow functions with a few coefficients

2. Use the function finder “Equation Family Inclusion”  
and disallow the polyfunctionals - these generate  
many thousands of basically random functions to test.
