Changeset 726 for trunk/yat


Ignore:
Timestamp:
Jan 4, 2007, 3:38:56 PM (16 years ago)
Author:
Peter
Message:

fixes #165 added test checking Linear Regression is equivalent to Polynomial regression of degree one.

Location:
trunk/yat/regression
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/regression/Linear.cc

    r724 r726  
    3232  Linear::Linear(void)
    3333    : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0),
    34       chisq_(0), m_x_(0)
     34      chisq_(0)
    3535  {
    3636  }
     
    4545  }
    4646
    47   double Linear::alpha_err(void) const
     47  double Linear::alpha_var(void) const
    4848  {
    49     return sqrt(alpha_var_);
     49    return alpha_var_;
    5050  }
    5151
     
    5555  }
    5656
    57   double Linear::beta_err(void) const
     57  double Linear::beta_var(void) const
    5858  {
    59     return sqrt(beta_var_);
     59    return beta_var_;
    6060  }
    6161
     
    7575
    7676    // calculating deviation between data and model
    77     chisq_ = ( (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()*
    78               ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() ) /
    79                (x.size()-2) );
    80     r2_= 1-chisq_/ap_.x_averager().variance();
    81     alpha_var_ = chisq_ / x.size();
    82     beta_var_ = chisq_ / ap_.x_averager().sum_xx_centered();
    83     m_x_ = ap_.x_averager().mean();
     77    chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()*
     78              ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() );
     79    r2_= 1-chisq_/ap_.x_averager().sum_xx_centered();
     80    alpha_var_ = s2() / x.size();
     81    beta_var_ = s2() / ap_.x_averager().sum_xx_centered();
    8482  }
    8583
    8684  double Linear::predict(const double x) const
    8785  {
    88     return alpha_ + beta_ * (x-m_x_);
     86    return alpha_ + beta_ * (x - ap_.x_averager().mean());
    8987  }
    9088
     
    9492  }
    9593
     94  double Linear::s2(void) const
     95  {
     96    return chisq()/(ap_.n()-2);
     97  }
     98
    9699  double Linear::standard_error(const double x) const
    97100  {
    98     return sqrt( alpha_var_+beta_var_*(x-m_x_)*(x-m_x_));
     101    return sqrt( alpha_var_+beta_var_*(x-ap_.x_averager().mean())*
     102                 (x-ap_.x_averager().mean()) );
    99103  }
    100104
  • trunk/yat/regression/Linear.h

    r718 r726  
    6565
    6666    /**
    67        The standard deviation is estimated as \f$
    68        \sqrt{\frac{\chi^2}{n}} \f$
     67       The standard deviation is estimated as \f$ \sqrt{\frac{s^2}{n}}
     68       \f$ where \f$ s^2 = \frac{\sum \epsilon^2}{n-2} \f$
    6969       
    7070       @return standard deviation of parameter \f$ \alpha \f$
    7171    */
    72     double alpha_err(void) const;
     72    double alpha_var(void) const;
    7373
    7474    /**
     
    8181
    8282    /**
    83        The standard deviation is estimated as \f$
    84        \sqrt{\frac{\chi^2}{\sum (x_i-m_x)^2}} \f$
    85        
     83       The standard deviation is estimated as \f$ \frac{s^2}{\sum
     84       (x-m_x)^2} \f$ where \f$ s^2 = \frac{\sum \epsilon^2}{n-2} \f$
     85
    8686       @return standard deviation of parameter \f$ \beta \f$
    8787    */
    88     double beta_err(void) const;
     88    double beta_var(void) const;
    8989
    9090  /**
    91        @brief Mean Squared Error
    92    
    93        Chisq is calculated as \f$ \frac{\sum
    94        (y_i-\alpha-\beta(x_i-m_x))^2}{n-2} \f$
     91       Chi-squared is calculated as \f$ \sum
     92       (y_i-\alpha-\beta(x_i-m_x))^2 \f$
    9593    */
    9694    double chisq(void) const;
     
    129127    Linear(const Linear&);
    130128
     129    double s2(void) const;
     130
    131131    double alpha_;
    132132    double alpha_var_;
     
    134134    double beta_var_;
    135135    double chisq_;
    136     double m_x_; // average of x values
    137136    double r2_; // coefficient of determination
    138137  };
  • trunk/yat/regression/Local.h

    r718 r726  
    102102
    103103  ///
    104   /// The output operator for the RegressionLocal class.
     104  /// The output operator for the Regression::Local class.
    105105  ///
    106106  std::ostream& operator<<(std::ostream&, const Local& );
  • trunk/yat/regression/MultiDimensional.cc

    r718 r726  
    4141    if (work_)
    4242      gsl_multifit_linear_free(work_);
     43  }
     44
     45
     46  const utility::matrix& MultiDimensional::covariance(void) const
     47  {
     48    return covariance_;
    4349  }
    4450
  • trunk/yat/regression/MultiDimensional.h

    r718 r726  
    5252
    5353    ///
     54    /// @brief covariance of parameters
     55    ///
     56    const utility::matrix& covariance(void) const;
     57
     58    ///
    5459    /// Function fitting parameters of the linear model by miminizing
    5560    /// the quadratic deviation between model and data.
  • trunk/yat/regression/Naive.cc

    r713 r726  
    4747  double Naive::chisq(void) const
    4848  {
    49     return ap_.y_averager().sum_xx_centered()/(ap_.n()-1);
     49    return ap_.y_averager().sum_xx_centered();
    5050  }
    5151 
  • trunk/yat/regression/Naive.h

    r713 r726  
    5858         
    5959    /**
    60         \f$\frac{1}{N-1} \sum (x_i-m)^2 \f$
    61    
    62         @brief Mean Squared Error
     60        Chi-squared \f$ \sum (x_i-m)^2 \f$
    6361    */
    6462    double chisq(void) const;
  • trunk/yat/regression/OneDimensional.h

    r718 r726  
    5656 
    5757    /**
    58        @brief Mean Squared Error
     58       @brief Chi-squared
    5959       
    60        Mean Squared Error is defined as the \f$ \frac
    61        {\sum{(\hat{y_i}-y_i)^2}}{df} \f$ where \f$ df \f$ number of
    62        degree of freedom typically is number data points minus number
    63        of paramers in model.
     60       Chi-squared is defined as the \f$ \frac
     61       {\sum{(\hat{y_i}-y_i)^2}}{1} \f$
    6462    */
    6563    virtual double chisq(void) const=0;
  • trunk/yat/regression/Polynomial.cc

    r713 r726  
    4747
    4848
     49  const utility::matrix& Polynomial::covariance(void) const
     50  {
     51    return md_.covariance();
     52  }
     53
     54
    4955  void Polynomial::fit(const utility::vector& x, const utility::vector& y)
    5056  {
  • trunk/yat/regression/Polynomial.h

    r713 r726  
    5858
    5959    ///
     60    /// @brief covariance of parameters
     61    ///
     62    const utility::matrix& covariance(void) const;
     63
     64    ///
    6065    /// Fit the model by minimizing the mean squared deviation between
    6166    /// model and data.
     
    7176
    7277    ///
    73     /// @brief Variance of residuals
     78    /// @brief Sum of squared residuals
    7479    ///
    7580    double chisq(void) const;
Note: See TracChangeset for help on using the changeset viewer.