Skip to content

Instantly share code, notes, and snippets.

@MaxGhenis
Created July 9, 2020 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxGhenis/3701b4bad827ea545ece2bcb8805d6fc to your computer and use it in GitHub Desktop.
Save MaxGhenis/3701b4bad827ea545ece2bcb8805d6fc to your computer and use it in GitHub Desktop.
# Sphinx version: 2.4.4
# Python version: 3.7.7 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.2
# Last messages:
# Running Sphinx v2.4.4
# making output directory...
# done
# building [mo]: targets for 0 po files that are out of date
# building [html]: targets for 2 source files that are out of date
# updating environment:
# [new config]
# 2 added, 2 changed, 0 removed
# reading sources... [ 50%] 20200707/adult_child_ubi
# Executing: 20200707/adult_child_ubi
# Loaded extensions:
# sphinx.ext.mathjax (2.4.4) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/ext/mathjax.py
# sphinxcontrib.applehelp (1.0.2) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinxcontrib/applehelp/__init__.py
# sphinxcontrib.devhelp (1.0.2) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinxcontrib/devhelp/__init__.py
# sphinxcontrib.htmlhelp (1.0.3) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinxcontrib/htmlhelp/__init__.py
# sphinxcontrib.serializinghtml (1.1.4) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinxcontrib/serializinghtml/__init__.py
# sphinxcontrib.qthelp (1.0.3) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinxcontrib/qthelp/__init__.py
# alabaster (0.7.12) from /home/mghenis/anaconda3/lib/python3.7/site-packages/alabaster/__init__.py
# sphinx_togglebutton (0.2.0) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx_togglebutton/__init__.py
# sphinx_copybutton (0.2.12) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx_copybutton/__init__.py
# jupyter_sphinx (0.2.4) from /home/mghenis/anaconda3/lib/python3.7/site-packages/jupyter_sphinx/__init__.py
# myst_nb (0.8.3) from /home/mghenis/anaconda3/lib/python3.7/site-packages/myst_nb/__init__.py
# jupyter_book (0.7.0) from /home/mghenis/anaconda3/lib/python3.7/site-packages/jupyter_book/__init__.py
# sphinxcontrib.bibtex (unknown version) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinxcontrib/bibtex/__init__.py
# sphinx_book_theme (unknown version) from /home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx_book_theme/__init__.py
# pydata_sphinx_theme (unknown version) from /home/mghenis/anaconda3/lib/python3.7/site-packages/pydata_sphinx_theme/__init__.py
Traceback (most recent call last):
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/jupyter_book/sphinx.py", line 222, in build_sphinx
app.build(force_all, filenames)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/application.py", line 349, in build
self.builder.build_update()
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 299, in build_update
len(to_build))
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 311, in build
updated_docnames = set(self.read())
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 418, in read
self._read_serial(docnames)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 439, in _read_serial
self.read_doc(docname)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 479, in read_doc
doctree = read_doc(self.app, self.env, self.env.doc2path(docname))
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/io.py", line 316, in read_doc
pub.publish()
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/docutils/core.py", line 218, in publish
self.settings)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/sphinx/io.py", line 130, in read
self.parse()
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/docutils/readers/__init__.py", line 77, in parse
self.parser.parse(self.input, document)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/myst_nb/parser.py", line 65, in parse
ntbk = add_notebook_outputs(self.env, ntbk)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/myst_nb/cache.py", line 149, in add_notebook_outputs
ntbk = execute(ntbk, cwd=Path(file_path).parent)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/nbclient/client.py", line 968, in execute
return NotebookClient(nb=nb, resources=resources, km=km, **kwargs).execute()
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/nbclient/util.py", line 72, in wrapped
return just_run(coro(*args, **kwargs))
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/nbclient/util.py", line 51, in just_run
return loop.run_until_complete(coro)
File "/home/mghenis/anaconda3/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
return future.result()
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/nbclient/client.py", line 509, in async_execute
cell, index, execution_count=self.code_cells_executed + 1
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/nbclient/client.py", line 747, in async_execute_cell
self._check_raise_for_error(cell, exec_reply)
File "/home/mghenis/anaconda3/lib/python3.7/site-packages/nbclient/client.py", line 671, in _check_raise_for_error
raise CellExecutionError.from_cell_and_msg(cell, exec_reply['content'])
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
### LOAD PACKAGES ####
import pandas as pd
import numpy as np
import plotly.express as px
import plotly
### LOAD DATA ###
person_raw = pd.read_csv('https://github.com/MaxGhenis/datarepo/raw/master/pppub19.csv.gz',
usecols=['MARSUPWT', 'SPM_ID', 'SPM_POVTHRESHOLD',
'SPM_RESOURCES', 'A_AGE'])
### PREPROCESS ###
person = person_raw.copy(deep=True)
person.columns = person.columns.str.lower()
person['weight'] = person.marsupwt/100
#Compute total children and adults in each resource sharing group.
person['child'] = person.a_age < 18
person['adult'] = person.a_age >= 18
spmu_ages = person.groupby('spm_id')[['child','adult']].sum()
spmu_ages.columns = ['children', 'total_adults']
person2 = person.merge(spmu_ages,left_on='spm_id', right_index=True)
total_children = (person2.child * person2.weight).sum()
total_adults = (person2.adult * person2.weight).sum()
### CALCULATIONS ###
child_allowance_overall = []
child_allowance_child = []
child_allowance_adults = []
# Determine the poverty rate impact of a Child Allownace from $0 in new spending to $1 trillion.
for spending in range(0, 1000000000001, 50000000000):
child_allowance_per_child = spending/total_children
total_child_allowance = person2.children * child_allowance_per_child
new_spm_resources_ca = person2.spm_resources + total_child_allowance
new_poor_ca = new_spm_resources_ca < person2.spm_povthreshold
new_total_child_poor = ((person2.child * person2.weight *
new_poor_ca).sum())
new_child_poverty_rate = ((new_total_child_poor)/
(person2.child * person2.weight).sum())
new_total_adult_poor = ((person2.adult * person2.weight *
new_poor_ca).sum())
new_adult_poverty_rate = ((new_total_adult_poor)/
(person2.adult * person2.weight).sum())
new_total_poor_ca = (new_poor_ca * person2.weight).sum()
new_poverty_rate_ca = new_total_poor_ca/person2.weight.sum()
child_allowance_overall.append(new_poverty_rate_ca)
child_allowance_child.append(new_child_poverty_rate)
child_allowance_adults.append(new_adult_poverty_rate)
ubi_adults_overall = []
ubi_adults_child = []
ubi_adults_adults = []
# Determine the poverty rate impact of a Adult UBI from $0 in new spending to $1 trillion.
for spending in range(0, 1000000000001, 50000000000):
adult_ubi = spending/total_adults
total_adult_ubi = person2.total_adults * adult_ubi
new_spm_resources_ubi = person2.spm_resources + total_adult_ubi
new_poor_ubi = new_spm_resources_ubi < person2.spm_povthreshold
new_total_child_poor = ((person2.child * person2.weight *
new_poor_ubi).sum())
new_child_poverty_rate = ((new_total_child_poor)/
(person2.child * person2.weight).sum())
new_total_adult_poor = ((person2.adult * person2.weight *
new_poor_ubi).sum())
new_adult_poverty_rate = ((new_total_adult_poor)/
(person2.adult * person2.weight).sum())
new_total_poor_ubi = (new_poor_ubi * person2.weight).sum()
new_poverty_rate_ubi = new_total_poor_ubi/person2.weight.sum()
ubi_adults_overall.append(new_poverty_rate_ubi)
ubi_adults_child.append(new_child_poverty_rate)
ubi_adults_adults.append(new_adult_poverty_rate)
ubi_all_overall = []
ubi_all_child = []
ubi_all_adults = []
# Determine the poverty rate impact of a All UBI from $0 in new spending to $1 trillion.
for spending in range(0, 1000000000001, 50000000000):
all_ubi_per_person = spending/(total_adults + total_children)
total_all_ubi = ((person2.children * all_ubi_per_person) +
(person2.total_adults * all_ubi_per_person))
new_spm_resources_all_ubi = person2.spm_resources + total_all_ubi
new_poor_all_ubi = new_spm_resources_all_ubi < person2.spm_povthreshold
new_total_child_poor = ((person2.child * person2.weight *
new_poor_all_ubi).sum())
new_child_poverty_rate = ((new_total_child_poor)/
(person2.child * person2.weight).sum())
new_total_adult_poor = ((person2.adult * person2.weight *
new_poor_all_ubi).sum())
new_adult_poverty_rate = ((new_total_adult_poor)/
(person2.adult * person2.weight).sum())
new_total_poor_all_ubi = (new_poor_all_ubi * person2.weight).sum()
new_poverty_rate_all_ubi = new_total_poor_all_ubi/person2.weight.sum()
ubi_all_overall.append(new_poverty_rate_all_ubi)
ubi_all_child.append(new_child_poverty_rate)
ubi_all_adults.append(new_adult_poverty_rate)
spending_data = []
for spending in range(0, 1001, 50):
spending = spending/100
spending_data.append(spending)
### ANALYSIS ###
# Create a DataFrame grouped by each plans impact on the overall poverty rate.
overall = {'spending_in_billions': spending_data,
'child_allowance': child_allowance_overall,
'adult_ubi': ubi_adults_overall,
'all_ubi': ubi_all_overall}
overall_df = pd.DataFrame(overall)
overall_df = pd.DataFrame(overall_df).round(3)
# Create a DataFrame grouped by each plans impact on the child poverty rate.
child = {'spending_in_billions': spending_data,
'child_allowance': child_allowance_child,
'adult_ubi': ubi_adults_child,
'all_ubi': ubi_all_child}
child_df = pd.DataFrame(child)
child_df = pd.DataFrame(child_df).round(3)
# Create a DataFrame grouped by each plans impact on the adult poverty rate.
adult = {'spending_in_billions': spending_data,
'child_allowance': child_allowance_adults,
'adult_ubi': ubi_adults_adults,
'all_ubi': ubi_all_adults}
adult_df = pd.DataFrame(adult)
adult_df = pd.DataFrame(adult_df).round(3)
# Join different programs together for plotly.
program = (pd.melt(overall_df, 'spending_in_billions',
var_name='ubi_type',value_name='poverty_rate'))
def melt_dict(d):
""" produce long version of data frame represented by dictionary (d).
Arguments
d: Dictionary where each element represents a differnt UBI type and spending levels and the poverty impacts.
Returns
DataFrame where every row is the combination of UBI type and spending level.
"""
df = pd.DataFrame(d).round(3) * 100
program = pd.melt(df, 'spending_in_billions', var_name='ubi_type',value_name='poverty_rate')
program['ubi_type'] = program.ubi_type.map({'child_allowance': 'Child allowance',
'adult_ubi': 'Adult UBI',
'all_ubi': 'All UBI'})
return program
program_overall = melt_dict(overall)
program_child = melt_dict(child)
program_adult = melt_dict(adult)
def line_graph(df, x, y, color, title, xaxis_title, yaxis_title):
"""Style for line graphs.
Arguments
df: DataFrame with data to be plotted.
x: The string representing the column in df that holds the new spending in billions.
y: The string representing the column in df that holds the poverty rate.
color: The string representing the UBI type.
xaxis_title: The string represnting the xaxis-title.
yaxis_title: The string representing the yaxis-title.
Returns
Nothing. Shows the plot.
"""
fig = px.line(df, x=x, y=y, color=color)
fig.update_layout(
title=title,
xaxis_title=xaxis_title,
yaxis_title=yaxis_title,
yaxis_ticksuffix='%',
font=dict(family='Roboto'),
hovermode='x',
xaxis_tickprefix='$',
xaxis_ticksuffix='B',
plot_bgcolor='white',
legend_title_text=''
)
fig.update_traces(mode='markers+lines', hovertemplate=None)
fig.show()
line_graph(df=program_overall, x='spending_in_billions',
y='poverty_rate', color='ubi_type',
title='Overall poverty rate and spending on cash transfer programs',
xaxis_title='Spending in billions',
yaxis_title='SPM poverty rate')
------------------
---------------------------------------------------------------------------
gaierror Traceback (most recent call last)
~/anaconda3/lib/python3.7/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
 1318 h.request(req.get_method(), req.selector, req.data, headers,
-> 1319 encode_chunked=req.has_header('Transfer-encoding'))
 1320 except OSError as err: # timeout error
