コード例 #1
0
 def test_without_simcov(self):
     simdict = Simulations()
     meadict = Measurements()
     covdict = Covariances()
     # mock measurements
     arr_a = np.random.rand(1, 4*mpisize)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(name=('test', None, 4*mpisize, None),
                    data=mea, otype='plain')
     # mock covariance
     arr_c = np.random.rand(4, 4*mpisize)
     cov = Observable(arr_c, 'covariance')
     covdict.append(name=('test', None, 4*mpisize, None),
                    cov_data=cov)
     # mock observable with repeated single realisation
     arr_b = np.random.rand(1, 4*mpisize)
     comm.Bcast(arr_b, root=0)
     arr_ens = np.zeros((2, 4*mpisize))
     for i in range(len(arr_ens)):
         arr_ens[i] = arr_b
     sim = Observable(arr_ens, 'simulated')
     simdict.append(name=('test', None, 4*mpisize, None),
                    data=sim, otype='plain')
     # simplelikelihood
     lh_simple = SimpleLikelihood(meadict, covdict)
     rslt_simple = lh_simple(simdict)
     # ensemblelikelihood
     lh_ensemble = EnsembleLikelihood(meadict, covdict)
     rslt_ensemble = lh_ensemble(simdict)
     assert rslt_ensemble == rslt_simple
コード例 #2
0
    def test_diag(self):
        simdict = Simulations()
        meadict = Measurements()
        covdict = Covariances()
        # mock measurements
        arr_a = np.random.rand(1, 40)
        mea = Observable(arr_a, 'measured')
        meadict.append(name=('test', None, 40, None),
                       data=mea, otype='plain')
        # mock (diagonal) covariance
        arr_var = np.random.rand(40)
        cov = Observable(np.diag(arr_var), 'covariance')
        covdict.append(name=('test', None, 40, None),
                       cov_data=cov)
        # mock observable
        arr_ens = np.random.rand(10, 40)

        sim = Observable(arr_ens, 'simulated')
        simdict.append(name=('test', None, 40, None),
                       data=sim, otype='plain')
        # ensemblelikelihood + diagonal_covcov
        lh_ens = EnsembleLikelihood(meadict, covdict, cov_func=diagonal_mcov)
        result_ens = lh_ens(simdict)
        # EnsembleLikelihoodDiagonal
        lh_diag = EnsembleLikelihoodDiagonal(meadict, covdict)
        result_diag = lh_diag(simdict)

        assert np.allclose(result_diag, result_ens)
コード例 #3
0
 def test_with_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     covdict = Covariances()
     # mock measurements
     arr_a = np.random.rand(1, 4*mpisize)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(name=('test', None, 4*mpisize, None),
                    data=mea, otype='plain')
     # mock sims
     arr_b = np.random.rand(5, 4*mpisize)
     sim = Observable(arr_b, 'simulated')
     simdict.append(name=('test', None, 4*mpisize, None),
                    data=sim, otype='plain')
     # mock covariance
     arr_c = np.random.rand(4, 4*mpisize)
     cov = Observable(arr_c, 'covariance')
     covdict.append(name=('test', None, 4*mpisize, None),
                    cov_data=cov)
     # with covariance
     lh = SimpleLikelihood(meadict, covdict)
     # calc by likelihood
     rslt = lh(simdict)  # feed variable value, not parameter value
     # calc by hand
     full_b = np.vstack(comm.allgather(arr_b))  # global arr_b
     diff = (np.mean(full_b, axis=0) - arr_a)
     full_cov = np.vstack(comm.allgather(arr_c))  # global covariance
     (sign, logdet) = np.linalg.slogdet(full_cov*2.*np.pi)
     baseline = -0.5*(np.vdot(diff, np.linalg.solve(full_cov, diff.T))+sign*logdet)
     assert np.allclose(rslt, baseline)
コード例 #4
0
 def test_without_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     # mock measurements
     arr_a = np.random.rand(1, 12*mpisize**2)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(name=('test', None, mpisize, None),
                    data=mea, otype='HEALPix')
     # mock observable with repeated single realisation
     arr_b = np.random.rand(1, 12*mpisize**2)
     comm.Bcast(arr_b, root=0)
     if not mpirank:
         arr_ens = np.zeros((3, 12*mpisize**2))
     else:
         arr_ens = np.zeros((2, 12*mpisize**2))
     for i in range(len(arr_ens)):
         arr_ens[i] = arr_b
     sim = Observable(arr_ens, 'simulated')
     simdict.append(name=('test', None, mpisize, None),
                    data=sim, otype='HEALPix')
     # simplelikelihood
     lh_simple = SimpleLikelihood(meadict)
     rslt_simple = lh_simple(simdict)
     # ensemblelikelihood
     lh_ensemble = EnsembleLikelihood(meadict)
     rslt_ensemble = lh_ensemble(simdict)
     assert rslt_ensemble == rslt_simple
