/* * multiprofile.i * * $Id: $ * * This file is part of Yutils * Copyright (C) 2008 Thibaut Paumard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * */ require,"lmfit.i"; local multiprofile; /* DOCUMENT multiprofile.i PURPOSE multiprofile.i is a helper library for use with lmfit. It allows creating complex user functions by adding up already existing, simpler ones. RATIONALE Remember the lmfit calling sequence: result=lmfit(f, x, a, y, w, ...). where F is the model function to fit, X the "independent variables", which can indeed be anything required by F, A is the vector of parameters, and Y the observed values. lmfit finds the optimal values for A, in order to minimize the distance between F(X,A) and Y. In order for lmfit to converge efficiently, it is better if F is able to compute its own gradient when called as f(x, a, grad, deriv=1). Writing model functions that provide derivatives can be tiresome and error-prone. The goal of multiprofile.i is to make this process faster and more reliable, by allowing one to build complex model functions from pre-existing, well-optimized and well-tested primitives with derivatives. Of course, multiprofile.i is limited to a particular sub-set of all the types of functions one might want to use lmfit on: it concentrates on the case where the model function is the sum of simpler profiles, either of the same type (produced by the same primitive function, like 4 Gaussian), or different from each other (e.g. a Gaussian plus a Moffat). To do that, the multiprofile model function mp_func(x, a, grad, deriv=) accepts as first positional argument X a complex object which has to be set-up using the helper function mp_setx. X contains in itself a description of the profile: the primitive functions to use, the number of instances of each type of primitive function, and, naturally, whatever X parameter each primitive function requires to function properly. You then call lmfit with mp_func as its first argument and this complex X as its second argument. EXAMPLE For instance, assume you want to fit an observed Y, which seems to be well described as a sum of 3 Gaussian profiles. The usual process would require you to write a new function (e.g. gaussx3) specifically for this purpose. gaussx3() would have to compute both the sum of 3 Gaussian profiles, and the corresponding gradient. This is how you can do it with multiprofile.i (assuming x and y are already known, and you have found a reasonable first guess a1, a2 and a3 for each of the 3 components). #include "gauss.i" #include "multiprofile.i" MultiX=mp_setx(profile=gauss, realX=x, npar=3, ncomp=3); a=_(a1, a2, a3); result=mpfit(mp_func, MultiX, a, y, deriv=1); Now, assume you want to add a linear baseline to the three Gaussian profiles (note that linear() is provided by multiprofile.i). You have "guessed" as l0 and l1 the two corresponding parameters: linX=mp_setx(profile=linear, realX=x, npar=2); MultiX=mp_setx(profile=gauss, realX=x, npar=3, ncomp=3, more=linX); a=_(a1, a2, a3, [l0, l1]); result=mpfit(mp_func, MultiX, a, y, deriv=1); FUNCTIONS PROVIDED BY MULTIPROFILE.I mp_func: the F parameter to lmfit when using multiprofile.i; mp_setx: helper function to set-up the complex model function; mp_getx: reverse from mp_setx mp_seta: helper function to combine individual first guesses for each component into a first guess for the complex model function (use GET keyword fro the reverse); linear : a*x+b, with lmfit-friendly calling sequence and derivatives; linear2d: a*x+b*y+c, with lmfit-friendly calling sequence and derivatives; poly2 : same as poly() (compute 1D polynomials), with lmfit-friendly calling sequence and derivatives. offsetlines: an lmfit-friendly function for fitting lines on a spectrum. It exemplifies advanced usage of multiprofile. ol_setx: helper function akin to mp_setx, for use with offsetlines. SEE ALSO: lmfit, mp_func, mp_setx, mp_getx, mp_seta, linear, linear2d, poly2, gauss, gauss2d, moffat, moffat2d, offsetlines, ol_setx */ func mp_func(x,a,&grad,&comps,deriv=,returncomps=){ /* DOCUMENT mp_func(x,a,&grad,deriv=) A general purpose routine to easily create multicomponents profiles. The parameter scheme may seem odd, but it is intended to be easily used with lmfit. See "multiprofile" for an introduction. X: this parameter should be set using MP_SETX (which see). It contains both the "real" independent variables and a description of the model, split into several components. A: vector of parameters. MP_SETA can be used to set it up. If base profile needs NPAR parameters, MP_FUNC will transmit A(NPAR*I+1:NPAR*(I+1)) to Ith instance of PROFILE. In case some parameters must be equal for every components, give their index in X (using mp_setx(equal=...)), and simply suppress this parameter from the list of parameters for all components except the first. For instance, if three components of parameters [a1,b1,c1], [a2,b2,c2], and [a3,b3,c3] are to be adjusted with the restriction that b1=b2=b3, A is of the form A=[a1,b1,c1,a2,c2,a3,c3] and equal=[2] in the call to mp_setx. GRAD: upon return, derivatives of output Y relative to each parameter in A, if DERIV is set to non void an non null. Can be used only if base profile function is able to compute derivatives. DERIV: whether or not to compute derivatives. COMPS: multiprofile can return the individual profiles of each component in a 4th positional argument. Set RETURNCOMPS for this to happen. COMPS(,C) is the C-th component. EXAMPLE: require,"gauss.i" require,"multiprofile.i" axis=span(-10,10,101); more=mp_setx(profile=linear,npar=2); x=mp_setx(profile=gauss,npar=3,ncomp=2,realX=axis,more=more); a=[10,-5,2.,7,4,1.5,100,0.5]; y=mp_func(x,a); plg,y,axis; SEE ALSO: lmfit, multiprofile */ mp_getx, x, profile, realX, npar, ncomp, equal, more; y=profile(realX,a(1:npar),gradc,deriv=deriv); if (returncomps) { comps=array(double,numberof(y),ncomp); comps(,1)=y; } if (deriv) { grad=array(y,numberof(a)); grad(..,1:npar)=gradc; } if (is_void(equal)){ for (i=1;i1 && npeq >0) peigne(2:)+=npeq; for (p=1;p<=npars;p++){ if (noneof(equal==p)) { if (!is_void(get)) params(,p)=a(p+peigne); else a(p+peigne)=params(,p); } else { if (!is_void(get)) params(,p)=a(p); else a(p)=params(1,p); if (ncomp >1) peigne(2:)--; } } if (!is_void(more)) { if (!is_void(get)) more=a(1-numberof(more):); a(1-numberof(more):)=more; } return a; } // primitives func linear(x,a,&grad,deriv=) { /* DOCUMENT linear(x,a) or linear(x,a,grad,deriv=1) a(1)+x*a(2) Returns derivatives if DERIV set to a "true" value. Very simplistic, but might come in handy, as it is compatible with lmfit (and multiprofile). SEE ALSO: linear2d, poly2, lmfit, multiprofile */ if (deriv) grad=[array(1.,dimsof(x)), x]; return a(1)+a(2)*x; } func linear2d(xy,a,&grad,deriv=) { /* DOCUMENT linear2d(xy,a) or linear2d(xy,a,grad,deriv=1) a(1)+x*a(2)+y*a(3) where x=xy(..,1); y=xy(..,2). Returns derivatives if DERIV set to a "true" value. Very simplistic, but might come in handy, as it is compatible with lmfit (and multiprofile). SEE ALSO: linear, lmfit, multiprofile */ if (deriv) grad=_(array(1.,dimsof(xy(..,1)),1), xy); return a(1)+a(2)*xy(..,1)+a(3)*xy(..,2); } func poly2(x,a,&grad,deriv=) { /* DOCUMENT poly2(x,a) or poly2(x,a,grad,deriv=1) Returns the polynomial sum(a(n)*x^(n-1)), with derivatives in GRAD if DERIV set to a "true" value. Very simplistic, but might come in handy, as it is compatible with lmfit (and multiprofile). SEE ALSO: poly, linear, lmfit, multiprofile */ degp1=numberof(a); if (deriv){ grad=array(1.,dimsof(x),degp1); //grad(,1)=0; //useless grad(..,1)=1; if (degp1>=2) grad(..,2)=x; for (n=2;n