Skip to content

Commit

Permalink
Functionality besides serialization restored
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Jan 30, 2024
1 parent 897646a commit a1b364e
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
IntegerDistribution as PMIntegerDistribution,
DiscreteDistribution as PMDiscreteDistribution,
UnivariateDistribution as PMUnivariateDistribution)
from ..probabilistic_circuit import (DeterministicSumUnit, ProbabilisticCircuitMixin,
DirectedWeightedEdge, cache_inference_result)
from ..probabilistic_circuit import (DeterministicSumUnit, ProbabilisticCircuitMixin, cache_inference_result)
from ...distributions.uniform import UniformDistribution as PMUniformDistribution
from ...distributions.gaussian import (GaussianDistribution as PMGaussianDistribution,
TruncatedGaussianDistribution as PMTruncatedGaussianDistribution)
Expand All @@ -25,6 +24,9 @@ class UnivariateDistribution(PMUnivariateDistribution, ProbabilisticCircuitMixin
def variables(self) -> Tuple[Variable]:
return self._variables

def __hash__(self):
return ProbabilisticCircuitMixin.__hash__(self)


class ContinuousDistribution(UnivariateDistribution, PMContinuousDistribution, ProbabilisticCircuitMixin):

Expand Down Expand Up @@ -57,50 +59,27 @@ def conditional_from_complex_interval(self, interval: portion.Interval) -> \

# create and add the deterministic mixture as result
conditional = DeterministicSumUnit()
self.probabilistic_circuit.add_node(conditional)

# for every distribution and its normalized probability
for distribution, probability in zip(resulting_distributions, normalized_probabilities):

# create an edge from the mixture to the distribution
edge = DirectedWeightedEdge(conditional, distribution, probability)
conditional.probabilistic_circuit.add_edge(edge)
for probability, distribution in zip(normalized_probabilities, resulting_distributions):
conditional.mount(distribution)
conditional.probabilistic_circuit.add_edge(conditional, distribution, weight=probability)

return conditional, total_probability

def conditional_from_singleton(self, singleton: portion.Interval) -> \
Tuple[Optional['DiracDeltaDistribution'], float]:
Tuple['DiracDeltaDistribution', float]:
conditional, probability = super().conditional_from_singleton(singleton)
return DiracDeltaDistribution(conditional.variable, conditional.location, conditional.density_cap), probability

@cache_inference_result
def _conditional(self, event: EncodedEvent) -> \
Tuple[Optional[Union['ContinuousDistribution', 'DiracDeltaDistribution', DeterministicSumUnit]], float]:
return super()._conditional(event)

# get the conditional from the superclass
conditional, probability = super()._conditional(event)

# if the conditional is None
if conditional is None:

# remove self from the circuit
self.probabilistic_circuit.remove_node(self)
return None, 0

# add the conditional node
self.probabilistic_circuit.add_node(conditional)

# get the edges
new_edges = [edge.__copy__() for edge in self.incoming_edges()]
for edge in new_edges:
edge.target = conditional

self.probabilistic_circuit.remove_node(self)
self.probabilistic_circuit.add_edges_from(new_edges)
return conditional, probability

@cache_inference_result
def marginal(self, variables: Iterable[Variable]) -> Optional[Self]:
return ProbabilisticCircuitMixin.marginal(self, variables)
return PMContinuousDistribution.marginal(self, variables)


class DiscreteDistribution(UnivariateDistribution, PMDiscreteDistribution, ProbabilisticCircuitMixin):
Expand Down
Loading

0 comments on commit a1b364e

Please sign in to comment.