-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started to refactor and limit the scope of bayesian networks.
- Loading branch information
Showing
5 changed files
with
147 additions
and
44 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from random_events.events import Event, EncodedEvent | ||
from typing_extensions import Tuple, Dict, Iterable, List | ||
|
||
from .bayesian_network import BayesianNetworkMixin | ||
from ..probabilistic_model import ProbabilisticModel | ||
from random_events.variables import Discrete | ||
from ..distributions.distributions import DiscreteDistribution | ||
|
||
|
||
class RootDistribution(BayesianNetworkMixin, DiscreteDistribution): | ||
|
||
def forward_pass(self, event: EncodedEvent): | ||
self.forward_message, self.forward_probability = self._conditional(event) | ||
|
||
|
||
class ConditionalProbabilityTable(BayesianNetworkMixin): | ||
|
||
variables: Tuple[Discrete, ...] | ||
conditional_probability_distributions: Dict[Tuple, DiscreteDistribution] = dict() | ||
|
||
def __init__(self, variable: Discrete): | ||
ProbabilisticModel.__init__(self, [variable]) | ||
|
||
@property | ||
def variable(self) -> Discrete: | ||
return self.variables[0] | ||
|
||
def likelihood(self, event: Iterable) -> float: | ||
return self._likelihood([variable.encode(value) for variable, value in zip(self.parent_and_node_variables, event)]) | ||
|
||
def _likelihood(self, event: Iterable) -> float: | ||
parent_event = tuple(event[:1]) | ||
node_event = tuple(event[1:]) | ||
return self.conditional_probability_distributions[parent_event]._likelihood(node_event) | ||
|
||
def __repr__(self): | ||
return f"P({self.variable.name}|{self.parent.variable.name})" | ||
|
||
def to_tabulate(self) -> List[List[str]]: | ||
table = [[self.parent.variable.name, self.variable.name, repr(self)]] | ||
for parent_event, distribution in self.conditional_probability_distributions.items(): | ||
for event, probability in zip(self.variable.domain, distribution.weights): | ||
table.append([str(parent_event[0]), str(event), str(probability)]) | ||
return table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
|
||
from random_events.variables import Symbolic | ||
|
||
from probabilistic_model.bayesian_network.distributions import ConditionalProbabilityTable, RootDistribution | ||
from probabilistic_model.bayesian_network.bayesian_network import BayesianNetwork | ||
from probabilistic_model.distributions.distributions import SymbolicDistribution | ||
|
||
import tabulate | ||
|
||
|
||
class DistributionTestCase(unittest.TestCase): | ||
|
||
x = Symbolic("x", [0, 1, 2]) | ||
y = Symbolic("y", [0, 1]) | ||
|
||
p_x = ConditionalProbabilityTable(x) | ||
p_yx = ConditionalProbabilityTable(y) | ||
|
||
def setUp(self): | ||
bayesian_network = BayesianNetwork() | ||
|
||
# create the root distribution for x | ||
self.p_x = RootDistribution(self.x, [0.5, 0.3, 0.2]) | ||
|
||
# create the conditional probability table for y | ||
self.p_yx.conditional_probability_distributions[(0,)] = SymbolicDistribution(self.y, [0.5, 0.5]) | ||
self.p_yx.conditional_probability_distributions[(1,)] = SymbolicDistribution(self.y, [0.3, 0.7]) | ||
self.p_yx.conditional_probability_distributions[(2,)] = SymbolicDistribution(self.y, [0.1, 0.9]) | ||
|
||
# add the distributions to the bayesian network | ||
bayesian_network.add_node(self.p_x) | ||
bayesian_network.add_node(self.p_yx) | ||
|
||
# add the edge between x and y | ||
bayesian_network.add_edge(self.p_x, self.p_yx) | ||
|
||
def test_to_tabulate(self): | ||
table = tabulate.tabulate(self.p_yx.to_tabulate()) | ||
self.assertIsInstance(table, str) | ||
# print(table) | ||
|
||
def test_likelihood(self): | ||
self.assertEqual(self.p_yx.likelihood([0, 1]), 0.5) | ||
|
||
def test_probability(self): | ||
... | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |