Changeset 204


Ignore:
Timestamp:
Nov 1, 2004, 7:13:55 PM (19 years ago)
Author:
Peter
Message:

Minor changes

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/RegressionLinear.h

    r202 r204  
    4343    /// This function computes the best-fit linear regression
    4444    /// coefficients (k,m) of the model  \f$ y = kx + m \f$ from
    45     /// vectors \a x and \a y, by minimizing \f$ \sum{(kx_i+m-x_i)^2}
     45    /// vectors \a x and \a y, by minimizing \f$ \sum{(kx_i+m-y_i)^2}
    4646    /// \f$.
    4747    ///
    4848    inline int fit(const gslapi::vector& x, const gslapi::vector& y)
    49       { return gsl_fit_linear_vector(x.gsl_vector_pointer(),
    50                                     y.gsl_vector_pointer(), &m_, &k_,
    51                                      &cov00_, &cov01_, &cov11_, &sumsq_); }
     49    { return gsl_fit_linear_vector(x.gsl_vector_pointer(),
     50                                  y.gsl_vector_pointer(), &m_, &k_,
     51                                   &cov00_, &cov01_, &cov11_, &sumsq_); } 
    5252
    5353    ///
    5454    /// This function computes the best-fit linear regression
    55     /// coefficients (k,m) of the model \f$ y = kx + m \f$ from from
    56     /// vectors \a x and \a y, by minimizing \f$ \sum
    57     /// w_i(kx_i+m-x_i)^2 \f$.
     55    /// coefficients (k,m) of the model \f$ y = kx + m \f$ from
     56    /// vectors \a x and \a y, by minimizing \f$ \sum w_i(kx_i+m-y_i)^2
     57    /// \f$. The weight \f$ w_i \f$ is the inverse of the variance for
     58    /// \f$ y_i \f$
    5859    ///
    59     inline int fit_weighted(const gslapi::vector x, const gslapi::vector y,
    60                            const gslapi::vector w)
    61       { return gsl_fit_wlinear_vector(x.gsl_vector_pointer(),
    62                                       w.gsl_vector_pointer(),
    63                                       y.gsl_vector_pointer(), &m_, &k_,
    64                                       &cov00_, &cov01_, &cov11_, &sumsq_); }
     60    inline int fit_weighted(gslapi::vector& x,gslapi::vector& y,
     61                            gslapi::vector& w)
     62    { return gsl_fit_wlinear_vector(x.gsl_vector_pointer(),
     63                                    w.gsl_vector_pointer(),
     64                                    y.gsl_vector_pointer(),
     65                                    &m_, &k_,
     66                                    &cov00_, &cov01_, &cov11_, &sumsq_); } 
    6567         
    6668  private:
  • trunk/src/RegressionNaive.cc

    r202 r204  
    2121  }
    2222
    23   int RegressionNaive::fit(const gslapi::vector& x, const gslapi::vector& y)
    24   {
    25     Averager a;
    26     for (size_t i=0; i<y.size(); i++)
    27       a.add(y(i));
    28     m_=a.mean();
    29     var_=a.variance();
    30     return 0;
    31   }
    3223
    33   int RegressionNaive::fit_weighted(const gslapi::vector& x,
    34                                     const gslapi::vector& y,
    35                                     const gslapi::vector& w)
    36   {
    37     WeightedAverager a;
    38     for (size_t i=0; i<y.size(); i++)
    39       a.add(y(i),w(i));
    40     m_=a.mean();
    41     var_=a.standard_error()*a.standard_error();
    42     return 0;
    43   }
    4424
    4525}} // of namespace cpptools and namespace theplu
  • trunk/src/RegressionNaive.h

    r202 r204  
    66// C++ tools include
    77/////////////////////
     8#include "Averager.h"
    89#include "Regression.h"
    910#include "vector.h"
    10 
     11#include "WeightedAverager.h"
    1112// Standard C++ includes
    1213////////////////////////
     
    4546    /// \sum{(y_i-m)^2} \f$.
    4647    ///
    47     int fit(const gslapi::vector& x, const gslapi::vector& y);
    48    
     48    inline int fit(const gslapi::vector& x, const gslapi::vector& y)
     49    {
     50      Averager a;
     51      for (size_t i=0; i<y.size(); i++)
     52        a.add(y(i));
     53      m_=a.mean();
     54      var_=a.standard_error()*a.standard_error();
     55      return 0;
     56    }
    4957
    5058    ///
    5159    /// This function computes the best-fit for the naive model \f$ y
    5260    /// = m \f$ from vectors \a x and \a y, by minimizing \f$ \sum
    53     /// w_i(y_i-m)^2 \f$.
     61    /// w_i(y_i-m)^2 \f$. The weight \f$ w_i \f$ is the inverse of the
     62    /// variance for \f$ y_i \f$
    5463    ///
    55     int fit_weighted(const gslapi::vector& x, const gslapi::vector& y,
    56                      const gslapi::vector& w);
    57    
     64    inline int  fit_weighted(gslapi::vector x,gslapi::vector y,gslapi::vector w)
     65    {
     66      WeightedAverager a;
     67      for (size_t i=0; i<y.size(); i++)
     68        a.add(y(i), w(i));
     69      m_=a.mean();
     70      var_=a.standard_error()*a.standard_error();
     71      return 0;
     72    }
     73
    5874         
    5975  private:
    60     double m_;    // only parameter in naive model
    61     double var_;  // variance of the parameter m_
     76    double var_;
     77    double m_;
     78    double sumsq_;
    6279
    6380  };
Note: See TracChangeset for help on using the changeset viewer.