Ejemplo n.º 1
0
def make_factor_graph_model():
    model_factor_1 = g.AnalysisFactor(af.Collection(one=af.UniformPrior()),
                                      af.m.MockAnalysis())
    model_factor_2 = g.AnalysisFactor(af.Collection(one=af.UniformPrior()),
                                      af.m.MockAnalysis())

    return g.FactorGraphModel(model_factor_1, model_factor_2)
Ejemplo n.º 2
0
def test_graph(hierarchical_factor):
    graph = g.FactorGraphModel(hierarchical_factor)
    assert len(graph.model_factors) == 1

    hierarchical_factor.add_drawn_variable(
        af.UniformPrior(lower_limit=0.0, upper_limit=1.0))
    assert len(graph.model_factors) == 2
Ejemplo n.º 3
0
    def test_two_factor(self, model_factor, model_factor_2, unit_vector,
                        likelihood):
        collection = g.FactorGraphModel(model_factor, model_factor_2)

        assert (collection.log_likelihood_function(
            collection.global_prior_model.instance_from_unit_vector(
                unit_vector)) == likelihood)
Ejemplo n.º 4
0
def make_factor_graph_model(
    model_factor_1,
    model_factor_2,
    hierarchical_factor,
):
    return g.FactorGraphModel(model_factor_1, model_factor_2,
                              hierarchical_factor)
Ejemplo n.º 5
0
    def test_global_search(self, model_factor, model_factor_2):
        collection = g.FactorGraphModel(model_factor, model_factor_2)
        search = af.MockSearch()

        class Analysis(af.Analysis):
            def log_likelihood_function(self, instance):
                return collection.log_likelihood_function(instance)

        search.fit(collection.global_prior_model, Analysis())
Ejemplo n.º 6
0
def make_non_trivial_model():
    one = af.Model(af.Gaussian)
    two = af.Model(af.Gaussian)

    one.centre = two.centre

    model_factor_1 = g.AnalysisFactor(one, af.m.MockAnalysis())
    model_factor_2 = g.AnalysisFactor(two, af.m.MockAnalysis())

    return g.FactorGraphModel(model_factor_1, model_factor_2)
def test_custom_optimiser(make_model_factor):
    factor_1 = make_model_factor(centre=40, sigma=10, optimiser="optimiser")
    factor_2 = make_model_factor(centre=60, sigma=15)

    factor_model = ep.FactorGraphModel(factor_1, factor_2)

    default_optimiser = ep.LaplaceFactorOptimiser()
    ep_optimiser = factor_model._make_ep_optimiser(default_optimiser)

    factor_optimisers = ep_optimiser.factor_optimisers
    assert factor_optimisers[factor_1] == "optimiser"
    assert factor_optimisers[factor_2] == default_optimiser
def make_factor_model_collection(make_model_factor):
    """
    Here's a good example in which we have two Gaussians fit with a shared variable

    We have a shared intensity value and a shared intensity prior

    Multiplying together multiple LikelihoodModels gives us a factor model.

    The factor model can compute all the variables and messages required as well as construct
    a factor graph representing a fit on the ensemble.
    """
    return ep.FactorGraphModel(make_model_factor(centre=40, sigma=10),
                               make_model_factor(centre=60, sigma=15))
Ejemplo n.º 9
0
def test_custom_optimiser(make_model_factor):
    other_optimiser = ep.LaplaceOptimiser()

    factor_1 = make_model_factor(centre=40, sigma=10, optimiser=other_optimiser)
    factor_2 = make_model_factor(centre=60, sigma=15)

    factor_model = ep.FactorGraphModel(factor_1, factor_2)

    default_optimiser = ep.LaplaceOptimiser()
    ep_optimiser = factor_model._make_ep_optimiser(default_optimiser)

    factor_optimisers = ep_optimiser.factor_optimisers
    assert factor_optimisers[factor_1] is other_optimiser
    assert factor_optimisers[factor_2] is default_optimiser
Ejemplo n.º 10
0
def test_full_fit(centre_model, data, centres):
    graph = g.FactorGraphModel()
    for i, y in enumerate(data):
        prior_model = af.PriorModel(
            af.Gaussian,
            centre=af.GaussianPrior(mean=100, sigma=20),
            normalization=20,
            sigma=5,
        )
        graph.add(g.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y)))
        centre_model.add_drawn_variable(prior_model.centre)

    graph.add(centre_model)

    optimiser = g.LaplaceOptimiser()

    collection = graph.optimise(optimiser, max_steps=10).model
Ejemplo n.º 11
0
def test_visualize():
    analysis = Analysis()

    gaussian = af.Model(af.Gaussian)

    analysis_factor = g.AnalysisFactor(
        prior_model=gaussian,
        analysis=analysis
    )

    factor_graph = g.FactorGraphModel(
        analysis_factor
    )

    model = factor_graph.global_prior_model
    instance = model.instance_from_prior_medians()

    factor_graph.visualize(
        af.DirectoryPaths(),
        instance,
        False
    )

    assert analysis.did_call_visualise is True
- `prior_model_1` fits `data_1` via `analysis_1`.
- `prior_model_2` fits `data_2` via `analysis_2`.

