00001 #include "FittingStatistics.h"
00002 #include "Logger.h"
00003 #include "MainWindow.h"
00004 #include <qmath.h>
00005 #include <QPointF>
00006 #include <QtTest/QtTest>
00007 #include "Test/TestFitting.h"
00008
00009 QTEST_MAIN (TestFitting)
00010
00011 using namespace std;
00012
00013 TestFitting::TestFitting(QObject *parent) :
00014 QObject(parent)
00015 {
00016 }
00017
00018 void TestFitting::cleanupTestCase ()
00019 {
00020
00021 }
00022
00023 bool TestFitting::generalTest (int order,
00024 int numPoints) const
00025 {
00026 int orderReduced = qMin (order, numPoints - 1);
00027
00028 const double EPSILON = 0.0001;
00029 FittingStatistics fitting;
00030 double mse, rms, rSquared;
00031 FittingCurveCoefficients coefficientsGot (MAX_POLYNOMIAL_ORDER + 1);
00032
00033
00034 bool isOverfitting = (order >= numPoints - 1);
00035
00036
00037 FittingPointsConvenient points;
00038 for (int iPoint = 0; iPoint < numPoints; iPoint++) {
00039 double x = iPoint;
00040 double y = 0;
00041 if (orderReduced > 0) {
00042 y = 1;
00043 for (int ord = 0; ord < orderReduced; ord++) {
00044 y *= (x + ord + 1);
00045 }
00046 }
00047
00048 points.append (QPointF (x, y));
00049 }
00050
00051 fitting.calculateCurveFitAndStatistics (order,
00052 points,
00053 coefficientsGot,
00054 mse,
00055 rms,
00056 rSquared);
00057
00058 bool success = true;
00059
00060
00061 FittingCurveCoefficients coefficientsExpected (orderReduced + 1);
00062 switch (orderReduced)
00063 {
00064 case 0:
00065 coefficientsExpected [0] = 0;
00066 break;
00067 case 1:
00068 coefficientsExpected [0] = 1;
00069 coefficientsExpected [1] = 1;
00070 break;
00071 case 2:
00072 coefficientsExpected [0] = 2;
00073 coefficientsExpected [1] = 3;
00074 coefficientsExpected [2] = 1;
00075 break;
00076 case 3:
00077 coefficientsExpected [0] = 6;
00078 coefficientsExpected [1] = 11;
00079 coefficientsExpected [2] = 6;
00080 coefficientsExpected [3] = 1;
00081 break;
00082 case 4:
00083 coefficientsExpected [0] = 24;
00084 coefficientsExpected [1] = 50;
00085 coefficientsExpected [2] = 35;
00086 coefficientsExpected [3] = 10;
00087 coefficientsExpected [4] = 1;
00088 break;
00089 }
00090
00091 for (int coef = 0; coef < order + 1; coef++) {
00092 double coefGot = coefficientsGot [coef];
00093
00094 double coefExpected = 0;
00095 if (coef <= orderReduced) {
00096 coefExpected = coefficientsExpected [coef];
00097 }
00098
00099 success = (success && ((qAbs (coefGot - coefExpected) < EPSILON)));
00100 }
00101
00102 if (isOverfitting) {
00103
00104 success = (success && ((qAbs (mse) < EPSILON)));
00105 }
00106
00107 return success;
00108 }
00109
00110 void TestFitting::initTestCase ()
00111 {
00112 const QString NO_ERROR_REPORT_LOG_FILE;
00113 const QString NO_REGRESSION_OPEN_FILE;
00114 const bool NO_GNUPLOT_LOG_FILES = false;
00115 const bool NO_REGRESSION_IMPORT = false;
00116 const bool NO_RESET = false;
00117 const bool DEBUG_FLAG = false;
00118 const QStringList NO_LOAD_STARTUP_FILES;
00119
00120 initializeLogging ("engauge_test",
00121 "engauge_test.log",
00122 DEBUG_FLAG);
00123
00124 MainWindow w (NO_ERROR_REPORT_LOG_FILE,
00125 NO_REGRESSION_OPEN_FILE,
00126 NO_GNUPLOT_LOG_FILES,
00127 NO_REGRESSION_IMPORT,
00128 NO_RESET,
00129 NO_LOAD_STARTUP_FILES);
00130 w.show ();
00131 }
00132
00133 void TestFitting::testExactFit01 ()
00134 {
00135 QVERIFY (generalTest (0, 1));
00136 }
00137
00138 void TestFitting::testExactFit12 ()
00139 {
00140 QVERIFY (generalTest (1, 2));
00141 }
00142
00143 void TestFitting::testExactFit23 ()
00144 {
00145 QVERIFY (generalTest (2, 3));
00146 }
00147
00148 void TestFitting::testExactFit34 ()
00149 {
00150 QVERIFY (generalTest (3, 4));
00151 }
00152
00153 void TestFitting::testOverfit11 ()
00154 {
00155 QVERIFY (generalTest (1, 1));
00156 }
00157
00158 void TestFitting::testOverfit22 ()
00159 {
00160 QVERIFY (generalTest (2, 2));
00161 }
00162
00163 void TestFitting::testOverfit33 ()
00164 {
00165 QVERIFY (generalTest (3, 3));
00166 }
00167
00168 void TestFitting::testOverfit44 ()
00169 {
00170 QVERIFY (generalTest (4, 4));
00171 }
00172
00173 void TestFitting::testUnderfit02 ()
00174 {
00175 QVERIFY (generalTest (0, 2));
00176 }
00177
00178 void TestFitting::testUnderfit13 ()
00179 {
00180 QVERIFY (generalTest (1, 3));
00181 }
00182
00183 void TestFitting::testUnderfit24 ()
00184 {
00185 QVERIFY (generalTest (2, 4));
00186 }
00187
00188 void TestFitting::testUnderfit35 ()
00189 {
00190 QVERIFY (generalTest (3, 5));
00191 }