~/anaconda3/lib/python3.7/http/client.py in request(self, method, url, body, headers, encode_chunked)
 1251 """Send a complete request to the server."""
-> 1252 self._send_request(method, url, body, headers, encode_chunked)
 1253 
~/anaconda3/lib/python3.7/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
 1297 body = _encode(body, 'body')
-> 1298 self.endheaders(body, encode_chunked=encode_chunked)
 1299 
~/anaconda3/lib/python3.7/http/client.py in endheaders(self, message_body, encode_chunked)
 1246 raise CannotSendHeader()
-> 1247 self._send_output(message_body, encode_chunked=encode_chunked)
 1248 
~/anaconda3/lib/python3.7/http/client.py in _send_output(self, message_body, encode_chunked)
 1025 del self._buffer[:]
-> 1026 self.send(msg)
 1027 
~/anaconda3/lib/python3.7/http/client.py in send(self, data)
 965 if self.auto_open:
--> 966 self.connect()
 967 else:
~/anaconda3/lib/python3.7/http/client.py in connect(self)
 1413 
-> 1414 super().connect()
 1415 
~/anaconda3/lib/python3.7/http/client.py in connect(self)
 937 self.sock = self._create_connection(
--> 938 (self.host,self.port), self.timeout, self.source_address)
 939 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
