Esempio n. 1
0
 def test_fit_profile_cov_mat_correlated(self):
     _fit_with_constraint = IndexedFit(self._data_container,
                                       model_function=self._model)
     _fit_with_constraint.add_matrix_parameter_constraint(['a', 'b'],
                                                          self._means,
                                                          self._cov_mat_cor)
     self._test_consistency(_fit_with_constraint, self._cov_mat_cor_inv)
Esempio n. 2
0
    def setUp(self):
        _data = [-2.1, 0.2, 1.9, 3.8, 6.1]
        self._means = np.array([3.654, 7.789])
        self._vars = np.array([2.467, 1.543])
        self._cov_mat_uncor = np.array([[self._vars[0], 0.0], [0.0, self._vars[1]]])
        self._cov_mat_uncor_inv = np.linalg.inv(self._cov_mat_uncor)
        self._cov_mat_cor = np.array([[self._vars[0], 0.1], [0.1, self._vars[1]]])
        self._cov_mat_cor_inv = np.linalg.inv(self._cov_mat_cor)
        self._cov_mat_simple_a_inv = np.array([[1.0 / self._vars[0], 0.0], [0.0, 0.0]])
        self._cov_mat_simple_b_inv = np.array([[0.0, 0.0], [0.0, 1.0 / self._vars[1]]])

        self._data_container = IndexedContainer(data=_data)
        self._data_container.add_error(err_val=1.0)

        _a_test = np.linspace(start=0,  stop=4, num=9, endpoint=True)
        _b_test = np.linspace(start=-4, stop=0, num=9, endpoint=True)
        self._test_par_values = np.zeros((4, 2, 9))
        self._test_par_values[0, 0] = _a_test
        self._test_par_values[1, 1] = _b_test
        self._test_par_values[2, 0] = _a_test
        self._test_par_values[2, 1] = _b_test
        self._test_par_values[3, 0] = _a_test
        self._test_par_values[3, 1] = -_b_test
        self._test_par_res = self._test_par_values - self._means.reshape((1, 2, 1))
        self._test_par_res = np.transpose(self._test_par_res, axes=(0, 2, 1))

        self._fit_no_constraints = IndexedFit(self._data_container, model_function=self._model)
        self._fit_no_constraints.do_fit()
        _cost_function = self._fit_no_constraints._fitter._fcn_wrapper
        self._profile_no_constraints = np.zeros((4, 9))
        for _i in range(4):
            for _j in range(9):
                self._profile_no_constraints[_i, _j] = _cost_function(
                    self._test_par_values[_i, 0, _j],
                    self._test_par_values[_i, 1, _j])
Esempio n. 3
0
 def test_fit_profile_simple_b(self):
     _fit_with_constraint = IndexedFit(self._data_container,
                                       model_function=self._model)
     _fit_with_constraint.add_parameter_constraint('b', self._means[1],
                                                   np.sqrt(self._vars[1]))
     self._test_consistency(_fit_with_constraint,
                            self._cov_mat_simple_b_inv)
Esempio n. 4
0
 def test_bad_input_exception(self):
     _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model)
     with self.assertRaises(IndexedFitException):
         _fit_with_constraint.add_parameter_constraint('c', 1.0, 1.0)
     with self.assertRaises(IndexedFitException):
         _fit_with_constraint.add_matrix_parameter_constraint(['a', 'c'], [1.0, 2.0], [[0.2, 0.0], [0.0, 0.1]])
     with self.assertRaises(IndexedFitException):
         _fit_with_constraint.add_matrix_parameter_constraint(['a'], [1.0, 2.0], [[0.2, 0.0], [0.0, 0.1]])
Esempio n. 5
0
    def _get_fit(self, errors=None):
        '''convenience'''

        errors = errors or [dict(err_val=1.0)]

        _fit = IndexedFit(
            data=self._ref_data,
            model_function=simple_indexed_model,
            cost_function=IndexedCostFunction_Chi2(errors_to_use='covariance'),
            minimizer=self.MINIMIZER)

        for _err in errors:
            _fit.add_error(**_err)

        return _fit
