How to use Maxima to do a nonlinear fit?

View: New views
13 Messages — Rating Filter:   Alert me  

How to use Maxima to do a nonlinear fit?

by Robert Gloeckner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

** Reply Requested When Convenient **

Hello,

is there a package/code-snippet for fitting nonlinear functions to datasets?


Thank you for hints/urls ...
Robert

_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: How to use Maxima to do a nonlinear fit?

by Raymond Toy (RT/EUS) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>>>>> "Robert" == Robert Gloeckner <RGloeckner@...> writes:

    Robert> ** Reply Requested When Convenient **
    Robert> Hello,

    Robert> is there a package/code-snippet for fitting nonlinear functions to datasets?

There is the lbfgs package written by Robert Dodier.  Perhaps that
will do what you want.  Type "load(lbfgs);" to load it.

I have a translation of minpack (see netlib) that might be useful, but
it's not integrated with maxima.

Ray
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: How to use Maxima to do a nonlinear fit?

by Mario Rodriguez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


>
> is there a package/code-snippet for fitting nonlinear functions to datasets?
>

Did you try lsquares?

--
Mario Rodriguez Riotorto
www.biomates.net

_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: How to use Maxima to do a nonlinear fit?

by Robert Dodier-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Robert,

> is there a package/code-snippet for fitting nonlinear functions to datasets?

The lbfgs function implements a numerical unconstrained minimization
algorithm. To use lbfgs to fit a function to data, you can let the
figure of merit be the sum of squares for some function with free
parameters. lbfgs differentiates the FOM wrt the free parameters
to get the gradient and then takes steps based on that. (Don't worry,
lbfgs is not gradient descent -- it is a quasi-Newton method.)

Here is an example using lbfgs to fit a function to some data.
By the way, I find that this works OK with Maxima + GCL (e.g.
default Windows installation) but with Maxima + Clisp I get an
error message about attempting to write on a closed stream.
I'll try to figure out the origin of that error.

