Ejemplo n.º 1
0
def build_sampling_algorithm(
        algo, proposal_distribution: Optional[str],
        proposal_distribution_sigma: Optional[numpy_types],
        proposal_listeners: List[proposal_listener_types]):
    if algo != "metropolis":
        if proposal_distribution is not None:
            raise TypeError(
                "Only Metropolis Hastings supports the proposal_distribution parameter"
            )
        if len(proposal_listeners) > 0:
            raise TypeError(
                "Only Metropolis Hastings supports the proposal_listeners parameter"
            )

    if (proposal_distribution is None and len(proposal_listeners) > 0):
        raise TypeError(
            "If you pass in proposal_listeners you must also specify proposal_distribution"
        )

    builder: JavaObject = algorithms[algo].builder()

    if proposal_distribution is not None:
        proposal_distribution_object = ProposalDistribution(
            type_=proposal_distribution,
            sigma=proposal_distribution_sigma,
            listeners=proposal_listeners)
        builder = builder.proposalDistribution(
            proposal_distribution_object.unwrap())
    sampling_algorithm: JavaObject = builder.build()
    return sampling_algorithm
Ejemplo n.º 2
0
    def __init__(
        self,
        proposal_distribution: str,
        latents: Iterable[Vertex],
        proposal_listeners=[],
        proposal_distribution_sigma: Union[tensor_arg_types,
                                           List[tensor_arg_types]] = None):
        if (proposal_distribution is None and len(proposal_listeners) > 0):
            raise TypeError(
                "If you pass in proposal_listeners you must also specify proposal_distribution"
            )

        builder: JavaObject = k.jvm_view().MetropolisHastings.builder()

        latents = list(latents)

        proposal_distribution_object = ProposalDistribution(
            type_=proposal_distribution,
            sigma=proposal_distribution_sigma,
            latents=latents,
            listeners=proposal_listeners)

        rejection_strategy = k.jvm_view().RollBackToCachedValuesOnRejection()

        builder = builder.proposalDistribution(
            proposal_distribution_object.unwrap())
        builder = builder.rejectionStrategy(rejection_strategy)

        super().__init__(builder.build())
Ejemplo n.º 3
0
def test_it_throws_if_it_doesnt_recognise_the_type() -> None:
    with pytest.raises(KeyError, match=r"'foo'"):
        ProposalDistribution("foo")
Ejemplo n.º 4
0
def test_it_throws_if_you_specify_sigma_but_the_type_isnt_gaussian() -> None:
    with pytest.raises(TypeError, match=r'Parameter sigma is not valid unless type is "gaussian"'):
        ProposalDistribution("prior", sigma=1.)
Ejemplo n.º 5
0
def test_it_throws_if_you_specify_gaussian_with_empty_list_of_latents(net: BayesNet) -> None:
    with pytest.raises(TypeError, match=r"Gaussian Proposal Distribution requires values for latents"):
        ProposalDistribution("gaussian", latents=[], sigma=[])
Ejemplo n.º 6
0
def test_it_throws_if_you_specify_gaussian_without_values_for_latents() -> None:
    with pytest.raises(TypeError, match=r"Gaussian Proposal Distribution requires values for latents"):
        ProposalDistribution("gaussian", sigma=1.)
Ejemplo n.º 7
0
def test_it_throws_if_you_specify_gaussian_with_not_enough_sigmas_for_each_latent(net: BayesNet) -> None:
    with pytest.raises(
            TypeError, match=r"Gaussian Proposal Distribution requires a sigma or a list of sigmas for each latent"):
        ProposalDistribution("gaussian", latents=list(net.iter_latent_vertices()), sigma=[1.])
Ejemplo n.º 8
0
def test_you_can_create_a_gaussian_proposal_distribution(sigma: tensor_arg_types, net: BayesNet) -> None:
    ProposalDistribution("gaussian", latents=list(net.iter_latent_vertices()), sigma=sigma)
Ejemplo n.º 9
0
def test_you_can_create_a_prior_proposal_distribution(net) -> None:
    ProposalDistribution("prior", latents=list(net.iter_latent_vertices()))
Ejemplo n.º 10
0
def test_you_can_create_a_prior_proposal_distribution() -> None:
    ProposalDistribution("prior")
Ejemplo n.º 11
0
def test_it_throws_if_it_doesnt_recognise_the_type() -> None:
    with pytest.raises(KeyError) as excinfo:
        ProposalDistribution("foo")

    assert str(excinfo.value) == "'foo'"
Ejemplo n.º 12
0
def test_it_throws_if_you_specify_sigma_but_the_type_isnt_gaussian() -> None:
    with pytest.raises(TypeError) as excinfo:
        ProposalDistribution("prior", sigma=np.array(1.))

    assert str(excinfo.value
               ) == 'Parameter sigma is not valid unless type is "gaussian"'
Ejemplo n.º 13
0
def test_it_throws_if_you_specify_gaussian_without_a_value_for_sigma() -> None:
    with pytest.raises(TypeError) as excinfo:
        ProposalDistribution("gaussian")

    assert str(excinfo.value
               ) == "Gaussian Proposal Distribution requires a value for sigma"
Ejemplo n.º 14
0
def test_you_can_create_a_gaussian_proposal_distribution() -> None:
    ProposalDistribution("gaussian", sigma=np.array(1.))