Skip to content

Commit

Permalink
First working version for marginals and MPEs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Nov 6, 2023
1 parent dc37f8b commit 2b3cb25
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI
name: Publish Python 🐍 distribution 📦 to PyPI

on: push

Expand Down
4 changes: 1 addition & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
fglib==0.2.3
portion>=2.4.1
pydantic>=2.4.2
fglib==0.2.3
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
networkx>=3.0
numpy>=1.24.4
random_events>=1.1.1
random_events>=1.1.3
tabulate>=0.9.0
probabilistic-model>=1.1.0
2 changes: 1 addition & 1 deletion src/fglib2/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.2'
__version__ = '1.1.3'
22 changes: 3 additions & 19 deletions src/fglib2/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from random_events.events import Event, EncodedEvent

import tabulate
from probabilistic_model.probabilistic_model import ProbabilisticModel


class Multinomial:
class Multinomial(ProbabilisticModel):
"""
A multinomial distribution over discrete random variables.
"""
Expand All @@ -27,7 +28,7 @@ class Multinomial:
"""

def __init__(self, variables: Iterable[Discrete], probabilities: Optional[np.ndarray] = None):
self.variables = tuple(sorted(variables))
super().__init__(variables)

shape = tuple(len(variable.domain) for variable in self.variables)

Expand Down Expand Up @@ -67,14 +68,6 @@ def _mode(self) -> Tuple[List[EncodedEvent], float]:
mode = [EncodedEvent(zip(self.variables, event)) for event in events.tolist()]
return mode, likelihood

def mode(self) -> Tuple[List, float]:
"""
Calculate the most likely event.
:return: The mode of the distribution as Event and its likelihood.
"""
mode, likelihood = self._mode()
return [mode_.decode() for mode_ in mode], likelihood

def __copy__(self) -> 'Multinomial':
"""
:return: a shallow copy of the distribution.
Expand Down Expand Up @@ -196,15 +189,6 @@ def _likelihood(self, event: List[int]) -> float:
"""
return float(self.probabilities[tuple(event)])

def likelihood(self, event: List) -> float:
"""
Calculate the likelihood of a full evidence query.
The event is a list of values for the variables in the same order
:param event:
:return: P(event)
"""
return self._likelihood(self.encode(event))

def _conditional(self, event: EncodedEvent) -> 'Multinomial':
"""
Calculate the conditional distribution given an event encoded.
Expand Down

0 comments on commit 2b3cb25

Please sign in to comment.