segindex 0.1.3__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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Acheul
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,69 @@
1
+ Metadata-Version: 2.1
2
+ Name: segindex
3
+ Version: 0.1.3
4
+ Summary: Segregation Index
5
+ Author-email: Jang Hyomin <seoul605.21@gmail.com>
6
+ Project-URL: Homepage, https://github.com/acheul/reardon-segregation-index
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Environment :: Console
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3.8
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Topic :: Sociology
17
+ Requires-Python: >=3.8
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE.txt
20
+ Requires-Dist: statsmodels>=0.14.1
21
+ Requires-Dist: numpy>=1.18.5
22
+ Requires-Dist: pandas>=2.1.3
23
+ Requires-Dist: scipy>=1.7.0
24
+
25
+ # Reardon's Segregation Index for Continuous Variables
26
+
27
+ * [Repository](""https://github.com/acheul/reardon-segregation-index")
28
+
29
+ ## Use
30
+ ```python
31
+ ! pip install segindex==0.1.3
32
+ from segindex import estimate_Hp
33
+
34
+ # Say, each variables are income.
35
+ # Inner lists of each area stand for sectors.
36
+ # Each area has three sectors in this case.
37
+ # How much is each area segregated by sectors in terms of income?
38
+ area1 = [[80, 80, 70, 70], [50, 45, 40],[20, 20, 20, 10]]
39
+ area2 = [[80, 70, 50], [80, 70, 45, 20, 20], [40, 20, 10]]
40
+
41
+ print(estimate_Hp(area1))
42
+ print(estimate_Hp(area2))
43
+
44
+ >> 0.7182
45
+ >> 0.3191
46
+ # area1 is more income-way segregated than area2. In other words, area2 is more mixed.
47
+ ```
48
+
49
+ ## Description
50
+
51
+ * Many kinds of segregation index are used for various purposes like from policies to studies. While there are a wide range of categorical variables like race group to meausre an amount of segregation, continuous values like income are also important but do not fit very well with categorical segregation index.
52
+
53
+ * [Reardon(2011)](https://cepa.stanford.edu/sites/default/files/reardon%20&%20bischoff%20income%20inequality%20segregation%20AJS%20final.pdf), [Reardon and Bischoff(2011)](https://cepa.stanford.edu/sites/default/files/reardon%20&%20bischoff%20income%20inequality%20segregation%20AJS%20final.pdf) propsed a rank-order segregation index based on Theil index which is based on the concept of Entropy. This index is widely accepted for practical and academic uses to calculate continuous value based segregation index like in [Chetty et al. 2014](https://www.nber.org/system/files/working_papers/w19843/w19843.pdf).
54
+
55
+ * The proposed method of them is a bit intricate however and there seems to be no good online library or code that implements it. Therefore, here is one. Python codes inside [```src/segdex/segregation_index.py```](./src/segdex/segregation_index.py) implements Rank-Order Information Theory Index of Reardon(2011).
56
+
57
+ * Essentials of the Index
58
+
59
+ * The inequality index *H* is an average of each value from a total of K sectors, which is total region's entropy(*E*) minus each sector's entropy(*E_K*). It is weighted by each sector's relative popultaion size(*t_k/T*). Here the entropy stands for how equally variables(ex. income) are distributed over sectors.
60
+
61
+ <img src="./assets/eq3-5.png">
62
+
63
+ * Below is an equation to calculate entropy when there is two groups. *p* is a ratio of a group. As the variable here is continuous not categorical, one needs to integrate the below equation over *p* with a range of 0≤p≤1. Thus transformation of raw values into rank ordered values is required.
64
+
65
+ <img src="./assets/eq3-6.png">
66
+
67
+ * Combining above equations, we can calculate below one to get a Rank-Order Information Theory Index, which is the segregation index for continuous variables. 0 means perfect equality. 1 means perfect segregation.
68
+
69
+ <img src="./assets/eq3-7.png">
@@ -0,0 +1,45 @@
1
+ # Reardon's Segregation Index for Continuous Variables
2
+
3
+ * [Repository](""https://github.com/acheul/reardon-segregation-index")
4
+
5
+ ## Use
6
+ ```python
7
+ ! pip install segindex==0.1.3
8
+ from segindex import estimate_Hp
9
+
10
+ # Say, each variables are income.
11
+ # Inner lists of each area stand for sectors.
12
+ # Each area has three sectors in this case.
13
+ # How much is each area segregated by sectors in terms of income?
14
+ area1 = [[80, 80, 70, 70], [50, 45, 40],[20, 20, 20, 10]]
15
+ area2 = [[80, 70, 50], [80, 70, 45, 20, 20], [40, 20, 10]]
16
+
17
+ print(estimate_Hp(area1))
18
+ print(estimate_Hp(area2))
19
+
20
+ >> 0.7182
21
+ >> 0.3191
22
+ # area1 is more income-way segregated than area2. In other words, area2 is more mixed.
23
+ ```
24
+
25
+ ## Description
26
+
27
+ * Many kinds of segregation index are used for various purposes like from policies to studies. While there are a wide range of categorical variables like race group to meausre an amount of segregation, continuous values like income are also important but do not fit very well with categorical segregation index.
28
+
29
+ * [Reardon(2011)](https://cepa.stanford.edu/sites/default/files/reardon%20&%20bischoff%20income%20inequality%20segregation%20AJS%20final.pdf), [Reardon and Bischoff(2011)](https://cepa.stanford.edu/sites/default/files/reardon%20&%20bischoff%20income%20inequality%20segregation%20AJS%20final.pdf) propsed a rank-order segregation index based on Theil index which is based on the concept of Entropy. This index is widely accepted for practical and academic uses to calculate continuous value based segregation index like in [Chetty et al. 2014](https://www.nber.org/system/files/working_papers/w19843/w19843.pdf).
30
+
31
+ * The proposed method of them is a bit intricate however and there seems to be no good online library or code that implements it. Therefore, here is one. Python codes inside [```src/segdex/segregation_index.py```](./src/segdex/segregation_index.py) implements Rank-Order Information Theory Index of Reardon(2011).
32
+
33
+ * Essentials of the Index
34
+
35
+ * The inequality index *H* is an average of each value from a total of K sectors, which is total region's entropy(*E*) minus each sector's entropy(*E_K*). It is weighted by each sector's relative popultaion size(*t_k/T*). Here the entropy stands for how equally variables(ex. income) are distributed over sectors.
36
+
37
+ <img src="./assets/eq3-5.png">
38
+
39
+ * Below is an equation to calculate entropy when there is two groups. *p* is a ratio of a group. As the variable here is continuous not categorical, one needs to integrate the below equation over *p* with a range of 0≤p≤1. Thus transformation of raw values into rank ordered values is required.
40
+
41
+ <img src="./assets/eq3-6.png">
42
+
43
+ * Combining above equations, we can calculate below one to get a Rank-Order Information Theory Index, which is the segregation index for continuous variables. 0 means perfect equality. 1 means perfect segregation.
44
+
45
+ <img src="./assets/eq3-7.png">
@@ -0,0 +1,36 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "segindex"
7
+ version = "0.1.3"
8
+ authors = [
9
+ { name="Jang Hyomin", email="seoul605.21@gmail.com" },
10
+ ]
11
+ description = "Segregation Index"
12
+ readme = "README.md"
13
+ requires-python = ">=3.8"
14
+
15
+ dependencies = [
16
+ 'statsmodels>=0.14.1',
17
+ 'numpy >= 1.18.5',
18
+ 'pandas>=2.1.3',
19
+ 'scipy >= 1.7.0',
20
+ ]
21
+
22
+ classifiers = [
23
+ 'Development Status :: 4 - Beta',
24
+ 'Environment :: Console',
25
+ 'Intended Audience :: Science/Research',
26
+ 'Operating System :: OS Independent',
27
+ 'Programming Language :: Python :: 3.8',
28
+ 'Programming Language :: Python :: 3.9',
29
+ 'Programming Language :: Python :: 3.10',
30
+ 'Programming Language :: Python :: 3.11',
31
+ 'Programming Language :: Python :: 3 :: Only',
32
+ 'Topic :: Sociology'
33
+ ]
34
+
35
+ [project.urls]
36
+ Homepage = "https://github.com/acheul/reardon-segregation-index"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ from .segregation_index import estimate_Hp
@@ -0,0 +1,114 @@
1
+ # Implementing Segregation Index based on Theil Index
2
+ # References: Reardon(2011), Reardon&Bischoff(2011)(Appendix A)
3
+
4
+ import statsmodels.api as sm
5
+ import statsmodels.formula.api as smf
6
+ from scipy.stats import rankdata
7
+ import math
8
+ import numpy as np
9
+ import pandas as pd
10
+ from typing import List, Any
11
+
12
+
13
+ def delta_m(m):
14
+ S = 0
15
+ for n in range(m+1):
16
+ s = ((-1)**(m-n))*math.comb(m,n)
17
+ s = s/(m-n+2)**2
18
+ S += s
19
+ d = (2/(m+2)**2) + 2*S
20
+ return d
21
+
22
+ def entropy_p(p):
23
+ if p==0: p+=1e-6
24
+ elif p==1: p-=1e-6
25
+ e = -(p*np.log2(p) + (1-p)*np.log2(1-p))
26
+ return e
27
+
28
+ def Theil_p(tt,pp):
29
+ tt,pp = map(np.array, [tt,pp])
30
+ T = np.sum(tt)
31
+ P = np.sum(tt*pp)/T
32
+ e_P = entropy_p(P)
33
+ e_pp = np.array([entropy_p(p) for p in pp])
34
+
35
+ H = 1 - np.sum(tt*e_pp)/(T*e_P)
36
+ return H
37
+
38
+ def get_weighted_rank(ss,ww):
39
+ ss,ww = map(np.array, [ss,ww])
40
+ ss_ = ss*ww
41
+ rr = rankdata(ss_)
42
+ # ~1 normalization (X)
43
+ # rr = rr/len(rr)
44
+ return rr
45
+
46
+ def from_vvs_to_rrs(vv_s, ww_s=None):
47
+ if ww_s==None:
48
+ tt = [len(e) for e in vv_s]
49
+ vvs = []
50
+ for vv in vv_s:
51
+ vvs.extend(vv)
52
+ rrs = rankdata(vvs)
53
+ ttc = np.cumsum(tt)
54
+ rr_s = [rrs[:ttc[0]]]
55
+ for t1,t2 in zip(ttc[:-1],ttc[1:]):
56
+ rr_s.append(rrs[t1:t2])
57
+ rr_s = np.array(rr_s, dtype=object)
58
+ rr_s = rr_s/len(rrs)
59
+ return rr_s
60
+ else:
61
+ tt = [len(e) for e in vv_s]
62
+ vvs, wws = [],[]
63
+ for vv,ww in zip(vv_s,ww_s):
64
+ vvs.extend(vv)
65
+ wws.extend(ww)
66
+ rrs = get_weighted_rank(vvs,wws)
67
+ ttc = np.cumsum(tt)
68
+ rr_s = [rrs[:ttc[0]]]
69
+ for t1,t2 in zip(ttc[:-1],ttc[1:]):
70
+ rr_s.append(rrs[t1:t2])
71
+ rr_s = np.array(rr_s, dtype=object)
72
+ rr_s = rr_s/len(rrs)
73
+ return rr_s
74
+
75
+ def estimate_Hp(vv_s: List[List[Any]], ww_s=None, K=14, m=4):
76
+ '''
77
+ Arguments:
78
+ vv_s (List[List[values]]): list of lists of values
79
+ ww_s: weights over vv_s
80
+ K: counts of thresholds
81
+ m: degree of regression equation used for the estimation
82
+ '''
83
+ vv_s = from_vvs_to_rrs(vv_s, ww_s=ww_s) # rank-order transformation
84
+ kk = np.array([e/K for e in range(1,K)]) # 1/K ~ (K-1)/K
85
+ e_kk = np.array([entropy_p(k) for k in kk])
86
+ w_kk = e_kk**2
87
+ #
88
+ tt = [len(vv) for vv in vv_s]
89
+ Hp_kk = []
90
+ for k in kk:
91
+ pp = []
92
+ for vv in vv_s:
93
+ vv_k = [e for e in vv if e<=k]
94
+ pp.append(len(vv_k)/len(vv))
95
+ Hp_k = Theil_p(tt,pp)
96
+ Hp_kk.append(Hp_k)
97
+ #
98
+ d = pd.DataFrame({'Hp':Hp_kk, 'p':kk, 'weight': w_kk})
99
+ for n in range(2,m+1):
100
+ d[f"p_{n}"] = d['p']**n
101
+ exog = d[[c for c in d.columns if 'p' in c and 'Hp' not in c]]
102
+ exog = sm.add_constant(exog)
103
+ global f
104
+ f = sm.WLS(endog=d['Hp'], exog=exog, weights=d['weight']).fit()
105
+ #
106
+ betas = [f.params['const'], f.params['p']]
107
+ for n in range(2,m+1):
108
+ betas.append(f.params[f"p_{n}"])
109
+ #
110
+ deltas = [delta_m(e) for e in range(m+1)]
111
+ betas, deltas = map(np.array, [betas, deltas])
112
+ #
113
+ H_r = np.sum(betas*deltas)
114
+ return H_r
@@ -0,0 +1,69 @@
1
+ Metadata-Version: 2.1
2
+ Name: segindex
3
+ Version: 0.1.3
4
+ Summary: Segregation Index
5
+ Author-email: Jang Hyomin <seoul605.21@gmail.com>
6
+ Project-URL: Homepage, https://github.com/acheul/reardon-segregation-index
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Environment :: Console
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3.8
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Topic :: Sociology
17
+ Requires-Python: >=3.8
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE.txt
20
+ Requires-Dist: statsmodels>=0.14.1
21
+ Requires-Dist: numpy>=1.18.5
22
+ Requires-Dist: pandas>=2.1.3
23
+ Requires-Dist: scipy>=1.7.0
24
+
25
+ # Reardon's Segregation Index for Continuous Variables
26
+
27
+ * [Repository](""https://github.com/acheul/reardon-segregation-index")
28
+
29
+ ## Use
30
+ ```python
31
+ ! pip install segindex==0.1.3
32
+ from segindex import estimate_Hp
33
+
34
+ # Say, each variables are income.
35
+ # Inner lists of each area stand for sectors.
36
+ # Each area has three sectors in this case.
37
+ # How much is each area segregated by sectors in terms of income?
38
+ area1 = [[80, 80, 70, 70], [50, 45, 40],[20, 20, 20, 10]]
39
+ area2 = [[80, 70, 50], [80, 70, 45, 20, 20], [40, 20, 10]]
40
+
41
+ print(estimate_Hp(area1))
42
+ print(estimate_Hp(area2))
43
+
44
+ >> 0.7182
45
+ >> 0.3191
46
+ # area1 is more income-way segregated than area2. In other words, area2 is more mixed.
47
+ ```
48
+
49
+ ## Description
50
+
51
+ * Many kinds of segregation index are used for various purposes like from policies to studies. While there are a wide range of categorical variables like race group to meausre an amount of segregation, continuous values like income are also important but do not fit very well with categorical segregation index.
52
+
53
+ * [Reardon(2011)](https://cepa.stanford.edu/sites/default/files/reardon%20&%20bischoff%20income%20inequality%20segregation%20AJS%20final.pdf), [Reardon and Bischoff(2011)](https://cepa.stanford.edu/sites/default/files/reardon%20&%20bischoff%20income%20inequality%20segregation%20AJS%20final.pdf) propsed a rank-order segregation index based on Theil index which is based on the concept of Entropy. This index is widely accepted for practical and academic uses to calculate continuous value based segregation index like in [Chetty et al. 2014](https://www.nber.org/system/files/working_papers/w19843/w19843.pdf).
54
+
55
+ * The proposed method of them is a bit intricate however and there seems to be no good online library or code that implements it. Therefore, here is one. Python codes inside [```src/segdex/segregation_index.py```](./src/segdex/segregation_index.py) implements Rank-Order Information Theory Index of Reardon(2011).
56
+
57
+ * Essentials of the Index
58
+
59
+ * The inequality index *H* is an average of each value from a total of K sectors, which is total region's entropy(*E*) minus each sector's entropy(*E_K*). It is weighted by each sector's relative popultaion size(*t_k/T*). Here the entropy stands for how equally variables(ex. income) are distributed over sectors.
60
+
61
+ <img src="./assets/eq3-5.png">
62
+
63
+ * Below is an equation to calculate entropy when there is two groups. *p* is a ratio of a group. As the variable here is continuous not categorical, one needs to integrate the below equation over *p* with a range of 0≤p≤1. Thus transformation of raw values into rank ordered values is required.
64
+
65
+ <img src="./assets/eq3-6.png">
66
+
67
+ * Combining above equations, we can calculate below one to get a Rank-Order Information Theory Index, which is the segregation index for continuous variables. 0 means perfect equality. 1 means perfect segregation.
68
+
69
+ <img src="./assets/eq3-7.png">
@@ -0,0 +1,11 @@
1
+ LICENSE.txt
2
+ README.md
3
+ pyproject.toml
4
+ src/segindex/__init__.py
5
+ src/segindex/segregation_index.py
6
+ src/segindex.egg-info/PKG-INFO
7
+ src/segindex.egg-info/SOURCES.txt
8
+ src/segindex.egg-info/dependency_links.txt
9
+ src/segindex.egg-info/requires.txt
10
+ src/segindex.egg-info/top_level.txt
11
+ src/tests/test.py
@@ -0,0 +1,4 @@
1
+ statsmodels>=0.14.1
2
+ numpy>=1.18.5
3
+ pandas>=2.1.3
4
+ scipy>=1.7.0
@@ -0,0 +1,2 @@
1
+ segindex
2
+ tests
@@ -0,0 +1,12 @@
1
+ import sys, os
2
+ sys.path.append(os.path.dirname("../src"))
3
+
4
+ from segindex import estimate_Hp
5
+
6
+ if __name__ == "__main__":
7
+
8
+ areas1 = [[80, 80, 70, 70], [50, 45, 40], [20, 20, 20, 10]]
9
+ areas2 = [[80, 70, 50], [80, 70, 45, 20, 20], [40, 20, 10]]
10
+
11
+ print(estimate_Hp(areas1))
12
+ print(estimate_Hp(areas2))