Code Monkey home page Code Monkey logo

matlabrobustnonlinlsq's Introduction

View MatlabRobustNonlinLsq on File Exchange

MatlabRobustNonlinLsq

MATLAB's function robustfit() enables to fit a polynomial to data whose residuals are not normally distributed, which is the implicit assumption of least squares (LSQ) methods. Non-normal residuals result, for example, from outliers in empirical data or noise which is not normally distributed. The function utilizes the Iteratively Reweighted Least Squares (IRLS) approach with data-adaptive weights.

The downside is that robustfit() does not support non-linear least squares problems which often arise if the mathematical model is non-linear in its parameters. The function lsqcurvefit() and lsqnonlin() provide means to solve non-linear LSQ with optional parameter bounds (due to the trust-region-reflective algorithm) but don't support weighted LSQ for robust estimation in the presence of outliers.

This MATLAB function is intended to give the best of both worlds, i.e. combine methods of robustfit() and lsqnonlin() to accomplish robust non-linear least squares calculations.

Dependencies

This function is tested in MATLAB R2016b but should scale to any modern MATLAB release.

The function depends on the following toolboxes:

  • Statistics and Machine Learning Toolbox
  • Optimization Toolbox

Usage

Consider the following problem. We want to fit a function to data which stems from a square-root function with a scale and location parameter. The data is noisy and exhibits strong outliers. Let's see how ordinary and robust non-linear least squares perform in this situation. The example is taken from the provided demo file.

% the model's true parameters
trueParams = [50, -0.7];

% create some noisy data using a square root function with 2 parameters and nasty noise
x = linspace(0, 20, 100);
modelFun = @(param, x) param(1)*sqrt(max(0, x + param(2)));

noise = 10*sin(x).^2 .* randn(size(x));

y = modelFun(trueParams, x) + noise;

% introduce outliers
y(11)  = -300;
y(end) = 2000;

%%%% Fit the model %%%%
% make sure the parameters are within the following range
lb = [1, -inf];
ub = [inf, 10];

% initial guess
x0 = [1, -1];

% don't show infos from lsqcurvefit()
options = optimset(@lsqcurvefit);
options.Display = 'off';

% estimate parameters using ordinary and robust LSQ
estParamsOrdinary = lsqcurvefit(modelFun, x0, x, y, lb, ub, options);
estParamsRobust   = robustlsqcurvefit(modelFun, x0, x, y, lb, ub, [], options);

figure;
hold on;
scatter(x, y, 'filled');
plot(x, modelFun(trueParams, x), 'k--', 'linewidth', 2);
plot(x, modelFun(estParamsOrdinary, x), 'linewidth', 2');
plot(x, modelFun(estParamsRobust, x), 'linewidth', 2);
hold off;
grid on;

legend(...
    {'Noisy Data', 'True Regression', 'Ordinary non-lin LSQ', 'Robust non-lin LSQ'}, ...
    'fontsize', 12 ...
    );

example.png

License

The code is licensed under BSD 3-Clause license.

matlabrobustnonlinlsq's People

Contributors

jaadrian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

matlabrobustnonlinlsq's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.