Method ao/xfit


  XFIT fit a function of x to data.
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
  DESCRIPTION: XFIT performs a non-linear fit of a function of x to data.
  smodels fitting is also supported.
  
  ALGORITHM: XFIT does a chi-squared minimization by means of different
  algorithms (see details in the default plist). Covariance matrix is also
  computed from the Fisher's Information Matrix. In case the Information
  Matrix is not positive-definite, uncertainties will not be stored in the
  output. 
 
  CALL:        b = xfit(a, pl)
 
  INPUTS:      a  - input AO to fit to
               pl - parameter list (see below)
 
  OUTPUTs:     b  - a pest object containing the best-fit parameters,
                    goodness-of-fit reduced chi-squared, fit degree-of-freedom
                    covariance matrix and uncertainties. Additional
                    quantities, like the Information Matrix, are contained 
                    within the procinfo. The best-fit model can be evaluated
                    from pest\eval.
 
  Parameters Description
 
  EXAMPLES:
 
  % 1) Fit to a frequency-series
 
    % Create a frequency-series
    datapl = plist('fsfcn', '0.01./(0.0001+f) + 5*abs(randn(size(f))) ', 'f1', 1e-5, 'f2', 5, 'nf', 1000, ...
       'xunits', 'Hz', 'yunits', 'N/Hz');
    data = ao(datapl);
    data.setName;
  
    % Do fit
    fitpl = plist('Function', 'P(1)./(P(2) + Xdata) + P(3)', ...
      'P0', [0.1 0.01 1]);
    params = xfit(data, fitpl)
  
    % Evaluate model
    BestModel = eval(params,plist('type','fsdata','xdata',data,'xfield','x'));
    BestModel.setName;
    
    % Display results
    iplot(data,BestModel)
 
  % 2) Fit to a noisy sine-wave
 
    % Create a noisy sine-wave
    fs    = 10;
    nsecs = 500;
    datapl = plist('waveform', 'Sine wave', 'f', 0.01, 'A', 0.6, 'fs', fs, 'nsecs', nsecs, ...
      'xunits', 's', 'yunits', 'm');
    sw = ao(datapl);
    noise = ao(plist('tsfcn', '0.01*randn(size(t))', 'fs', fs, 'nsecs', nsecs));
    data = sw+noise;
    data.setName;
  
    % Do fit
    fitpl = plist('Function', 'P(1).*sin(2*pi*P(2).*Xdata + P(3))', ...
      'P0', [1 0.01 0]);
    params = xfit(data, fitpl)
  
    % Evaluate model
    BestModel = eval(params,plist('type','tsdata','xdata',sw,'xfield','x'));
    BestModel.setName;
    
    % Display results
    iplot(data,BestModel)
 
  % 3) Fit an smodel of a straight line to some data
 
    % Create a noisy straight-line
    datapl = plist('xyfcn', '2.33 + 0.1*x + 0.01*randn(size(x))', 'x', 0:0.1:10, ...
      'xunits', 's', 'yunits', 'm');
    data = ao(datapl);
    data.setName;
  
    % Model to fit
    mdl = smodel('a + b*x');
    mdl.setXvar('x');
    mdl.setParams({'a', 'b'}, {1 2});
  
    % Fit model
    fitpl = plist('Function', mdl, 'P0', [1 1]);
    params = xfit(data, fitpl)
  
    % Evaluate model
    BestModel = eval(params,plist('xdata',data,'xfield','x'));
    BestModel.setName;
    
    % Display results
    iplot(data,BestModel)
 
  % 4) Fit a chirp-sine firstly starting from an initial guess (quite close
  % to the true values) (bad convergency) and secondly by a Monte Carlo
  % search (good convergency)
 
    % Create a noisy chirp-sine
    fs    = 10;
    nsecs = 1000;
    
    % Model to fit and generate signal
    mdl = smodel(plist('name', 'chirp', 'expression', 'A.*sin(2*pi*(f + f0.*t).*t + p)', ...
      'params', {'A','f','f0','p'}, 'xvar', 't', 'xunits', 's', 'yunits', 'm'));
  
    % signal
    s = mdl.setValues({10,1e-4,1e-5,0.3});
    s.setXvals(0:1/fs:nsecs-1/fs);
    signal = s.eval;
    signal.setName;
  
    % noise
    noise = ao(plist('tsfcn', '1*randn(size(t))', 'fs', fs, 'nsecs', nsecs));
  
    % data
    data = signal + noise;
    data.setName;
  
    % Fit model from the starting guess
    fitpl_ig = plist('Function', mdl, 'P0',[8,9e-5,9e-6,0]);
    params_ig = xfit(data, fitpl_ig);
  
    % Evaluate model
    BestModel_ig = eval(params_ig,plist('xdata',data,'xfield','x'));
    BestModel_ig.setName;
    
    % Display results
    iplot(data,BestModel_ig)
    
    % Fit model by a Monte Carlo search
    fitpl_mc = plist('Function', mdl, ...
      'MonteCarlo', true, 'Npoints', 1000, 'LB', [8,9e-5,9e-6,0], 'UB', [11,3e-4,2e-5,2*pi]);
    params_mc = xfit(data, fitpl_mc)
    
    % Evaluate model
    BestModel_mc = eval(params_mc,plist('xdata',data,'xfield','x'));
    BestModel_mc.setName;
    
    % Display results
    iplot(data,BestModel_mc)
 
  % 5) Multichannel fit of smodels
 
    % Ch.1 data
    datapl = plist('xyfcn', '0.1*x + 0.01*randn(size(x))', 'x', 0:0.1:10, 'name', 'channel 1', ...
      'xunits', 'K', 'yunits', 'Pa');
    a1 = ao(datapl);
    % Ch.2 data
    datapl = plist('xyfcn', '2.5*x + 0.1*sin(2*pi*x) + 0.01*randn(size(x))', 'x', 0:0.1:10, 'name', 'channel 2', ...
      'xunits', 'K', 'yunits', 'T');
    a2 = ao(datapl);
    
    % Model to fit
    mdl1 = smodel('a*x');
    mdl1.setXvar('x');
    mdl1.setParams({'a'}, {1});
    mdl1.setXunits('K');
    mdl1.setYunits('Pa');
    
    mdl2 = smodel('b*x + a*sin(2*pi*x)');
    mdl2.setXvar('x');
    mdl2.setParams({'a','b'}, {1,2});
    mdl2.setXunits('K');
    mdl2.setYunits('T');
    
    % Fit model
    params = xfit(a1,a2, plist('Function', [mdl1,mdl2]));
    
    % evaluate model
    b = eval(params, plist('index',1,'xdata',a1,'xfield','x'));
    b.setName('fit Ch.1');
    r = a1-b;
    r.setName('residuals');
    iplot(a1,b,r)
    
    b = eval(params, plist('index',2,'xdata',a2,'xfield','x'));
    b.setName('fit Ch.2');
    r = a2-b;
    r.setName('residuals');
    iplot(a2,b,r)
 
  Parameters Description
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Method Details
Access public
Defining Class ao
Sealed 0
Static 0

