def test_non_gradient_can_set_bounds_range_builder_properties(
        model: Model) -> None:
    non_gradient_optimizer = NonGradientOptimizer(model.a, bounds_range=0.1)
    logProb = non_gradient_optimizer.max_a_posteriori()

    sum_ab = model.a.get_value() + model.b.get_value()
    assert not (19.9 < sum_ab < 20.1)
def test_max_likelihood_non_gradient(model: Model) -> None:
    non_gradient_optimizer = NonGradientOptimizer(model.a)
    logProb = non_gradient_optimizer.max_likelihood()
    assert logProb < 0.

    sum_ab = model.a.get_value() + model.b.get_value()
    assert 19.9 < sum_ab < 20.1
Beispiel #3
0
def test_max_likelihood_non_gradient(model: Model) -> None:
    non_gradient_optimizer = NonGradientOptimizer(model.a)
    result = non_gradient_optimizer.max_likelihood()
    assert result.fitness() < 0.

    sum_ab = result.value_for(model.a) + result.value_for(model.b)
    assert 19.9 < sum_ab < 20.1
def test_non_gradient_can_set_max_eval_builder_properties(
        model: Model) -> None:
    non_gradient_optimizer = NonGradientOptimizer(model.a, max_evaluations=5)

    with pytest.raises(Py4JJavaError):
        #This throws a Gradient Optimizer: "Reached Max Evaluations" error
        logProb = non_gradient_optimizer.max_a_posteriori()
Beispiel #5
0
def test_non_gradient_can_set_max_eval_builder_properties(
        model: Model) -> None:
    non_gradient_optimizer = NonGradientOptimizer(model.a,
                                                  BOBYQA(max_evaluations=5))

    with pytest.raises(
            Py4JJavaError,
            match=r"An error occurred while calling o[\d]*.maxAPosteriori."):
        # This throws a Gradient Optimizer: "Reached Max Evaluations" error
        logProb = non_gradient_optimizer.max_a_posteriori()
Beispiel #6
0
def non_gradient_optimizer_example():
    model = build_model()
    model.first_thermometer.observe(25)
    bayes_net = model.to_bayes_net()
    # %%SNIPPET_START%% PythonNonGradientOptimizer
    optimizer = NonGradientOptimizer(bayes_net,
                                     max_evaluations=5000,
                                     bounds_range=100000.,
                                     initial_trust_region_radius=5.,
                                     stopping_trust_region_radius=2e-8)
    optimizer.max_a_posteriori()
    calculated_temperature = model.temperature.get_value()
Beispiel #7
0
def test_non_gradient_op_throws_with_invalid_net_param() -> None:
    with pytest.raises(
            TypeError,
            match=r"net must be a Vertex or a BayesNet. Was given {}".format(
                int)):
        NonGradientOptimizer(
            500)  # type: ignore # this is expected to fail mypy
def test_non_gradient_op_vertex(model: Model) -> None:
    non_gradient_optimizer = NonGradientOptimizer(model.a)
    assert len(list(non_gradient_optimizer.net.get_latent_vertices())) == 2
def test_non_gradient_op_bayes_net(model: Model) -> None:
    net = BayesNet(model.a.get_connected_graph())
    gradient_optimizer = NonGradientOptimizer(net)
    assert gradient_optimizer.net is net