pgmpy.ci_tests.get_ci_test#

pgmpy.ci_tests.get_ci_test(test=None, data=None)[source]#

Return an instantiated CI test object given a test name, instance, or data.

This is the recommended factory for obtaining a CI test. It supports four calling patterns:

  1. Pass-through: if test is already a _BaseCITest instance, it is returned as-is.

  2. Callable: if test is any other callable (e.g. a custom function), it is returned as-is.

  3. By name: if test is a string, the registered CI test whose name tag matches (case-insensitive) is instantiated and returned.

  4. Auto-detect: if test is None, the default CI test for the data type inferred from data is instantiated and returned.

Parameters:
teststr, _BaseCITest instance, callable, or None

The CI test to retrieve. If a string, must match the name tag of a registered CI test (e.g. "chi_square", "pearsonr"). If None, the default test for the data type of data is used.

datapandas.DataFrame or None

The dataset to pass to the CI test constructor. Required when test is None or when the resolved test has requires_data=True.

Returns:
_BaseCITest or callable

An instantiated CI test object ready to call, or the original callable if test was already callable.

Raises:
ValueError

If test is None and data is also None.

ValueError

If test is a string that does not match any registered CI test name.

ValueError

If the resolved CI test requires data but data is None.

ValueError

If test is not a string, _BaseCITest instance, callable, or None.

Examples

Get the default CI test for a continuous dataset (returns Pearsonr):

>>> import pandas as pd
>>> import numpy as np
>>> from pgmpy.ci_tests import ChiSquare, Pearsonr
>>> rng = np.random.default_rng(seed=42)
>>> data = pd.DataFrame(data=rng.standard_normal(size=(100, 3)), columns=["X", "Y", "Z"])
>>> test = get_ci_test(data=data)
>>> isinstance(test, Pearsonr)
True

Get a CI test by name:

>>> test = get_ci_test(test="chi_square", data=data)
>>> isinstance(test, ChiSquare)
True

Pass an already-instantiated CI test (returned unchanged):

>>> existing = Pearsonr(data=data)
>>> get_ci_test(test=existing) is existing
True

Pass any callable (e.g. a custom function) and it is returned unchanged:

>>> def my_ci_test(X, Y, Z, significance_level=0.05):
...     return True
...
>>> get_ci_test(test=my_ci_test) is my_ci_test
True