Evidence contexts

Issue #59 resolved
Pierre Denis repo owner created an issue

In Lea, all evidence conditions for calculating conditional probabilities need to be passed in the given method. This may be cumbersome if the same evidences are considered granted, especially in interactive sessions.

...
lea.P(burglary.given(john_calls, mary_calls))
lea.P(earthquake.given(john_calls, mary_calls))
...

It is proposed to add methods for declaring evidences once for all. Two mechanisms are envisioned (they can be proposed together):

1- Evidence context managers, using the with keyword of Python:

with lea.evidence(john_calls, mary_calls):
    lea.P(burglary)
    lea.P(earthquake)

The declared evidences hold in the with block and are removed as soon as the this block is exited. These context managers can be embedded:

with lea.evidence(john_calls):
    with lea.evidence(mary_calls):
        lea.P(burglary)
        lea.P(earthquake)

2- Global evidence contexts, using simple functions for defining global evidences.

lea.add_evidence(john_calls, mary_calls)
lea.P(burglary)
lea.P(earthquake)

The evidences hold until end of the program/session or until a

lea.clear_evidence()

is executed. The evidences can be added piece by piece:

lea.add_evidence(john_calls)
lea.add_evidence(mary_calls)
lea.P(burglary)
lea.P(earthquake)

Each of the two approaches has merits and liabilities. Evidence context managers are good to clearly show the user the current evidences and to highlight that embededded probability expressions are actually conditional probabilities. It requires however to make the correct indenting, which can be cumbersome. Global evidence contexts are definitely easier for interactive sessions but the user has to remember the current evidences defined in order to interpret the results without error.

Comments (7)

  1. Log in to comment