ba

ba: Bayesian Association — unified probabilistic framework for categorical data.

Three-tier API:

Façade (one-liner):

result = ba.analyze(df, outcome='Y')
result.summary()

Paradigm (per-tradition):

ba.bayesian.posterior(table, prior='jeffreys')
ba.rules.mine(df, min_support=0.1)
ba.qca.truth_table(binary_df, outcome='Y', conditions=['A','B'])

Primitives (direct access):

from ba.core import ContingencyTable, MeasureRegistry
class ba.AnalysisResult(observed_data, contingency_tables, metrics, posterior, rules, config, warnings)[source]

Container for all analysis outputs from ba.analyze().

>>> import pandas as pd
>>> df = pd.DataFrame({'A': [1,0,1,0], 'B': [1,1,0,0]})
>>> result = analyze(df)
>>> len(result.contingency_tables) == 1
True
summary(sort_by: str | None = None) pd.DataFrame[source]

Metrics DataFrame, optionally sorted.

>>> import pandas as pd
>>> result = analyze(pd.DataFrame({'A': [1,0], 'B': [0,1]}))
>>> 'pair' in result.summary().columns
True
top_pairs(n: int = 10, *, sort_by: str = 'bayes_factor') pd.DataFrame[source]

Top n pairs by the given metric.

top_rules(n: int = 10, *, sort_by: str = 'lift') pd.DataFrame[source]

Top n rules (if rules were mined).

ba.analyze(data, *, outcome: str | None = None, variables: list[str] | None = None, prior: str = 'jeffreys', bayesian: bool = True, rules: bool = False, min_support: float | None = None) AnalysisResult[source]

Analyze all pairwise associations in a DataFrame.

This is the top-level entry point. It computes contingency tables, metrics, and optionally Bayesian posteriors and association rules for all variable pairs.

Parameters:
  • data – DataFrame or path to CSV.

  • outcome – If given, only pairs involving this variable.

  • variables – Subset of columns. Default: all.

  • prior – Bayesian prior specification.

  • bayesian – Compute Bayesian posteriors (default True).

  • rules – Mine association rules (default False).

  • min_support – For rule mining; defaults to 2/n.

Returns:

AnalysisResult with all computed outputs.

>>> import pandas as pd
>>> df = pd.DataFrame({
...     'A': [1,1,0,0,1,0],
...     'B': [1,0,1,0,1,0],
...     'Y': [1,1,0,0,1,0],
... })
>>> result = analyze(df, outcome='Y')
>>> len(result.contingency_tables) == 2
True
>>> result.summary() is not None
True
ba.contingency_table(a: int, b: int, c: int, d: int, *, row_var: str = 'X', col_var: str = 'Y') ContingencyTable2x2[source]

Create a 2×2 contingency table from cell counts.

Layout:

     Y=1  Y=0
X=1 [  a    b ]
X=0 [  c    d ]
>>> ct = contingency_table(10, 5, 3, 12)
>>> ct.n
30
>>> ct.odds_ratio
8.0
ba.from_dataframe(df, row_var: str, col_var: str) ContingencyTable[source]

Cross-tabulate two columns into a contingency table.

Returns ContingencyTable2x2 if both variables have exactly 2 levels.

>>> import pandas as pd
>>> df = pd.DataFrame({'X': [1,1,0,0,1], 'Y': [1,0,1,0,1]})
>>> ct = from_dataframe(df, 'X', 'Y')
>>> ct.n
5