def test_anova(self): "Tests anova" (E, NOx, _, _, _, results) = self.d gas = loess(E,NOx, span=2./3.) gas.fit() gas_null = loess(E, NOx, span=1.0) gas_null.fit() gas_anova = loess_anova(gas, gas_null) gas_anova_theo = results[4] assert_almost_equal(gas_anova.dfn, gas_anova_theo[0], 5) assert_almost_equal(gas_anova.dfd, gas_anova_theo[1], 5) assert_almost_equal(gas_anova.F_value, gas_anova_theo[2], 5) assert_almost_equal(gas_anova.Pr_F, gas_anova_theo[3], 5)
def test_failures(self): "Tests failures" (E, NOx, gas_fit_E, _, _, _) = self.d gas = loess(E,NOx, span=2./3.) # This one should fail (all parametric) gas.model.parametric_flags = True self.assertRaises(ValueError, gas.fit) # This one also (all drop_square) gas.model.drop_square_flags = True self.assertRaises(ValueError, gas.fit) gas.model.degree = 1 self.assertRaises(ValueError, gas.fit) # This one should not (revert to std) gas.model.parametric_flags = False gas.model.drop_square_flags = False gas.model.degree = 2 gas.fit() # Now, for predict ................. gas.predict(gas_fit_E, stderror=False) # This one should fail (extrapolation & blending) self.assertRaises(ValueError, gas.predict, gas.predicted.values, stderror=False) # But this one should not .......... gas.predict(gas_fit_E, stderror=False) print "OK"
def test_1dpredict(self): "Basic test 1d - prediction" (E, NOx, gas_fit_E, _, _, results) = self.d gas = loess(E,NOx, span=2./3.) gas.fit() gas.predict(gas_fit_E, stderror=False) assert_almost_equal(gas.predicted.values, results[2], 6)
def test_1dbasic_alt(self): "Basic test 1d - part #2" (E, NOx, _, _, _, results) = self.d gas_null = loess(E, NOx) gas_null.model.span = 1.0 gas_null.fit() assert_almost_equal(gas_null.outputs.fitted_values, results[1], 6) assert_almost_equal(gas_null.outputs.enp, 3.5, 1) assert_almost_equal(gas_null.outputs.s, 0.5197, 4)
def test_1dbasic(self): "Basic test 1d" (E, NOx, _, _, _, results) = self.d gas = loess(E,NOx) gas.model.span = 2./3. gas.fit() assert_almost_equal(gas.outputs.fitted_values, results[0], 6) assert_almost_equal(gas.outputs.enp, 5.5, 1) assert_almost_equal(gas.outputs.s, 0.3404, 4)
def python2_loess_fit(x, y, w, span, get_stderror, statistics): from pyloess import loess # @UnresolvedImport lo = loess(x, y, span=span, weights=w) lo.fit() yfit = lo.outputs.fitted_values.tolist() if get_stderror: stderr = lo.predict(x, stderror=True).stderr return yfit, stderr.tolist() else: return yfit
def test_2dbasic(self): "2D standard" (x, y, results, _, _, madeup) = self.d madeup = loess(x,y) madeup.model.span = 0.5 madeup.model.normalize = True madeup.fit() assert_almost_equal(madeup.outputs.fitted_values, results[0], 5) assert_almost_equal(madeup.outputs.enp, 14.9, 1) assert_almost_equal(madeup.outputs.s, 0.9693, 4)
def test_1dpredict_2(self): "Basic test 1d - new predictions" (E, NOx, _, newdata, _, results) = self.d gas = loess(E,NOx, span=2./3.) gas.predict(newdata, stderror=True) gas.predicted.confidence(0.99) assert_almost_equal(gas.predicted.confidence_intervals.lower, results[3][0::3], 6) assert_almost_equal(gas.predicted.confidence_intervals.fit, results[3][1::3], 6) assert_almost_equal(gas.predicted.confidence_intervals.upper, results[3][2::3], 6)
def test_2d_modflags_tot(self): "2D - modification of model flags" (x, y, results, _, _, madeup) = self.d madeup = loess(x,y) madeup.model.span = 0.8 madeup.model.drop_square_flags = [True, False] madeup.model.parametric_flags = [True, False] assert_equal(madeup.model.parametric_flags[:2],[1,0]) madeup.fit() assert_almost_equal(madeup.outputs.fitted_values, results[1], 5) assert_almost_equal(madeup.outputs.enp, 6.9, 1) assert_almost_equal(madeup.outputs.s, 1.4804, 4)
def __init__(self, *args, **kwds): TestCase.__init__(self, *args, **kwds) dfile = open(os.path.join(data_path,'madeup_data'), 'r') dfile.readline() x = fromiter((float(v) for v in dfile.readline().rstrip().split()), float_).reshape(-1,2) dfile.readline() y = fromiter((float(v) for v in dfile.readline().rstrip().split()), float_) # rfile = open(os.path.join(data_path,'madeup_result'), 'r') results = [] for i in range(8): rfile.readline() z = fromiter((float(v) for v in rfile.readline().rstrip().split()), float_) results.append(z) # newdata1 = numpy.array([[-2.5, 0.0, 2.5], [0., 0., 0.]]) newdata2 = numpy.array([[-0.5, 0.5], [0., 0.]]) # madeup = loess(x,y) self.d = (x, y, results, newdata1, newdata2, madeup)