计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
inference-sampling-and-simulation【效率与协作】Guide pgmpy exact and approximate inference, sampling, simulation, and DBN inference workflows.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Use this sub-skill when a task asks for posterior probabilities, MAP assignments, exact or approximate inference, samples from an already-parameterized model, Dynamic Bayesian Network inference, or model-level simulation.
Do not use this sub-skill to build or fit the model first. Route graph/CPD
construction and check_model() failures to modeling-and-factors, structure or
parameter learning to learning-structure-and-parameters, causal identification,
ATE, and effect regressors to causal-identification-and-effects, and model/data
loading or metric evaluation to data-io-and-evaluation.
model.check_model() on Bayesian-network-style models after CPDs are
attached.query(..., evidence={...}) and simulate(..., evidence={...}) condition on
observed variables.simulate(..., do={...}) generates data under an intervention. If the task
asks to identify or estimate a causal effect, route to
causal-identification-and-effects instead of treating conditioning as a
substitute for do.{"Test": "positive"} is valid but {"Test": 1}
may not be.show_progress=False or globally disable progress bars with
pgmpy.global_vars.config.set_show_progress(False) for automated scripts.seed= for repeatable simulation and approximate inference.| Need | Primary pgmpy entry point | Notes |
|---|---|---|
| Exact posterior on a discrete BN, Markov model, factor graph, or junction tree | pgmpy.inference.VariableElimination or BeliefPropagation | Prefer VariableElimination for one-off exact queries; consider BeliefPropagation when repeatedly querying a calibrated junction-tree workflow. |
| MAP assignment | infer.map_query(...) | Query variables must not overlap evidence variables. |
| Large discrete model where exact inference is too expensive | pgmpy.inference.ApproxInference | Sampling-based; increase n_samples and use seed for reproducibility. |
| Forward, rejection, or likelihood-weighted samples from a BN | pgmpy.sampling.BayesianModelSampling | Direct sampler API; model.simulate(...) wraps it for common workflows. |
| Gibbs samples from a BN or Markov network | pgmpy.sampling.GibbsSampling | Uses integer state encodings in generated Markov-chain states; provide a valid start state when needed. |
| Model-level synthetic data with evidence, interventions, soft evidence, or missingness | DiscreteBayesianNetwork.simulate(...) or DynamicBayesianNetwork.simulate(...) | Returns a pandas object or array depending on DBN return_format. |
| Dynamic Bayesian Network exact inference | pgmpy.inference.DBNInference | Variables and evidence use tuple nodes like ("X", 2). |
from pgmpy.global_vars import config
from pgmpy.inference import VariableElimination
config.set_show_progress(False)
model.check_model()
infer = VariableElimination(model)
posterior = infer.query(
variables=["Disease"],
evidence={"Test": "positive"},
show_progress=False,
)
map_state = infer.map_query(
variables=["Disease"],
evidence={"Test": "positive"},
show_progress=False,
)
If that code fails, inspect the model's CPDs and state names before changing the
algorithm. Most inference errors come from an invalid model, evidence variable
not in the graph, evidence state not in a CPD, or asking for the same variable in
both variables and evidence.