~/anaconda3/lib/python3.7/socket.py in create_connection(address, timeout, source_address)
 706 err = None
--> 707 for res in getaddrinfo(host, port, 0, SOCK_STREAM):
 708 af, socktype, proto, canonname, sa = res
~/anaconda3/lib/python3.7/socket.py in getaddrinfo(host, port, family, type, proto, flags)
 751 addrlist = []
--> 752 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
 753 af, socktype, proto, canonname, sa = res
gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
<ipython-input-1-c893d11fa016> in <module>
 10 person_raw = pd.read_csv('https://github.com/MaxGhenis/datarepo/raw/master/pppub19.csv.gz',
 11 usecols=['MARSUPWT', 'SPM_ID', 'SPM_POVTHRESHOLD',
---> 12 'SPM_RESOURCES', 'A_AGE'])
 13 
 14 ### PREPROCESS ###
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
 674 )
 675 
--> 676 return _read(filepath_or_buffer, kwds)
 677 
 678 parser_f.__name__ = name
~/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
 429 # See https://github.com/python/mypy/issues/1297
 430 fp_or_buf, _, compression, should_close = get_filepath_or_buffer(
--> 431 filepath_or_buffer, encoding, compression
 432 )
 433 kwds["compression"] = compression
~/anaconda3/lib/python3.7/site-packages/pandas/io/common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode)
 170 
 171 if isinstance(filepath_or_buffer, str) and is_url(filepath_or_buffer):
