001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.math.model.fit;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import org.openimaj.math.model.EstimatableModel;
036import org.openimaj.math.model.fit.residuals.ResidualCalculator;
037import org.openimaj.math.util.distance.DistanceCheck;
038import org.openimaj.math.util.distance.ThresholdDistanceCheck;
039import org.openimaj.util.pair.IndependentPair;
040
041/**
042 * Example robust fitting, that simply wraps the models estimate method. Inliers
043 * and outliers are estimated by verifying the model against the data.
044 *
045 * @author Jonathon Hare
046 *
047 * @param <I>
048 *            type of independent data
049 * @param <D>
050 *            type of dependent data
051 * @param <M>
052 *            concrete type of model learned
053 */
054public class SimpleModelFitting<I, D, M extends EstimatableModel<I, D>> implements RobustModelFitting<I, D, M> {
055        List<IndependentPair<I, D>> inl;
056        List<IndependentPair<I, D>> outl;
057        M model;
058
059        ResidualCalculator<I, D, M> errorModel;
060        DistanceCheck dc;
061
062        /**
063         * Creates a SimpleModelFitting object to fit data to a model
064         *
065         * @param m
066         *            model to fit data to
067         * @param errorModel
068         *            the error model
069         * @param errorThreshold
070         *            the error threshold
071         */
072        public SimpleModelFitting(M m, ResidualCalculator<I, D, M> errorModel,
073                        double errorThreshold)
074        {
075                model = m;
076                this.errorModel = errorModel;
077                this.dc = new ThresholdDistanceCheck(errorThreshold);
078        }
079
080        /**
081         * Creates a SimpleModelFitting object to fit data to a model
082         *
083         * @param m
084         *            model to fit data to
085         * @param errorModel
086         *            the error model
087         * @param dc
088         *            the error/distance check that determines whether a point is
089         *            valid
090         */
091        public SimpleModelFitting(M m, ResidualCalculator<I, D, M> errorModel, DistanceCheck dc)
092        {
093                model = m;
094                this.errorModel = errorModel;
095                this.dc = dc;
096        }
097
098        @Override
099        public List<? extends IndependentPair<I, D>> getInliers() {
100                return inl;
101        }
102
103        @Override
104        public List<? extends IndependentPair<I, D>> getOutliers() {
105                return outl;
106        }
107
108        @Override
109        public boolean fitData(List<? extends IndependentPair<I, D>> data) {
110                if (!model.estimate(data))
111                        return false;
112
113                errorModel.setModel(model);
114
115                inl = new ArrayList<IndependentPair<I, D>>();
116                outl = new ArrayList<IndependentPair<I, D>>();
117
118                for (int i = 0; i < data.size(); i++) {
119                        if (dc.check(errorModel.computeResidual(data.get(i))))
120                                inl.add(data.get(i));
121                        else
122                                outl.add(data.get(i));
123                }
124
125                return true;
126        }
127
128        @Override
129        public M getModel() {
130                return model;
131        }
132
133        @Override
134        public int numItemsToEstimate() {
135                return model.numItemsToEstimate();
136        }
137}