Changeset 739


Ignore:
Timestamp:
Jan 12, 2007, 1:14:21 PM (15 years ago)
Author:
Peter
Message:

fixes #178 and removes a bug in MultiDimensional?

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/regression_test.cc

    r731 r739  
    372372    ok = false;
    373373    *error << "Error: predict not equal\n"
    374            << "   weighted: " << mdw.predict(1) << "\n"
    375            << "   non-weighted: " << md.predict(1)
    376            << std::endl;
    377   }
    378   /*
     374           << "   weighted: " << mdw.predict(z) << "\n"
     375           << "   non-weighted: " << md.predict(z)
     376           << std::endl;
     377  }
     378
    379379  if (md.standard_error2(z) != mdw.standard_error2(z)){
    380380    ok = false;
     
    384384           << std::endl;
    385385  }
    386   */
     386  if (fabs(md.prediction_error2(z)-mdw.prediction_error2(z,1.0))>10e-7){
     387    ok = false;
     388    *error << "Error: prediction_error2 not equal\n"
     389           << "   weighted: " << mdw.prediction_error2(z,1.0) << "\n"
     390           << "   non-weighted: " << md.prediction_error2(z)
     391           << std::endl;
     392  }
     393
    387394  return ok;
    388395}
  • trunk/yat/regression/MultiDimensional.cc

    r731 r739  
    6363                        fit_parameters_.gsl_vector_p(),
    6464                        covariance_.gsl_matrix_p(),&chisquare_,work_);
     65    s2_ = chisquare_/(x.rows()-x.columns());
    6566  }
    6667
     
    8687  double MultiDimensional::prediction_error2(const utility::vector& x) const
    8788  {
    88     return standard_error2(x)+chisquare_;
     89    return standard_error2(x) + s2_;
    8990  }
    9091
  • trunk/yat/regression/MultiDimensional.h

    r731 r739  
    9090  private:
    9191    double chisquare_;
     92    double s2_;
    9293    utility::matrix covariance_;
    9394    utility::vector fit_parameters_;
  • trunk/yat/regression/MultiDimensionalWeighted.cc

    r731 r739  
    2323
    2424#include "MultiDimensionalWeighted.h"
     25#include "yat/statistics/AveragerWeighted.h"
    2526#include "yat/utility/matrix.h"
    2627#include "yat/utility/vector.h"
     
    5859                         fit_parameters_.gsl_vector_p(),
    5960                         covariance_.gsl_matrix_p(),&chisquare_,work_);
     61
     62    statistics::AveragerWeighted aw;
     63    aw.add_values(y,w);
     64    s2_ = chisquare_ / (aw.n()-fit_parameters_.size());
     65    covariance_ *= s2_;
    6066  }
    6167
     
    6672  }
    6773
    68   double MultiDimensionalWeighted::prediction_error(const utility::vector& x,
    69                                                     const double w) const
     74  double MultiDimensionalWeighted::prediction_error2(const utility::vector& x,
     75                                                     const double w) const
    7076  {
    71     double s2 = 0;
    72     for (size_t i=0; i<x.size(); ++i){
    73       s2 += covariance_(i,i)*x(i)*x(i);
    74       for (size_t j=i+1; j<x.size(); ++j)
    75         s2 += 2*covariance_(i,j)*x(i)*x(j);
    76     }
    77     return sqrt(s2+chisquare_/w);
     77    return standard_error2(x) + s2_/w;
    7878  }
    7979
     
    8282  MultiDimensionalWeighted::standard_error2(const utility::vector& x) const
    8383  {
    84     double s2 = 0;
     84    double c = 0;
    8585    for (size_t i=0; i<x.size(); ++i){
    86       s2 += covariance_(i,i)*x(i)*x(i);
     86      c += covariance_(i,i)*x(i)*x(i);
    8787      for (size_t j=i+1; j<x.size(); ++j)
    88         s2 += 2*covariance_(i,j)*x(i)*x(j);
     88        c += 2*covariance_(i,j)*x(i)*x(j);
    8989    }
    90     return s2;
     90    return c;
    9191  }
    9292
  • trunk/yat/regression/MultiDimensionalWeighted.h

    r731 r739  
    6363
    6464    ///
    65     /// @return expected prediction error for a new data point in @a x
     65    /// @return expected squared prediction error for a new data point
     66    /// in @a x
    6667    ///
    67     double prediction_error(const utility::vector& x, const double w) const;
     68    double prediction_error2(const utility::vector& x, const double w) const;
    6869
    6970    ///
     
    8182    utility::matrix covariance_;
    8283    utility::vector fit_parameters_;
     84    double s2_;
    8385    gsl_multifit_linear_workspace* work_;
    8486
  • trunk/yat/regression/PolynomialWeighted.cc

    r731 r739  
    6767  }
    6868
    69   double PolynomialWeighted::prediction_error(const double x,
    70                                               const double w) const
     69  double PolynomialWeighted::prediction_error2(const double x,
     70                                               const double w) const
    7171  {
    7272    utility::vector vec(power_+1,1);
    7373    for (size_t i=1; i<=power_; ++i)
    7474      vec(i) = vec(i-1)*x;
    75     return md_.prediction_error(vec, w);
     75    return md_.prediction_error2(vec, w);
    7676  }
    7777
  • trunk/yat/regression/PolynomialWeighted.h

    r729 r739  
    8181    /// with weight @a w
    8282    ///
    83     double prediction_error(const double x, const double w=1) const;
     83    double prediction_error2(const double x, const double w=1) const;
    8484
    8585    ///
Note: See TracChangeset for help on using the changeset viewer.