1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2019 by Christopher C. Little. |
4
|
|
|
# This file is part of Abydos. |
5
|
|
|
# |
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
1 |
|
"""abydos.distance._horn_morisita. |
20
|
|
|
|
21
|
|
|
Horn-Morisita index of overlap |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from ._token_distance import _TokenDistance |
32
|
|
|
|
33
|
1 |
|
__all__ = ['HornMorisita'] |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
class HornMorisita(_TokenDistance): |
37
|
|
|
r"""Horn-Morisita index of overlap. |
38
|
|
|
|
39
|
|
|
Horn-Morisita index of overlap :cite:`Horn:1966`, given two populations X |
40
|
|
|
and Y drawn from S species, is: |
41
|
|
|
|
42
|
|
|
.. math:: |
43
|
|
|
|
44
|
|
|
sim_{Horn-Morisita}(X, Y) = |
45
|
|
|
C_{\lambda} = \frac{2\sum_{i=1}^S x_i y_i} |
46
|
|
|
{(\hat{\lambda}_x + \hat{\lambda}_y)XY} |
47
|
|
|
|
48
|
|
|
where |
49
|
|
|
|
50
|
|
|
.. math:: |
51
|
|
|
|
52
|
|
|
X = \sum_{i=1}^S x_i ~~;~~ Y = \sum_{i=1}^S y_i |
53
|
|
|
|
54
|
|
|
.. math:: |
55
|
|
|
|
56
|
|
|
\hat{\lambda}_x = \frac{\sum_{i=1}^S x_i^2}{X^2} ~~;~~ |
57
|
|
|
\hat{\lambda}_y = \frac{\sum_{i=1}^S y_i^2}{Y^2} |
58
|
|
|
|
59
|
|
|
Observe that this is identical to Morisita similarity, except for the |
60
|
|
|
definition of the :math:`\lambda` values in the denominator. |
61
|
|
|
|
62
|
|
|
.. versionadded:: 0.4.1 |
63
|
|
|
""" |
64
|
|
|
|
65
|
1 |
|
def __init__(self, **kwargs): |
66
|
|
|
"""Initialize HornMorisita instance. |
67
|
|
|
|
68
|
|
|
Parameters |
69
|
|
|
---------- |
70
|
|
|
**kwargs |
71
|
|
|
Arbitrary keyword arguments |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
.. versionadded:: 0.4.1 |
75
|
|
|
|
76
|
|
|
""" |
77
|
1 |
|
super(HornMorisita, self).__init__(**kwargs) |
78
|
|
|
|
79
|
1 |
|
def sim(self, src, tar): |
80
|
|
|
"""Return the Horn-Morisita similarity of two strings. |
81
|
|
|
|
82
|
|
|
Parameters |
83
|
|
|
---------- |
84
|
|
|
src : str |
85
|
|
|
Source string for comparison |
86
|
|
|
tar : str |
87
|
|
|
Target string for comparison |
88
|
|
|
|
89
|
|
|
Returns |
90
|
|
|
------- |
91
|
|
|
float |
92
|
|
|
Horn-Morisita similarity |
93
|
|
|
|
94
|
|
|
Examples |
95
|
|
|
-------- |
96
|
|
|
>>> cmp = HornMorisita() |
97
|
|
|
>>> cmp.sim('cat', 'hat') |
98
|
|
|
0.5 |
99
|
|
|
>>> cmp.sim('Niall', 'Neil') |
100
|
|
|
0.3636363636363636 |
101
|
|
|
>>> cmp.sim('aluminum', 'Catalan') |
102
|
|
|
0.10650887573964497 |
103
|
|
|
>>> cmp.sim('ATCG', 'TAGC') |
104
|
|
|
0.0 |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
.. versionadded:: 0.4.1 |
108
|
|
|
|
109
|
|
|
""" |
110
|
1 |
|
self._tokenize(src, tar) |
111
|
|
|
|
112
|
1 |
|
intersection = self._intersection() |
113
|
|
|
|
114
|
1 |
|
src_card = self._src_card() |
115
|
1 |
|
tar_card = self._tar_card() |
116
|
|
|
|
117
|
1 |
|
src_lambda = 0 |
118
|
1 |
|
tar_lambda = 0 |
119
|
1 |
|
for val in self._src_tokens.values(): |
120
|
1 |
|
src_lambda += val * val |
121
|
1 |
|
if src_lambda: |
122
|
1 |
|
src_lambda /= src_card * src_card |
123
|
1 |
|
for val in self._tar_tokens.values(): |
124
|
1 |
|
tar_lambda += val * val |
125
|
1 |
|
if tar_lambda: |
126
|
1 |
|
tar_lambda /= tar_card * tar_card |
127
|
|
|
|
128
|
1 |
|
sim = 0 |
129
|
1 |
|
for symbol in intersection.keys(): |
130
|
1 |
|
sim += self._src_tokens[symbol] * self._tar_tokens[symbol] |
131
|
1 |
|
sim *= 2 |
132
|
1 |
|
if src_card: |
133
|
1 |
|
sim /= src_card |
134
|
1 |
|
if tar_card: |
135
|
1 |
|
sim /= tar_card |
136
|
1 |
|
if src_lambda + tar_lambda: |
137
|
1 |
|
sim /= src_lambda + tar_lambda |
138
|
|
|
|
139
|
1 |
|
return sim |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
if __name__ == '__main__': |
143
|
|
|
import doctest |
144
|
|
|
|
145
|
|
|
doctest.testmod() |
146
|
|
|
|