asymcat 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ Credits
2
+ =======
3
+
4
+ Development
5
+ -----------
6
+
7
+ * Tiago Tresoldi <tiago.tresoldi@lingfil.uu.se>
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+
3
+ Version 0.3:
4
+ - Renamed from `catcoocc` to `asymcat`, to better reflect the
5
+ functionality of the library
6
+
7
+ Version 0.2.2:
8
+ - Added function for inverting a scorer
9
+
10
+ Version 0.2.1:
11
+
12
+ - Added basic functions for double series correlation
asymcat-0.3.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Tiago Tresoldi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ include AUTHORS.md
2
+ include CHANGELOG.md
3
+ include LICENSE
4
+ include README.md
5
+ include docs/*
6
+ include pyproject.toml
7
+ include requirements.txt
8
+ include resources/*
asymcat-0.3.0/PKG-INFO ADDED
@@ -0,0 +1,266 @@
1
+ Metadata-Version: 2.1
2
+ Name: asymcat
3
+ Version: 0.3.0
4
+ Summary: A Python library for obtaining asymmetric measures of association between categorical variables in data exploration and description
5
+ Home-page: http://github.com/tresoldi/asymcat
6
+ Author: Tiago Tresoldi
7
+ License: MIT
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
12
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
13
+ Classifier: Topic :: Software Development :: Libraries
14
+ Requires-Python: >=3.8, <4
15
+ Description-Content-Type: text/markdown
16
+ Provides-Extra: dev
17
+ License-File: LICENSE
18
+ License-File: AUTHORS.md
19
+
20
+ # ASymCat
21
+
22
+ ASymCat is a powerful Python library designed to analyze co-occurrence association between categorical
23
+ variables using various symmetric and asymmetric measures of association. Whether you are working with
24
+ linguistic data, or any other kind of categorical data, ASymCat can help you explore and describe your
25
+ data in new and meaningful ways.
26
+
27
+ Using ASymCat, you can input co-occurrence observations such as records, alignments, and matrices of
28
+ presence-absence, and generate dictionaries that provide association scores between categories. You can also
29
+ focus on the strength or direction of association, or both, depending on your research questions.
30
+
31
+ In addition to the main methods for numeric computation of measures of association, ASymCat includes auxiliary
32
+ methods that can help you deal with relational data, n-grams from sequences, alignments, and binary
33
+ matrices of presence/absence.
34
+
35
+ ASymCat is a versatile library that can be used by researchers in various fields, and it can help you gain
36
+ new insights into your data.
37
+
38
+ ## Background
39
+
40
+ Measures of association play a crucial role in statistical analysis as they help to quantify the relationship
41
+ between variables. While a variety of measures exist, correlation coefficients are widely used to determine
42
+ the strength and direction of the relationship between two variables. Pearson's **rho**, for example,
43
+ is frequently used to assess the strength of the linear relationship between continuous variables, whereas
44
+ Spearman's **rho** is employed to evaluate the strength of the monotonic relationship between ordinal or
45
+ ranked variables. The Chi-square measure, on the other hand, is a useful tool for measuring the
46
+ association between categorical variables.
47
+
48
+ It is important to note that most measures of association are symmetric, indicating that the association
49
+ between any `x` and `y` value is equal to that between `y` and `x`. In general, these measures are
50
+ used to investigate either the strength or significance of the association.
51
+
52
+ For researchers in various fields, an understanding of measures of association is essential for
53
+ conducting meaningful statistical analyses. By using appropriate measures of association, researchers can
54
+ gain valuable insights into the relationships between variables, draw conclusions, and make informed decisions.
55
+
56
+ While symmetric measures are commonly used to measure the relationship between numeric variables, studies
57
+ and applications involving categorical variables would typically benefit from the use of asymmetric measures.
58
+ Asymmetric measures are useful in determining the fraction of variability in `x` that can be explained
59
+ by variations in `y` (Pearson, 2016). This property is easily demonstrated through a modification
60
+ of the example provided by Zychlinski (2018), with the introduction of the `dython` library:
61
+
62
+ | | X | Y |
63
+ |---------------|---|---|
64
+ | Observation 1 | A | c |
65
+ | Observation 2 | A | d |
66
+ | Observation 3 | A | c |
67
+ | Observation 4 | B | g |
68
+ | Observation 5 | B | g |
69
+ | Observation 6 | B | f |
70
+
71
+ In the given example, it is not possible to determine the categorical value of `y` with complete certainty
72
+ based on `x`. However, it is possible to determine `x` with certainty based on `y`. The symmetric version
73
+ of Maximum-Likelihood estimation (MLE), which simply divides the number of cases by the total number
74
+ of observations (i.e., Cxy/Cx and Cxy/Cy, where C represents the overall count), produces tables that
75
+ are transposed versions of each other for XY and YX.
76
+
77
+ | X given Y | `A` | `B` |
78
+ |-----------|------|------|
79
+ | **`c`** | 0.75 | 0.75 |
80
+ | **`d`** | 0.00 | 0.00 |
81
+ | **`f`** | 0.00 | 0.00 |
82
+ | **`g`** | 0.75 | 0.75 |
83
+
84
+ | Y given X | `c` | `d` | `f` | `g` |
85
+ |-----------|------|------|------|------|
86
+ | **`A`** | 0.75 | 0.00 | 0.00 | 0.75 |
87
+ | **`B`** | 0.75 | 0.00 | 0.00 | 0.75 |
88
+
89
+ In contrast to symmetric tables, asymmetric tables can capture the difference in information between variables,
90
+ indicating that if `y` is known in a dataset, `x` can be predicted with certainty. Using the
91
+ Maximum-Likelihood estimation scorer, we can see that the asymmetric tables more accurately reflect
92
+ this relationship:
93
+
94
+ | X given Y | `A` | `B` |
95
+ |------------|------|------|
96
+ | **`c`** | 1.00 | 0.00 |
97
+ | **`d`** | 1.00 | 0.00 |
98
+ | **`f`** | 0.00 | 1.00 |
99
+ | **`g`** | 0.00 | 1.00 |
100
+
101
+ | Y given X | `c` | `d` | `f` | `g` |
102
+ |-----------|------|------|------|------|
103
+ | **`A`** | 0.67 | 0.33 | 0.00 | 0.00 |
104
+ | **`B`** | 0.00 | 0.00 | 0.33 | 0.67 |
105
+
106
+ The assessment of categorical association typically involves the use of measures such as Chi-square
107
+ and Cramer's V, which are symmetric in nature. However, there are some widely-used asymmetric
108
+ measures that include Theil's U and Goodman and Kruskal's tau. The Theil's U measure is
109
+ well-suited for fields in the humanities, including linguistic research, as it considers the conditional
110
+ entropy between `x` and `y`, which reflects the number of possible states of `y` given `x` and
111
+ their frequency of occurrence.
112
+
113
+ In the ASymCat library, various scoring methods are available to measure asymmetric associations,
114
+ including Maximum-Likelihood Estimation, Pointwise Mutual Information, Normalized Pointwise Mutual
115
+ Information, Chi-square (over both 2x2 and 3x2 contingency tables), Cramér's V (over both 2x2
116
+ and 3x2 contingency tables), Fisher Exact Odds Ratio (over unconditional MLE), Theil's U
117
+ ("uncertainty score"), Conditional Entropy, and a new scorer `tresoldi` that combines information
118
+ from MLE and PMI, tailored for studies in computational historical linguistics.
119
+
120
+
121
+ The most popular methods for measure of categorical association are the
122
+ aforementioned Chi-square and Cramer's V, defined as the square root of a
123
+ normalized chi-square value. Both are symmetric values. Among the best
124
+ known asymmetric measures are Theil's U and Goodman and Kruskal's tau.
125
+ The former is particularly useful for domains of the humanities such as
126
+ lingustic research, as it is ultimately based on the conditional entropy
127
+ between `x` and `y`, that is, how many possible states of `y` are observed
128
+ given `x` and how often they occur.
129
+
130
+ In addition to the various categorical association scorers, the ASymCat library provides several
131
+ useful features. Users can scale the scores with their desired ranges using methods such as
132
+ `minmax`, `mean`, and `stdev`. The library also includes functions for generating heatmaps of the
133
+ scorers. Moreover, it provides a range of smoothing methods for frequency counts, including Laplace
134
+ smoothing and more sophisticated approaches like the Certainty Degree scorer, which was specifically
135
+ designed for use with this library.
136
+
137
+ ## Installation and usage
138
+
139
+ The library can be installed as any standard Python library with `pip`:
140
+
141
+ ```bash
142
+ pip install asymcat
143
+ ```
144
+
145
+ Detailed instructions on how to use the library can be found in
146
+ the [official documentation]().
147
+
148
+ A show-case example with a subset of the `mushroom` dataset is shown here:
149
+
150
+ ```python
151
+ import tabulate
152
+ import asymcat
153
+ from catcoocc.scorer import CatScorer
154
+
155
+ mushroom_data = asymcat.read_sequences("resources/mushroom-small.tsv")
156
+ mushroom_cooccs = asymcat.collect_cooccs(mushroom_data)
157
+ scorer = asymcat.scorer.CatScorer(mushroom_cooccs)
158
+
159
+ mle = scorer.mle()
160
+ pmi = scorer.pmi()
161
+ npmi = scorer.pmi(True)
162
+ chi2 = scorer.chi2()
163
+ chi2_ns = scorer.chi2(False)
164
+ cramersv = scorer.cramers_v()
165
+ cramersv_ns = scorer.cramers_v(False)
166
+ fisher = scorer.fisher()
167
+ theil_u = scorer.theil_u()
168
+ cond_entropy = scorer.cond_entropy()
169
+ tresoldi = scorer.tresoldi()
170
+
171
+ headers = [
172
+ 'pair',
173
+ 'mle_xy', 'mle_yx',
174
+ 'pmi_xy', 'pmi_yx',
175
+ 'npmi_xy', 'npmi_yx',
176
+ 'chi2_xy', 'chi2_yx',
177
+ 'chi2ns_xy', 'chi2ns_yx',
178
+ 'cremersv_xy', 'cremersv_yx',
179
+ 'cremersvns_xy', 'cremersvns_yx',
180
+ 'fisher_xy', 'fisher_yx',
181
+ 'theilu_xy', 'theilu_yx',
182
+ 'cond_entropy_xy', 'cond_entropy_yx',
183
+ 'tresoldi_xy', 'tresoldi_yx'
184
+ ]
185
+
186
+ table = []
187
+ for pair in sorted(scorer.obs):
188
+ buf = [
189
+ pair,
190
+ "%0.4f" % mle[pair][0], "%0.4f" % mle[pair][1],
191
+ "%0.4f" % pmi[pair][0], "%0.4f" % pmi[pair][1],
192
+ "%0.4f" % npmi[pair][0], "%0.4f" % npmi[pair][1],
193
+ "%0.4f" % chi2[pair][0], "%0.4f" % chi2[pair][1],
194
+ "%0.4f" % chi2_ns[pair][0], "%0.4f" % chi2_ns[pair][1],
195
+ "%0.4f" % cramersv[pair][0], "%0.4f" % cramersv[pair][1],
196
+ "%0.4f" % cramersv_ns[pair][0], "%0.4f" % cramersv_ns[pair][1],
197
+ "%0.4f" % fisher[pair][0], "%0.4f" % fisher[pair][1],
198
+ "%0.4f" % theil_u[pair][0], "%0.4f" % theil_u[pair][1],
199
+ "%0.4f" % cond_entropy[pair][0], "%0.4f" % cond_entropy[pair][1],
200
+ "%0.4f" % tresoldi[pair][0], "%0.4f" % tresoldi[pair][1],
201
+ ]
202
+ table.append(buf)
203
+
204
+ print(tabulate.tabulate(table, headers=headers, tablefmt='markdown'))
205
+ ```
206
+
207
+ Which will output:
208
+
209
+ | pair | mle_xy | mle_yx | pmi_xy | pmi_yx | npmi_xy | npmi_yx | chi2_xy | chi2_yx | chi2ns_xy | chi2ns_yx | cremersv_xy | cremersv_yx | cremersvns_xy | cremersvns_yx | fisher_xy | fisher_yx | theilu_xy | theilu_yx | cond_entropy_xy | cond_entropy_yx | tresoldi_xy | tresoldi_yx |
210
+ |-------------------------|----------|----------|----------|----------|-----------|-----------|-----------|-----------|-------------|-------------|---------------|---------------|-----------------|-----------------|-------------|-------------|-------------|-------------|-------------------|-------------------|---------------|---------------|
211
+ | ('edible', 'bell') | 0.3846 | 1 | 0.4308 | 0.4308 | 0.3107 | 0.3107 | 1.8315 | 1.8315 | 3.5897 | 3.5897 | 0.2027 | 0.2027 | 0.1987 | 0.1987 | inf | inf | 0 | 1 | 1.119 | 0 | 0.5956 | 1 |
212
+ | ('edible', 'convex') | 0.4615 | 0.4615 | -0.3424 | -0.3424 | -0.2844 | -0.2844 | 3.6735 | 3.6735 | 5.7988 | 5.7988 | 0.3719 | 0.3719 | 0.3101 | 0.3101 | 0 | 0 | 0.2147 | 0.3071 | 0.7273 | 0.4486 | -0.5615 | -0.5615 |
213
+ | ('edible', 'flat') | 0.0769 | 1 | 0.4308 | 0.4308 | 0.1438 | 0.1438 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | inf | inf | 0 | 1 | 1.119 | 0 | 0.4596 | 1 |
214
+ | ('edible', 'sunken') | 0.0769 | 1 | 0.4308 | 0.4308 | 0.1438 | 0.1438 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | inf | inf | 0 | 1 | 1.119 | 0 | 0.4596 | 1 |
215
+ | ('poisonous', 'bell') | 0 | 0 | -3.5553 | -3.5553 | -0.5934 | -0.5934 | 1.8315 | 1.8315 | 3.5897 | 3.5897 | 0.2027 | 0.2027 | 0.1987 | 0.1987 | 0 | 0 | 1 | 1 | 0 | 0 | -3.5553 | -3.5553 |
216
+ | ('poisonous', 'convex') | 1 | 0.5385 | 0.4308 | 0.4308 | 0.4103 | 0.4103 | 3.6735 | 3.6735 | 5.7988 | 5.7988 | 0.3719 | 0.3719 | 0.3101 | 0.3101 | inf | inf | 1 | 0 | 0 | 0.6902 | 1 | 0.6779 |
217
+ | ('poisonous', 'flat') | 0 | 0 | -1.9459 | -1.9459 | -0.3248 | -0.3248 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | -1.9459 | -1.9459 |
218
+ | ('poisonous', 'sunken') | 0 | 0 | -1.9459 | -1.9459 | -0.3248 | -0.3248 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | -1.9459 | -1.9459 |
219
+
220
+ ## Similar Projects
221
+
222
+ https://github.com/pafoster/pyitlib
223
+
224
+ Griffith, Daniel M.; Veech, Joseph A.; and Marsh, Charles J. (2016)
225
+ *cooccur: Probabilistic Species Co-Occurrence Analysis in R*. Journal
226
+ of Statistical Software (69). doi: 10.18627/jss.v069.c02
227
+
228
+ https://cran.r-project.org/web/packages/GoodmanKruskal/vignettes/GoodmanKruskal.html
229
+
230
+ ## Community guidelines
231
+
232
+ While the author can be contacted directly for support, it is recommended
233
+ that third parties use GitHub standard features, such as issues and
234
+ pull requests, to contribute, report problems, or seek support.
235
+
236
+ ## Author and citation
237
+
238
+
239
+ The library is developed by Tiago Tresoldi (tiago.tresoldi@lingfil.uu.se). The library is developed in the context of
240
+ the [Cultural Evolution of Texts](https://github.com/evotext/) project, with funding from the
241
+ [Riksbankens Jubileumsfond](https://www.rj.se/) (grant agreement ID:
242
+ [MXM19-1087:1](https://www.rj.se/en/anslag/2019/cultural-evolution-of-texts/)).
243
+
244
+ During the first stages of development, the author received funding from the
245
+ [European Research Council](https://erc.europa.eu/) (ERC) under the European Union’s Horizon 2020
246
+ research and innovation programme (grant agreement
247
+ No. [ERC Grant #715618](https://cordis.europa.eu/project/rcn/206320/factsheet/en),
248
+ [Computer-Assisted Language Comparison](https://digling.org/calc/)).
249
+
250
+ If you use `asymcat`, please cite it as:
251
+
252
+ > Tresoldi, Tiago (2023). `asymcat`, a library for symmetric and asymmetric
253
+ analysis of categorical co-occurrences. Version 0.3. Uppsala. Available at:
254
+ > <https://github.com/tresoldi/asymcat>
255
+
256
+ In BibTeX:
257
+
258
+ ```bibtex
259
+ @misc{Tresoldi2023asymcat,
260
+ author = {Tresoldi, Tiago},
261
+ title = {asymcat, a library for symmetric and asymmetric analysis of categorical co-occurrences. Version 0.3.},
262
+ howpublished = {\url{https://github.com/tresoldi/asymcat}},
263
+ address = {Uppsala},
264
+ year = {2023},
265
+ }
266
+ ```
@@ -0,0 +1,247 @@
1
+ # ASymCat
2
+
3
+ ASymCat is a powerful Python library designed to analyze co-occurrence association between categorical
4
+ variables using various symmetric and asymmetric measures of association. Whether you are working with
5
+ linguistic data, or any other kind of categorical data, ASymCat can help you explore and describe your
6
+ data in new and meaningful ways.
7
+
8
+ Using ASymCat, you can input co-occurrence observations such as records, alignments, and matrices of
9
+ presence-absence, and generate dictionaries that provide association scores between categories. You can also
10
+ focus on the strength or direction of association, or both, depending on your research questions.
11
+
12
+ In addition to the main methods for numeric computation of measures of association, ASymCat includes auxiliary
13
+ methods that can help you deal with relational data, n-grams from sequences, alignments, and binary
14
+ matrices of presence/absence.
15
+
16
+ ASymCat is a versatile library that can be used by researchers in various fields, and it can help you gain
17
+ new insights into your data.
18
+
19
+ ## Background
20
+
21
+ Measures of association play a crucial role in statistical analysis as they help to quantify the relationship
22
+ between variables. While a variety of measures exist, correlation coefficients are widely used to determine
23
+ the strength and direction of the relationship between two variables. Pearson's **rho**, for example,
24
+ is frequently used to assess the strength of the linear relationship between continuous variables, whereas
25
+ Spearman's **rho** is employed to evaluate the strength of the monotonic relationship between ordinal or
26
+ ranked variables. The Chi-square measure, on the other hand, is a useful tool for measuring the
27
+ association between categorical variables.
28
+
29
+ It is important to note that most measures of association are symmetric, indicating that the association
30
+ between any `x` and `y` value is equal to that between `y` and `x`. In general, these measures are
31
+ used to investigate either the strength or significance of the association.
32
+
33
+ For researchers in various fields, an understanding of measures of association is essential for
34
+ conducting meaningful statistical analyses. By using appropriate measures of association, researchers can
35
+ gain valuable insights into the relationships between variables, draw conclusions, and make informed decisions.
36
+
37
+ While symmetric measures are commonly used to measure the relationship between numeric variables, studies
38
+ and applications involving categorical variables would typically benefit from the use of asymmetric measures.
39
+ Asymmetric measures are useful in determining the fraction of variability in `x` that can be explained
40
+ by variations in `y` (Pearson, 2016). This property is easily demonstrated through a modification
41
+ of the example provided by Zychlinski (2018), with the introduction of the `dython` library:
42
+
43
+ | | X | Y |
44
+ |---------------|---|---|
45
+ | Observation 1 | A | c |
46
+ | Observation 2 | A | d |
47
+ | Observation 3 | A | c |
48
+ | Observation 4 | B | g |
49
+ | Observation 5 | B | g |
50
+ | Observation 6 | B | f |
51
+
52
+ In the given example, it is not possible to determine the categorical value of `y` with complete certainty
53
+ based on `x`. However, it is possible to determine `x` with certainty based on `y`. The symmetric version
54
+ of Maximum-Likelihood estimation (MLE), which simply divides the number of cases by the total number
55
+ of observations (i.e., Cxy/Cx and Cxy/Cy, where C represents the overall count), produces tables that
56
+ are transposed versions of each other for XY and YX.
57
+
58
+ | X given Y | `A` | `B` |
59
+ |-----------|------|------|
60
+ | **`c`** | 0.75 | 0.75 |
61
+ | **`d`** | 0.00 | 0.00 |
62
+ | **`f`** | 0.00 | 0.00 |
63
+ | **`g`** | 0.75 | 0.75 |
64
+
65
+ | Y given X | `c` | `d` | `f` | `g` |
66
+ |-----------|------|------|------|------|
67
+ | **`A`** | 0.75 | 0.00 | 0.00 | 0.75 |
68
+ | **`B`** | 0.75 | 0.00 | 0.00 | 0.75 |
69
+
70
+ In contrast to symmetric tables, asymmetric tables can capture the difference in information between variables,
71
+ indicating that if `y` is known in a dataset, `x` can be predicted with certainty. Using the
72
+ Maximum-Likelihood estimation scorer, we can see that the asymmetric tables more accurately reflect
73
+ this relationship:
74
+
75
+ | X given Y | `A` | `B` |
76
+ |------------|------|------|
77
+ | **`c`** | 1.00 | 0.00 |
78
+ | **`d`** | 1.00 | 0.00 |
79
+ | **`f`** | 0.00 | 1.00 |
80
+ | **`g`** | 0.00 | 1.00 |
81
+
82
+ | Y given X | `c` | `d` | `f` | `g` |
83
+ |-----------|------|------|------|------|
84
+ | **`A`** | 0.67 | 0.33 | 0.00 | 0.00 |
85
+ | **`B`** | 0.00 | 0.00 | 0.33 | 0.67 |
86
+
87
+ The assessment of categorical association typically involves the use of measures such as Chi-square
88
+ and Cramer's V, which are symmetric in nature. However, there are some widely-used asymmetric
89
+ measures that include Theil's U and Goodman and Kruskal's tau. The Theil's U measure is
90
+ well-suited for fields in the humanities, including linguistic research, as it considers the conditional
91
+ entropy between `x` and `y`, which reflects the number of possible states of `y` given `x` and
92
+ their frequency of occurrence.
93
+
94
+ In the ASymCat library, various scoring methods are available to measure asymmetric associations,
95
+ including Maximum-Likelihood Estimation, Pointwise Mutual Information, Normalized Pointwise Mutual
96
+ Information, Chi-square (over both 2x2 and 3x2 contingency tables), Cramér's V (over both 2x2
97
+ and 3x2 contingency tables), Fisher Exact Odds Ratio (over unconditional MLE), Theil's U
98
+ ("uncertainty score"), Conditional Entropy, and a new scorer `tresoldi` that combines information
99
+ from MLE and PMI, tailored for studies in computational historical linguistics.
100
+
101
+
102
+ The most popular methods for measure of categorical association are the
103
+ aforementioned Chi-square and Cramer's V, defined as the square root of a
104
+ normalized chi-square value. Both are symmetric values. Among the best
105
+ known asymmetric measures are Theil's U and Goodman and Kruskal's tau.
106
+ The former is particularly useful for domains of the humanities such as
107
+ lingustic research, as it is ultimately based on the conditional entropy
108
+ between `x` and `y`, that is, how many possible states of `y` are observed
109
+ given `x` and how often they occur.
110
+
111
+ In addition to the various categorical association scorers, the ASymCat library provides several
112
+ useful features. Users can scale the scores with their desired ranges using methods such as
113
+ `minmax`, `mean`, and `stdev`. The library also includes functions for generating heatmaps of the
114
+ scorers. Moreover, it provides a range of smoothing methods for frequency counts, including Laplace
115
+ smoothing and more sophisticated approaches like the Certainty Degree scorer, which was specifically
116
+ designed for use with this library.
117
+
118
+ ## Installation and usage
119
+
120
+ The library can be installed as any standard Python library with `pip`:
121
+
122
+ ```bash
123
+ pip install asymcat
124
+ ```
125
+
126
+ Detailed instructions on how to use the library can be found in
127
+ the [official documentation]().
128
+
129
+ A show-case example with a subset of the `mushroom` dataset is shown here:
130
+
131
+ ```python
132
+ import tabulate
133
+ import asymcat
134
+ from catcoocc.scorer import CatScorer
135
+
136
+ mushroom_data = asymcat.read_sequences("resources/mushroom-small.tsv")
137
+ mushroom_cooccs = asymcat.collect_cooccs(mushroom_data)
138
+ scorer = asymcat.scorer.CatScorer(mushroom_cooccs)
139
+
140
+ mle = scorer.mle()
141
+ pmi = scorer.pmi()
142
+ npmi = scorer.pmi(True)
143
+ chi2 = scorer.chi2()
144
+ chi2_ns = scorer.chi2(False)
145
+ cramersv = scorer.cramers_v()
146
+ cramersv_ns = scorer.cramers_v(False)
147
+ fisher = scorer.fisher()
148
+ theil_u = scorer.theil_u()
149
+ cond_entropy = scorer.cond_entropy()
150
+ tresoldi = scorer.tresoldi()
151
+
152
+ headers = [
153
+ 'pair',
154
+ 'mle_xy', 'mle_yx',
155
+ 'pmi_xy', 'pmi_yx',
156
+ 'npmi_xy', 'npmi_yx',
157
+ 'chi2_xy', 'chi2_yx',
158
+ 'chi2ns_xy', 'chi2ns_yx',
159
+ 'cremersv_xy', 'cremersv_yx',
160
+ 'cremersvns_xy', 'cremersvns_yx',
161
+ 'fisher_xy', 'fisher_yx',
162
+ 'theilu_xy', 'theilu_yx',
163
+ 'cond_entropy_xy', 'cond_entropy_yx',
164
+ 'tresoldi_xy', 'tresoldi_yx'
165
+ ]
166
+
167
+ table = []
168
+ for pair in sorted(scorer.obs):
169
+ buf = [
170
+ pair,
171
+ "%0.4f" % mle[pair][0], "%0.4f" % mle[pair][1],
172
+ "%0.4f" % pmi[pair][0], "%0.4f" % pmi[pair][1],
173
+ "%0.4f" % npmi[pair][0], "%0.4f" % npmi[pair][1],
174
+ "%0.4f" % chi2[pair][0], "%0.4f" % chi2[pair][1],
175
+ "%0.4f" % chi2_ns[pair][0], "%0.4f" % chi2_ns[pair][1],
176
+ "%0.4f" % cramersv[pair][0], "%0.4f" % cramersv[pair][1],
177
+ "%0.4f" % cramersv_ns[pair][0], "%0.4f" % cramersv_ns[pair][1],
178
+ "%0.4f" % fisher[pair][0], "%0.4f" % fisher[pair][1],
179
+ "%0.4f" % theil_u[pair][0], "%0.4f" % theil_u[pair][1],
180
+ "%0.4f" % cond_entropy[pair][0], "%0.4f" % cond_entropy[pair][1],
181
+ "%0.4f" % tresoldi[pair][0], "%0.4f" % tresoldi[pair][1],
182
+ ]
183
+ table.append(buf)
184
+
185
+ print(tabulate.tabulate(table, headers=headers, tablefmt='markdown'))
186
+ ```
187
+
188
+ Which will output:
189
+
190
+ | pair | mle_xy | mle_yx | pmi_xy | pmi_yx | npmi_xy | npmi_yx | chi2_xy | chi2_yx | chi2ns_xy | chi2ns_yx | cremersv_xy | cremersv_yx | cremersvns_xy | cremersvns_yx | fisher_xy | fisher_yx | theilu_xy | theilu_yx | cond_entropy_xy | cond_entropy_yx | tresoldi_xy | tresoldi_yx |
191
+ |-------------------------|----------|----------|----------|----------|-----------|-----------|-----------|-----------|-------------|-------------|---------------|---------------|-----------------|-----------------|-------------|-------------|-------------|-------------|-------------------|-------------------|---------------|---------------|
192
+ | ('edible', 'bell') | 0.3846 | 1 | 0.4308 | 0.4308 | 0.3107 | 0.3107 | 1.8315 | 1.8315 | 3.5897 | 3.5897 | 0.2027 | 0.2027 | 0.1987 | 0.1987 | inf | inf | 0 | 1 | 1.119 | 0 | 0.5956 | 1 |
193
+ | ('edible', 'convex') | 0.4615 | 0.4615 | -0.3424 | -0.3424 | -0.2844 | -0.2844 | 3.6735 | 3.6735 | 5.7988 | 5.7988 | 0.3719 | 0.3719 | 0.3101 | 0.3101 | 0 | 0 | 0.2147 | 0.3071 | 0.7273 | 0.4486 | -0.5615 | -0.5615 |
194
+ | ('edible', 'flat') | 0.0769 | 1 | 0.4308 | 0.4308 | 0.1438 | 0.1438 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | inf | inf | 0 | 1 | 1.119 | 0 | 0.4596 | 1 |
195
+ | ('edible', 'sunken') | 0.0769 | 1 | 0.4308 | 0.4308 | 0.1438 | 0.1438 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | inf | inf | 0 | 1 | 1.119 | 0 | 0.4596 | 1 |
196
+ | ('poisonous', 'bell') | 0 | 0 | -3.5553 | -3.5553 | -0.5934 | -0.5934 | 1.8315 | 1.8315 | 3.5897 | 3.5897 | 0.2027 | 0.2027 | 0.1987 | 0.1987 | 0 | 0 | 1 | 1 | 0 | 0 | -3.5553 | -3.5553 |
197
+ | ('poisonous', 'convex') | 1 | 0.5385 | 0.4308 | 0.4308 | 0.4103 | 0.4103 | 3.6735 | 3.6735 | 5.7988 | 5.7988 | 0.3719 | 0.3719 | 0.3101 | 0.3101 | inf | inf | 1 | 0 | 0 | 0.6902 | 1 | 0.6779 |
198
+ | ('poisonous', 'flat') | 0 | 0 | -1.9459 | -1.9459 | -0.3248 | -0.3248 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | -1.9459 | -1.9459 |
199
+ | ('poisonous', 'sunken') | 0 | 0 | -1.9459 | -1.9459 | -0.3248 | -0.3248 | 0.1041 | 0.1041 | 0.5668 | 0.5668 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | -1.9459 | -1.9459 |
200
+
201
+ ## Similar Projects
202
+
203
+ https://github.com/pafoster/pyitlib
204
+
205
+ Griffith, Daniel M.; Veech, Joseph A.; and Marsh, Charles J. (2016)
206
+ *cooccur: Probabilistic Species Co-Occurrence Analysis in R*. Journal
207
+ of Statistical Software (69). doi: 10.18627/jss.v069.c02
208
+
209
+ https://cran.r-project.org/web/packages/GoodmanKruskal/vignettes/GoodmanKruskal.html
210
+
211
+ ## Community guidelines
212
+
213
+ While the author can be contacted directly for support, it is recommended
214
+ that third parties use GitHub standard features, such as issues and
215
+ pull requests, to contribute, report problems, or seek support.
216
+
217
+ ## Author and citation
218
+
219
+
220
+ The library is developed by Tiago Tresoldi (tiago.tresoldi@lingfil.uu.se). The library is developed in the context of
221
+ the [Cultural Evolution of Texts](https://github.com/evotext/) project, with funding from the
222
+ [Riksbankens Jubileumsfond](https://www.rj.se/) (grant agreement ID:
223
+ [MXM19-1087:1](https://www.rj.se/en/anslag/2019/cultural-evolution-of-texts/)).
224
+
225
+ During the first stages of development, the author received funding from the
226
+ [European Research Council](https://erc.europa.eu/) (ERC) under the European Union’s Horizon 2020
227
+ research and innovation programme (grant agreement
228
+ No. [ERC Grant #715618](https://cordis.europa.eu/project/rcn/206320/factsheet/en),
229
+ [Computer-Assisted Language Comparison](https://digling.org/calc/)).
230
+
231
+ If you use `asymcat`, please cite it as:
232
+
233
+ > Tresoldi, Tiago (2023). `asymcat`, a library for symmetric and asymmetric
234
+ analysis of categorical co-occurrences. Version 0.3. Uppsala. Available at:
235
+ > <https://github.com/tresoldi/asymcat>
236
+
237
+ In BibTeX:
238
+
239
+ ```bibtex
240
+ @misc{Tresoldi2023asymcat,
241
+ author = {Tresoldi, Tiago},
242
+ title = {asymcat, a library for symmetric and asymmetric analysis of categorical co-occurrences. Version 0.3.},
243
+ howpublished = {\url{https://github.com/tresoldi/asymcat}},
244
+ address = {Uppsala},
245
+ year = {2023},
246
+ }
247
+ ```
@@ -0,0 +1,37 @@
1
+ # __init__.py for the ASymCat library
2
+
3
+ """
4
+ ASymCat: a library for symmetric and asymmetric analysis of categorical co-occurrences
5
+
6
+ ASymCat is a Python library for obtaining symmetric and asymmetric measures of association between categorical
7
+ variables in data exploration and description.
8
+ """
9
+
10
+ # Information on the package
11
+ __author__ = "Tiago Tresoldi"
12
+ __email__ = "tiago.tresoldi@lingfil.uu.se"
13
+ __version__ = "0.3" # sync with setup.py
14
+
15
+ # Build the namespace
16
+ from asymcat import correlation, scorer
17
+ from asymcat.common import (
18
+ build_ct,
19
+ collect_alphabets,
20
+ collect_cooccs,
21
+ collect_ngrams,
22
+ collect_observations,
23
+ read_pa_matrix,
24
+ read_sequences,
25
+ )
26
+
27
+ __all__ = [
28
+ "correlation",
29
+ "scorer",
30
+ "collect_alphabets",
31
+ "collect_ngrams",
32
+ "collect_cooccs",
33
+ "collect_observations",
34
+ "build_ct",
35
+ "read_sequences",
36
+ "read_pa_matrix",
37
+ ]
@@ -0,0 +1,6 @@
1
+ def main():
2
+ raise NotImplementedError("This is a placeholder for the main function.")
3
+
4
+
5
+ if __name__ == "__main__":
6
+ main()