Fitting a known model robustly to data using bayesian iteration. The two implementations use
- RANSAC
- M-Estimates
The robust part is implemented, fitting the function is not. Model fitting is borrowed from the scipy.minimize. Feel free to use a different model fitting method.
numpy is the only pre-requisite for robust_lsq.py. robust_lsq.py requires a least squares fitting function (or some other fitting function), such as scipy.optimize.minimize. Please see example models.py.
- numpy
- scipy
- numpy
- scipy
- numpy
- matplotlib
Please run test.py for an example of fitting a straight line to data robustly with bayesian estimation.
The key idea is to determine the samples that fit the model best. Bayesian updates are used. Bayes rule is given by:
P(data/model) = P(model/data)*P(data)/p(model)
P(data/model) := normalization(P(model/data)*P(data))
Note:
- P(model) is a constant and can be ignored.
- In the next iteration P(data/model) becomes P(data).
From an implementation perspective, these are the steps:
-
Build P(data) uniform distribution (or with prior knowledge) over data.
-
Sample n samples from data distribution.
-
Fit model to the selected n samples. Essentially we are selecting(sampling) the best model given the data. This is the P(model/data) step.
-
Estimate a probability distribution: P(data/model).
- These are the errors of the data given the selected model.
- It is wise to use a function such as arctan(1/errors) so errors are not amplified and create a useless probability distribution.
-
Compute P(data) with update: P(data/model) = normalize(P(data/model)*P(data))
- Normalize probability distribution.
- This is the bayesian update step.
-
Go to step 2. and iterate until desired convergence of P(data).
For a RANSAC flavor of bayesian robust fitting, k samples are selected to fit the model.
- The minimum number of samples (k) to fit a model is used.
- k samples are randomly selected p times.
- The best set in p of k samples that fit all the data is selected.
- k samples are selected and fit using least squares (or something else).
- Samples are selected from a probability distribution estimated using bayesian updates.
This is similar to RANSAC except when fitting the model, all samples are used to fit the model but are weighed according to their probability distribution. The probability distribution(weights) is updated using bayesian updates.
The probability distribution over the data P(data) provides a way to perform outlier detection. Simply apply a threshold over this distribution.
Copyright 2018 Guru Subramani
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.