示例#1
0
    def _load_correlation_matrix(self):
        filename = self.cfg.get('correlations', None)
        if filename is None:
            return

        mat = N.loadtxt(filename)

        if mat.shape[0] != mat.shape[1]:
            raise Exception('Non square matrix provided:', mat.shape[0],
                            mat.shape[1])

        if len(self._par_container) != mat.shape[0]:
            raise Exception(
                'Unable to set correlation to %i parameters with %ix%i matrix'
                % (len(pars, mat.shape[0], mat.shape[1])))

        mmin, mmax = mat.min(), mat.max()
        if mmin < -1 - 1.e-12 or mmax > 1.0 + 1.e-12:
            raise Exception('Invalid min/max correlation values:', mmin, mmax)

        diag = mat.diagonal()
        ones = diag == 1.0
        if not ones.all():
            raise Exception('There are values !=1 on the diagonal (d-1): ',
                            diag[~ones] - 1.0)

        if self.cfg.get('verbose', 0) > 1:
            print('Load correlation matrix from %s:' % filename)
            print(mat)

        from gna.parameters import covariance_helpers as ch
        ch.covariate_pars(self._par_container, mat)
示例#2
0
    def _load_covariance_matrix(self):
        filename = self.cfg.get('covariance', None)
        assert filename

        covmat = N.loadtxt(filename)
        if self.cfg.get('verbose', 0) > 1:
            print('Load covariance matrix from %s:' % filename)
            print(covmat)

        sigma_inv = N.diag(covmat.diagonal()**-0.5)
        corrmat = N.matmul(N.matmul(sigma_inv, covmat), sigma_inv)

        if self.cfg.get('verbose', 0) > 1:
            print('Compute correlation matrix from:')
            print(corrmat)

        self._checkmatrix(covmat, is_correlation=False)

        self.covmat = covmat
        self.corrmat = corrmat

        for par, sigma2 in zip(self._par_container, covmat.diagonal()):
            par.setSigma(sigma2**0.5)

        from gna.parameters import covariance_helpers as ch
        ch.covariate_pars(self._par_container, corrmat)
示例#3
0
    def _load_correlation_matrix(self):
        filename = self.cfg.get('correlations', None)
        assert filename

        corrmat = N.loadtxt(filename)
        if self.cfg.get('verbose', 0) > 1:
            print('Load correlation matrix from %s:' % filename)
            print(corrmat)

        self._checkmatrix(corrmat, is_correlation=True)

        self.corrmat = corrmat

        from gna.parameters import covariance_helpers as ch
        ch.covariate_pars(self._par_container, corrmat)
示例#4
0
    def defparameter_group(self, *args, **kwargs):
        import gna.parameters.covariance_helpers as ch
        pars = [
            self.defparameter(name, **ctor_args) for name, ctor_args in args
        ]

        covmat_passed = kwargs.get('covmat')
        if covmat_passed is not None:
            ch.covariate_pars(pars, covmat_passed)

        cov_from_cfg = kwargs.get('covmat_cfg')
        if cov_from_cfg is not None:
            ch.CovarianceHandler(cov_from_cfg, pars).covariate_pars()

        return pars
示例#5
0
 def reqparameter_group(self, *args, **kwargs):
     import gna.parameters.covariance_helpers as ch
     args_patched = [(name, dict(ctor_args, with_status=True))
                     for name, ctor_args in args]
     pars_with_status = [
         self.reqparameter(name, **ctor_args)
         for name, ctor_args in args_patched
     ]
     statuses = [status for _, status in pars_with_status]
     pars = [par for par, _ in pars_with_status]
     if not any(statuses):
         covmat_passed = kwargs.get('covmat')
         if covmat_passed is not None:
             ch.covariate_pars(pars, covmat_passed)
         cov_from_cfg = kwargs.get('covmat_cfg')
         if cov_from_cfg is not None:
             ch.CovarianceHandler(cov_from_cfg, pars).covariate_pars()
     return pars