Esempio n. 6
0
    def _get_fit(self, model_function=None, cost_function=None, error=None):
        '''convenience'''
        model_function = model_function or simple_indexed_model
        # TODO: fix default
        cost_function = cost_function or IndexedCostFunction_Chi2(
            errors_to_use='covariance')
        error = error or 1.0

        _fit = IndexedFit(data=self._ref_data,
                          model_function=model_function,
                          cost_function=cost_function,
                          minimizer=self.MINIMIZER)
        _fit.add_error(err_val=error)

        return _fit
Esempio n. 7
0
    def dummy_model_no_pars_raise(self):
        def dummy_model():
            pass

        with self.assertRaises(IndexedModelFunctionException) as _exc:
            IndexedFit(data=self._ref_data,
                       model_function=dummy_model,
                       minimizer=self.MINIMIZER)

        self.assertIn('needs at least one parameter', _exc.exception.args[0])
Esempio n. 8
0
    def test_reserved_parameter_names_raise(self):
        def dummy_model(data):
            pass

        with self.assertRaises(IndexedFitException) as _exc:
            IndexedFit(data=self._ref_data,
                       model_function=dummy_model,
                       minimizer=self.MINIMIZER)

        self.assertIn('reserved', _exc.exception.args[0])
        self.assertIn('data', _exc.exception.args[0])
Esempio n. 9
0
    def dummy_model_varargs_varkwargs_raise(self):
        # TODO: raise even without 'par'
        def dummy_model(x, par, *varargs, **varkwargs):
            pass

        with self.assertRaises(IndexedModelFunctionException) as _exc:
            IndexedFit(data=self._ref_data,
                       model_function=dummy_model,
                       minimizer=self.MINIMIZER)

        self.assertIn('variable', _exc.exception.args[0])
        self.assertIn('varargs', _exc.exception.args[0])
Esempio n. 10
0
    def _get_fit(self,
                 model_function=None,
                 cost_function=None,
                 errors=None,
                 dynamic_error_algorithm=None):
        '''convenience'''
        model_function = model_function or simple_indexed_model
        # TODO: fix default
        cost_function = cost_function or IndexedCostFunction_Chi2(
            errors_to_use='covariance')
        errors = errors or [dict(err_val=1.0)]
        dynamic_error_algorithm = dynamic_error_algorithm or "nonlinear"

        _fit = IndexedFit(data=self._ref_data,
                          model_function=model_function,
                          cost_function=cost_function,
                          minimizer=self.MINIMIZER,
                          dynamic_error_algorithm=dynamic_error_algorithm)
        for _err in errors:
            _fit.add_error(**_err)

        return _fit
Esempio n. 11
0
    def _get_fit(self, errors=None):
        '''convenience'''

        _fit = IndexedFit(
            data=self._ref_data,
            model_function=simple_indexed_model,
            cost_function=IndexedCostFunction_Chi2(errors_to_use='covariance'),
            minimizer=self.MINIMIZER)

        if errors is None:
            _fit.add_matrix_error(err_matrix=np.eye(self._n_points),
                                  matrix_type='cov')
        else:
            for _err in errors:
                if 'err_matrix' in _err:
                    _fit.add_matrix_error(**_err)
                else:
                    _fit.add_error(**_err)

        return _fit
