示例#1
0
    def test_fit_method_fails_with_too_many_tries(self):
        lpost = LogLikelihoodDummy(self.ps.freq, self.ps.power, self.model)
        pe = ParameterEstimation()
        t0 = [2.0]

        with pytest.raises(Exception):
            res = pe.fit(lpost, t0, neg=True)
示例#2
0
 def test_fit_method_returns_optimization_results_object(self):
     pe = ParameterEstimation()
     t0 = [2.0]
     res = pe.fit(self.lpost, t0)
     assert isinstance(
         res,
         OptimizationResults), "res must be of type OptimizationResults"
    def test_fit_method_fails_with_too_many_tries(self):
        lpost = LogLikelihoodDummy(self.ps.freq, self.ps.power, self.model)
        pe = ParameterEstimation()
        t0 = [2.0]

        with pytest.raises(Exception):
            res = pe.fit(lpost, t0, neg=True)
示例#4
0
    def test_object_works_with_loglikelihood_object(self):
        llike = PSDLogLikelihood(self.ps.freq, self.ps.power,
                                 self.model, m=self.ps.m)
        pe = ParameterEstimation()
        res = pe.fit(llike, [2.0])

        pass
示例#5
0
    def test_compute_lrt_computes_deviance_correctly(self):
        t0 = [2.0]
        pe = ParameterEstimation()

        delta_deviance = pe.compute_lrt(self.lpost, t0, self.lpost, t0)

        assert delta_deviance < 1e-7
示例#6
0
    def test_compute_lrt_sets_max_post_to_false(self):
        t0 = [2.0]
        pe = ParameterEstimation(max_post=True)

        assert pe.max_post is True
        delta_deviance = pe.compute_lrt(self.lpost, t0, self.lpost, t0)

        assert pe.max_post is False
    def test_compute_lrt_computes_deviance_correctly(self):
        t0 = [2.0]
        pe = ParameterEstimation()

        delta_deviance, opt1, opt2 = pe.compute_lrt(self.lpost, t0,
                                                    self.lpost, t0)

        assert delta_deviance < 1e-7
示例#8
0
 def test_object_works_with_loglikelihood_object(self):
     llike = PSDLogLikelihood(self.ps.freq, self.ps.power,
                              self.model, m=self.ps.m)
     pe = ParameterEstimation()
     res = pe.fit(llike, [2.0])
     assert isinstance(res,
                       OptimizationResults), "res must be of " \
                                             "type OptimizationResults"
    def test_compute_lrt_sets_max_post_to_false(self):
        t0 = [2.0]
        pe = ParameterEstimation(max_post=True)

        assert pe.max_post is True
        delta_deviance = pe.compute_lrt(self.lpost, t0, self.lpost, t0)

        assert pe.max_post is False
    def test_compute_lrt_fails_when_garbage_goes_in(self):
        pe = ParameterEstimation()
        t0 = [2.0]

        with pytest.raises(TypeError):
            pe.compute_lrt(self.lpost, t0, None, t0)

        with pytest.raises(ValueError):
            pe.compute_lrt(self.lpost, t0[:-1], self.lpost, t0)
示例#11
0
def fit_data_with_gaussian(x_values, y_values, amplitude=1., mean=0, stddev=1.):
    g_init = Gaussian1D(amplitude, mean, stddev)
    lpost = PSDLogLikelihood(x_values, y_values, g_init)
    parest = ParameterEstimation()
    res = parest.fit(lpost, [amplitude, mean, stddev], neg=True)
    opt_amplitude = res.p_opt[0]
    opt_mean = res.p_opt[1]
    opt_stddev = res.p_opt[2]
    return opt_amplitude, opt_mean, opt_stddev
示例#12
0
def fit_data_with_gaussian(x_values, y_values, amplitude=1., mean=0, stddev=1.):
    g_init = Gaussian1D(amplitude, mean, stddev)
    lpost = PSDLogLikelihood(x_values, y_values, g_init)
    parest = ParameterEstimation()
    res = parest.fit(lpost, [amplitude, mean, stddev], neg=True)
    opt_amplitude = res.p_opt[0]
    opt_mean = res.p_opt[1]
    opt_stddev = res.p_opt[2]
    return opt_amplitude, opt_mean, opt_stddev
