The goal of this assignment is to work with lists and dictionaries in Python.
You will be doing your work in a Jupyter notebook for this assignment. You may choose to work on this assignment on a hosted environment (e.g. tiger) or on your own local installation of Jupyter and Python. You should use Python 3.8 or higher for your work. To use tiger, use the credentials you received. If you work remotely, make sure to download the .ipynb file to turn in. If you choose to work locally, Anaconda is the easiest way to install and manage Python. If you work locally, you may launch Jupyter Lab either from the Navigator application or via the command-line as jupyter-lab
.
In this assignment, we will be working with data about US Senators’ stock trading practices. In this case, we will be using the Senate Stock Watcher which was built by Timothy Carambat. Rather than using this dataset directly, I have created a subset of this data, which can be read as a list of dictionaries. That data is located here, and I have created a template notebook, a3.ipynb, that contains a cell that will download and read that data. You can right-click and save-as the a3.ipynb file, and, if working on tiger, upload that file. Once loaded, the data is a list of dictionaries where each dictionary has ten key-value pairs. Those keys and a brief description are:
transaction_date
: the date of the transaction as a string in mm/dd/yyyy formatowner
: the owner of the stock (the senator or a family member)ticker
: the stock ticker symbol (e.g. AAPL)asset_type
: whether the asset is a stock, bond, cryptocurrency, etc.type
: the type of transaction (purchase or sale)amount_range
: the amount of the transaction (a range specified by a tuple (min_amount, max_amount)). max_amount can be None
to indicate no upper boundsenator
: the name of the senator involved in the transactionYou will be answering queries and writing functions to help analyze this data. You may not use external libraries including statistics, collections, datetime, or pandas for this assignment.
The assignment is due at 11:59pm on Thursday, September 23.
You should submit the completed notebook file required for this assignment on Blackboard. The filename of the notebook should be a3.ipynb
.
Please make sure to follow instructions to receive full credit. Use a markdown cell to Label each part of the assignment with the number of the section you are completing. You may put the code for each part into one or more cells.
The first cell of your notebook should be a markdown cell with a line for your name and a line for your Z-ID. If you wish to add other information (the assignment name, a description of the assignment), you may do so after these two lines.
Find all of the possible values for owner types. List each type only once!
Write code to find the trade in the dataset that involved the most money (amount). Output the name of the senator who was involved in that trade. Remember that you will need to iterate through each element of the list, and each element is a dictionary which has various keys including amount_range
and name
.
Write code to create a dictionary that keeps track of how many sales (full or partial) transactions each senator has been involved in.
Write code to create a dictionary that keeps track of the sum of the sales that each senator has made. Since we only have ranges, your output should also be a range. For example, if a senator has two sales of [1001,15000]
and [100001, 250000]
, the result will be [101002, 265000]
. Note that one senator (the same one who has the largest transaction), will have an upper bound of None
(indicating no upper bound). Your result should be a dictionary whose keys are the senators’ names and whose values are their sales sums.
None
value in the upper part of the range, and set the sum of two values to None
whenever either value is None
.Write a function get_ticker_median
that, given a ticker symbol, returns the median transaction range for that ticker symbol. Recall the median is the middle value. For a sorted list of values [1, 3, 4, 7, 21]
, the median is 4; for [1, 3, 4, 7, 13, 21]
, it is the average of the two middle values 4 and 7 = 5.5. The median range, unlike the sum, will be the middle range (after sorting) if we have an odd number of ranges for a particular ticker, and the union of two middle ranges (the lower bound from lower middle range and upper bound from higher middle range) if we have an even number of ranges. For example, the median of [[0,1], [1,3], [4,7]]
is [1,3]
while the median of [[0,1], [1,3], [4,7], [8,15]]
is [1,7]
.
For example,
'AAPL') # returns [1001, 15000]
get_ticker_median('NVDA') # returns [15001, 50000] get_ticker_median(
Only CSCI 503 students need to complete this part. CSCI 490 students may complete it for extra credit.
Write a function transactions_in_range
that will filter the sales by date (inclusive). Specifically, given a start date and an end date, return the transactions that fall in that range. Note that you will need to parse the date strings, and then compare the dates in the correct order. Do not use a python library for this, but rather create a tuple that encodes the date and makes the comparison operators work as desired. For example, transactions_in_range('9/30/2016','10/3/2016')
returns
'transaction_date': '10/03/2016',
[{'owner': 'Child',
'ticker': 'ASIX',
'asset_type': 'Stock',
'type': 'Exchange',
'amount_range': [1001, 15000],
'senator': 'Sheldon Whitehouse'},
'transaction_date': '10/03/2016',
{'owner': 'Joint',
'ticker': 'ASIX',
'asset_type': 'Stock',
'type': 'Exchange',
'amount_range': [1001, 15000],
'senator': 'Sheldon Whitehouse'},
'transaction_date': '10/03/2016',
{'owner': 'Self',
'ticker': 'ASIX',
'asset_type': 'Stock',
'type': 'Exchange',
'amount_range': [1001, 15000],
'senator': 'Sheldon Whitehouse'},
'transaction_date': '09/30/2016',
{'owner': 'Self',
'ticker': 'DIS',
'asset_type': 'Stock',
'type': 'Purchase',
'amount_range': [50001, 100000],
'senator': 'James M Inhofe'},
'transaction_date': '09/30/2016',
{'owner': 'Self',
'ticker': 'VZ',
'asset_type': 'Corporate Bond',
'type': 'Purchase',
'amount_range': [1001, 15000],
'senator': 'Gary C Peters'}]