Esempio n. 12
0
class TestParameterConstraintInIndexedFit(unittest.TestCase):

    def _expected_profile_diff(self, res, cov_mat_inv):
        return res.dot(cov_mat_inv).dot(res)

    def _test_consistency(self, constrained_fit, par_cov_mat_inv):
        constrained_fit.do_fit()
        _cost_function = constrained_fit._fitter._fcn_wrapper
        for _i in range(4):
            for _j in range(9):
                _profile_constrained = _cost_function(self._test_par_values[_i, 0, _j], self._test_par_values[_i, 1, _j])
                _diff = _profile_constrained - self._profile_no_constraints[_i, _j]
                _expected_profile_diff = self._expected_profile_diff(self._test_par_res[_i, _j], par_cov_mat_inv)
                self.assertTrue(np.abs(_diff - _expected_profile_diff) < 1e-12)

    @staticmethod
    def _model(a, b):
        return a * np.arange(5) + b

    def setUp(self):
        _data = [-2.1, 0.2, 1.9, 3.8, 6.1]
        self._means = np.array([3.654, 7.789])
        self._vars = np.array([2.467, 1.543])
        self._cov_mat_uncor = np.array([[self._vars[0], 0.0], [0.0, self._vars[1]]])
        self._cov_mat_uncor_inv = np.linalg.inv(self._cov_mat_uncor)
        self._cov_mat_cor = np.array([[self._vars[0], 0.1], [0.1, self._vars[1]]])
        self._cov_mat_cor_inv = np.linalg.inv(self._cov_mat_cor)
        self._cov_mat_simple_a_inv = np.array([[1.0 / self._vars[0], 0.0], [0.0, 0.0]])
        self._cov_mat_simple_b_inv = np.array([[0.0, 0.0], [0.0, 1.0 / self._vars[1]]])

        self._data_container = IndexedContainer(data=_data)
        self._data_container.add_error(err_val=1.0)

        _a_test = np.linspace(start=0,  stop=4, num=9, endpoint=True)
        _b_test = np.linspace(start=-4, stop=0, num=9, endpoint=True)
        self._test_par_values = np.zeros((4, 2, 9))
        self._test_par_values[0, 0] = _a_test
        self._test_par_values[1, 1] = _b_test
        self._test_par_values[2, 0] = _a_test
        self._test_par_values[2, 1] = _b_test
        self._test_par_values[3, 0] = _a_test
        self._test_par_values[3, 1] = -_b_test
        self._test_par_res = self._test_par_values - self._means.reshape((1, 2, 1))
        self._test_par_res = np.transpose(self._test_par_res, axes=(0, 2, 1))

        self._fit_no_constraints = IndexedFit(self._data_container, model_function=self._model)
        self._fit_no_constraints.do_fit()
        _cost_function = self._fit_no_constraints._fitter._fcn_wrapper
        self._profile_no_constraints = np.zeros((4, 9))
        for _i in range(4):
            for _j in range(9):
                self._profile_no_constraints[_i, _j] = _cost_function(
                    self._test_par_values[_i, 0, _j],
                    self._test_par_values[_i, 1, _j])

    def test_bad_input_exception(self):
        _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model)
        with self.assertRaises(IndexedFitException):
            _fit_with_constraint.add_parameter_constraint('c', 1.0, 1.0)
        with self.assertRaises(IndexedFitException):
            _fit_with_constraint.add_matrix_parameter_constraint(['a', 'c'], [1.0, 2.0], [[0.2, 0.0], [0.0, 0.1]])
        with self.assertRaises(IndexedFitException):
            _fit_with_constraint.add_matrix_parameter_constraint(['a'], [1.0, 2.0], [[0.2, 0.0], [0.0, 0.1]])

    def test_fit_profile_cov_mat_uncorrelated(self):
        _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model)
        _fit_with_constraint.add_matrix_parameter_constraint(['a', 'b'], self._means, self._cov_mat_uncor)
        self._test_consistency(_fit_with_constraint, self._cov_mat_uncor_inv)
        _fit_with_constraint_alt = IndexedFit(self._data_container, model_function=self._model)
        _fit_with_constraint_alt.add_parameter_constraint('a', self._means[0], np.sqrt(self._vars[0]))
        _fit_with_constraint_alt.add_parameter_constraint('b', self._means[1], np.sqrt(self._vars[1]))
        self._test_consistency(_fit_with_constraint_alt, self._cov_mat_uncor_inv)

    def test_fit_profile_cov_mat_correlated(self):
        _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model)
        _fit_with_constraint.add_matrix_parameter_constraint(['a', 'b'], self._means, self._cov_mat_cor)
        self._test_consistency(_fit_with_constraint, self._cov_mat_cor_inv)

    def test_fit_profile_simple_a(self):
        _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model)
        _fit_with_constraint.add_parameter_constraint('a', self._means[0], np.sqrt(self._vars[0]))
        self._test_consistency(_fit_with_constraint, self._cov_mat_simple_a_inv)

    def test_fit_profile_simple_b(self):
        _fit_with_constraint = IndexedFit(self._data_container, model_function=self._model)
        _fit_with_constraint.add_parameter_constraint('b', self._means[1], np.sqrt(self._vars[1]))
        self._test_consistency(_fit_with_constraint, self._cov_mat_simple_b_inv)