Ejemplo n.º 1
0
def _test_gaussian():
    n_observations = 100
    x = np.arange(n_observations)
    y = make_data(Gaussian(centre=50.0, normalization=25.0, sigma=10.0), x)

    prior_model = af.PriorModel(
        Gaussian,
        # centre=af.GaussianPrior(mean=50, sigma=10),
        # normalization=af.GaussianPrior(mean=25, sigma=10),
        sigma=af.GaussianPrior(mean=10, sigma=10),
        centre=af.UniformPrior(lower_limit=30, upper_limit=70),
        normalization=af.UniformPrior(lower_limit=15, upper_limit=35),
        # sigma=af.UniformPrior(lower_limit=5, upper_limit=15),
    )

    factor_model = ep.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y))

    # optimiser = ep.LaplaceOptimiser(
    #     transform_cls=DiagonalMatrix
    # )
    optimiser = af.DynestyStatic()
    model = factor_model.optimise(optimiser)

    assert model.centre.mean == pytest.approx(50, rel=0.1)
    assert model.normalization.mean == pytest.approx(25, rel=0.1)
    assert model.sigma.mean == pytest.approx(10, rel=0.1)
    def make_factor_model(centre: float,
                          sigma: float,
                          optimiser=None) -> ep.ModelFactor:
        """
        We'll make a LikelihoodModel for each Gaussian we're fitting.

        First we'll make the actual data to be fit.

        Note that the intensity value is shared.
        """
        y = make_data(
            Gaussian(centre=centre, intensity=intensity, sigma=sigma), x)
        """
        Next we need a prior model.
    
        Note that the intensity prior is shared.
        """
        prior_model = af.PriorModel(
            Gaussian,
            centre=af.GaussianPrior(mean=50, sigma=20),
            intensity=intensity_prior,
            sigma=af.GaussianPrior(mean=10, sigma=10),
        )
        """
        Finally we combine the likelihood function with the prior model to produce a likelihood
        factor - this will be converted into a ModelFactor which is like any other factor in the
        factor graph.
        
        We can also pass a custom optimiser in here that will be used to fit the factor instead
        of the default optimiser.
        """
        return ep.ModelFactor(prior_model,
                              analysis=Analysis(x=x, y=y),
                              optimiser=optimiser)
Ejemplo n.º 3
0
    def make_factor_model(centre: float,
                          sigma: float,
                          optimiser=None) -> ep.AnalysisFactor:
        y = make_data(
            Gaussian(centre=centre, normalization=normalization, sigma=sigma),
            x)

        prior_model = af.PriorModel(
            Gaussian,
            centre=af.UniformPrior(lower_limit=10, upper_limit=100),
            normalization=normalization_prior,
            sigma=af.UniformPrior(lower_limit=0, upper_limit=20),
        )

        return ep.AnalysisFactor(prior_model,
                                 analysis=Analysis(x=x, y=y),
                                 optimiser=optimiser)
Ejemplo n.º 4
0
def test_gaussian():
    n_observations = 100
    x = np.arange(n_observations)
    y = make_data(Gaussian(centre=50.0, normalization=25.0, sigma=10.0), x)

    prior_model = af.PriorModel(
        Gaussian,
        centre=af.GaussianPrior(mean=50, sigma=20),
        normalization=af.GaussianPrior(mean=25, sigma=10),
        sigma=af.GaussianPrior(mean=10, sigma=10),
    )

    factor_model = ep.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y))

    laplace = ep.LaplaceOptimiser()
    model = factor_model.optimise(laplace)

    assert model.centre.mean == pytest.approx(50, rel=0.1)
    assert model.normalization.mean == pytest.approx(25, rel=0.1)
    assert model.sigma.mean == pytest.approx(10, rel=0.1)