示例#6
0
    def define_variables(self):
        from gna.parameters import covariance_helpers as ch

        self._par_container = []
        separate_uncertainty = self.cfg.get('separate_uncertainty', False)
        parname = self.cfg.parameter
        labelfmt = self.cfg.get('label', '')

        data = self.data
        values = data['values']
        names = data['names']
        unc = data['uncertainties']
        mode = data['mode']
        if mode == 'percent':
            relunc = unc / 100.
            unc = values * relunc
        elif mode == 'relative':
            relunc = unc
            unc = values * relunc
        elif mode == 'absolute':
            relunc = unc / values
        else:
            raise Exception('Invalid uncertainty mode: ' + mode)

        for it_minor in self.nidx_minor:
            container = []

            for it_major in self.nidx_major:
                major_values, = it_major.current_values()
                if not major_values in names:
                    raise Exception(
                        'Variable {} is not in the list'.format(major_values))
                data_idx = names.index(major_values)

                it = it_major + it_minor
                label = it.current_format(labelfmt) if labelfmt else ''

                if separate_uncertainty:
                    uncpar = self.reqparameter(separate_uncertainty,
                                               it,
                                               central=1.0,
                                               sigma=relunc[data_idx],
                                               label=label + ' (norm)')
                    par = self.reqparameter(parname,
                                            it,
                                            central=values[data_idx],
                                            fixed=True,
                                            label=label)
                    container.append(uncpar)
                else:
                    par = self.reqparameter(parname,
                                            it,
                                            central=values[data_idx],
                                            sigma=unc[data_idx],
                                            label=label)
                    container.append(par)

                if self.cfg.get("objectize"):
                    trans = par.transformations.value
                    trans.setLabel(label)
                    self.set_output(parname, it, trans.single())

            self._par_container.append(container)

            if 'correlations' in self.data:
                cormat = self.data['correlations']
                self._checkmatrix(cormat, container, is_correlation=True)
                cormat = self.filter_matrix(cormat, container)

                if cormat is not None:
                    ch.covariate_pars(container, cormat)
示例#7
0
    def define_variables(self):
        with entryContext(subgraph="LSNL"):
            from physlib import pdg
            ns = self.namespace

            #
            # Some constants
            #
            emass = ns.reqparameter("emass",
                                    central=pdg['live']['ElectronMass'],
                                    fixed=True,
                                    label='Electron mass, MeV')
            self.doubleme = 2 * emass.value()
            ns.defparameter("ngamma",
                            central=2.0,
                            fixed=True,
                            label='Number of e+e- annihilation gammas')

            labels = OrderedDict([('birks.Kb0', 'Kb0=1'),
                                  ('birks.Kb1', "Birk's 1st constant (E')"),
                                  ('birks.Kb2', "Birk's 2nd constant (E'')"),
                                  ('cherenkov.E_0', ''), ('cherenkov.p0', ''),
                                  ('cherenkov.p1', ''), ('cherenkov.p2', ''),
                                  ('cherenkov.p3', ''), ('cherenkov.p4', ''),
                                  ('Npescint', ''), ('kC', ''),
                                  ('normalizationEnergy', '')])

            #
            # Define parameters according to predefined list and input configuration
            #
            for name, label in labels.items():
                parcfg = self.cfg.pars.get(name, None)
                if parcfg is None:
                    if 'Kb2' in name:
                        continue
                    raise self.exception(
                        'Parameter {} configuration is not provided'.format(
                            name))
                self.reqparameter(name, None, cfg=parcfg, label=label)

            #
            # Set the correlation matrix if provided by configuration
            #
            correlations_pars = self.cfg.correlations_pars
            correlations = self.cfg.correlations

            if not correlations_pars and not correlations:
                return
            if not correlations_pars or not correlations:
                raise self.exception(
                    "Both 'correlations' and 'correlations_pars' should be defined"
                )

            npars = len(correlations_pars)
            corrmatrix = N.array(correlations, dtype='d').reshape(npars, npars)

            # Check matrix sanity
            if (corrmatrix.diagonal() != 1.0).any():
                raise self.exception(
                    'There should be no non-unitary elements on the correlation matrix'
                )
            if (N.fabs(corrmatrix) > 1.0).any():
                raise self.exception(
                    'Correlation matrix values should be within -1.0 and 1.0')
            if (corrmatrix != corrmatrix.T).all():
                raise self.exception(
                    'Correlation matrix is expected to be diagonal')

            # Take parameters and set correlations
            from gna.parameters import covariance_helpers as ch
            pars = [ns[par] for par in correlations_pars]
            ch.covariate_pars(pars, corrmatrix)
