y0-causal-inference / y0

❓y0 (pronounced "why not?") is for causal inference in Python
https://y0.readthedocs.io
BSD 3-Clause "New" or "Revised" License
44 stars 9 forks source link

Implement simplify() algorithm #9

Open cthoyt opened 3 years ago

cthoyt commented 3 years ago

The simplify algorithm takes a graph, a probabilistic expression, and a topological sort order and produces a simplified probabilistic expression

Originally published with https://github.com/cran/causaleffect

class Simplifier:
    """A data structure to support application of the simplify algorithm.
    The simplify function is implemented inside a class since there is shared state between recursive calls
    and this makes it much easier to reference rather than making the simplify function itself have many
    arguments
    """

    #: The constant topological ordering
    ordering: Sequence[Variable]

    def __init__(
        self,
        graph:NxMixedGraph,
        ordering: Optional[Sequence[Union[str, Variable]]] = None,
    ) -> None:
        """Instantiate the simplifier.
        :param graph: A graph
        :param ordering: An optional topological ordering. If not provided, one will be generated from the graph.
        """
        self.graph = graph

        if ordering is None:
            self.ordering = [Variable(name) for name in self.graph.topological_sort()]
        else:
            self.ordering = _upgrade_ordering(ordering)

    def simplify(self, expression: Expression) -> Expression:
        """Simplify the expression given the internal topological ordering.
        :param expression: The expression to simplify
        :returns: The simplified expression
        """
        raise NotImplementedError
djinnome commented 3 years ago
cthoyt commented 3 years ago

It would be really great to have a y0 DSL to causaleffect converter, then we could create objects to pass directly to the causaleffect simplify implementation as an oracle for the tests