Skip to content

asymcat.scorer functions

Functions that compute a measure from raw series or a contingency table, and helpers that transform the dictionaries returned by CatScorer.

Defines the various scorers for categorical co-occurrence analysis.

scale_scorer

scale_scorer(scorer: dict[tuple[Any, Any], tuple[float, float]], method: str = 'minmax', nrange: tuple[float, float] = (0, 1)) -> dict[tuple[Any, Any], tuple[float, float]]

Scale a scorer.

The function returns a scaled version of a scorer considering all the asymmetric scorers (i.e., both x given y and y given x). Implemented scoring methods are "minmax" (by default on range [0, 1], which can be modified by the nrange parameter) and "mean".

Parameters:

Name Type Description Default
scorer dict

A scoring dictionary.

required
method str

The scoring method to be used, either "minmax" or "mean" or "stdev" (default: "minmax").

'minmax'
nrange tuple

A tuple with the scaling range, to be used when applicable (default: (0, 1)).

(0, 1)

Returns:

Type Description
dict

A scaled version of the scorer.

invert_scorer

invert_scorer(scorer: dict[tuple[Any, Any], tuple[float, float]]) -> dict[tuple[Any, Any], tuple[float, float]]

Inverts a scorer, so that the higher the affinity, the higher the score.

It is recommended than only scorers in range [0..] are inverted.

Parameters:

Name Type Description Default
scorer dict

A scoring dictionary.

required

Returns:

Type Description
dict

An inverted version of the scorer.

scorer2matrices

scorer2matrices(scorer: dict[tuple[Any, Any], tuple[float, float]]) -> tuple[np.ndarray, np.ndarray, list[Any], list[Any]]

Return the asymmetric matrices implied by a scorer and their alphabets.

Parameters:

Name Type Description Default
scorer dict

A scoring dictionary.

required

Returns:

Type Description
tuple

A tuple with the following elements: a scoring matrix of y given x, a scoring matrix of x given y, the alphabet for matrix x, and the alphabet for matrix y.

conditional_entropy

conditional_entropy(x_symbols: list[Any], y_symbols: list[Any], estimator: str = 'plugin') -> float

Computes the entropy of x given y.

Parameters:

Name Type Description Default
x_symbols List[Any]

A list of all observed x symbols.

required
y_symbols List[Any]

A list of all observed y symbols.

required
estimator str

Entropy estimator: "plugin" (default, the maximum-likelihood estimate), "chao_shen" or "nsb". The bias-corrected estimators compute H(X | Y) = H(X, Y) - H(Y) from separately corrected entropies, taking the observed symbols as the alphabet.

'plugin'

Returns:

Type Description
float

The conditional entropy of x given y.

compute_theil_u

compute_theil_u(x_symbols: list[Any], y_symbols: list[Any], estimator: str = 'plugin') -> float

Compute the uncertainty coefficient Theil's U.

Parameters:

Name Type Description Default
x_symbols List[Any]

The list of observed symbols in series x.

required
y_symbols List[Any]

The list of observed symbols in series y.

required
estimator str

Entropy estimator: "plugin" (default), "chao_shen" or "nsb" (see conditional_entropy). Bias-corrected estimates are not confined to [0, 1]: on small samples a slightly negative value means no detectable association.

'plugin'

Returns:

Type Description
float

The uncertainty coefficient given x and y.

compute_mutual_information

compute_mutual_information(x_symbols: list[Any], y_symbols: list[Any], estimator: str = 'plugin') -> float

Compute the mutual information between X and Y.

MI(X;Y) = Σ_x Σ_y p(x,y) * log(p(x,y) / (p(x) * p(y)))

Parameters:

Name Type Description Default
x_symbols List[Any]

A list of all observed x symbols.

required
y_symbols List[Any]

A list of all observed y symbols.

required
estimator str

Entropy estimator: "plugin" (default), "chao_shen" or "nsb". The bias-corrected estimators compute MI = H(X) + H(Y) - H(X, Y) from separately corrected entropies, which can be slightly negative on small samples.

'plugin'

Returns:

Type Description
float

The mutual information between X and Y.

compute_normalized_mutual_information

compute_normalized_mutual_information(x_symbols: list[Any], y_symbols: list[Any], estimator: str = 'plugin') -> float

Compute the normalized mutual information between X and Y.

NMI(X;Y) = MI(X;Y) / H(X,Y) where H(X,Y) is the joint entropy.

Parameters:

Name Type Description Default
x_symbols List[Any]

A list of all observed x symbols.

required
y_symbols List[Any]

A list of all observed y symbols.

required
estimator str

Entropy estimator: "plugin" (default), "chao_shen" or "nsb" (see compute_mutual_information). Bias-corrected estimates are not confined to [0, 1].

'plugin'

Returns:

Type Description
float

The normalized mutual information between X and Y, in range [0, 1] for the plug-in estimator.

compute_cramers_v

compute_cramers_v(cont_table: list[list[float]]) -> float

Compute Cramer's V from a contingency table.

Parameters:

Name Type Description Default
cont_table array

The contingency table for computation.

required

Returns:

Type Description
float

The Cramér's V measure for the given contingency table.

compute_pmi

compute_pmi(p_x: float, p_y: float, p_xy: float, normalized: bool, limit: float = 1e-06) -> float

Compute the Pointwise Mutual Information.

Parameters:

Name Type Description Default
p_x float

The probability of x, p(x).

required
p_y float

The probability of y, p(y).

required
p_xy float

The probability of xy, p(xy).

required
normalized bool

Whether to return the normalized Pointwise Mutual Information in range [-1, 1].

required
limit float

The value to use for computation when p_xy is 0.0 (i.e., non observed), as the limit of PMI to zero (default: 1e-6).

1e-06

Returns:

Type Description
float

The Pointwise Mutual Information.

compute_jaccard_index

compute_jaccard_index(x_contexts: list[Any], y_contexts: list[Any]) -> float

Compute the Jaccard Index between two sets of contexts.

J(X,Y) = |X ∩ Y| / |X ∪ Y|

Parameters:

Name Type Description Default
x_contexts List[Any]

Contexts where symbol x appears.

required
y_contexts List[Any]

Contexts where symbol y appears.

required

Returns:

Type Description
float

The Jaccard Index between the two context sets, in range [0, 1].

compute_goodman_kruskal_lambda

compute_goodman_kruskal_lambda(x_symbols: list[Any], y_symbols: list[Any], direction: str = 'y_given_x') -> float

Compute Goodman and Kruskal's Lambda for asymmetric association.

λ(Y|X) = (Σ_x max(n_x,y) - max(n_y)) / (n - max(n_y))

Parameters:

Name Type Description Default
x_symbols List[Any]

A list of all observed x symbols.

required
y_symbols List[Any]

A list of all observed y symbols.

required
direction str

Either "y_given_x" or "x_given_y" to specify direction.

'y_given_x'

Returns:

Type Description
float

The Goodman-Kruskal Lambda coefficient, in range [0, 1]. When the predicted variable takes a single value there are no errors to reduce, and the coefficient is 1.0 by convention, as for compute_theil_u.

compute_log_likelihood_ratio

compute_log_likelihood_ratio(cont_table: list[list[float]]) -> float

Compute the Log-Likelihood Ratio (G²) from a contingency table.

G² = 2 * Σ O_ij * ln(O_ij / E_ij) where O_ij are observed frequencies and E_ij are expected frequencies.

Parameters:

Name Type Description Default
cont_table List[List[float]]

The contingency table for computation.

required

Returns:

Type Description
float

The Log-Likelihood Ratio statistic.

compute_log_likelihood_ratio_pvalue

compute_log_likelihood_ratio_pvalue(cont_table: list[list[float]]) -> float

Compute the p-value of the Log-Likelihood Ratio (G²) statistic.

Under the null hypothesis of independence, G² is asymptotically chi-square distributed with (rows - 1) * (cols - 1) degrees of freedom.

Parameters:

Name Type Description Default
cont_table List[List[float]]

The contingency table for computation.

required

Returns:

Type Description
float

The p-value for the Log-Likelihood Ratio statistic, in range [0, 1].