.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/02_probability.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_02_probability.py: Probabilities under changing assumptions ============================================ Rain or a sprinkler makes the ground wet: ``W = R ∨ S``. Given wet ground, what is the probability of rain? We build the events once, then evaluate them under two sets of independent priors. A third variable, wind, is unconstrained by these events. .. GENERATED FROM PYTHON SOURCE LINES 11-20 .. code-block:: Python from fractions import Fraction from tididi import Vtree, literal vtree = Vtree.balanced(3) rain = literal(vtree, 1) sprinkler = literal(vtree, 2) wet = rain.copy() | sprinkler rain_and_wet = rain & wet.copy() .. GENERATED FROM PYTHON SOURCE LINES 21-29 Exact fractions ------------------- ``Fraction`` comes from Python's standard library. Each variable has a pair of weights: false first, true second. For independent probabilities these sum to one. In particular, the free wind variable contributes one. In both scenarios below the evidence has positive probability, so ``P(R | W) = P(R ∧ W) / P(W)`` is defined. .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python for rain_probability in [Fraction(1, 5), Fraction(3, 5)]: priors = {1: rain_probability, 2: Fraction(1, 10), 3: Fraction(2, 5)} weights = {variable: (1 - p, p) for variable, p in priors.items()} wet_probability = wet.weighted_count(weights) conditional = rain_and_wet.weighted_count(weights) / wet_probability print(f"P(rain) = {rain_probability}, P(wet) = {wet_probability}") print("P(rain | wet) =", conditional) .. rst-class:: sphx-glr-script-out .. code-block:: none P(rain) = 1/5, P(wet) = 7/25 P(rain | wet) = 5/7 P(rain) = 3/5, P(wet) = 16/25 P(rain | wet) = 15/16 .. GENERATED FROM PYTHON SOURCE LINES 38-41 Both evaluations borrowed their circuits. No rebuilding or copying was needed when the priors changed. More generally, weights need not sum to one: unit weights recover the model count. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python unit_weights = {variable: (1, 1) for variable in vtree.variables} print("Models of wet:", wet.weighted_count(unit_weights)) .. rst-class:: sphx-glr-script-out .. code-block:: none Models of wet: 6 .. GENERATED FROM PYTHON SOURCE LINES 46-51 Change observations ------------------- An evaluator caches values so changing an observation refreshes only affected branches. It consumes ``wet``; ``finish()`` returns the circuit when we are done. The value with no rain is the joint probability ``P(W ∧ ¬R)``, without normalization. .. GENERATED FROM PYTHON SOURCE LINES 51-57 .. code-block:: Python evaluator = wet.evaluator(weights) evaluator.observe([-1]) print("P(wet and no rain) =", evaluator.value()) evaluator.clear_observations() print("P(wet) =", evaluator.value()) wet = evaluator.finish() .. rst-class:: sphx-glr-script-out .. code-block:: none P(wet and no rain) = 1/25 P(wet) = 16/25 .. _sphx_glr_download_tutorials_02_probability.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_probability.ipynb <02_probability.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_probability.py <02_probability.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02_probability.zip <02_probability.zip>`