示例#13
0
    def test_sampler_runs(self):
        pe = ParameterEstimation()
        if os.path.exists("test_corner.pdf"):
            os.unlink("test_corner.pdf")
        sample_res = pe.sample(self.lpost, [2.0], nwalkers=100, niter=10,
                               burnin=50, print_results=True, plot=True)

        assert os.path.exists("test_corner.pdf")
        assert sample_res.acceptance > 0.25
        assert isinstance(sample_res, SamplingResults)
        def setup_class(cls):
            m = 1
            nfreq = 100
            freq = np.arange(nfreq)
            noise = np.random.exponential(size=nfreq)
            power = noise * 2.0

            ps = Powerspectrum()
            ps.freq = freq
            ps.power = power
            ps.m = m
            ps.df = freq[1] - freq[0]
            ps.norm = "leahy"

            cls.ps = ps
            cls.a_mean, cls.a_var = 2.0, 1.0

            cls.model = models.Const1D()

            p_amplitude = lambda amplitude: \
                scipy.stats.norm(loc=cls.a_mean, scale=cls.a_var).pdf(
                    amplitude)

            cls.priors = {"amplitude": p_amplitude}
            cls.lpost = PSDPosterior(cls.ps.freq,
                                     cls.ps.power,
                                     cls.model,
                                     m=cls.ps.m)
            cls.lpost.logprior = set_logprior(cls.lpost, cls.priors)

            cls.fitmethod = "BFGS"
            cls.max_post = True
            cls.t0 = [2.0]
            cls.neg = True

            pe = ParameterEstimation()
            res = pe.fit(cls.lpost, cls.t0)

            cls.nwalkers = 50
            cls.niter = 100

            np.random.seed(200)
            p0 = np.array([
                np.random.multivariate_normal(res.p_opt, res.cov)
                for i in range(cls.nwalkers)
            ])

            cls.sampler = emcee.EnsembleSampler(cls.nwalkers,
                                                len(res.p_opt),
                                                cls.lpost,
                                                args=[False],
                                                threads=1)

            with catch_warnings(RuntimeWarning):
                _, _, _ = cls.sampler.run_mcmc(p0, cls.niter)
        def setup_class(cls):
            m = 1
            nfreq = 100000
            freq = np.arange(nfreq)
            noise = np.random.exponential(size=nfreq)
            power = noise * 2.0

            ps = Powerspectrum()
            ps.freq = freq
            ps.power = power
            ps.m = m
            ps.df = freq[1] - freq[0]
            ps.norm = "leahy"

            cls.ps = ps
            cls.a_mean, cls.a_var = 2.0, 1.0

            cls.model = models.Const1D()

            p_amplitude = lambda amplitude: \
                scipy.stats.norm(loc=cls.a_mean, scale=cls.a_var).pdf(
                    amplitude)

            cls.priors = {"amplitude": p_amplitude}
            cls.lpost = PSDPosterior(cls.ps.freq, cls.ps.power,
                                     cls.model, m=cls.ps.m)
            cls.lpost.logprior = set_logprior(cls.lpost, cls.priors)

            cls.fitmethod = "BFGS"
            cls.max_post = True
            cls.t0 = [2.0]
            cls.neg = True

            pe = ParameterEstimation()
            res = pe.fit(cls.lpost, cls.t0)

            cls.nwalkers = 100
            cls.niter = 200

            np.random.seed(200)
            p0 = np.array(
                [np.random.multivariate_normal(res.p_opt, res.cov) for
                 i in range(cls.nwalkers)])

            cls.sampler = emcee.EnsembleSampler(cls.nwalkers,
                                                len(res.p_opt), cls.lpost,
                                                args=[False], threads=1)

            _, _, _ = cls.sampler.run_mcmc(p0, cls.niter)
示例#16
0
def fit_data_with_lorentz_and_const(x_values, y_values):
    amplitude = 5.
    x_0 = 1
    fwhm = 0.5
    const = 5.
    g_init = Lorentz1D(amplitude, x_0, fwhm)
    g_init += Const1D(const)
    lpost = PSDLogLikelihood(x_values, y_values, g_init)
    parest = ParameterEstimation()
    res = parest.fit(lpost, [amplitude, x_0, fwhm, const], neg=True)
    opt_amplitude = res.p_opt[0]
    opt_x_0 = res.p_opt[1]
    opt_fwhm = res.p_opt[2]
    opt_const = res.p_opt[3]
    return opt_amplitude, opt_x_0, opt_fwhm, opt_const
