liveBook · Manning

Conditional probability

As explained in Causal AI

Conditional probability is the probability distribution of one or more variables given specified values or events for other variables.

Suppose a product team wants to predict in-game purchases among highly engaged players. The distribution for those players may differ from the distribution across all players. Conditional probability describes the narrower group: P(I | E="high") is the distribution of purchases I among players whose engagement E is high. The condition to the right of | filters the population; it does not set anyone’s engagement level. section 8.2.2 section 7.3.1

More generally, P(Y | X=x) gives the distribution of Y when X has the specified value x. For discrete variables, compute P(Y=y | X=x) by dividing the joint probability P(X=x, Y=y) by the marginal probability P(X=x). The probabilities for all Y outcomes at that fixed value of X sum to 1. section 2.1.3

From a joint table to a conditional distribution

Suppose P(X=1, Y=0)=0.25 and P(X=1)=0.45. Then P(Y=0 | X=1)=0.25/0.45≈0.556. The other outcome has probability P(Y=1 | X=1)=0.20/0.45≈0.444. Together, these describe Y for the X=1 group—not the distribution of Y across every value of X. section 2.1.3

from pgmpy.factors.discrete.CPD import TabularCPD
PYgivenX = TabularCPD(
    variable='Y',    #1
    variable_card=2,     #2
    values=[  
        [.25/.45, .20/.30, .15/.25],     #3
        [.20/.45, .10/.30, .10/.25],     #3
    ], 
    evidence=['X'],
    evidence_card=[3],
    state_names = {
        'X': ['1', '2', '3'],
        'Y': ['0', '1']
    })

print(PYgivenX)

The pgmpy TabularCPD listing encodes a separate distribution of Y for each value of X, with each column formed from joint probabilities divided by its X marginal. section 2.1.3

Conditioning is useful when a population is heterogeneous. P(E | F=True) describes engagement among users exposed to a website feature; adding G="millennial" narrows the target to exposed users in that group. Such a comparison can guide segmentation, but the conditioning itself is a statement about a subpopulation. section 2.3.1

Conditional distributions also make larger models manageable. The chain rule breaks a joint distribution into factors, while a factor such as P(Y | X,Z) can use a canonical distribution whose parameters depend on X and Z. With a discrete X, plot one curve for each X value to see how the distribution of Y changes. section 2.1.4 section 2.1.7 section 2.1.8

Estimate a conditional probability table from data

In pandas, filter players by guild membership, count high and low side-quest engagement within each group, then divide by that group’s total. In the resulting data, about 0.798 of members and 0.184 of nonmembers have high engagement. Rounded values of 0.8 and 0.2 can parameterize the engagement distribution in a Pyro model. section 7.1.6

Choose the condition with care: conditioning can change relationships between other variables. Sprinkler state and rain can be independent, yet become dependent once wet grass—their shared effect—is known. Likewise, P(I | E="high") describes purchases for observed highly engaged players; P(I | do(E="high")) asks about setting engagement to high by intervention. These are different questions. section 4.1.1 section 7.3.1

Table 7.11 Examples of do-notation and counterfactual notation
Literal
Do-notation
Counterfactual notation
The probability distribution of In-Game Purchases across all players
P( I  )
P( I  )
The probability distribution of In-Game Purchases for players with “high” engagement
P( I  | E=“high”)
P( I  | E=“high”)
The probability distribution of In-Game Purchases when a player’s engagement level is set (by intervention) to “high”
P( I  |do( E= “high”))
P( I   E = “high”)
The joint probability distribution of In-Game Purchases and Guild Membership when engagement is set to “high”
P( I, G|do( E=“high”))
P( I   E=“high”, G E=“high”)
The probability distribution of In-Game Purchases when engagement is set to “high” and membership is set to “nonmember”
P( I  |do( E  =“high”, G=“nonmember”))
P( I   E = “high”, G =“nonmember”)
The probability distribution of In-Game Purchases for guild members when engagement is set to “high”
P( I  |do( E  =“high”), G  =  “member”)
P( I E   = “high”| G  =  “member”)

The notation table places an observed-subgroup purchase distribution beside its intervention counterpart, making the difference between filtering and setting engagement explicit. section 7.3.1

How Causal AI covers it

How deep it goes
Works through joint-table normalization and grouped-data estimates, then distinguishes factual conditioning from interventions.
Examples
Uses a numeric joint table, website-feature subpopulations, player engagement estimates, and wet grass as a collider.
What it assumes
Builds from joint and marginal probabilities before introducing conditional tables and their use in models.
What you can do after
Apply it via pgmpy or grouped pandas counts to construct conditional distributions, while distinguishing observed groups from interventions.
How it connects
Links joint and marginal distributions, the chain rule, canonical distributions, conditional independence, colliders, and interventions.

Also explained in

Compare how 11 books explain conditional probability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Causal AI. The section links point to the parts of the book it draws on.