Source code for compml.bayesian.posteriors.base
"""Base class for posterior distributions."""
from abc import ABC, abstractmethod
[docs]
class Posterior(ABC):
"""Abstract base class for posterior distributions."""
def __init__(self, rope: float):
"""Initialize the Posterior class.
Parameters
----------
rope : float
Width of the ROPE.
"""
self.__validate_arguments(rope=rope)
self.rope = rope
[docs]
def compute_probs(self) -> dict[str, float]:
"""Compute the posterior probabilities for the ROPE.
Returns
-------
dict
Posterior probabilities with the keys ``"p_left"``, ``"p_right"`` and ``"p_rope"``
if a ROPE is being used.
"""
return (
self._compute_probs_with_rope()
if self.rope != 0.0
else self._compute_probs_without_rope()
)
[docs]
@abstractmethod
def plot(self):
"""Plot the posterior distribution."""
pass
@abstractmethod
def _compute_probs_with_rope(self):
"""Compute the posterior probabilities with the ROPE.
Returns
-------
dict
Posterior probabilities with the keys ``"p_left"``, ``"p_rope"``, and ``"p_right"``.
"""
pass
@abstractmethod
def _compute_probs_without_rope(self):
"""Compute the posterior probabilities without the ROPE.
Returns
-------
dict
Posterior probabilities with the keys ``"p_left"``, ``"p_right"``.
"""
pass
def __validate_arguments(self, rope: float):
"""Validate the arguments for the Posterior class.
Raises
------
ValueError
If the arguments are invalid.
"""
if not isinstance(rope, (float, int)) or rope < 0:
raise ValueError(f"rope must be a non-negative number, got {rope}")