示例#17
0
def fit_data_with_lorentz_and_const(x_values, y_values):
    amplitude=5.
    x_0=1
    fwhm=0.5
    const=5.
    g_init = Lorentz1D(amplitude, x_0, fwhm)
    g_init += Const1D(const)
    lpost = PSDLogLikelihood(x_values, y_values, g_init)
    parest = ParameterEstimation()
    res = parest.fit(lpost, [amplitude, x_0, fwhm, const], neg=True)
    opt_amplitude = res.p_opt[0]
    opt_x_0 = res.p_opt[1]
    opt_fwhm = res.p_opt[2]
    opt_const = res.p_opt[3]
    return opt_amplitude, opt_x_0, opt_fwhm, opt_const
示例#18
0
    def test_parest_stores_max_post_correctly(self):
        """
        Make sure the keyword for Maximum A Posteriori fits is stored correctly
        as a default.
        """
        pe = ParameterEstimation()

        assert pe.max_post is True, "max_post should be set to True as a default."
示例#19
0
    def test_compute_lrt_fails_when_garbage_goes_in(self):
        pe = ParameterEstimation()
        t0 = [2.0]

        with pytest.raises(TypeError):
            pe.compute_lrt(self.lpost, t0, None, t0)

        with pytest.raises(ValueError):
            pe.compute_lrt(self.lpost, t0[:-1], self.lpost, t0)
示例#20
0
    def test_simulate_lrt_fails_in_superclass(self):

        pe = ParameterEstimation()
        with pytest.raises(NotImplementedError):
            pe.simulate_lrts(None, None, None, None, None)
示例#21
0
    def test_sample_raises_error_without_emcee(self):
        pe = ParameterEstimation()

        with pytest.raises(ImportError):
            sample_res = pe.sample(self.lpost, [2.0])
 def test_fit_fails_with_incorrect_number_of_parameters(self):
     pe = ParameterEstimation()
     t0 = [1, 2]
     with pytest.raises(ValueError):
         res = pe.fit(self.lpost, t0)
示例#23
0
 def test_fit_fails_with_incorrect_number_of_parameters(self):
     pe = ParameterEstimation()
     t0 = [1, 2]
     with pytest.raises(ValueError):
         res = pe.fit(self.lpost, t0)
示例#24
0
 def test_fit_method_works_with_correct_parameter(self):
     pe = ParameterEstimation()
     t0 = [2.0]
     res = pe.fit(self.lpost, t0)
示例#25
0
 def test_fit_fails_when_object_is_not_posterior_or_likelihood(self):
     x = np.ones(10)
     y = np.ones(10)
     pe = ParameterEstimation()
     with pytest.raises(TypeError):
         res = pe.fit(x, y)
    def test_res_is_of_correct_type(self):
        pe = ParameterEstimation()
        t0 = [2.0]
        res = pe.fit(self.lpost, t0)

        assert isinstance(res, OptimizationResults)
 def test_fit_method_returns_optimization_results_object(self):
     pe = ParameterEstimation()
     t0 = [2.0]
     res = pe.fit(self.lpost, t0)
     assert isinstance(res,
                       OptimizationResults), "res must be of type OptimizationResults"
 def test_fit_method_works_with_correct_parameter(self):
     pe = ParameterEstimation()
     t0 = [2.0]
     res = pe.fit(self.lpost, t0)
示例#29
0
    def test_res_is_of_correct_type(self):
        pe = ParameterEstimation()
        t0 = [2.0]
        res = pe.fit(self.lpost, t0)

        assert isinstance(res, OptimizationResults)
    def test_sample_raises_error_without_emcee(self):
        pe = ParameterEstimation()

        with pytest.raises(ImportError):
            sample_res = pe.sample(self.lpost, [2.0])
    def test_simulate_lrt_fails_in_superclass(self):

        pe = ParameterEstimation()
        with pytest.raises(NotImplementedError):
            pe.simulate_lrts(None, None, None, None, None)
示例#32
0
 def test_par_est_initializes(self):
     pe = ParameterEstimation()
示例#33
0
 def test_fit_fails_without_t0(self):
     pe = ParameterEstimation()
     with pytest.raises(TypeError):
         res = pe.fit(np.ones(10))
 def test_fit_fails_without_t0(self):
     pe = ParameterEstimation()
     with pytest.raises(TypeError):
         res = pe.fit(np.ones(10))
 def test_fit_fails_when_object_is_not_posterior_or_likelihood(self):
     x = np.ones(10)
     y = np.ones(10)
     pe = ParameterEstimation()
     with pytest.raises(TypeError):
         res = pe.fit(x, y)