@@ -0,0 +1,133 @@
1+{
2+"cells": [
3+ {
4+"cell_type": "code",
5+"execution_count": 1,
6+"id": "258b4bc9-2964-470a-8010-05c2162f5e05",
7+"metadata": {},
8+"outputs": [
9+ {
10+"name": "stdout",
11+"output_type": "stream",
12+"text": [
13+"Requirement already satisfied: wbgapi in /Users/longye/anaconda3/lib/python3.10/site-packages (1.0.12)\n",
14+"Requirement already satisfied: plotly in /Users/longye/anaconda3/lib/python3.10/site-packages (5.22.0)\n",
15+"Requirement already satisfied: requests in /Users/longye/anaconda3/lib/python3.10/site-packages (from wbgapi) (2.31.0)\n",
16+"Requirement already satisfied: tabulate in /Users/longye/anaconda3/lib/python3.10/site-packages (from wbgapi) (0.9.0)\n",
17+"Requirement already satisfied: PyYAML in /Users/longye/anaconda3/lib/python3.10/site-packages (from wbgapi) (6.0)\n",
18+"Requirement already satisfied: tenacity>=6.2.0 in /Users/longye/anaconda3/lib/python3.10/site-packages (from plotly) (8.4.1)\n",
19+"Requirement already satisfied: packaging in /Users/longye/anaconda3/lib/python3.10/site-packages (from plotly) (23.1)\n",
20+"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->wbgapi) (1.26.16)\n",
21+"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->wbgapi) (2.0.4)\n",
22+"Requirement already satisfied: idna<4,>=2.5 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->wbgapi) (3.4)\n",
23+"Requirement already satisfied: certifi>=2017.4.17 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->wbgapi) (2024.6.2)\n"
24+ ]
25+ }
26+ ],
27+"source": [
28+"!pip install wbgapi plotly\n",
29+"\n",
30+"import pandas as pd\n",
31+"import numpy as np\n",
32+"import matplotlib.pyplot as plt\n",
33+"import random as rd\n",
34+"import wbgapi as wb\n",
35+"import plotly.express as px\n",
36+"\n",
37+"url = 'https://media.githubusercontent.com/media/QuantEcon/high_dim_data/main/SCF_plus/SCF_plus_mini.csv'\n",
38+"df = pd.read_csv(url)\n",
39+"df_income_wealth = df.dropna()"
40+ ]
41+ },
42+ {
43+"cell_type": "code",
44+"execution_count": 4,
45+"id": "9630a07a-fce5-474e-92af-104e67e82be5",
46+"metadata": {},
47+"outputs": [
48+ {
49+"name": "stdout",
50+"output_type": "stream",
51+"text": [
52+"Requirement already satisfied: quantecon in /Users/longye/anaconda3/lib/python3.10/site-packages (0.7.1)\n",
53+"Requirement already satisfied: requests in /Users/longye/anaconda3/lib/python3.10/site-packages (from quantecon) (2.31.0)\n",
54+"Requirement already satisfied: numpy>=1.17.0 in /Users/longye/anaconda3/lib/python3.10/site-packages (from quantecon) (1.26.3)\n",
55+"Requirement already satisfied: numba>=0.49.0 in /Users/longye/anaconda3/lib/python3.10/site-packages (from quantecon) (0.59.1)\n",
56+"Requirement already satisfied: sympy in /Users/longye/anaconda3/lib/python3.10/site-packages (from quantecon) (1.12)\n",
57+"Requirement already satisfied: scipy>=1.5.0 in /Users/longye/anaconda3/lib/python3.10/site-packages (from quantecon) (1.12.0)\n",
58+"Requirement already satisfied: llvmlite<0.43,>=0.42.0dev0 in /Users/longye/anaconda3/lib/python3.10/site-packages (from numba>=0.49.0->quantecon) (0.42.0)\n",
59+"Requirement already satisfied: certifi>=2017.4.17 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->quantecon) (2024.6.2)\n",
60+"Requirement already satisfied: idna<4,>=2.5 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->quantecon) (3.4)\n",
61+"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->quantecon) (2.0.4)\n",
62+"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/longye/anaconda3/lib/python3.10/site-packages (from requests->quantecon) (1.26.16)\n",
63+"Requirement already satisfied: mpmath>=0.19 in /Users/longye/anaconda3/lib/python3.10/site-packages (from sympy->quantecon) (1.3.0)\n"
64+ ]
65+ }
66+ ],
67+"source": [
68+"!pip install quantecon\n",
69+"import quantecon as qe\n",
70+"\n",
71+"varlist = ['n_wealth', # net wealth \n",
72+" 't_income', # total income\n",
73+" 'l_income'] # labor income\n",
74+"\n",
75+"df = df_income_wealth\n",
76+"years = df.year.unique()\n",
77+"\n",
78+"# create lists to store Gini for each inequality measure\n",
79+"results = {}\n",
80+"\n",
81+"for var in varlist:\n",
82+" # create lists to store Gini\n",
83+" gini_yr = []\n",
84+" for year in years:\n",
85+" # repeat the observations according to their weights\n",
86+" counts = list(round(df[df['year'] == year]['weights'] ))\n",
87+" y = df[df['year'] == year][var].repeat(counts)\n",
88+" y = np.asarray(y)\n",
89+" \n",
90+" rd.shuffle(y) # shuffle the sequence\n",
91+" \n",
92+" # calculate and store Gini\n",
93+" gini = qe.gini_coefficient(y)\n",
94+" gini_yr.append(gini)\n",
95+" \n",
96+" results[var] = gini_yr\n",
97+"\n",
98+"# Convert to DataFrame\n",
99+"results = pd.DataFrame(results, index=years)\n",
100+"results.to_csv(\"usa-gini-nwealth-tincome-lincome.csv\", index_label='year')"
101+ ]
102+ },
103+ {
104+"cell_type": "code",
105+"execution_count": null,
106+"id": "d59e876b-2f77-4fa7-b79a-8e455ad82d43",
107+"metadata": {},
108+"outputs": [],
109+"source": []
110+ }
111+ ],
112+"metadata": {
113+"kernelspec": {
114+"display_name": "Python 3 (ipykernel)",
115+"language": "python",
116+"name": "python3"
117+ },
118+"language_info": {
119+"codemirror_mode": {
120+"name": "ipython",
121+"version": 3
122+ },
123+"file_extension": ".py",
124+"mimetype": "text/x-python",
125+"name": "python",
126+"nbconvert_exporter": "python",
127+"pygments_lexer": "ipython3",
128+"version": "3.10.12"
129+ }
130+ },
131+"nbformat": 4,
132+"nbformat_minor": 5
133+}