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'
|
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 |
conditional_entropy ¶
Computes the entropy of x given y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_symbols
|
List[Any]
|
A list of all observed |
required |
y_symbols
|
List[Any]
|
A list of all observed |
required |
estimator
|
str
|
Entropy estimator: |
'plugin'
|
Returns:
| Type | Description |
|---|---|
float
|
The conditional entropy of |
compute_theil_u ¶
Compute the uncertainty coefficient Theil's U.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_symbols
|
List[Any]
|
The list of observed symbols in series |
required |
y_symbols
|
List[Any]
|
The list of observed symbols in series |
required |
estimator
|
str
|
Entropy estimator: |
'plugin'
|
Returns:
| Type | Description |
|---|---|
float
|
The uncertainty coefficient given |
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 |
required |
y_symbols
|
List[Any]
|
A list of all observed |
required |
estimator
|
str
|
Entropy estimator: |
'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 |
required |
y_symbols
|
List[Any]
|
A list of all observed |
required |
estimator
|
str
|
Entropy estimator: |
'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 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 the Pointwise Mutual Information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p_x
|
float
|
The probability of |
required |
p_y
|
float
|
The probability of |
required |
p_xy
|
float
|
The probability of |
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 |
1e-06
|
Returns:
| Type | Description |
|---|---|
float
|
The Pointwise Mutual Information. |
compute_jaccard_index ¶
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 |
required |
y_symbols
|
List[Any]
|
A list of all observed |
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_log_likelihood_ratio ¶
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 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]. |