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.12 for your work, but versions 3.9+ should work for this
assignment. 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. You may use the following code to download and read this data into a python list of dictionaries (copy and paste into a cell):
from pathlib import Path
import json
from urllib.request import urlretrieve
# download the data if we don't have it locally
= "https://faculty.cs.niu.edu/~dakoop/cs503-2024sp/a3/senate-stock-trades.json"
url = "senate-stock-trades.json"
local_fname if not Path(local_fname).exists():
urlretrieve(url, local_fname)
= json.load(open(local_fname)); data
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 asset (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)).senator
: 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 os, json, and urlretrieve modules as used in the snippet above are ok for that purpose).
The assignment is due at 11:59pm on Monday, February 19.
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.
List the names of all senators who have been involved in transactions involving Cryptocurrency. List each senator 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]
. Your result should be a dictionary whose
keys are the senators’ names and whose values are their sales sums.
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("1/17/2023", "1/20/2023")
returns
'transaction_date': '01/18/2023',
[{'owner': 'Joint',
'ticker': 'CLF',
'asset_type': 'Stock Option',
'type': 'Sale (Partial)',
'amount_range': [1001, 15000],
'senator': 'Tommy Tuberville'},
'transaction_date': '01/18/2023',
{'owner': 'Joint',
'ticker': 'CLF',
'asset_type': 'Stock Option',
'type': 'Sale (Partial)',
'amount_range': [1001, 15000],
'senator': 'Tommy Tuberville'},
'transaction_date': '01/18/2023',
{'owner': 'Joint',
'ticker': 'CLF',
'asset_type': 'Stock',
'type': 'Purchase',
'amount_range': [100001, 250000],
'senator': 'Tommy Tuberville'},
'transaction_date': '01/18/2023',
{'owner': 'Spouse',
'ticker': 'RIO',
'asset_type': 'Stock',
'type': 'Sale (Full)',
'amount_range': [1001, 15000],
'senator': 'Thomas R. Carper'},
'transaction_date': '01/18/2023',
{'owner': 'Spouse',
'ticker': 'FPI',
'asset_type': 'Stock',
'type': 'Purchase',
'amount_range': [1001, 15000],
'senator': 'Thomas R. Carper'}]