load (lbfgs);
FOM : '((1/length(X))*sum((F(X[i]) - Y[i])^2, i, 1, length(X)));
X : [1, 2, 3, 4, 5];
Y : [0, 0.5, 1, 1.25, 1.5];
F(x) := A/(1 + exp(-B*(x - C)));
estimates : lbfgs (FOM, '[A, B, C], [1, 1, 1], 1e-4, [1, 3]);
plot2d ([F(x), [discrete, X, Y]], [x, -1, 6]), ''estimates;

In the final line, that is two single quotes (not one double quote).

lbfgs is packaged with Maxima 5.10.0 (not any earlier version).

Hope this helps
Robert Dodier
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: How to use Maxima to do a nonlinear fit?

by Robert Dodier-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11/15/06, Raymond Toy <raymond.toy@...> wrote:

> There is the lbfgs package written by Robert Dodier.  Perhaps that
> will do what you want.  Type "load(lbfgs);" to load it.

Well, to give credit where credit is due, the LBFGS code (Fortran)
was written by Jorge Nocedal and released into the public domain.
I only made a Lisp translation (via f2cl) and made a Maxima interface.

best
Robert
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Antw: Re: How to use Maxima to do a nonlinear fit?

by Robert Gloeckner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

** Reply Requested When Convenient **

>>> Mario Rodriguez <biomates@...> schrieb am Mittwoch, 15. November
2006 um 20:55:

>>
>> is there a package/code-snippet for fitting nonlinear functions to datasets?
>>
>
> Did you try lsquares?

yes. but i did not get very far as you can see here. i think i need to know how to read data from file to matrix. i have seen the post from r. fateman about reading a list via lisp-reader (http://article.gmane.org/gmane.comp.mathematics.maxima.general/11671), but i have to admit, that i do not know how to  use this to generate matrices....

(%i4) m : matrix ( [280, 0, 0, 0.251],  [260, 0, 0, 0.237],  [240, 0, 0, 0.231],  [220, 0, 0, 0.229],  [200, 0, 0, 0.236],  [180, 0, 0, 0.232],  [160, 0, 0, 0.227],  [140, 0, 0, 0.228],  [120, 0, 0, 0.231],  [100, 0, 0, 0.227],  [80, 0, 0, 0.224],  [60, 0, 0, 0.219],  [40, 0, 0, 0.237],  [280, 0, 2, 0.255],  [260, 0, 2, 0.245],  [240, 0, 2, 0.239],  [220, 0, 2, 0.244],  [200, 0, 2, 0.246],  [180, 0, 2, 0.234],  [160, 0, 2, 0.234],  [140, 0, 2, 0.233],  [120, 0, 2, 0.227],  [100, 0, 2, 0.220],  [80, 0, 2, 0.217],  [60, 0, 2, 0.217],  [40, 0, 2, 0.230],  [280, 0, 4, 0.264],  [260, 0, 4, 0.243],  [240, 0, 4, 0.237],  [220, 0, 4, 0.244],  [200, 0, 4, 0.248],  [180, 0, 4, 0.240],  [160, 0, 4, 0.237],  [140, 0, 4, 0.231],  [120, 0, 4, 0.227],  [100, 0, 4, 0.229],  [80, 0, 4, 0.227],  [60, 0, 4, 0.219],  [40, 0, 4, 0.241],  [280, 0, 8, 0.265],  [260, 0, 8, 0.251],  [240, 0, 8, 0.244],  [220, 0, 8, 0.248],  [200, 0, 8, 0.256],  [180, 0, 8, 0.243],  [160, 0, 8, 0.241],  [140, 0, 8, 0!
 .236],  [120, 0, 8, 0.231],  [100, 0, 8, 0.225],  [80, 0, 8, 0.218],  [60, 0, 8, 0.214],  [40, 0, 8, 0.233],  [280, 7, 0, 0.254],  [260, 7, 0, 0.243],  [240, 7, 0, 0.239],  [220, 7, 0, 0.242],  [200, 7, 0, 0.245],  [180, 7, 0, 0.244],  [160, 7, 0, 0.240],  [140, 7, 0, 0.239],  [120, 7, 0, 0.234],  [100, 7, 0, 0.238],  [80, 7, 0, 0.229],  [60, 7, 0, 0.230],  [40, 7, 0, 0.249],  [280, 7, 2, 0.257],  [260, 7, 2, 0.248],  [240, 7, 2, 0.240],  [220, 7, 2, 0.254],  [200, 7, 2, 0.249],  [180, 7, 2, 0.239], [160, 7, 2, 0.237],  [140, 7, 2, 0.229],  [120, 7, 2, 0.224],  [100, 7, 2, 0.224],  [80, 7, 2, 0.222],  [60, 7, 2, 0.231],  [40, 7, 2, 0.250],  [280, 7, 4, 0.268],  [260, 7, 4, 0.252],  [240, 7, 4, 0.246],  [220, 7, 4, 0.250],  [200, 7, 4, 0.256],  [180, 7, 4, 0.244],  [160, 7, 4, 0.243],  [140, 7, 4, 0.239],  [120, 7, 4, 0.231],  [100, 7, 4, 0.224],  [80, 7, 4, 0.217],  [60, 7, 4, 0.211],  [40, 7, 4, 0.226], [280, 7, 8, 0.274],  [260, 7, 8, 0.259],  [240, 7, 8, 0.255],  [220, 7!
 , 8, 0.253],  [200, 7, 8, 0.262],  [180, 7, 8, 0.256],  [160, 7, 8, 0.
251],  [140, 7, 8, 0.247],  [120, 7, 8, 0.237],  [100, 7, 8, 0.232],  [80, 7, 8, 0.225],  [60, 7, 8, 0.225],  [40, 7, 8, 0.239],  [280, 22, 0, 0.271],  [260, 22, 0, 0.256],  [240, 22, 0, 0.256],  [220, 22, 0, 0.255],  [200, 22, 0, 0.257],  [180, 22, 0, 0.247],  [160, 22, 0, 0.240],  [140, 22, 0, 0.239],  [120, 22, 0, 0.232],  [100, 22, 0, 0.230],  [80, 22, 0, 0.228],  [60, 22, 0, 0.231],  [40, 22, 0, 0.257],  [280, 22, 2, 0.278],  [260, 22, 2, 0.263],  [240, 22, 2, 0.257],  [220, 22, 2, 0.257],  [200, 22, 2, 0.260],  [180, 22, 2, 0.254],  [160, 22, 2, 0.248],  [140, 22, 2, 0.242],  [120, 22, 2, 0.230],  [100, 22, 2, 0.224],  [80, 22, 2, 0.216],  [60, 22, 2, 0.213],  [40, 22, 2, 0.237],  [280, 22, 4, 0.279],  [260, 22, 4, 0.267],  [240, 22, 4, 0.260],  [220, 22, 4, 0.262],  [200, 22, 4, 0.268],  [180, 22, 4, 0.261],  [160, 22, 4, 0.255],  [140, 22, 4, 0.247],  [120, 22, 4, 0.238],  [100, 22, 4, 0.230],  [80, 22, 4, 0.224],  [60, 22, 4, 0.217],  [40, 22, 4, 0.235],  [280, 22, !
 8, 0.290],  [260, 22, 8, 0.272],  [240, 22, 8, 0.270],  [220, 22, 8, 0.265],  [200, 22, 8, 0.277],  [180, 22, 8, 0.269],  [160, 22, 8, 0.264],  [140, 22, 8, 0.254],  [120, 22, 8, 0.244],  [100, 22, 8, 0.236],  [80, 22, 8, 0.229],  [60, 22, 8, 0.223],  [40, 22, 8, 0.238])$

Maxima encountered a Lisp error:

 Error in MACSYMA-TOP-LEVEL [or a callee]: MACSYMA-TOP-LEVEL [or a callee] requires less than one hundred fifty-six arguments.

Automatically continuing.
To reenable the Lisp debugger set *debugger-hook* to nil.
(%i5)


Tanks for your answer,
Robert

_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Antw: Re: How to use Maxima to do a nonlinear fit?

by Robert Gloeckner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

** Reply Requested When Convenient **

Hello Robert,

Thanks for your answer.
What is the benefit of lbfgs over lsquares?
Besides this, i do not know (till now) how to read data from a file into maxima-matrix. I saw an earlier post of R. Fateman describing an easy way to read data into a list, but not into a matrix. If put my values into a matrix maxima complains:

> Maxima encountered a Lisp error:
>
> Error in MACSYMA-TOP-LEVEL [or a callee]: MACSYMA-TOP-LEVEL [or a callee] requires less
> than one hundred fifty-six arguments.



Thanks,
Robert

P.S.: I recognized that clisp is very slow compared to sbcl. under windows i estimated a factor of 400 = speed(sbcl) / speed(clisp) for generating & inverting random single-float matrices.

_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: Antw: Re: How to use Maxima to do a nonlinear fit?

by Mario Rodriguez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> yes. but i did not get very far as you can see here. i think i need to know how to read data from file to matrix.

Try the 'numericalio' package, 'read_matrix' is the function you need.

If data.txt is of the form

280 0 0 0.251
260 0 0 0.237
240 0 0 0.231
220 0 0 0.229
....

write:

load(numericalio)$
m: read_matrix("path_to/data.txt");



--
Mario Rodriguez Riotorto
www.biomates.net
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: Antw: Re: How to use Maxima to do a nonlinear fit?

by Robert Gloeckner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

** Reply Requested When Convenient **

Hello Mario,

Mario Rodriguez <biomates@...> schrieb am Donnerstag, 16. November
2006 um 23:10:

> load(numericalio)$
> m: read_matrix("path_to/data.txt");

i did so, too & and now i know that only /single/ white-spaces are allowed as delimiter.

after that i did:  

(%i16) lsquares(m, [T, g, n, l], l = 0.01 * g * (a1 * T + b1)  +  0.01 * n * (a2 * T + b2)  +  (1 - 0.01 * (g + n)) * (a3 * T + b3), [a1, a2, a3, b1, b2, b3]);

but the result was just an expansion:


(%o16) lsquares(matrix([280, 0, 0, 0.251], [260, 0, 0, 0.237],
[240, 0, 0, 0.231], [220, 0, 0, 0.229], [200, 0, 0, 0.236],
...
[60, 22, 8, 0.223], [40, 22, 8, 0.238]), [T, g, n, l],
l = (1 - 0.01 (n + g)) (a3 T + b3) + 0.01 n (a2 T + b2) + 0.01 g (a1 T + b1),
[a1, a2, a3, b1, b2, b3])

are there limits in dimension/amount of data?


Ciao,
Robert



_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: How to use Maxima to do a nonlinear fit?

by Robert Gloeckner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

** Reply Requested When Convenient **

Sorry, sorry, i won't post here anymore tonight.

Not maxima is limited, but my brains seems to be.

I just had to reload lsquares, and everything worked like a charm.

After all, i will put this in my german introduction of maxima.



sorry stealing your (and roberts) time,
Robert


_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: Antw: Re: How to use Maxima to do a nonlinear fit?

by Robert Dodier-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

About matrix(...) triggering this error,

>  Error in MACSYMA-TOP-LEVEL [or a callee]: MACSYMA-TOP-LEVEL [or a callee] requires less than one hundred fifty-six arguments.

This is a GCL-specific behavior (number of arguments for a function call
is limited to 64). There is a work-around in current Maxima CVS,
which will be in the next release. If you are brave, you can copy the
new definition of $matrix from
http://maxima.cvs.sourceforge.net/maxima/maxima/src/mlisp.lisp
(Look for (defmspec $matrix ...) in mlisp.lisp.)

Hope this helps,
Robert Dodier
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: How to use Maxima to do a nonlinear fit?

by osman b :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday 15 November 2006 14:55, Mario Rodriguez wrote:
> > is there a package/code-snippet for fitting nonlinear functions to
> > datasets?
>
For numerical fitting try gnuplot "fit" function.
regards,
--
Osman F. Buyukisik
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Ante: Re: How to use Maxima to do a nonlinear fit?

by Robert Gloeckner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

** Reply Requested When Convenient **

Hello osman,

osman b <osman@...> schrieb am Freitag, 17. November 2006 um 17:10:
> On Wednesday 15 November 2006 14:55, Mario Rodriguez wrote:
>> > is there a package/code-snippet for fitting nonlinear functions to
>> > datasets?
>>
> For numerical fitting try gnuplot "fit" function.
> regards,

gnuplot only can do fits for f(x) and f(x,y). Maxima can do f(x,y,z,t...).

Ciao
Robert


_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima