Changeset 741


Ignore:
Timestamp:
Jan 13, 2007, 3:41:40 PM (15 years ago)
Author:
Peter
Message:

fixes #161 and #164

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/regression_test.cc

    r740 r741  
    188188  regression::PolynomialWeighted pol_weighted(2);
    189189  regression::Polynomial polynomial2(2);
    190   // ok = equal(polynomial2, pol_weighted, error) && ok;
     190  ok = equal(polynomial2, pol_weighted, error) && ok;
    191191
    192192  ok = multidim(error) && ok;
     
    234234           << std::endl;
    235235  }
    236   if (fabs(r.prediction_error2(2000)-rw.prediction_error2(2000))>10e-7){
    237     ok = false;
    238     *error << "Error: prediction_error2 not equal non-weighted version.\n"
    239            << "       weighted: " << rw.prediction_error2(2000) << "\n"
    240            << "   non-weighted: " << r.prediction_error2(2000)
    241            << std::endl;
    242   }
    243   if (fabs(r.s2()-rw.s2())>10E-7){
     236  if (fabs(r.s2()-rw.s2(1.0))>10E-7){
     237    ok = false;
     238    *error << "Error: s2 not equal non-weighted version." << std::endl;
     239    *error << "weighted s2 = " << rw.s2(1.0) << std::endl;
     240    *error << "non-weighted s2 = " << r.s2() << std::endl;
     241  }
     242  if (fabs(r.standard_error2(2000)-rw.standard_error2(2000))>10e-7){
     243    ok = false;
     244    *error << "Error: standard_error not equal non-weighted version."
     245           << std::endl;
     246  }
     247  if (fabs(r.r2()-rw.r2())>10E-7){
    244248    ok = false;
    245249    *error << "Error: r2 not equal non-weighted version." << std::endl;
     
    247251    *error << "non-weighted r2 = " << r.r2() << std::endl;
    248252  }
    249   if (fabs(r.s2()-rw.s2())>10E-7){
    250     ok = false;
    251     *error << "Error: s2 not equal non-weighted version." << std::endl;
    252     *error << "weighted s2 = " << rw.s2() << std::endl;
    253     *error << "non-weighted s2 = " << r.s2() << std::endl;
    254   }
    255   if (fabs(r.standard_error2(2000)-rw.standard_error2(2000))>10e-7){
    256     ok = false;
    257     *error << "Error: standard_error not equal non-weighted version."
     253  if (fabs(r.prediction_error2(2000)-rw.prediction_error2(2000,1))>10e-7){
     254    ok = false;
     255    *error << "Error: prediction_error2 not equal non-weighted version.\n"
     256           << "       weighted: " << rw.prediction_error2(2000,1) << "\n"
     257           << "   non-weighted: " << r.prediction_error2(2000)
    258258           << std::endl;
    259259  }
     
    278278  w.scale(2);
    279279  wr.fit(x,y,w);
    280   if (wr.predict(2000) != predict){
     280  if (fabs(wr.predict(2000)-predict)>10e-11){
    281281    ok = false;
    282282    *error << "Error: predict not equal after rescaling.\n";
    283   }
    284   if (wr.prediction_error2(2000,2) != prediction_error2){
    285     ok = false;
    286     *error << "Error: prediction_error2 not equal after rescaling.\n";
    287   }
    288   if (wr.r2() != r2){
    289     ok = false;
    290     *error << "Error: r2 not equal after rescaling.\n";
    291   }
    292   if (wr.s2(2) != s2){
     283    *error << "       predict = " << predict
     284           << " and after doubling weights.\n";
     285    *error << "       predict = " << wr.predict(2000) << "\n";
     286  }
     287  if (fabs(wr.s2(2)-s2)>10e-11){
    293288    ok = false;
    294289    *error << "Error: s2 not equal after rescaling.\n";
    295290    *error << "       s2 = " << s2 << " and after doubling weights.\n";
    296291    *error << "       s2 = " << wr.s2(2) << "\n";
    297   }
    298   if (wr.standard_error2(2000) != standard_error2){
     292    *error << "difference " << s2-wr.s2(2.0) << std::endl;
     293  }
     294  if (fabs(wr.standard_error2(2000)-standard_error2)>10e-6){
    299295    ok = false;
    300296    *error << "Error: standard_error2 not equal after rescaling.\n";
     297    *error << "       standard_error2 = " << standard_error2
     298           << " and after doubling weights.\n";
     299    *error << "       standard_error2 = "
     300           << wr.standard_error2(2000) << "\n";
     301    *error << " difference: " << wr.standard_error2(2000)-standard_error2
     302           << std::endl;
     303  }
     304  if (fabs(wr.r2()-r2)>10e-6){
     305    ok = false;
     306    *error << "Error: r2 not equal after rescaling.\n";
     307  }
     308  if (fabs(wr.prediction_error2(2000,2)-prediction_error2)>10e-6){
     309    ok = false;
     310    *error << "Error: prediction_error2 not equal after rescaling.\n";
     311    *error << "       prediction_error2 = " << prediction_error2
     312           << " and after doubling weights.\n";
     313    *error << "       prediction_error2 = "
     314           << wr.prediction_error2(2000,2) << "\n";
    301315  }
    302316  return ok;
     
    339353    ok = false;
    340354    *error << "Error: r2 not equal.\n";
     355    *error << "   r2: " << r2 << "\n";
     356    *error << "   r2: " << wr.r2() << "\n";
    341357  }
    342358  if (wr.s2() != s2){
     
    411427    *error << "   predict = " << mdw.predict(z) << "\n";
    412428  }
    413   if (fabs(mdw.prediction_error2(z,2)-prediction_error2)>10e-7){
     429  if (fabs(mdw.prediction_error2(z,2)-prediction_error2)>10e-7){ 
    414430    ok = false;
    415431    *error << "Error: prediction_error2 not equal after rescaling.\n";
  • trunk/yat/regression/MultiDimensionalWeighted.cc

    r740 r741  
    4343      gsl_multifit_linear_free(work_);
    4444  }
     45
     46
     47  double MultiDimensionalWeighted::chisq() const
     48  {
     49    return chisquare_;
     50  }
     51
    4552
    4653  void MultiDimensionalWeighted::fit(const utility::matrix& x,
  • trunk/yat/regression/MultiDimensionalWeighted.h

    r740 r741  
    5252
    5353    ///
     54    /// @return sum of squared residuals
     55    ///
     56    double chisq(void) const;
     57
     58    ///
    5459    /// @see gsl_multifit_wlinear
    5560    ///
  • trunk/yat/regression/Polynomial.cc

    r729 r741  
    4949  void Polynomial::fit(const utility::vector& x, const utility::vector& y)
    5050  {
     51    ap_.add_values(x,y);
    5152    utility::matrix X=utility::matrix(x.size(),power_+1,1);
    5253    for (size_t i=0; i<X.rows(); ++i)
  • trunk/yat/regression/PolynomialWeighted.cc

    r740 r741  
    4747    assert(x.size()==y.size());
    4848    assert(y.size()==w.size());
     49    ap_.reset();
     50    // AveragerPairWeighted requires 2 weights but works only on the
     51    // product wx*wy, so we can send in w and a dummie to get what we
     52    // want.
     53    ap_.add_values(x,y,utility::vector(x.size(),1),w);
    4954    utility::matrix X=utility::matrix(x.size(),power_+1,1);
    5055    for (size_t i=0; i<X.rows(); ++i)
     
    5257        X(i,j)=X(i,j-1)*x(i);
    5358    md_.fit(X,y,w);
     59    chisq_=md_.chisq();
    5460  }
    5561
     
    7480  }
    7581
    76   double PolynomialWeighted::prediction_error2(const double x,
    77                                                const double w) const
    78   {
    79     utility::vector vec(power_+1,1);
    80     for (size_t i=1; i<=power_; ++i)
    81       vec(i) = vec(i-1)*x;
    82     return md_.prediction_error2(vec, w);
    83   }
    84 
    8582  double PolynomialWeighted::standard_error2(const double x) const
    8683  {
  • trunk/yat/regression/PolynomialWeighted.h

    r740 r741  
    8585
    8686    ///
    87     /// @return expected prediction error for a new data point in @a x
    88     /// with weight @a w
    89     ///
    90     double prediction_error2(const double x, const double w=1) const;
    91 
    92     ///
    9387    /// @return error of model value in @a x
    9488    ///
Note: See TracChangeset for help on using the changeset viewer.