示例#8
0
def test_par_cov():
    probe_1 = env.defparameter('probe1', central=0., sigma=1.)
    probe_2 = env.defparameter('probe2', central=0., sigma=1.)
    probe_3 = env.defparameter('probe3', central=0., sigma=1.)
    test_ns = env.ns('test_ns')
    test_ns.defparameter('test0', central=1., sigma=0.1)
    test_ns.defparameter('test1', central=1., sigma=0.1)
    test_ns.defparameter('test2', central=1., sigma=0.1)
    test_ns.defparameter('test3', central=1., sigma=0.1)
    extra_test_ns = env.ns('extra_test_ns')
    extra1 = extra_test_ns.defparameter('extra1', central=1., sigma=0.1)
    extra2 = extra_test_ns.defparameter('extra2', central=1., sigma=0.1)
    extra3 = extra_test_ns.defparameter('extra3', central=1., sigma=0.1)
    extra4 = extra_test_ns.defparameter('extra4', central=1., sigma=0.1)

    cov1 = 0.1
    print("Setting covariance of probe_1 with probe_2 to {0}".format(cov1))
    probe_1.setCovariance(probe_2, cov1)
    print("Check that they are mutually correlated now.")

    assert probe_1.isCorrelated(probe_2) and probe_2.isCorrelated(probe_1)
    print("Success")
    print("Get covariance from both -- {0} and {1}\n".format(
        probe_1.getCovariance(probe_2), probe_2.getCovariance(probe_1)))

    print("Checks that change of one propagates to another")
    cov2 = 0.2
    probe_1.setCovariance(probe_2, cov2)
    assert (probe_1.getCovariance(probe_2) == cov2
            and probe_2.getCovariance(probe_1) == cov2)
    print("Success\n")

    test_pars = get_parameters(
        ['test_ns.test0', 'test_ns.test1', 'test_ns.test2', 'test_ns.test3'])
    print("Test pars sequence is {}".format([_.name() for _ in test_pars]))
    cov_matrix1 = make_fake_covmat(4)
    print("Test covariance matrix is \n", cov_matrix1)

    ch.covariate_pars(test_pars, cov_matrix1)
    for first, second in itertools.combinations_with_replacement(
            range(len(test_pars)), 2):
        try:
            if first != second:
                assert test_pars[first].getCovariance(
                    test_pars[second]) == cov_matrix1[first, second]
            else:
                assert test_pars[first].sigma() == np.sqrt(cov_matrix1[first,
                                                                       second])
        except AssertionError:
            print((first, second),
                  test_pars[first].getCovariance(test_pars[second])**2,
                  cov_matrix1[first, second])
            raise

    extra_pars = [extra1, extra2, extra3]
    cov_mat_extra = make_fake_covmat(3)
    cov_storage = ch.CovarianceStorage("extra_store", extra_pars,
                                       cov_mat_extra)
    ch.covariate_ns('extra_test_ns', cov_storage)
    for first, second in itertools.combinations_with_replacement(
            range(len(extra_pars)), 2):
        try:
            if first != second:
                assert test_pars[first].getCovariance(
                    test_pars[second]) == cov_matrix1[first, second]
            else:
                assert test_pars[first].sigma() == np.sqrt(cov_matrix1[first,
                                                                       second])
        except AssertionError:
            print((first, second),
                  test_pars[first].getCovariance(test_pars[second])**2,
                  cov_matrix1[first, second])
            raise