# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Effect of UBI on Supplemental Poverty Measure
#
# $500 per person.

# ## Setup
# ### Imports

import pandas as pd
import numpy as np
import microdf as mdf
import os

# ### Load data
#
# Looks for the 2019 March Supplement in a `~/data` folder.

ASEC_F = '~/data/cpspb/asec/prod/data/2019/pppub19.csv'

if not os.path.isfile(os.path.expanduser(ASEC_F)):
    # !mkdir ~/data
    # !wget -O ~/data/asecpub19csv.zip http://thedataweb.rm.census.gov/pub/cps/march/asecpub19csv.zip
    # !unzip ~/data/asecpub19csv.zip -d ~/data

SPM_COLS = ['povthreshold', 'resources', 'poor', 'numper', 'numkids', 'numadults', 'id']
OTHER_COLS = ['A_AGE', 'MARSUPWT', 'PRCITSHP']
cols = ['SPM_' + i.upper() for i in SPM_COLS] + OTHER_COLS

raw = pd.read_csv(ASEC_F, usecols=cols)

# ## Preprocess

df = raw.copy(deep=True)

df.columns = map(str.lower, df.columns)

# Add true weight by dividing by 100.

df['w'] = df.marsupwt / 100.

# Add citizenship indicator, per https://www2.census.gov/programs-surveys/cps/techdocs/cpsmar19.pdf.

df['is_citizen'] = df.prcitshp != 5

df['is_kid'] = df.a_age < 18
df['is_adult'] = df.a_age >= 18
df['is_kid_citizen'] = df.is_citizen & df.is_kid
df['is_adult_citizen'] = df.is_citizen & df.is_adult

# Number of citizens per SPM unit.

spmu = df.groupby('spm_id')[['is_kid', 'is_adult', 'is_kid_citizen', 'is_adult_citizen']].sum()

spmu.columns = ['spm_nu18', 'spm_n18', 'spm_numkidcitizens', 'spm_numadultcitizens']

df = df.merge(spmu, on='spm_id')

# ## Analysis

# Percent citizen.

mdf.weighted_mean(df, 'is_citizen', 'w')

mdf.weighted_mean(df[df.a_age < 18], 'is_citizen', 'w')

mdf.weighted_mean(df[df.a_age >= 18], 'is_citizen', 'w')


def rounded_pct(x):
    print(str((x * 100).round(1)) + '%')


def print_pov(df, weight='w'):
    rounded_pct(mdf.weighted_mean(df, 'spm_poor', weight))


def ubi_pov(ubi_adult=0, ubi_kid=0, ubi_adult_citizen=0, ubi_kid_citizen=0):
    resources = (
        df.spm_resources + 
        ubi_adult * df.spm_n18 +
        ubi_kid * df.spm_nu18 +
        ubi_adult_citizen * df.spm_numadultcitizens +
        ubi_kid_citizen * df.spm_numkidcitizens)
    is_pov = resources < df.spm_povthreshold
    return (is_pov * df.w).sum() / df.w.sum()


def ubi_cost(ubi_adult=0, ubi_kid=0, ubi_adult_citizen=0, ubi_kid_citizen=0):
    ubi_adult_cost = (ubi_adult * df.is_adult * df.w).sum()
    ubi_kid_cost = (ubi_kid * df.is_kid * df.w).sum()
    ubi_adult_citizen_cost = (ubi_adult_citizen * df.is_adult_citizen * df.w).sum()
    ubi_kid_citizen_cost = (ubi_kid_citizen * df.is_kid_citizen * df.w).sum()
    return ubi_adult_cost + ubi_kid_cost + ubi_adult_citizen_cost + ubi_kid_citizen_cost


ubi_cost(6000, 6000) / 1e12

ubi_cost(0, 0, 6000, 6000) / 1e12

ubi_cost(7800, 0)  / 1e12

ubi_pov(0, 0)

ubi_pov(6000, 6000)

ubi_pov(0, 0, 6000, 6000)

1 - ubi_pov(6000, 6000) / ubi_pov(0, 0)

ubi_pov(7800, 0)

1 - ubi_pov(7800, 0) / ubi_pov(0, 0)

ubi_cost(12000, 0) / 1e12

# ## Miscellaneous

print_pov(df[df.is_citizen])

print_pov(df[~df.is_citizen])

StarcoderdataJupyterScriptsDedupFiltered