--> 172 req = urlopen(filepath_or_buffer)
 173 content_encoding = req.headers.get("Content-Encoding", None)
 174 if content_encoding == "gzip":
~/anaconda3/lib/python3.7/site-packages/pandas/io/common.py in urlopen(*args, **kwargs)
 139 import urllib.request
 140 
--> 141 return urllib.request.urlopen(*args, **kwargs)
 142 
 143 
~/anaconda3/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
 220 else:
 221 opener = _opener
--> 222 return opener.open(url, data, timeout)
 223 
 224 def install_opener(opener):
~/anaconda3/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
 529 for processor in self.process_response.get(protocol, []):
 530 meth = getattr(processor, meth_name)
--> 531 response = meth(req, response)
 532 
 533 return response
~/anaconda3/lib/python3.7/urllib/request.py in http_response(self, request, response)
 639 if not (200 <= code < 300):
 640 response = self.parent.error(
--> 641 'http', request, response, code, msg, hdrs)
 642 
 643 return response
~/anaconda3/lib/python3.7/urllib/request.py in error(self, proto, *args)
 561 http_err = 0
 562 args = (dict, proto, meth_name) + args
--> 563 result = self._call_chain(*args)
 564 if result:
 565 return result
~/anaconda3/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
 501 for handler in handlers:
 502 func = getattr(handler, meth_name)
--> 503 result = func(*args)
 504 if result is not None:
 505 return result
~/anaconda3/lib/python3.7/urllib/request.py in http_error_302(self, req, fp, code, msg, headers)
 753 fp.close()
 754 
--> 755 return self.parent.open(new, timeout=req.timeout)
 756 
 757 http_error_301 = http_error_303 = http_error_307 = http_error_302
~/anaconda3/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
 523 req = meth(req)
 524 
--> 525 response = self._open(req, data)
 526 
 527 # post-process response
~/anaconda3/lib/python3.7/urllib/request.py in _open(self, req, data)
 541 protocol = req.type
 542 result = self._call_chain(self.handle_open, protocol, protocol +
--> 543 '_open', req)
 544 if result:
 545 return result
~/anaconda3/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
 501 for handler in handlers:
 502 func = getattr(handler, meth_name)
--> 503 result = func(*args)
 504 if result is not None:
 505 return result
~/anaconda3/lib/python3.7/urllib/request.py in https_open(self, req)
 1360 def https_open(self, req):
 1361 return self.do_open(http.client.HTTPSConnection, req,
-> 1362 context=self._context, check_hostname=self._check_hostname)
 1363 
 1364 https_request = AbstractHTTPHandler.do_request_
~/anaconda3/lib/python3.7/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
 1319 encode_chunked=req.has_header('Transfer-encoding'))
 1320 except OSError as err: # timeout error
-> 1321 raise URLError(err)
 1322 r = h.getresponse()
 1323 except:
URLError: <urlopen error [Errno -2] Name or service not known>
URLError: <urlopen error [Errno -2] Name or service not known>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment