Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

LinearRegression.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Clemens Groepl $
00032 // $Authors: $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_MATH_STATISTICS_LINEARREGRESSION_H
00036 #define OPENMS_MATH_STATISTICS_LINEARREGRESSION_H
00037 
00038 #include <OpenMS/CONCEPT/Types.h>
00039 #include <OpenMS/CONCEPT/Exception.h>
00040 
00041 #include <iostream>
00042 #include <vector>
00043 #include <gsl/gsl_fit.h>
00044 #include <gsl/gsl_statistics.h>
00045 #include <gsl/gsl_cdf.h>
00046 
00047 namespace OpenMS
00048 {
00049   namespace Math
00050   {
00069     class OPENMS_DLLAPI LinearRegression
00070     {
00071 public:
00072 
00074       LinearRegression() :
00075         intercept_(0),
00076         slope_(0),
00077         x_intercept_(0),
00078         lower_(0),
00079         upper_(0),
00080         t_star_(0),
00081         r_squared_(0),
00082         stand_dev_residuals_(0),
00083         mean_residuals_(0),
00084         stand_error_slope_(0),
00085         chi_squared_(0),
00086         rsd_(0)
00087       {
00088       }
00089 
00091       virtual ~LinearRegression()
00092       {
00093       }
00094 
00111       template <typename Iterator>
00112       void computeRegression(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin);
00113 
00129       template <typename Iterator>
00130       void computeRegressionNoIntercept(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin);
00131 
00148       template <typename Iterator>
00149       void computeRegressionWeighted(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin);
00150 
00151 
00153       DoubleReal getIntercept() const;
00155       DoubleReal getSlope() const;
00157       DoubleReal getXIntercept() const;
00159       DoubleReal getLower() const;
00161       DoubleReal getUpper() const;
00163       DoubleReal getTValue() const;
00165       DoubleReal getRSquared() const;
00167       DoubleReal getStandDevRes() const;
00169       DoubleReal getMeanRes() const;
00171       DoubleReal getStandErrSlope() const;
00173       DoubleReal getChiSquared() const;
00175       DoubleReal getRSD() const;
00176 
00177 protected:
00178 
00180       double intercept_;
00182       double slope_;
00184       double x_intercept_;
00186       double lower_;
00188       double upper_;
00190       double t_star_;
00192       double r_squared_;
00194       double stand_dev_residuals_;
00196       double mean_residuals_;
00198       double stand_error_slope_;
00200       double chi_squared_;
00202       double rsd_;
00203 
00204 
00206       void computeGoodness_(double * X, double * Y, int N, double confidence_interval_P);
00207 
00209       template <typename Iterator>
00210       void iteratorRange2Arrays_(Iterator x_begin, Iterator x_end, Iterator y_begin, double * x_array, double * y_array);
00211 
00213       template <typename Iterator>
00214       void iteratorRange3Arrays_(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double * x_array, double * y_array, double * w_array);
00215 
00216 private:
00217 
00219       LinearRegression(const LinearRegression & arg);
00221       LinearRegression & operator=(const LinearRegression & arg);
00222     };
00223 
00224     template <typename Iterator>
00225     void LinearRegression::computeRegression(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin)
00226     {
00227       int N = int(distance(x_begin, x_end));
00228 
00229       double * X = new double[N];
00230       double * Y = new double[N];
00231       iteratorRange2Arrays_(x_begin, x_end, y_begin, X, Y);
00232 
00233       double cov00, cov01, cov11;
00234 
00235       // Compute the unweighted linear fit.
00236       // Get the intercept and the slope of the regression Y_hat=intercept_+slope_*X
00237       // and the value of Chi squared, the covariances of the intercept and the slope
00238       int error = gsl_fit_linear(X, 1, Y, 1, N, &intercept_, &slope_, &cov00, &cov01, &cov11, &chi_squared_);
00239 
00240       if (!error)
00241       {
00242         computeGoodness_(X, Y, N, confidence_interval_P);
00243       }
00244 
00245       delete[] X;
00246       delete[] Y;
00247 
00248       if (error)
00249       {
00250         throw Exception::UnableToFit(__FILE__, __LINE__, __PRETTY_FUNCTION__, "UnableToFit-LinearRegression", "Could not fit a linear model to the data");
00251       }
00252     }
00253 
00254     template <typename Iterator>
00255     void LinearRegression::computeRegressionNoIntercept(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin)
00256     {
00257       int N = int(distance(x_begin, x_end));
00258 
00259       double * X = new double[N];
00260       double * Y = new double[N];
00261       iteratorRange2Arrays_(x_begin, x_end, y_begin, X, Y);
00262 
00263       double cov;
00264 
00265       // Compute the linear fit.
00266       // Get the intercept and the slope of the regression Y_hat=intercept_+slope_*X
00267       // and the value of Chi squared, the covariances of the intercept and the slope
00268       int error = gsl_fit_mul(X, 1, Y, 1, N, &slope_, &cov, &chi_squared_);
00269       intercept_ = 0.0;
00270 
00271       if (!error)
00272       {
00273         computeGoodness_(X, Y, N, confidence_interval_P);
00274       }
00275 
00276       delete[] X;
00277       delete[] Y;
00278 
00279       if (error)
00280       {
00281         throw Exception::UnableToFit(__FILE__, __LINE__, __PRETTY_FUNCTION__, "UnableToFit-LinearRegression", "Could not fit a linear model to the data");
00282       }
00283     }
00284 
00285     template <typename Iterator>
00286     void LinearRegression::computeRegressionWeighted(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
00287     {
00288       int N = int(distance(x_begin, x_end));
00289 
00290       double * X = new double[N];
00291       double * Y = new double[N];
00292       double * W = new double[N];
00293       iteratorRange3Arrays_(x_begin, x_end, y_begin, w_begin, X, Y, W);
00294 
00295       double cov00, cov01, cov11;
00296 
00297       // Compute the weighted linear fit.
00298       // Get the intercept and the slope of the regression Y_hat=intercept_+slope_*X
00299       // and the value of Chi squared, the covariances of the intercept and the slope
00300       int error = gsl_fit_wlinear(X, 1, W, 1, Y, 1, N, &intercept_, &slope_, &cov00, &cov01, &cov11, &chi_squared_);
00301 
00302       if (!error)
00303       {
00304         computeGoodness_(X, Y, N, confidence_interval_P);
00305       }
00306 
00307       delete[] X;
00308       delete[] Y;
00309       delete[] W;
00310 
00311       if (error)
00312       {
00313         throw Exception::UnableToFit(__FILE__, __LINE__, __PRETTY_FUNCTION__, "UnableToFit-LinearRegression", "Could not fit a linear model to the data");
00314       }
00315     }
00316 
00317     template <typename Iterator>
00318     void LinearRegression::iteratorRange2Arrays_(Iterator x_begin, Iterator x_end, Iterator y_begin, double * x_array, double * y_array)
00319     {
00320       int i = 0;
00321       while (x_begin < x_end)
00322       {
00323         x_array[i] = *x_begin;
00324         y_array[i] = *y_begin;
00325         ++x_begin;
00326         ++y_begin;
00327         ++i;
00328       }
00329     }
00330 
00331     template <typename Iterator>
00332     void LinearRegression::iteratorRange3Arrays_(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double * x_array, double * y_array, double * w_array)
00333     {
00334       int i = 0;
00335       while (x_begin < x_end)
00336       {
00337         x_array[i] = *x_begin;
00338         y_array[i] = *y_begin;
00339         w_array[i] = *w_begin;
00340         ++x_begin;
00341         ++y_begin;
00342         ++w_begin;
00343         ++i;
00344       }
00345     }
00346 
00347   } // namespace Math
00348 } // namespace OpenMS
00349 
00350 
00351 #endif

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:40 using doxygen 1.7.1