{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `microdf` demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "import taxcalc as tc\n", "import microdf as mdf\n", "\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Chart options." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/mghenis/anaconda3/lib/python3.7/site-packages/microdf/style.py:24: MatplotlibDeprecationWarning: \n", "The createFontList function was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use FontManager.addfont instead.\n", " fm.fontManager.ttflist += fm.createFontList([\"Roboto-Regular.ttf\"])\n" ] } ], "source": [ "mdf.set_plot_style()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "base = mdf.calc_df(group_vars=['expanded_income', 'MARS'],\n", " metric_vars=['aftertax_income', 'XTOT'])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['e02400', 'mcare_ben', 'aftertax_income', 'ssi_ben', 'expanded_income',\n", " 'snap_ben', 'vet_ben', 'housing_ben', 's006', 'other_ben', 'e02300',\n", " 'mcaid_ben', 'XTOT', 'tanf_ben', 'MARS', 'wic_ben', 'market_income',\n", " 'bens', 'tax', 's006_m', 'aftertax_income_m', 'XTOT_m'],\n", " dtype='object')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base.columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a reform that treats capital gains as ordinary income and sets the top marginal rate to 70%." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "CG_REFORM = {\n", " 'CG_nodiff': {2019: True},\n", " 'II_rt7': {2019: 0.7}\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "reform = mdf.calc_df(reform=CG_REFORM, group_vars=['MARS'], group_n65=True, \n", " metric_vars=['aftertax_income', 'XTOT'])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['vet_ben', 's006', 'e02300', 'MARS', 'e02400', 'mcare_ben', 'ssi_ben',\n", " 'snap_ben', 'housing_ben', 'other_ben', 'aftertax_income',\n", " 'expanded_income', 'mcaid_ben', 'XTOT', 'tanf_ben', 'wic_ben',\n", " 'market_income', 'bens', 'tax', 'n65', 's006_m', 'aftertax_income_m',\n", " 'XTOT_m'],\n", " dtype='object')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reform.columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate senior UBI.\n", "\n", "Start with total revenue ($ billions)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "326.110945495585" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_rev_m = base.aftertax_income_m.sum() - reform.aftertax_income_m.sum()\n", "new_rev_m / 1e3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How many seniors are there?" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "59.21619976999999" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.add_weighted_metrics(reform, 'n65')\n", "\n", "n65_total_m = reform.n65_m.sum()\n", "n65_total_m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Divide." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5507.123840473106" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "senior_ubi = new_rev_m / reform.n65_m.sum()\n", "senior_ubi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add senior UBI to `aftertax_income` and recalculate" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "reform['ubi'] = senior_ubi * reform.n65\n", "reform['aftertax_income'] = reform.aftertax_income + reform.ubi\n", "mdf.add_weighted_metrics(reform, 'aftertax_income')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.allclose(base.aftertax_income_m.sum(), reform.aftertax_income_m.sum())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyze\n", "\n", "Gini, FPL, distributional impact chart\n", "\n", "### Change to Gini index" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5032911973267852" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.gini(base, 'aftertax_income', 's006')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.48752755152259336" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdf.gini(reform, 'aftertax_income', 's006')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change to poverty rate\n", "\n", "Add federal poverty line with `mdf.fpl`." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "base['fpl'] = mdf.fpl(base.XTOT)\n", "reform['fpl'] = mdf.fpl(reform.XTOT)\n", "\n", "base['fpl_XTOT_m'] = np.where(base.aftertax_income < base.fpl,\n", " base.XTOT_m, 0)\n", "reform['fpl_XTOT_m'] = np.where(reform.aftertax_income < reform.fpl,\n", " reform.XTOT_m, 0)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.022307196800575246" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reform.fpl_XTOT_m.sum() / base.fpl_XTOT_m.sum() - 1" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }