How to obtain curve fitting tool startpoints using code? Replicate Curve Fitter Toolbox
Show older comments
Hello,
I am trying to fit curves to time series.
I have a 100 different time series (all of different lengths) and for each of them, I know by trial and error on quite a few time series that gauss3 is a good fit.
Here's is what I have done until now. I obtain the time series and fit gauss 3 using the curve fitter toolbox GUI. I like the fits (on all the time series I tried) and so I export as code. I get the code as follows:
[xData, yData] = prepareCurveData( time_temp, state_timeseries_temp );
% Set up fittype and options.
ft = fittype( 'gauss3' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf 0 -Inf -Inf 0 -Inf -Inf 0];
opts.StartPoint = [2.01679210779712 262.632 7.13033226840792 0.012098277147597 242.358 16.7897888539238 0.0108815814086219 19.848 22.539819613689];
When I put this into my script, run the code and plot the fit on top of my actual time series the results (same as the curve fitter) are good.
But, the opts properties are specific to the time series I used in the curve fitter, so I remove the opts as follows and try again:
% Fit gaussian curve of degree 3
[xData, yData] = prepareCurveData( time_temp, state_timeseries_temp );
% Set up fittype and options.
ft = fittype( 'gauss3' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
The resultant fit is not as good as before.
How can I generate these opts properties programmatically (the way curve fitter toolbox generates for each time series I feed in), since I want to do this for a lot of different time series and the specific opts values obtained using curve fitter toolbox is not going to work with other time series.
Does the curve fitter toolbox do this iteratively? If so, how can I replicate that in a script?
Basically, I want to replicate (in a script) exactly what the curve fitter applet is doing when I select a particular time series.
Accepted Answer
More Answers (2)
Mrutyunjaya Hiremath
on 5 Sep 2023
% Prepare data
[xData, yData] = prepareCurveData( time_temp, state_timeseries_temp );
% Set up fittype
ft = fittype( 'gauss3' );
% Initialize options
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
% Data-driven initialization (Example: using mean and std)
mu_init = mean(yData);
sigma_init = std(yData);
% Set the StartPoint (modify this according to your specific model)
opts.StartPoint = [mu_init, 200, sigma_init, 0.1, 100, 10, 0.1, 20, 20];
% Fit model to data
[fitresult, gof] = fit( xData, yData, ft, opts );
In the above example, I've used mean and standard deviation as initial points for some parameters.
If you want, you can adjust this based on the specific characteristics of your data and what each parameter in the gauss3 model represents.
2 Comments
atharva aalok
on 5 Sep 2023
Mrutyunjaya Hiremath
on 6 Sep 2023
Replicating that is quite CHALLENGING.
The curve fitting toolbox in software like MATLAB or similar computational environments often provides options to automatically select initial "StartPoints" for curve fitting algorithms. However, the methods used for determining these starting points can be quite varied and are generally heuristic in nature. The algorithm's choice of initial points might depend on several factors including, but not limited to:
- Data Range: The range of the data set can be used to set initial parameter estimates. For example, for exponential fits, an initial guess for the rate parameter might be set based on the decay observed in the data.
- Data Moments: Statistical moments like mean, variance, etc., can provide good initial estimates for some types of functions.
- Random Sampling: Some algorithms might use Monte Carlo methods to sample the parameter space as a way of choosing an initial point.
- User-Specified: In many cases, the user has the option to specify initial points based on prior knowledge or intuition about the system being modeled.
- Built-in Heuristics: For certain types of commonly-used functions (like Gaussian, Lorentzian, etc.), the software might have built-in heuristics for choosing initial parameters that work well in a general sense for those functions.
- Linearization: For some types of nonlinear functions, a linear approximation can be made to estimate initial parameters, which are then refined through the nonlinear fitting process.
- Previous Fits: If multiple fits are being conducted on similar types of data, some algorithms may use the parameters from a previous fit as the initial guess for the next fit.
- Optimization Algorithms: Some curve fitting tools might run a preliminary optimization routine to find a "good enough" initial guess for the parameters.
- Statistical Methods: Methods like least squares estimates, maximum likelihood estimates, or Bayesian methods might be used to arrive at an initial guess.
- Analytical Solutions: For simpler models, analytical solutions might exist that can provide exact initial estimates based on the data.
It's worth noting that the quality of the initial guess can significantly impact the performance of the curve fitting algorithm, especially for non-linear models. Poor initial guesses can result in the algorithm converging to a local minimum rather than the global minimum.
Steven Lord
on 5 Sep 2023
0 votes
Under certain circumstances (when the Method is NonlinearLeastSquares and you're using certain library models) MATLAB uses heuristics to generate starting points as stated in the description of the StartPoint name-value argument for the fit function. I don't know if we expose the functions that compute those heuristics to be called by user code and if we do I don't know the names by which you'd call those heuristic functions. In other circumstances I believe the start points are generated randomly.
You may want to contact Technical Support directly using this link to request as an enhancement that Curve Fitting Toolbox expose those heuristic functions.
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!