# Changeset 1230

Ignore:
Timestamp:
Mar 14, 2008, 1:24:07 PM (16 years ago)
Message:

refs #223 - regression_test

File:
1 edited

Unmodified
Added
Removed
• ## trunk/test/regression_test.cc

 r1210 02111-1307, USA. */ #include "Suite.h" #include "yat/regression/KernelBox.h" using namespace theplu::yat; bool equal(regression::OneDimensional&, regression::OneDimensionalWeighted&, std::ostream*); bool multidim(std::ostream* error); bool unity_weights(regression::OneDimensional&, void equal(regression::OneDimensional&, regression::OneDimensionalWeighted&, test::Suite&); void multidim(test::Suite& suite); void unity_weights(regression::OneDimensional&, regression::OneDimensionalWeighted&, const utility::Vector&, const utility::Vector&, std::ostream*); bool rescale_weights(regression::OneDimensionalWeighted&, test::Suite&); void rescale_weights(regression::OneDimensionalWeighted&, const utility::Vector&, const utility::Vector&, std::ostream*); bool zero_weights(regression::OneDimensionalWeighted&, test::Suite&); void zero_weights(regression::OneDimensionalWeighted&, const utility::Vector&, const utility::Vector&, std::ostream*); test::Suite&); regression::Kernel&); int main(const int argc,const char* argv[]) { std::ostream* error; if (argc>1 && argv[1]==std::string("-v")) error = &std::cerr; else { error = new std::ofstream("/dev/null"); if (argc>1) std::cout << "regression_test -v : for printing extra information\n"; } *error << "  testing regression" << std::endl; bool ok = true; int main(int argc, char* argv[]) { test::Suite suite(argc, argv); suite.err() << "  testing regression" << std::endl; // test data for Linear and Naive (Weighted and non-weighted) regression::Polynomial polynomial(1); polynomial.fit(x,y); if ( std::abs(linear.beta()-polynomial.fit_parameters()(1))>0.0001 ){ *error << "error: beta and fit_parameters(1) not equal" << std::endl; *error << "       beta = " << linear.beta() << std::endl; *error << "       fit_parameters(1) = " if ( test::equal(linear.beta(),polynomial.fit_parameters()(1)) ){ suite.err() << "error: beta and fit_parameters(1) not equal" << std::endl; suite.err() << "       beta = " << linear.beta() << std::endl; suite.err() << "       fit_parameters(1) = " << polynomial.fit_parameters()(1) << std::endl; ok = false; } if ( std::abs(polynomial.fit_parameters()(0)-linear.alpha()+ linear.beta()*1985)>0.0001){ *error << "error: fit_parameters(0) = " suite.ok(false); } if (test::equal(polynomial.fit_parameters()(0), linear.alpha()-linear.beta()*1985)){ suite.err() << "error: fit_parameters(0) = " << polynomial.fit_parameters()(0)<< std::endl; *error << "error: alpha-beta*m_x = " suite.err() << "error: alpha-beta*m_x = " << linear.alpha()-linear.beta()*1985 << std::endl; ok = false; suite.ok(false); } if ( std::abs(polynomial.chisq()-linear.chisq())>0.0001){ *error << "error: chisq not same in linear and polynomial(1)" << std::endl; ok = false; suite.err() << "error: chisq not same in linear and polynomial(1)" << std::endl; suite.ok(false); } if ( std::abs(polynomial.predict(1.0)-linear.predict(1.0))>0.0001){ *error << "error: predict not same in linear and polynomial(1)" << std::endl; ok = false; suite.err() << "error: predict not same in linear and polynomial(1)" << std::endl; suite.ok(false); } if ( std::abs(polynomial.standard_error2(1985)-linear.standard_error2(1985)) >0.0001){ *error << "error: standard_error not same in linear and polynomial(1)" suite.err() << "error: standard_error not same in linear and polynomial(1)" << "\n  polynomial: " << polynomial.standard_error2(1985) << "\n  linear: " << linear.standard_error2(1985) << std::endl; ok = false; } *error << "  testing regression::LinearWeighted" << std::endl; suite.ok(false); } suite.err() << "  testing regression::LinearWeighted" << std::endl; regression::LinearWeighted linear_w; ok = equal(linear, linear_w, error) && ok; equal(linear, linear_w, suite); linear_w.fit(x,y,w); double y_predicted = linear_w.predict(1990); double y_predicted_err = linear_w.prediction_error2(1990); if (std::abs(y_predicted-12.8)>0.001){ *error << "error: cannot reproduce fit." << std::endl; *error << "predicted value: " << y_predicted << " expected 12.8" << std::endl; ok=false; suite.err() << "error: cannot reproduce fit." << std::endl; suite.err() << "predicted value: " << y_predicted << " expected 12.8" << std::endl; suite.ok(false); } // Comparing LinearWeighted and PolynomialWeighted(1) *error << "    comparing LinearWeighted and PolynomialWeighted(1)" suite.err() << "    comparing LinearWeighted and PolynomialWeighted(1)" << std::endl; linear_w.fit(x,y,w); polynomial_w.fit(x,y,w); if ( std::abs(linear_w.beta()-polynomial_w.fit_parameters()(1))>10e-7 ){ *error << "error: beta and fit_parameters(1) not equal" << std::endl; *error << "       beta = " << linear_w.beta() << std::endl; *error << "       fit_parameters(1) = " suite.err() << "error: beta and fit_parameters(1) not equal" << std::endl; suite.err() << "       beta = " << linear_w.beta() << std::endl; suite.err() << "       fit_parameters(1) = " << polynomial_w.fit_parameters()(1) << std::endl; ok = false; suite.ok(false); } if ( std::abs(polynomial_w.fit_parameters()(0)-linear_w.alpha()+ linear_w.beta()*1990)>0.0001){ *error << "error: fit_parameters(0) = " suite.err() << "error: fit_parameters(0) = " << polynomial.fit_parameters()(0)<< std::endl; *error << "error: alpha-beta*m_x = " suite.err() << "error: alpha-beta*m_x = " << linear.alpha()-linear.beta()*1990 << std::endl; ok = false; suite.ok(false); } if ( std::abs(polynomial_w.s2()-linear_w.s2())>0.0001){ *error << "error: chisq not same in linear and polynomial(1)" << std::endl; ok = false; suite.err() << "error: chisq not same in linear and polynomial(1)" << std::endl; suite.ok(false); } if ( std::abs(polynomial_w.predict(1.0)-linear_w.predict(1.0))>0.0001){ *error << "error: predict not same in linear and polynomial(1)" << std::endl; ok = false; suite.err() << "error: predict not same in linear and polynomial(1)" << std::endl; suite.ok(false); } if ( std::abs(polynomial_w.standard_error2(1985)-linear_w.standard_error2(1985)) >0.0001){ *error << "error: standard_error not same in linear and polynomial(1)" suite.err() << "error: standard_error not same in linear and polynomial(1)" << "\n  polynomial: " << polynomial.standard_error2(1985) << "\n  linear: " << linear.standard_error2(1985) << std::endl; ok = false; suite.ok(false); } // testing regression::NaiveWeighted *error << "  testing regression::NaiveWeighted" << std::endl; suite.err() << "  testing regression::NaiveWeighted" << std::endl; regression::NaiveWeighted naive_w; regression::Naive naive; ok = equal(naive, naive_w, error) && ok; equal(naive, naive_w, suite); naive_w.fit(x,y,w); y_predicted=naive_w.predict(0.0); y_predicted_err=naive_w.prediction_error2(0.0); if (y_predicted!=(0.1*12+0.2*11+0.3*14+0.4*13)) { *error << "regression_NaiveWeighted: cannot reproduce fit." << std::endl; *error << "returned value: " << y_predicted << std::endl; *error << "expected: " << 0.1*12+0.2*11+0.3*14+0.4*13 << std::endl; ok=false; if (!test::equal(y_predicted,0.1*12+0.2*11+0.3*14+0.4*13)) { suite.err() << "regression_NaiveWeighted: cannot reproduce fit.\n"; suite.err() << "returned value: " << y_predicted << std::endl; suite.err() << "expected: " << 0.1*12+0.2*11+0.3*14+0.4*13 << std::endl; suite.ok(false); } // testing regression::Local *error << "  testing regression::Local" << std::endl; suite.err() << "  testing regression::Local" << std::endl; regression::KernelBox kb; regression::LinearWeighted rl; if (!Local_test(rl,kb)) { *error << "regression_Local: Linear cannot reproduce fit." << std::endl; ok=false; suite.err() << "regression_Local: Linear cannot reproduce fit." << std::endl; suite.ok(false); } regression::NaiveWeighted rn; if (!Local_test(rn,kb)) { *error << "regression_Local: Naive cannot reproduce fit." << std::endl; ok=false; suite.err() << "regression_Local: Naive cannot reproduce fit." << std::endl; suite.ok(false); } // testing regression::Polynomial *error << "  testing regression::Polynomial" << std::endl; suite.err() << "  testing regression::Polynomial" << std::endl; { std::ifstream s("data/regression_gauss.data"); if (std::abs(fit(0)-1.012229646706 + fit(1)-0.012561322528 + fit(2)+1.159674470130)>1e-11) { *error << "regression_Polynomial: cannot reproduce fit." << std::endl; ok=false; suite.err() << "regression_Polynomial: cannot reproduce fit." << std::endl; suite.ok(false); } } *error << "  testing regression::PolynomialWeighted" << std::endl; suite.err() << "  testing regression::PolynomialWeighted" << std::endl; regression::PolynomialWeighted pol_weighted(2); regression::Polynomial polynomial2(2); ok = equal(polynomial2, pol_weighted, error) && ok; ok = multidim(error) && ok; if (!ok) *error << "Test failed\n" << std::endl; if (error!=&std::cerr) delete error; return (ok ? 0 : -1); } bool equal(regression::OneDimensional& r, equal(polynomial2, pol_weighted, suite); multidim(suite); return suite.return_value(); } void equal(regression::OneDimensional& r, regression::OneDimensionalWeighted& wr, std::ostream* error) { bool ok=true; test::Suite& suite) { utility::Vector x(5); x(0)=1970; x(1)=1980; x(2)=1990; x(3)=2000; x(4)=2010; utility::Vector y(5); y(0)=12;   y(1)=11;   y(2)=14;   y(3)=13;   y(4)=15; ok = unity_weights(r, wr, x, y, error) && ok; ok = rescale_weights(wr, x, y, error) && ok; ok = zero_weights(wr, x, y, error) && ok; return ok; } bool unity_weights(regression::OneDimensional& r, unity_weights(r, wr, x, y, suite); rescale_weights(wr, x, y, suite); zero_weights(wr, x, y, suite); } void unity_weights(regression::OneDimensional& r, regression::OneDimensionalWeighted& rw, const utility::Vector& x, const utility::Vector& y, std::ostream* error) { *error << "    testing unity weights equal to non-weighted version.\n"; bool ok=true; test::Suite& suite) { suite.err() << "    testing unity weights equal to non-weighted version.\n"; utility::Vector w(x.size(), 1.0); r.fit(x,y); rw.fit(x,y,w); if (std::abs(r.predict(2000) - rw.predict(2000))>1e-15) { ok = false; *error << "Error: predict not equal\n" suite.ok(false); suite.err() << "Error: predict not equal\n" << "   weighted: " << rw.predict(2000) << "\n" << "   non-weighted: " << r.predict(2000) } if (std::abs(r.s2()-rw.s2(1.0))>10E-7){ ok = false; *error << "Error: s2 not equal non-weighted version." << std::endl; *error << "weighted s2 = " << rw.s2(1.0) << std::endl; *error << "non-weighted s2 = " << r.s2() << std::endl; suite.ok(false); suite.err() << "Error: s2 not equal non-weighted version." << std::endl; suite.err() << "weighted s2 = " << rw.s2(1.0) << std::endl; suite.err() << "non-weighted s2 = " << r.s2() << std::endl; } if (std::abs(r.standard_error2(2000)-rw.standard_error2(2000))>10e-7){ ok = false; *error << "Error: standard_error not equal non-weighted version." suite.ok(false); suite.err() << "Error: standard_error not equal non-weighted version." << std::endl; } if (std::abs(r.r2()-rw.r2())>10E-7){ ok = false; *error << "Error: r2 not equal non-weighted version." << std::endl; *error << "weighted r2 = " << rw.r2() << std::endl; *error << "non-weighted r2 = " << r.r2() << std::endl; suite.ok(false); suite.err() << "Error: r2 not equal non-weighted version." << std::endl; suite.err() << "weighted r2 = " << rw.r2() << std::endl; suite.err() << "non-weighted r2 = " << r.r2() << std::endl; } if (std::abs(r.prediction_error2(2000)-rw.prediction_error2(2000,1))>10e-7){ ok = false; *error << "Error: prediction_error2 not equal non-weighted version.\n" suite.ok(false); suite.err() << "Error: prediction_error2 not equal non-weighted version.\n" << "       weighted: " << rw.prediction_error2(2000,1) << "\n" << "   non-weighted: " << r.prediction_error2(2000) << std::endl; } return ok; } bool rescale_weights(regression::OneDimensionalWeighted& wr, void rescale_weights(regression::OneDimensionalWeighted& wr, const utility::Vector& x, const utility::Vector& y, std::ostream* error) { *error << "    testing rescaling weights.\n"; bool ok = true; test::Suite& suite) { suite.err() << "    testing rescaling weights.\n"; utility::Vector w(5);  w(0)=1.0;  w(1)=1.0;  w(2)=0.5;  w(3)=0.2;  w(4)=0.2; wr.fit(x,y,w); wr.fit(x,y,w); if (std::abs(wr.predict(2000)-predict)>10e-11){ ok = false; *error << "Error: predict not equal after rescaling.\n"; *error << "       predict = " << predict suite.ok(false); suite.err() << "Error: predict not equal after rescaling.\n"; suite.err() << "       predict = " << predict << " and after doubling weights.\n"; *error << "       predict = " << wr.predict(2000) << "\n"; suite.err() << "       predict = " << wr.predict(2000) << "\n"; } if (std::abs(wr.s2(2)-s2)>10e-11){ ok = false; *error << "Error: s2 not equal after rescaling.\n"; *error << "       s2 = " << s2 << " and after doubling weights.\n"; *error << "       s2 = " << wr.s2(2) << "\n"; *error << "difference " << s2-wr.s2(2.0) << std::endl; suite.ok(false); suite.err() << "Error: s2 not equal after rescaling.\n"; suite.err() << "       s2 = " << s2 << " and after doubling weights.\n"; suite.err() << "       s2 = " << wr.s2(2) << "\n"; suite.err() << "difference " << s2-wr.s2(2.0) << std::endl; } if (std::abs(wr.standard_error2(2000)-standard_error2)>10e-6){ ok = false; *error << "Error: standard_error2 not equal after rescaling.\n"; *error << "       standard_error2 = " << standard_error2 suite.ok(false); suite.err() << "Error: standard_error2 not equal after rescaling.\n"; suite.err() << "       standard_error2 = " << standard_error2 << " and after doubling weights.\n"; *error << "       standard_error2 = " suite.err() << "       standard_error2 = " << wr.standard_error2(2000) << "\n"; *error << " difference: " << wr.standard_error2(2000)-standard_error2 suite.err() << " difference: " << wr.standard_error2(2000)-standard_error2 << std::endl; } if (std::abs(wr.r2()-r2)>10e-6){ ok = false; *error << "Error: r2 not equal after rescaling.\n"; suite.ok(false); suite.err() << "Error: r2 not equal after rescaling.\n"; } if (std::abs(wr.prediction_error2(2000,2)-prediction_error2)>10e-6){ ok = false; *error << "Error: prediction_error2 not equal after rescaling.\n"; *error << "       prediction_error2 = " << prediction_error2 suite.ok(false); suite.err() << "Error: prediction_error2 not equal after rescaling.\n"; suite.err() << "       prediction_error2 = " << prediction_error2 << " and after doubling weights.\n"; *error << "       prediction_error2 = " suite.err() << "       prediction_error2 = " << wr.prediction_error2(2000,2) << "\n"; } return ok; } bool zero_weights(regression::OneDimensionalWeighted& wr, } void zero_weights(regression::OneDimensionalWeighted& wr, const utility::Vector& x, const utility::Vector& y, std::ostream* error) { *error << "    testing zero weights equal to missing value.\n"; bool ok = true; test::Suite& suite) { suite.err() << "    testing zero weights equal to missing value.\n"; utility::Vector w(5);  w(0)=1.0;  w(1)=1.0;  w(2)=0.5;  w(3)=0.2;  w(4)=0; wr.fit(x,y,w); wr.fit(x2,y2,w2); if (std::abs(wr.predict(2000) - predict)>1e-10) { ok = false; *error << "Error: predict not equal.\n"; *error << "       weighted predict: " << wr.predict(2000) << "\n"; *error << "       unweighted predict: " << predict << "\n"; *error << "       difference: " << wr.predict(2000)-predict << "\n"; suite.ok(false); suite.err() << "Error: predict not equal.\n"; suite.err() << "       weighted predict: " << wr.predict(2000) << "\n"; suite.err() << "       unweighted predict: " << predict << "\n"; suite.err() << "       difference: " << wr.predict(2000)-predict << "\n"; } if (std::abs(wr.prediction_error2(2000) - prediction_error2)>1e-13) { ok = false; *error << "Error: prediction_error2 not equal.\n"; suite.ok(false); suite.err() << "Error: prediction_error2 not equal.\n"; } if (std::abs(wr.r2() - r2)>1e-15) { ok = false; *error << "Error: r2 not equal.\n"; *error << "   r2: " << r2 << "\n"; *error << "   r2: " << wr.r2() << "\n"; suite.ok(false); suite.err() << "Error: r2 not equal.\n"; suite.err() << "   r2: " << r2 << "\n"; suite.err() << "   r2: " << wr.r2() << "\n"; } if (std::abs(wr.s2() - s2)>1e-15) { ok = false; *error << "Error: s2 not equal.\n"; suite.ok(false); suite.err() << "Error: s2 not equal.\n"; } if (std::abs(wr.standard_error2(2000) - standard_error2)>1e-15) { ok = false; *error << "Error: standard_error2 not equal.\n"; } return ok; } bool multidim(std::ostream* error) { bool ok = true; *error << "  testing regression::MultiDimensionalWeighted" << std::endl; suite.ok(false); suite.err() << "Error: standard_error2 not equal.\n"; } } void multidim(test::Suite& suite) { suite.err() << "  testing regression::MultiDimensionalWeighted" << std::endl; utility::Vector x(5); x(0)=1970; x(1)=1980; x(2)=1990; x(3)=2000; x(4)=2010; utility::Vector y(5); y(0)=12;   y(1)=11;   y(2)=14;   y(3)=13;   y(4)=15; z(1)=2000; z(2)=2000*2000; if (md.predict(z) != mdw.predict(z)){ ok = false; *error << "Error: predict not equal\n" if (!test::equal(md.predict(z), mdw.predict(z))){ suite.ok(false); suite.err() << "Error: predict not equal\n" << "   weighted: " << mdw.predict(z) << "\n" << "   non-weighted: " << md.predict(z) if (std::abs(md.standard_error2(z)-mdw.standard_error2(z))>10e-6){ ok = false; *error << "Error: standard_error2 not equal\n" suite.ok(false); suite.err() << "Error: standard_error2 not equal\n" << "   weighted: " << mdw.standard_error2(z) << "\n" << "   non-weighted: " << md.standard_error2(z) } if (std::abs(md.prediction_error2(z)-mdw.prediction_error2(z,1.0))>10e-7){ ok = false; *error << "Error: prediction_error2 not equal\n" suite.ok(false); suite.err() << "Error: prediction_error2 not equal\n" << "   weighted: " << mdw.prediction_error2(z,1.0) << "\n" << "   non-weighted: " << md.prediction_error2(z) mdw.fit(data,y,w); if (std::abs(mdw.predict(z)-predict)>10e-10){ ok = false; *error << "Error: predict not equal after rescaling.\n"; *error << "   predict = " << predict << " and after doubling weights.\n"; *error << "   predict = " << mdw.predict(z) << "\n"; suite.ok(false); suite.err() << "Error: predict not equal after rescaling.\n"; suite.err() << "   predict = " << predict << " and after doubling weights.\n"; suite.err() << "   predict = " << mdw.predict(z) << "\n"; } if (std::abs(mdw.prediction_error2(z,2)-prediction_error2)>10e-7){ ok = false; *error << "Error: prediction_error2 not equal after rescaling.\n"; *error << "       predict_error2 = " << prediction_error2 suite.ok(false); suite.err() << "Error: prediction_error2 not equal after rescaling.\n"; suite.err() << "       predict_error2 = " << prediction_error2 << " and after doubling weights.\n"; *error << "       predict_error2 = " << mdw.prediction_error2(z,2) << "\n"; suite.err() << "       predict_error2 = " << mdw.prediction_error2(z,2) << "\n"; } if (std::abs(mdw.s2(2)-s2)>10e-10){ ok = false; *error << "Error: s2 not equal after rescaling.\n"; *error << "       s2 = " << s2 << " and after doubling weights.\n"; *error << "       s2 = " << mdw.s2(2) << "\n"; suite.ok(false); suite.err() << "Error: s2 not equal after rescaling.\n"; suite.err() << "       s2 = " << s2 << " and after doubling weights.\n"; suite.err() << "       s2 = " << mdw.s2(2) << "\n"; } if (std::abs(mdw.standard_error2(z)-standard_error2)>10e-7){ ok = false; *error << "Error: standard_error2 not equal after rescaling.\n"; *error << " standard_error2 = " << standard_error2 suite.ok(false); suite.err() << "Error: standard_error2 not equal after rescaling.\n"; suite.err() << " standard_error2 = " << standard_error2 << " and after doubling weights.\n"; *error << " standard_error2 = " << mdw.standard_error2(z) << "\n"; } return ok; suite.err() << " standard_error2 = " << mdw.standard_error2(z) << "\n"; } } utility::Vector y(rl.y_predicted()); for (size_t i=0; i
Note: See TracChangeset for help on using the changeset viewer.