コード例 #5
0
    def test_with_trace_approximation(self):
        simdict = Simulations()
        meadict = Measurements()
        covdict = Covariances()
        # mock measurements
        arr_a = np.random.rand(1, 4*mpisize)
        comm.Bcast(arr_a, root=0)
        mea = Observable(arr_a, 'measured')
        meadict.append(name=('test', None, 4*mpisize, None),
                       data=mea, otype='plain')
        # mock covariance (NB for the trace approximation to work, the data
        # covariance needs to be diagonal)
        arr_c = np.diag(np.random.rand(4))

        cov = Observable(arr_c, 'covariance')
        covdict.append(name=('test', None, 4*mpisize, None),
                       cov_data=cov)
        # mock observable with repeated single realisation
        arr_b = np.random.rand(1, 4*mpisize)
        comm.Bcast(arr_b, root=0)
        arr_ens = np.zeros((2, 4*mpisize))
        for i in range(len(arr_ens)):
            arr_ens[i] = arr_b
        sim = Observable(arr_ens, 'simulated')
        simdict.append(name=('test', None, 4*mpisize, None),
                       data=sim, otype='plain')

        # simplelikelihood
        lh_simple = SimpleLikelihood(meadict, covdict)
        result_simple = lh_simple(simdict)
        # ensemblelikelihood
        lh_ensemble = EnsembleLikelihood(meadict, covdict,
                                         use_trace_approximation=True)
        result_ensemble = lh_ensemble(simdict)
        assert result_ensemble == result_simple
コード例 #6
0
ファイル: simulator.py プロジェクト: luizfelippesr/imagine
    def __call__(self, field_list):
        """
        Runs the simulator over a Fields list

        Parameters
        ----------
        field_list : list
            List of imagine.Field object which must include all the `required_field_types`

        Returns
        -------
        sims : imagine.Simulations
            A Simulations object containing all the specified mock data
        """
        sims = Simulations()
        self.register_ensemble_size(field_list)
        for i in range(self._ensemble_size):
            # Prepares all fields
            self.prepare_fields(field_list, i)
            for key in self.observables:
                sim = self.simulate(key=key,
                                    coords_dict=self.output_coords[key],
                                    realization_id=i,
                                    output_units=self.output_units[key])
                sims.append(name=key,
                            data=sim[np.newaxis, :].to(self.output_units[key]),
                            coords=self.output_coords[key],
                            otype=self.output_type[key])
        return sims
コード例 #7
0
 def test_without_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     # mock measurements
     arr_a = np.random.rand(1, 48)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(name=('test', None, 2, None), data=mea, otype='HEALPix')
     # mock sims
     arr_b = np.random.rand(3, 48)
     sim = Observable(arr_b, 'simulated')
     simdict.append(name=('test', None, 2, None), data=sim, otype='HEALPix')
     # no covariance
     lh = SimpleLikelihood(meadict)
     # calc by likelihood
     rslt = lh(simdict)  # feed variable value, not parameter value
     # calc by hand
     full_b = np.vstack(comm.allgather(arr_b))  # global arr_b
     diff = (np.mean(full_b, axis=0) - arr_a)
     baseline = -float(0.5)*float(np.vdot(diff, diff))
     # comapre
     assert np.allclose(rslt, baseline)
コード例 #8
0
 def test_simdict_append_array_twice(self):
     arr = np.random.rand(2, 3)
     simdict = Simulations()
     simdict.append(name=('test', None, 3, None), data=arr,
                    otype='plain')  # plain array
     assert simdict[('test', None, 3, None)].shape == (2 * mpisize, 3)
     simdict.append(name=('test', None, 3, None), data=arr,
                    otype='plain')  # plain array
     assert simdict[('test', None, 3, None)].shape == (4 * mpisize, 3)
コード例 #9
0
 def test_simdict_append_array(self):
     arr = np.random.rand(2, 3)
     simdict = Simulations()
     simdict.append(name=('test', None, 3, None), data=arr,
                    otype='plain')  # plain array
     assert simdict[('test', None, 3, None)].shape == (2 * mpisize, 3)
     assert np.allclose(simdict[('test', None, 3, None)].data, arr)
     hrr = np.random.rand(3, 48)
     simdict.append(name=('test', None, 2, None), data=hrr,
                    otype='HEALPix')  # healpix array
     assert simdict[('test', None, 2, None)].shape == (3 * mpisize, 48)
     assert np.allclose(simdict[('test', None, 2, None)].data, hrr)
コード例 #10
0
 def test_simdict_append_observable(self):
     hrr = np.random.rand(2, 48)
     obs1 = Observable(hrr, 'simulated')
     simdict = Simulations()
     simdict.append(name=('test', None, 2, None),
                    data=obs1,
                    otype='HEALPix')  # healpix Observable
     assert simdict[('test', None, 2, None)].shape == (2 * mpisize, 48)
     assert np.allclose(simdict[('test', None, 2, None)].data, hrr)
     arr = np.random.rand(5, 3)
     obs2 = Observable(arr, 'simulated')
     simdict.append(name=('test', None, 3, None), data=obs2,
                    otype='plain')  # plain Observable
     assert simdict[('test', None, 3, None)].shape == (5 * mpisize, 3)
     assert np.allclose(simdict[('test', None, 3, None)].data, arr)