The point where a `Model` and `Analysis` class meet is called a `ModelFactor`. 

This term is used to denote that we are composing a graphical model, which is commonly termed a 'factor graph'. A 
factor defines a node on this graph where we have some data, a model, and we fit the two together. The 'links' between 
these different nodes then define the global model we are fitting.
"""
model_factor_0 = g.ModelFactor(prior_model=prior_model_0, analysis=analysis_0)
model_factor_1 = g.ModelFactor(prior_model=prior_model_1, analysis=analysis_1)
model_factor_2 = g.ModelFactor(prior_model=prior_model_2, analysis=analysis_2)
"""
We combine our `ModelFactors` into one, to compose the factor graph.
"""
factor_graph = g.FactorGraphModel(model_factor_0, model_factor_1,
                                  model_factor_2)
"""
So, what is a factor graph?

A factor graph defines the graphical model we have composed. For example, it defines the different model components 
that make up our model (e.g. the three `Gaussian` classes) and how their parameters are linked or shared (e.g. that
each `Gaussian` has its own unique `intensity` and `sigma`, but a shared `centre` parameter.

This is what our factor graph looks like: 

The factor graph above is made up of two components:

- Nodes: these are points on the graph where we have a unique set of data and a model that is made up of a subset of 
our overall graphical model. This is effectively the `ModelFactor` objects we created above. 

- Links: these define the model components and parameters that are shared across different nodes and thus retain the 
A factor graph defines the graphical model we have composed. For example, it defines the different model components 
that make up our model (e.g. the three `Gaussian` classes) and how their parameters are linked or shared (e.g. that
each `Gaussian` has its own unique `normalization` and `centre`, but a shared `sigma` parameter.

This is what our factor graph looks like: 

The factor graph above is made up of two components:

- Nodes: these are points on the graph where we have a unique set of data and a model that is made up of a subset of 
our overall graphical model. This is effectively the `AnalysisFactor` objects we created above. 

- Links: these define the model components and parameters that are shared across different nodes and thus retain the 
same values when fitting different datasets.
"""
factor_graph = g.FactorGraphModel(*analysis_factor_list)
"""
__Search__

We can now create a non-linear search and used it to the fit the factor graph, using its `global_prior_model` property.
"""
dynesty = af.DynestyStatic(
    path_prefix=path.join("imaging", "graphical"),
    name="slope",
    nlive=100,
    sample="rwalk",
)

result = dynesty.fit(model=factor_graph.global_prior_model,
                     analysis=factor_graph)
"""
for model in model_list:

    hierarchical_factor.add_drawn_variable(model.centre)
"""
__Factor Graph__

We now create the factor graph for this model, using the list of `AnalysisFactor`'s and the hierarchical factor.

Note that in previous tutorials, when we created the `FactorGraphModel` we only passed the list of `AnalysisFactor`'s,
which contained the necessary information on the model create the factor graph that was fitted. The `AnalysisFactor`'s
were created before we composed the `HierachicalFactor`, which is why we need to pass it separate when composing the
factor graph.
"""

factor_graph = g.FactorGraphModel(*analysis_factor_list, hierarchical_factor)
"""
__Model Fit__


"""
from autofit.mapper.operator import DiagonalMatrix

laplace = g.LaplaceFactorOptimiser(transform_cls=DiagonalMatrix)
collection = factor_graph.optimise(
    laplace,
    name=path.join("howtofit", "chapter_graphical_models",
                   "tutorial_4_hierachical"),
)
"""
__Output__
Ejemplo n.º 15
0
The point where a `Model` and `Analysis` class meet is called a `AnalysisFactor`. 

This term is used to denote that we are composing a graphical model, which is commonly termed a 'factor graph'. A 
factor defines a node on this graph where we have some data, a model, and we fit the two together. The 'links' between 
these different nodes then define the global model we are fitting.
"""
analysis_factor_0 = g.AnalysisFactor(prior_model=prior_model_0,
                                     analysis=analysis_0)
analysis_factor_1 = g.AnalysisFactor(prior_model=prior_model_1,
                                     analysis=analysis_1)
analysis_factor_2 = g.AnalysisFactor(prior_model=prior_model_2,
                                     analysis=analysis_2)
"""
We combine our `AnalysisFactors` into one, to compose the factor graph.
"""
factor_graph = g.FactorGraphModel(analysis_factor_0, analysis_factor_1,
                                  analysis_factor_2)
"""
So, what is a factor graph?

A factor graph defines the graphical model we have composed. For example, it defines the different model components 
that make up our model (e.g. the three `Gaussian` classes) and how their parameters are linked or shared (e.g. that
each `Gaussian` has its own unique `normalization` and `sigma`, but a shared `centre` parameter.

This is what our factor graph looks like: 

The factor graph above is made up of two components:

- Nodes: these are points on the graph where we have a unique set of data and a model that is made up of a subset of 
our overall graphical model. This is effectively the `AnalysisFactor` objects we created above. 

- Links: these define the model components and parameters that are shared across different nodes and thus retain the 
Ejemplo n.º 16
0
def make_factor_model_collection(make_model_factor):
    return ep.FactorGraphModel(make_model_factor(centre=40, sigma=10),
                               make_model_factor(centre=60, sigma=15))