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:
Pass-through: if
testis already a_BaseCITestinstance, it is returned as-is.Callable: if
testis any other callable (e.g. a custom function), it is returned as-is.By name: if
testis a string, the registered CI test whosenametag matches (case-insensitive) is instantiated and returned.Auto-detect: if
testisNone, the default CI test for the data type inferred fromdatais instantiated and returned.
- Parameters:
- teststr, _BaseCITest instance, callable, or None
The CI test to retrieve. If a string, must match the
nametag of a registered CI test (e.g."chi_square","pearsonr"). IfNone, the default test for the data type ofdatais used.- datapandas.DataFrame or None
The dataset to pass to the CI test constructor. Required when
testisNoneor when the resolved test hasrequires_data=True.
- Returns:
- _BaseCITest or callable
An instantiated CI test object ready to call, or the original callable if
testwas already callable.
- Raises:
- ValueError
If
testisNoneanddatais alsoNone.- ValueError
If
testis a string that does not match any registered CI test name.- ValueError
If the resolved CI test requires data but
dataisNone.- ValueError
If
testis not a string,_BaseCITestinstance, callable, orNone.
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