Class LeastMedianOfSquares<Model,Point>
java.lang.Object
org.ddogleg.fitting.modelset.lmeds.LeastMedianOfSquares<Model,Point>
- All Implemented Interfaces:
InlierFraction, ModelMatcher<Model,Point>, ModelMatcherPost<Model, Point>
- Direct Known Subclasses:
LeastMedianOfSquares_MT
public class LeastMedianOfSquares<Model,Point>
extends Object
implements ModelMatcherPost<Model,Point>, InlierFraction
Another technique similar to RANSAC known as Least Median of Squares (LMedS). For each iteration a small number N points are selected. A model is fit to these points and then the error is computed for the whole set. The model which minimizes the median is selected as the final model.
Inliers are selected using Rousseeuw reweighting by default, this uses the median value to derive a normal distribution. Alternatively, N-best error fraction can be used to define the inlier set.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected doubleprotected doubleprotected LeastMedianOfSquares<Model,Point>.@Nullable TrialHelper protected Ransac.InitializeModels<Model, Point> Optional function for initializing generator and distance functionsprotected doubleFraction of samples which will be considered an inlier.protected doubleComputes an inlier threshold by assuming the distribution is normal.protected doubleLargest error an inlier can have.protected int[]protected final doubleprotected final ModelManager<Model> protected inthow many points it samples to generate a model fromprotected final intnumber of times it performs its fit cycle -
Constructor Summary
ConstructorsConstructorDescriptionLeastMedianOfSquares(long randSeed, int totalCycles, double maxMedianError, ModelManager<Model> modelManager, Class<Point> pointType) Configures the algorithm.LeastMedianOfSquares(long randSeed, int totalCycles, ModelManager<Model> modelManager, Class<Point> pointType) Configures the algorithm. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidIf the maximum number of iterations has changed then re-generate the RNG for each trialprotected voidcomputeInliers(List<Point> dataSet, int N, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Computes the inlier set for the best model usinginlierFractionorinlierRouseeuwSigmaprotected voidcomputeInliersFromFraction(List<Point> dataSet, int n, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Selects theinlierFractionof points with the smallest error.protected voidcomputeInliersFromSigma(List<Point> dataSet, int N, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Computes inlier using Rousseeuw's reweighting.doubleGets the inlier set's fractional valuedoubleValue of the best median error.intgetInputIndex(int matchIndex) For an item in the match set, return the index of the item in the original input set.Returns the inlier set.intThis is the minimum number of observations which can be input and produce a valid model.Model for the match setReturns a class for the input point objectReturns a class for the input point objectbooleanFinds a set of points from the provided list that are a good fit for the internal model and computes the fit parameters for the model.voidreset()Resets the model matcher to its original state.protected voidselectSmallestErrors(List<Point> dataSet, int n, int numPts, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Adds the numPts points with the smallest errors to the inlier set and sets the threshold to the largest errorprotected static intselectThresholdIndex(int N, double fraction) Index of the element at the specified fraction.voidsetErrorFraction(double errorFraction) Specifies the inlier set as a fractionvoidsetInlierFraction(double inlierFraction) Fraction of samples which will be considered an inlier.voidsetModel(Factory<ModelGenerator<Model, Point>> factoryGenerator, Factory<DistanceFromModel<Model, Point>> factoryDistance) Specifies the internal model.
-
Field Details
-
trialRNG
-
totalCycles
protected final int totalCyclesnumber of times it performs its fit cycle -
sampleSize
protected int sampleSizehow many points it samples to generate a model from -
maxMedianError
protected final double maxMedianError -
modelManager
-
matchToInput
protected int[] matchToInput -
bestMedian
protected volatile double bestMedian -
errorFraction
protected double errorFraction -
inlierSet
-
inlierFraction
protected double inlierFractionFraction of samples which will be considered an inlier. <= 0 disables it -
inlierRouseeuwSigma
protected double inlierRouseeuwSigmaComputes an inlier threshold by assuming the distribution is normal. Sigma defines the number of stdev the error can be for it to be an inlier. If inlier fraction is specified it will take precedence. -
inlierThreshold
protected double inlierThresholdLargest error an inlier can have. WithinlierRouseeuwSigmait's the computed threshold. WithinlierFractionor too few points to estimate sigma, it's the error of the worst inlier and points with an identical error might have been excluded. NaN until process() has been called. -
helper
-
initializeModels
Optional function for initializing generator and distance functions
-
-
Constructor Details
-
LeastMedianOfSquares
public LeastMedianOfSquares(long randSeed, int totalCycles, double maxMedianError, ModelManager<Model> modelManager, Class<Point> pointType) Configures the algorithm.- Parameters:
randSeed- Random seed used internally.totalCycles- Number of random draws it will make when estimating model parameters.maxMedianError- If the best median error is larger than this it is considered a failure.
-
LeastMedianOfSquares
public LeastMedianOfSquares(long randSeed, int totalCycles, ModelManager<Model> modelManager, Class<Point> pointType) Configures the algorithm.- Parameters:
randSeed- Random seed used internally.totalCycles- Number of random draws it will make when estimating model parameters.
-
-
Method Details
-
setModel
public void setModel(Factory<ModelGenerator<Model, Point>> factoryGenerator, Factory<DistanceFromModel<Model, Point>> factoryDistance) Description copied from interface:ModelMatcherPostSpecifies the internal model. Factories are provided since each thread might need its own unique instance of the generator and distance function if they are not thread safe.- Specified by:
setModelin interfaceModelMatcherPost<Model,Point> - Parameters:
factoryGenerator-ModelGeneratorfactoryDistance-DistanceFromModel
-
setInlierFraction
public void setInlierFraction(double inlierFraction) Fraction of samples which will be considered an inlier. <= 0 disables it -
process
Description copied from interface:ModelMatcherFinds a set of points from the provided list that are a good fit for the internal model and computes the fit parameters for the model.- Specified by:
processin interfaceModelMatcher<Model,Point> - Parameters:
dataSet- Set of points (with noise) that are to be fit.- Returns:
- true if it successfully found a solution or false if not.
-
computeInliers
protected void computeInliers(List<Point> dataSet, int N, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Computes the inlier set for the best model usinginlierFractionorinlierRouseeuwSigma -
computeInliersFromFraction
protected void computeInliersFromFraction(List<Point> dataSet, int n, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Selects theinlierFractionof points with the smallest error. Assumes errors have been computed. -
selectSmallestErrors
protected void selectSmallestErrors(List<Point> dataSet, int n, int numPts, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Adds the numPts points with the smallest errors to the inlier set and sets the threshold to the largest error -
computeInliersFromSigma
protected void computeInliersFromSigma(List<Point> dataSet, int N, LeastMedianOfSquares<Model, Point>.TrialHelper helper) Computes inlier using Rousseeuw's reweighting. Assumes errors have been computed.
Rousseeuw and Leroy, "Robust Regression and Outlier Detection", 1987
-
selectThresholdIndex
protected static int selectThresholdIndex(int N, double fraction) Index of the element at the specified fraction. Clamped so it's always a valid index -
checkTrialGenerators
protected void checkTrialGenerators()If the maximum number of iterations has changed then re-generate the RNG for each trial -
getErrorFraction
public double getErrorFraction()Description copied from interface:InlierFractionGets the inlier set's fractional value- Specified by:
getErrorFractionin interfaceInlierFraction
-
setErrorFraction
public void setErrorFraction(double errorFraction) Description copied from interface:InlierFractionSpecifies the inlier set as a fraction- Specified by:
setErrorFractionin interfaceInlierFraction- Parameters:
errorFraction- inlier threshold. Must be from 0 to 1, inclusive.
-
getModelParameters
Description copied from interface:ModelMatcherModel for the match set- Specified by:
getModelParametersin interfaceModelMatcher<Model,Point> - Returns:
- model.
-
getMatchSet
Returns the inlier set. IfinlierFraction> 0 then it's that fraction of points with the smallest error, otherwise it's found using Rousseeuw's reweighting andinlierRouseeuwSigma.- Specified by:
getMatchSetin interfaceModelMatcher<Model,Point> - Returns:
- Set of points that are inliers to the returned model parameters.
-
getInputIndex
public int getInputIndex(int matchIndex) Description copied from interface:ModelMatcherFor an item in the match set, return the index of the item in the original input set.- Specified by:
getInputIndexin interfaceModelMatcher<Model,Point> - Parameters:
matchIndex- Index of an element in the match set.- Returns:
- Index of the same element in the original input list.
-
getFitQuality
public double getFitQuality()Value of the best median error.- Specified by:
getFitQualityin interfaceModelMatcher<Model,Point> - Returns:
- Quality of fit to matched set of points
-
getMinimumSize
public int getMinimumSize()Description copied from interface:ModelMatcherThis is the minimum number of observations which can be input and produce a valid model.- Specified by:
getMinimumSizein interfaceModelMatcher<Model,Point> - Returns:
- Minimum number of sample points
-
reset
public void reset()Description copied from interface:ModelMatcherResets the model matcher to its original state. This means that given identical inputs it would produce the same outputs- Specified by:
resetin interfaceModelMatcher<Model,Point>
-
getPointType
Description copied from interface:ModelMatcherReturns a class for the input point object- Specified by:
getPointTypein interfaceModelMatcher<Model,Point>
-
getModelType
Description copied from interface:ModelMatcherReturns a class for the input point object- Specified by:
getModelTypein interfaceModelMatcher<Model,Point>
-