Parameter Description

Default

no description
Key Default Value Options Description
xfit
FUNCTION '' none The function (or symbolic model smodel) of Xdata that you want to fit. For example: 'P(1)*Xdata'.
WEIGHTS '' none An array of weights, one value per X sample.
By default, xfit takes data uncertainties from the dy field of the input object.If provided, weights make xfit ignore these.Otherwise weights will be considered as ones.
P0 [] none An array of starting guesses for the parameters.
This is not necessary if you fit with smodels; in that case the parametervalues from the smodel are taken as the initial guess.
LB [] none Lower bounds for the parameters.
This improves the convergency. Mandatory for Monte Carlo.
For multichannel fitting it has to be a cell-array.
UB [] none Upper bounds for the parameters.
This improves the convergency. Mandatory for Monte Carlo.
For multichannel fitting it has to be a cell-array.
ALGORITHM 'fminsearch'
  • 'fminsearch'
  • 'fminunc'
  • 'fmincon'
  • 'patternsearch'
  • 'ga'
  • 'simulannealbnd'
A string defining the fitting algorithm.
fminunc, fmincon require 'Optimization Toolbox' to be installed.
patternsearch, ga, simulannealbnd require 'Genetic Algorithm and Direct Search' to be installed.
OPTSET '' none An optimisation structure to pass to the fitting algorithm.
See fminsearch, fminunc, fmincon, optimset, for details.
See patternsearch, psoptimset, for details.
See ga, gaoptimset, for details.
See simulannealbnd, saoptimset, for details.
FITUNC 1
  • 1
  • 0
Fit parameter uncertainties or not.
UNCMTD 'hessian'
  • 'hessian'
  • 'jacobian'
Choose the uncertainties estimation method.
For multi-channel fitting hessian is mandatory.
FASTHESS 0
  • 0
  • 1
Choose whether or not the hessian should be estimated from a fast-forward finite differences algorithm; use this method to achieve better performance in CPU-time, but accuracy is not ensured; otherwise, the default (computer expensive, but more accurate) method will be used.
MONTECARLO 0
  • 0
  • 1
Do a Monte Carlo search in the parameter space.
Useful when dealing with high multiplicity of local minima. May be computer-expensive.
Note that, if used, P0 will be ignored. It also requires to define LB and UB.
NPOINTS 100000 none Set the number of points in the parameter space to be extracted.
NOPTIMS 10 none Set the number of optimizations to be performed after the Monte Carlo.
ESTIMATOR 'chi2'
  • 'chi2'
  • 'abs'
  • 'log'
Choose the robust local M-estimate as the statistical estimator of the fit:
'chi2' (least square) for data with normal distribution;
'abs' (mean absolute deviation) for data with double exponential distribution;
'log' (log least square) for data with Lorentzian distribution.

Example

plist('FUNCTION', '', 'WEIGHTS', '', 'P0', [[]], 'LB', [[]], 'UB', [[]], 'ALGORITHM', 'fminsearch', 'OPTSET', '', 'FITUNC', [true], 'UNCMTD', 'hessian', 'FASTHESS', [false], 'MONTECARLO', [false], 'NPOINTS', [100000], 'NOPTIMS', [10], 'ESTIMATOR', 'chi2')

back to top back to top

Some information of the method ao/xfit are listed below:
Class name ao
Method name xfit
Category Signal Processing
Package name ltpda
VCS Version 967b0eec0dece803a81af8ef54ad2f8c784b20b2
Min input args 1
Max input args -1
Min output args 1
Max output args -1
Can be used as modifier 0
Supported numeric types {'double'}




©LTP Team