[Tutor] constructing semi-arbitrary functions

"André Walker-Loud <walksloud@gmail.com>" walksloud at gmail.com
Tue Feb 18 01:51:00 CET 2014


Hi Oscar,


On Feb 17, 2014, at 7:03 PM, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:

> On 17 February 2014 22:15, "André Walker-Loud <walksloud at gmail.com>"
> <walksloud at gmail.com> wrote:
>>> This particular case is easily solved:
>>> 
>>> def f_lambda(x,pars):
>>>    return lambda x: poly(x,*pars)
>>> 
>>> You let the closure take care of pars and return a function that takes
>>> exactly one argument x.
>> 
>> Hi Oscar,
>> 
>> This is the opposite of what I am trying to do.  In the example, x represents the data and pars represent the parameters I want to determine, so it is the pars which I need passed into the "func_code.co_varnames" part of f.
> 
> BTW if you're trying to fit the coefficients of a polynomial then a
> general purpose optimisation function is probably not what you want to
> use. I would probably solve (in a least squares sense and after
> suitable scaling) the Vandermonde matrix.
> 
> (I can explain that more if desired.)

That looks interesting (just reading the wikipedia entry on the Vandermonde matrix).
Is there a good algorithm for picking the values the polynomial is evaluated at to construct the matrix?
Is there a benefit to this method vs a standard linear least squares?
Given the Vandermonde matrix, how do you determine the uncertainty in the resulting parameters?
I guess yes, I am interested to learn more.

The most common problem I am solving is fitting a sum of real exponentials to noisy data, with the model function

C(t) = sum_n A_n e^{- E_n t}

the quantities of most interest are E_n, followed by A_n so I solve this with non-linear regression.
To stabilize the fit, I usually do a linear-least squares for the A_n first, solving as a function of the E_n, and then do a non-linear fit for the E_n.  Often, we construct multiple data sets which share values of E_n but have different A_n, where A_n is promoted to a matrix, sometimes square, sometimes not.  So I want a wrapper on my chisq which can take the input parameter values and deduce the fit function, and pass the variable names to my current minimizer - hence my original question, and why I don’t want to write a special case for each possible combination of n_max, and the shape of A_n.


Cheers,

Andre


More information about the Tutor mailing list