def test_fixed_parameters_2(): """ Make sure parameter boundaries are respected """ x = Parameter('x', min=1) y = Variable('y') model = Model({y: x**2}) bounded_minimizers = list(subclasses(BoundedMinimizer)) for minimizer in bounded_minimizers: if minimizer is MINPACK: # Not a MINPACKable problem because it only has a param continue fit = Fit(model, minimizer=minimizer) assert isinstance(fit.objective, MinimizeModel) if minimizer is DifferentialEvolution: # Also needs a max x.max = 10 fit_result = fit.execute() x.max = None else: fit_result = fit.execute() assert fit_result.value(x) >= 1.0 assert fit_result.value(x) <= 2.0 assert fit.minimizer.bounds == [(1, None)]
def test_fixed_parameters(): """ Make sure fixed parameters don't change on fitting """ a, b, c, d = parameters('a, b, c, d') x, y = variables('x, y') c.value = 4.0 a.min, a.max = 1.0, 5.0 # Bounds are needed for DifferentialEvolution b.min, b.max = 1.0, 5.0 c.min, c.max = 1.0, 5.0 d.min, d.max = 1.0, 5.0 c.fixed = True model = Model({y: a * exp(-(x - b)**2 / (2 * c**2)) + d}) # Generate data xdata = np.linspace(0, 100) ydata = model(xdata, a=2, b=3, c=2, d=2).y for minimizer in subclasses(BaseMinimizer): if minimizer is ChainedMinimizer: continue else: fit = Fit(model, x=xdata, y=ydata, minimizer=minimizer) fit_result = fit.execute() # Should still be 4.0, not 2.0! assert 4.0 == fit_result.params['c']
def test_constrainedminimizers(self): """ Compare the different constrained minimizers, to make sure all support constraints, and converge to the same answer. """ minimizers = list(subclasses(ScipyConstrainedMinimize)) x = Parameter('x', value=-1.0) y = Parameter('y', value=1.0) z = Variable('z') model = Model({z: 2 * x * y + 2 * x - x ** 2 - 2 * y ** 2}) # First we try an unconstrained fit results = [] for minimizer in minimizers: fit = Fit(- model, minimizer=minimizer) fit_result = fit.execute(tol=1e-15) results.append(fit_result) # Compare the parameter values. for r1, r2 in zip(results[:-1], results[1:]): self.assertAlmostEqual(r1.value(x), r2.value(x), 6) self.assertAlmostEqual(r1.value(y), r2.value(y), 6) np.testing.assert_almost_equal(r1.covariance_matrix, r2.covariance_matrix) constraints = [ Ge(y - 1, 0), # y - 1 >= 0, Eq(x ** 3 - y, 0), # x**3 - y == 0, ] # Constrained fit. results = [] for minimizer in minimizers: if minimizer is COBYLA: # COBYLA only supports inequility. continue fit = Fit(- model, constraints=constraints, minimizer=minimizer) fit_result = fit.execute(tol=1e-15) results.append(fit_result) for r1, r2 in zip(results[:-1], results[1:]): self.assertAlmostEqual(r1.value(x), r2.value(x), 6) self.assertAlmostEqual(r1.value(y), r2.value(y), 6) np.testing.assert_almost_equal(r1.covariance_matrix, r2.covariance_matrix)
def test_constrainedminimizers(): """ Compare the different constrained minimizers, to make sure all support constraints, and converge to the same answer. """ minimizers = list(subclasses(ScipyConstrainedMinimize)) x = Parameter('x', value=-1.0) y = Parameter('y', value=1.0) z = Variable('z') model = Model({z: 2 * x * y + 2 * x - x**2 - 2 * y**2}) # First we try an unconstrained fit results = [] for minimizer in minimizers: fit = Fit(-model, minimizer=minimizer) assert isinstance(fit.objective, MinimizeModel) fit_result = fit.execute(tol=1e-15) results.append(fit_result) # Compare the parameter values. for r1, r2 in zip(results[:-1], results[1:]): assert r1.value(x) == pytest.approx(r2.value(x), 1e-6) assert r1.value(y) == pytest.approx(r2.value(y), 1e-6) assert r1.covariance_matrix == pytest.approx(r2.covariance_matrix) constraints = [ Ge(y - 1, 0), # y - 1 >= 0, Eq(x**3 - y, 0), # x**3 - y == 0, ] # Constrained fit. results = [] for minimizer in minimizers: if minimizer is COBYLA: # COBYLA only supports inequality. continue fit = Fit(-model, constraints=constraints, minimizer=minimizer) fit_result = fit.execute(tol=1e-15) results.append(fit_result) for r1, r2 in zip(results[:-1], results[1:]): assert r1.value(x) == pytest.approx(r2.value(x), 1e-6) assert r1.value(y) == pytest.approx(r2.value(y), 1e-6) assert r1.covariance_matrix == pytest.approx(r2.covariance_matrix)
def test_non_boundaries(self): """ Make sure parameter boundaries are not invented """ x = Parameter('x') y = Variable('y') model = Model({y: x**2}) bounded_minimizers = list(subclasses(BoundedMinimizer)) bounded_minimizers = [minimizer for minimizer in bounded_minimizers if minimizer is not DifferentialEvolution] for minimizer in bounded_minimizers: fit = Fit(model, minimizer=minimizer) if minimizer is MINPACK: pass # Not a MINPACKable problem because it only has a param else: fit_result = fit.execute() self.assertAlmostEqual(fit_result.value(x), 0.0) self.assertEqual(fit.minimizer.bounds, [(None, None)])
def test_non_boundaries(): """ Make sure parameter boundaries are not invented """ x = Parameter('x') y = Variable('y') model = Model({y: x**2}) bounded_minimizers = list(subclasses(BoundedMinimizer)) bounded_minimizers = [ minimizer for minimizer in bounded_minimizers if minimizer is not DifferentialEvolution ] for minimizer in bounded_minimizers: # Not a MINPACKable problem because it only has a param if minimizer is MINPACK: continue fit = Fit(model, minimizer=minimizer) fit_result = fit.execute() assert fit_result.value(x) == pytest.approx(0.0) assert fit.minimizer.bounds == [(None, None)]
def test_boundaries(self): """ Make sure parameter boundaries are respected """ x = Parameter('x', min=1) y = Variable('y') model = Model({y: x**2}) bounded_minimizers = list(subclasses(BoundedMinimizer)) for minimizer in bounded_minimizers: fit = Fit(model, minimizer=minimizer) if minimizer is DifferentialEvolution: # Also needs a max x.max = 10 fit_result = fit.execute() x.max = None elif minimizer is MINPACK: pass # Not a MINPACKable problem because it only has a param else: fit_result = fit.execute() self.assertGreaterEqual(fit_result.value(x), 1.0) self.assertLessEqual(fit_result.value(x), 2.0) self.assertEqual(fit.minimizer.bounds, [(1, None)])