pyagrum.explain
The purpose of pyagrum.explain is to give tools to explain and interpret the structure and
parameters of a Bayesian network.
Dealing with independence
get the p-values of the chi2 test of a (as simple as possible) independence proposition for every non arc. |
Dealing with mutual information and entropy
get a HTML string for a bn annotated with results from inference : entropy and mutual information |
|
Create a dot representation of the information graph for this BN |
|
Display a bn annotated with results from inference : entropy and mutual information. |
Dealing with Shap Values and Shall Values
All attribution methods return an Explanation object, which maps
feature names to their individual attribution values and carries auxiliary data from the
computation.
- class pyagrum.explain.Explanation(values, importances, feature_names, data, baseline, func, values_type)
Structured container for the result of a Shap/Shall explanation.
Acts as a mapping from feature names to their attribution values, and also carries the auxiliary data produced during the computation.
Shap Values
Shap (SHapley Additive exPlanations) values decompose a model prediction into the additive contributions of each feature, based on the Shapley value from cooperative game theory. Three variants are available depending on how feature interactions are handled:
- class pyagrum.explain.MarginalShapValues(bn, target, background, sample_size=1000, logit=True)
Bases:
ShapleyValues,MarginalComputationThe MarginalShapValues class computes the Marginal Shapley values for a given target node in a Bayesian Network.
- Parameters:
bn (
BayesNet)target (
int)background (
DataFrame|tuple|None)
Scientific notes
MarginalShapValues computes the marginal (or interventional) Shapley values advocated by [1]
(see pyagrum.explain.ConditionalShapValues for the Shapley values and the options common to the explainers): the
features out of the coalition keep the values of a background sample \(b_1, \dots, b_N\), independently of the
values of the coalition,
each term being the posterior of the target given all the features (a hybrid row). The logit, if any, is applied to
this mean. The background is a data frame with a column for each variable of the network (the target column is
ignored); without it, sample_size (1000) rows are drawn from the network, so that the values are then random.
When all the variables are features, the posterior given all of them depends only on the Markov blanket of
\(Y\), and the features outside the blanket get a null value.
Methods
Computes the Shapley values for the target node based on the provided data. |
- class pyagrum.explain.ConditionalShapValues(bn, target, logit=True)
Bases:
ShapleyValuesThe ConditionalShapValues class computes the conditional Shapley values for a given target node in a Bayesian Network.
Scientific notes
The explainers of pyagrum.explain compute Shapley values [2], as in SHAP
[3], exactly and by inference in the Bayesian network. For an instance \(x\) and a game
\(v\) on the set \(F\) of the \(m\) features (the players), the value of a feature \(i\) is its mean
marginal contribution
The Shap explainers (pyagrum.explain.ConditionalShapValues, pyagrum.explain.MarginalShapValues,
pyagrum.explain.CausalShapValues) explain the posterior of a target \(Y\): the features are the variables given
in the data, except \(Y\), and each value \(y\) of the target has its own Shapley values, for the game
\(v(S) = g(p_S(y))\), where \(p_S(y)\) is a probability of \(y\) that depends on the explainer and
\(g\) is the logit \(\log\frac{p}{1-p}\) (default, logit=True; a probability of 0 or 1 gives 0) or the
identity. The baseline is \(v(\emptyset)\). All the \(2^m - 1\) coalitions are computed, so that the cost
grows exponentially with the number of features. Several rows are explained at once after removing the duplicated
rows (without data, 100 rows are drawn from the network); the importance of a feature is the mean of
\(|\phi_i|\) over these distinct rows.
ConditionalShapValues uses the posterior given the values of the coalition, as advocated by [4] for dependent features:
computed by exact inference (pyagrum.LazyPropagation), with \(p_\emptyset(y) = P(y)\). It needs no background
data. A coalition whose extra variable does not change the posterior (it is not in the minimal conditioning set of
\(Y\) given the coalition) reuses the value of the smaller coalition. Unlike the marginal values, a feature
outside the Markov blanket of \(Y\) may receive a non-null value, through its dependence with the features of
the blanket: the conditional values are “true to the data”, the marginal ones “true to the model”
[5].
Lloyd S. Shapley. A value for n-person games. In Harold W. Kuhn and Albert W. Tucker, editors, Contributions to the Theory of Games, Volume II, volume 28 of Annals of Mathematics Studies, pages 307–318. Princeton University Press, 1953. URL: https://doi.org/10.1515/9781400881970-018, doi:10.1515/9781400881970-018.
Scott M. Lundberg and Su-In Lee. A unified approach to interpreting model predictions. In Advances in Neural Information Processing Systems, volume 30. Curran Associates, Inc., 2017. URL: https://proceedings.neurips.cc/paper/2017/hash/8a20a8621978632d76c43dfd28b67767-Abstract.html.
Kjersti Aas, Martin Jullum, and Anders Løland. Explaining individual predictions when features are dependent: more accurate approximations to Shapley values. Artificial Intelligence, 298:103502, 2021. URL: https://arxiv.org/abs/1903.10464, doi:10.1016/j.artint.2021.103502.
Hugh Chen, Joseph D. Janizek, Scott Lundberg, and Su-In Lee. True to the model or true to the data? arXiv preprint arXiv:2006.16234, 2020. URL: https://arxiv.org/abs/2006.16234.
Methods
Computes the Shapley values for the target node based on the provided data. |
The causal Shapley values are computed following Heskes et al. [HSBC20].
- class pyagrum.explain.CausalShapValues(bn, target, background, sample_size=1000, logit=True)
Bases:
ShapleyValues,CausalComputationThe CausalShapValues class computes the Causal Shapley values for a given target node in a Bayesian Network.
- Parameters:
background (
DataFrame|tuple|None)
Scientific notes
CausalShapValues computes the causal Shapley values of [6] (see
pyagrum.explain.ConditionalShapValues for the Shapley values and the options common to the explainers): the game is
the expected prediction when the features of the coalition are set by an intervention,
where \(P(y \mid X_A)\) is the posterior of the target given its ancestors \(A\). The players are only these ancestors: an intervention on any other feature does not change \(Y\), and these features get a null value. For a coalition of ancestors, \(p_S(y)\) is the causal effect \(P(y \mid do(x_S))\).
The expectation is computed on a background sample (data frame with a column for each variable; without it,
sample_size (1000) rows are drawn from the network): the arcs into \(S\) are removed and \(S\) is set to
\(x_S\) (mutilated network \(P_S\)); in each background row the players of \(S\) are set to \(x_S\),
and each distinct configuration \(b\) of the players so obtained is weighted by \(P_S(b_{\bar S})\),
\(\bar S\) being the other players:
The number of occurrences of a configuration in the background is not used: its probability is already the weight. For the empty coalition, \(p_\emptyset(y) = P(y)\) (under the condition below).
Implementation limit. The expectation is exact when the background contains every configuration of the players of \(\bar S\) with a non-zero probability under \(P_S\); otherwise it is restricted to the configurations present (and renormalized).
Tom Heskes, Evi Sijben, Ioan Gabriel Bucur, and Tom Claassen. Causal Shapley values: exploiting causal knowledge to explain individual predictions of complex models. In Advances in Neural Information Processing Systems, volume 33, 4778–4789. Curran Associates, Inc., 2020. URL: https://arxiv.org/abs/2011.01625.
Methods
Computes the Shapley values for the target node based on the provided data. |
Shall Values
Shall (SHapley Additive Log-Likelihood) values are a pyAgrum-specific explanation method. Where Shap values attribute a prediction, Shall values attribute the log-likelihood of a baseline observation: they decompose \(\log P(e)\) — the log-probability of an evidence \(e\) — into the additive contribution of each variable. This makes them particularly suited for explaining why a given observation is more or less probable under the model, rather than explaining a prediction output.
The same three variants (marginal, conditional, causal) are available:
- class pyagrum.explain.MarginalShallValues(bn, background, sample_size=1000, log=True)
Bases:
ShallValues,MarginalComputationThe MarginalShallValues class computes the Marginal Shall values in a Bayesian Network.
- Parameters:
bn (
BayesNet)background (
tuple|None)sample_size (
int)log (
bool)
Scientific notes
The Shall values (Shapley additive log-likelihood) are specific to pyAgrum: instead of a prediction, they share the
log-likelihood \(\log P(x)\) of a complete instance \(x\) among the variables. They are the Shapley values [2] (see
pyagrum.explain.ConditionalShapValues) of a game whose players are all the variables of the network (the data must
give them all), the value \(v(S)\) of a coalition being an expected log-likelihood when the variables of
\(S\) take their values in \(x\). It is computed on a background sample \(b_1, \dots, b_N\) (data frame of
the variables; without it, sample_size (1000) rows are drawn from the network) with the joint probability given by
the network (natural logarithm; the probability itself with log=False). With \(v(F) = \log P(x)\), the values
sum to \(\log P(x) - v(\emptyset)\): a negative value marks a variable whose value makes the instance less likely
than the background. As for the Shap values, all the \(2^n - 1\) coalitions are computed, duplicated rows are
explained once and the importance is the mean of \(|\phi_i|\).
MarginalShallValues completes the coalition with each background row, independently of \(x_S\):
so that \(v(\emptyset)\) is the mean log-likelihood of the background, an estimate of \(-H(P)\) for a sample of the network.
Methods
Computes the SHALL values for all rows in the provided data. |
- class pyagrum.explain.ConditionalShallValues(bn, background, sample_size=1000, log=True)
Bases:
ShallValues,ConditionalComputationThe ConditionalShallValues class computes the conditional Shall values in a Bayesian Network.
- Parameters:
bn (
BayesNet)background (
tuple|None)sample_size (
int)log (
bool)
Scientific notes
ConditionalShallValues computes Shall values (see pyagrum.explain.MarginalShallValues) whose game is the mean
log-likelihood of the background rows that agree with the instance on the coalition, an estimate of
\(E[\log P(X) \mid X_S = x_S]\) under the distribution of the background (true to the data, in the sense of
[5]):
Implementation limit. When no background row agrees with \(x_S\) (large coalitions, rare values, small
background), \(v(S)\) is set to 0 (the log-likelihood of a sure event; a null probability with log=False), which biases the values: the background
must be large enough for the conditional means to exist.
Methods
Computes the SHALL values for all rows in the provided data. |
- class pyagrum.explain.CausalShallValues(bn, background, sample_size=1000, log=True)
Bases:
ShallValues,CausalComputationThe CausalShallValues class computes the Causal Shall values in a Bayesian Network.
- Parameters:
bn (
BayesNet)background (
tuple|None)sample_size (
int)log (
bool)
Scientific notes
CausalShallValues computes Shall values (see pyagrum.explain.MarginalShallValues) whose game follows the causal
Shapley values of [6] (see pyagrum.explain.CausalShapValues): the variables of the coalition
are set by an intervention, \(v(S) \approx E[\log P(X) \mid do(X_S = x_S)]\). In the mutilated network
\(P_S\) (arcs into \(S\) removed, \(S\) set to \(x_S\)), the background rows are given the values
\(x_S\) on \(S\), and each distinct row \(b\) so obtained is weighted by \(P_S(b_{\bar S})\) (its
number of occurrences is not used):
All the variables are players: there is no target, hence no restriction to its ancestors. The values are true to the model, not to the data: \(v(\emptyset) = \sum_b P(b) \log P(b) / \sum_b P(b)\), the opposite of the entropy when the background covers the support, differs from the mean log-likelihood of the background used by the marginal and conditional values.
Implementation limit. As for pyagrum.explain.CausalShapValues, the expectation is exact only when the background
contains every configuration of \(\bar S\) with a non-zero probability under \(P_S\); otherwise it is
restricted to the configurations present (and renormalized).
Methods
Computes the SHALL values for all rows in the provided data. |
Dealing with generalized Markov Blankets
A structural property of Bayesian networks is the Markov boundary of a node. A Markov blanket of a node is a set of nodes that renders the node independent of all other nodes in the network. The Markov boundary is the closest Markov blanket. A Markov boundary of a node is composed of its parents, its children, and the parents of its children. More generally, one can define the generalized \(k\)-Markov blanket of a node as the union of the markov blanket of the nodes of its \((k-1)\)-Markov blanket. So, if a node belongs to the \(k\)-Markov blanket of the node \(X\), \(k\) is a kind of measure of its proximity to \(X\).
Build a pydot.Dot representation of the nested Markov Blankets (of order k) of node x |
Tom Heskes, Evi Sijben, Ioan Gabriel Bucur, and Tom Claassen. Causal Shapley values: exploiting causal knowledge to explain individual predictions of complex models. In Advances in Neural Information Processing Systems, volume 33, 4778–4789. Curran Associates, Inc., 2020. URL: https://arxiv.org/abs/2011.01625.