Project 1: Stock price reaction to analyst recommendations (Due 11:59pm on Friday, November 6) Overview In this project, you will evaluate the stock price reaction to changes to analyst...

1 answer below »
I have a bunch of data for the python coding


Project 1: Stock price reaction to analyst recommendations (Due 11:59pm on Friday, November 6) Overview In this project, you will evaluate the stock price reaction to changes to analyst recommendations. Each obser- vation will represent an analyst either upgrading or downgrading a stock from their previous recommendation. The deadline for this project is 11:59pm on Friday, November 6. This project has three parts, each worth a third of the total mark for the project: Part 1: Combine stock price data distributed across multiple files. Part 2: Prepare the analyst recommendation data distributed across multiple files. Part 3: Perform the event study This project is to be completed individually by each student. You should not collaborate. You are allowed to post questions about the project in the thread I will create in the Discussion Board. However, please do not answer any questions posted by other students. I will answer any project-related questions myself. Files You received a zipped file with the following contents: / | _event_study.py | README.txt | TICKERS.txt |___data/ | | ff_daily.csv | | _prc.dat | | _rec.csv where represents your Zid, represents a ticker (in lower case) and: • _project1.py contains the functions you will use to perform this study. Some of the functions are already written, while others you will write yourself. Please see the instructions below for more information. • TICKERS.txt contains a list of tickers, one per line. These tickers may be in upper or lower case. • README.txt contains information about how the stock data is stored in the .dat files. • data is a folder containing all the data you will need to complete this project. Inside this folder: – _prc.dat contains stock price data for the ticker . – _rec.csv is a CSV file containing analyst recommendation data for the ticker . 1 Instructions 1. Decompress the contents of the zip file onto your computer. 2. Complete the user-written functions in _project1.py. 3. Submit the completed version of _project.py of your project. The following notes are very important: 1. Functions produced in one part of the project will be used in subsequent parts. That means you have to complete Parts 1, 2, and 3 below in sequence. 2. When completing your code, please keep the following in mind. We will run your functions through test cases designed to check specific parts of your code. The data we will use is different than what you received. This means that your code should be general (follow the instructions and it will be). For instance, you should not create a variable called tickers and then copy the specific tickers you received in your TICKERS.txt file. Instead, your code should read the TICKERS.txt file, produce a list of tickers, and store that in a variable. 3. When writing functions in the file _project1.py: • Do not modify the function names or the parameters. • Only modify the parts indicated by the “” tag. • You do not need to import any other module. Please do not modify the import statements. Part 1: Combine stock price data distributed across multiple files. It is often the case that one needs to put together a data set from different sources. The source data may not be stored in a format that is easy to import into a spreadsheet or directly into Pandas. To simulate this real-world problem, your data contains stock price information distributed across many _prc.dat files. We are only interested in the files containing stock price data (_prc.dat) for this part of the project. To complete this part of the project modify the following sections in _project1.py: 1. Set the correct expressions for the constants SRCDIR, TICKERS, and FF_CSV, which contain the file location of the source directory, the TICKERS.txt file, and the ff_daily.csv file, respectively. 2. Set the correct expressions for the variables SRC_COLS, SRC_COL_DTYPES, and SRC_COL_WIDTHS. These represent the source column names, a dictionary from column name to Pandas dtype, and the column widths, respectively. To complete this part, you will need the information in the README.txt file. 3. Complete the indicated part of the function get_tics. This function reads a file with tickers and returns a list with formatted tickers. 4. Complete the indicated part of the function dat_to_df. This functions reads a stock price data file and returns a Pandas DataFrame. This function is used by the function mk_prc_df, which you will also need to complete. 5. Complete the indicated part of the function mk_prc_df. The output of this function must meet the one specified in the docstring. This function will call the dat_to_df function you completed above. Part 2: Computing Abnormal Returns and Preparing Analysts’ Recommendation Data For this part of the project, you will compute abnormal returns and prepare the recommendations contained in each _rec.csv file. 2 1. Complete the indicated parts of the function mk_aret_df. This function will produce a dataframe with the daily abnormal returns for each ticker. Abnormal returns are defined as the stock’s return minus the market return. Market returns are provided in the ff_daily.csv file. 2. Complete the indicated parts of the function read_rec_csv. This function takes a ticker and returns a dataframe with the contents of the corresponding _rec.csv file. 3. Complete the indicated parts of the function proc_rec_df. This function will subset the dataframe from read_rec_csv to include only upgrades and downgrades from at most 30 firms, selected based on the number of recommendations. Part 3: Performing the Event Study 1. Complete the indicated parts of the function mk_event_df, which creates a dataframe with each event which will be included in the study. An event will be either a downgrade or upgrade for a firm in a given day. 2. Complete the docstring of the function mk_ret_dates. You should try (but are not required) to follow docstring style used for other functions in this module. You do not need to write a ‘Notes’ section. 3. Complete the function mk_ret_dates_by_group, which is used by the mk_ret_dates function above. 4. Preform the event study by running the function main. 3 Overview Files Instructions Part 1: Combine stock price data distributed across multiple files. Part 2: Computing Abnormal Returns and Preparing Analysts' Recommendation Data Part 3: Performing the Event Study # ---------------------------------------------------------------------------- # About this file # ---------------------------------------------------------------------------- This file contains information about the price DAT files. These files contain fixed-width data for the following columns: - 'Adj Close': The closing price of the stock, adjusted for dividends, splits, and other events. - 'Date': The calendar day of this data. Each date is represented as a four-digit year, a two-digit month, and a two-digit day separated by commas. This is YYYY-MM-DD format. - 'Volume': The total number of shares traded on the day (in millions) - 'High': The highest price at which the stock trades for the day, unadjusted for dividends, splits, and other events. - 'Open': The inital price at which the stock trades for the day, unadjusted for dividends, splits, and other events. The data is in a fixed-width format. Instead of using delimiters, such as a comma, to separate the data columns, data is simply presented as text, with one data field leading directly to the next. For example, if the first piece of information in a row was a four-character word (e.g. "blue") the next piece of information was a four-character number (e.g. "3.14"), and the third piece of information was a one-character boolean value (e.g. "t" for true), the row will be presented as "blue3.14t". All data would need to meet this format with no item longer than its specified length. Thus, identification of information in the file requires knowing the ordering of the columns and the width of each column. Each line in your actual stock price DAT file has exactly 63 characters. The information for the first column starts at the beginning of the line and stops after the `n` characters, where `n` is the width of the first column. For instance, the first column in each file contains data for the column `Adj Close.` Based on the information you are given below, the width of that column is `10`. This means that the data for this column starts at the beginning of the line and continue up to and including the 10th character in that line. The data for the next column begins immediately after the 10th character, with the 11th character in the line. For your specific DAT files, you have the following information: Column Name: column position : The order of this column in the file starting with 1. That is, the first column has column position 1, the second column has column position 2, and so on. dtype : The Pandas data type for this column width : The number of characters in this column # ---------------------------------------------------------------------------- # Column information # ---------------------------------------------------------------------------- Adj Close: column position: 1 dtype: float64 width: 10 Date: column position: 2 dtype: datetime64 width: 10 Volume: column position: 3 dtype: int64 width: 16 High: column position: 4 dtype: float64 width: 9 Open: column position: 5 dtype: float64 width: 18 KO FB DAL CSCO BAC AAL ABBV T TSLA DIS MSFT TSM PYPL V AAPL PG NVDA JNJ GE INTC
Answered Same DayNov 09, 2021

Answer To: Project 1: Stock price reaction to analyst recommendations (Due 11:59pm on Friday, November 6)...

Sandeep Kumar answered on Nov 12 2021
150 Votes
""" project1.py
Complete the sections below marked with ''

"""
import os
import datetime as dt
import numpy as np
import pandas as pd
# SRCDIR is the folder containing all the data:
# - `_prc.dat`
# - `_rec.csv`
# - ff_dailyc.csv`
#
SRCDIR = './data/'
# TICKERS is the location of the TICKERS.txt file
TICKERS = './TICKERS.txt'
# FF_CSV is the location of the ff_daily.csv file
FF_CSV = './data/'
# ----------------------------------------------------------------------------
# Modify these variables as specified by the README.txt file
# ----------------------------------------------------------------------------
# NOTE:
# - SRC_COLS must be a list
# - The order of the elements must match the order of the columns specified
# in the README.txt file.
#
SRC_COLS = ['Adj Close', 'Date', 'Volume', 'High', 'Open']
# NOTE:
# - SRC_COL_DTYPES must be a dict
# - The keys should be the column names, the values should b
e their dtype, as
# specified in the README.txt file.
#
SRC_COL_DTYPES = {
SRC_COLS[0]: 'float64',
SRC_COLS[1]: 'datetime64',
SRC_COLS[2]: 'int64',
SRC_COLS[3]: 'float64',
SRC_COLS[4]: 'float64',
}
# NOTE:
# - SRC_COL_WIDTHS should be a dict
# - The keys should be the column names, the values should be the width of
# that field in the DAT file. These should match the widths defined in the
# README.txt file.
#
SRC_COL_WIDTHS = {
SRC_COLS[0]: 10,
SRC_COLS[1]: 10,
SRC_COLS[2]: 16,
SRC_COLS[3]: 9,
SRC_COLS[4]: 18,
}
# ----------------------------------------------------------------------------
# Function get_tics
# ----------------------------------------------------------------------------
def get_tics(pth):
""" Reads a file with tickers (one per line) and returns a list
of formatted tickers (see the notes below).
Parameters
----------
pth : str
Location of the TICKERS.txt file
Returns
-------
list
List where each element represents a ticker (formatted as below)
Notes
-----
- The tickers returned must conform with the following rules:
- All characters are in lower case
- There are no spaces
- The list contains no empty/blank tickers
"""
with open(pth, 'r') as tickers:
return [t.strip().lower() for t in tickers]
# ----------------------------------------------------------------------------
# Function dat_to_df
# ----------------------------------------------------------------------------
def dat_to_df(pth,
src_cols,
src_col_dtypes,
src_col_widths,
):
""" This function creates a dataframe with the contents of a DAT file
containing stock price information for a given ticker.

Parameters
----------
pth : str
Location of the DAT file containing price information (i.e. some
`_prc.dta`)
src_cols : list
List containing the column names in the order they appear in each
source DAT file. The order of columns must match the order specified
in the README.txt file
src_col_dtypes : dict
A dictionary mapping each column name in `src_cols` to its data type,
as it appears in the `README.txt` file.
src_col_widths : dict
A dictionary mapping each column name in `src_cols` to its column
width as it appears in the `README.txt` file.
Returns
-------
df
A Pandas dataframe containing the stock price information from the DAT
file in `pth` This dataframe must meet the following criteria:

- df.index: DatetimeIndex with dates, matching the dates contained in
the DAT file. The labels in the index must be datetime objects.
- df.columns: each column label will be a column in `src_cols`, with the
exception of 'Date'. The order of the column labels in this index
must match the order specified in the README.txt file
- Each series inside this dataframe (must correspond to a column in
the README.txt file (with the exception of 'Date'). The datatype of
each series must match the data type specified in the README.txt
file.
"""
#
# ----------------------------------------------------------------------------
# Function mk_prc_df
# ----------------------------------------------------------------------------
def mk_prc_df(
tickers,
srcdir,
src_cols,
src_col_dtypes,
src_col_widths,
):
""" This function creates a dataframe from the information found in
a DAT file located at `pth`
Parameters
----------
tickers : list
List of tickers in the order they appear in the TICKERS.txt file
srcdir : str
Directory containing the source files:
- _prc.dat for each in TICKERS.txt
- _rec.csv for each in TICKERS.txt
src_cols : list
List containing the column names in the order they appear in each
source DAT file. The order of columns must match the order specified
in the README.txt file
src_col_dtypes : dict
A dictionary mapping each column name in `src_cols` to its data type,
as it appears in the `README.txt` file.
src_col_widths : dict
A dictionary mapping each column name in `src_cols` to its column
width as it appears in the `README.txt` file.
Returns
-------
df
A Pandas dataframe containing the adjusted closing price for each
stock identified in the TICKERS.txt file. This dataframe must match
the following criteria:

- df.index: DatetimeIndex with dates.
- df.columns: each column label will contain the ticker code
(in lower case). The number of columns in this dataframe must correspond
to the number of tickers in the `TICKERS.txt` file above. The order
of the columns must match the order of the tickers in `TICKERS.txt`.
- The data inside each column (i.e. series) will contain the closing
prices included in each DAT file (the Adj Close column). All valid
closing prices (for tickers in TICKERS.txt) must be included in this
dataframe. If the closing price for a ticker is not available in the
DAT file, it will take a NaN value.
Notes
-----
- This function will call the `dat_to_df` function for each ticker
- The output of this function is a dataframe that looks like this (the
contents of the df below are for illustration purposes only and will
**not** necessarily represent the actual contents of the dataframe you
create):
aapl ... tsla
Date
1980-12-12 0.101261 ... NaN
... ... ...
2020-10-05 116.500000 ... 425.679993
2020-10-06 113.160004 ... 413.980011
2020-10-07 115.080002 ... 425.299988
2020-10-08 114.970001 ... 425.920013
2020-10-09 116.970001 ... 434.000000
"""
#
# ----------------------------------------------------------------------------
# Function mk_aret_df
# ----------------------------------------------------------------------------
def mk_aret_df(prc_df):
""" Creates a dataframe with abnormal returns given the price information
contained in the `prc_df`
Parameters
----------
prc_df : dataframe
Dataframe produced by the function `mk_prc_df` above
Returns
-------
dataframe
Dataframe with abnormal returns for each ticker. Abnormal returns are
computed by subtracting the market return from that stock's returns.
- df.index: DatetimeIndex with dates, matching the dates contained in
the DAT file. The labels in the index must be datetime objects.
- df.columns: each column label will be a column in `src_cols`, with the
exception of 'Date'. The order of the column labels in this index
must match the order specified in the README.txt file
- Each series inside this dataframe contains the abnormal return for
each ticker in TICKERS.txt.
Notes
-----
The output of this function is a dataframe that looks like this (the
contents of the df below are for illustration purposes only and will
**not** necessarily represent the actual contents of the dataframe you
create):
aapl tsla
Date
1980-12-12 NaN NaN
1980-12-15 -0.052684 NaN
1980-12-16 -0.079905 NaN
1980-12-17 0.010143 NaN
1980-12-18 0.025475 NaN
... ... ...
2020-08-25 -0.011804 0.000938
... ... ...
"""
# --------------------------------------------------------
# Create returns
# ret_df must be similar to this:
#
# aapl tsla
# Date
# 1980-12-12 NaN NaN
# 1980-12-15 -0.052174 NaN
# 1980-12-16 -0.073395 NaN
# 1980-12-17 0.024753 NaN
# 1980-12-18 0.028985 NaN
# ... ... ...
# 2020-10-12 0.063521 0.019124
# ... ... ...
# --------------------------------------------------------
ret_df = ''
# --------------------------------------------------------
# Load FF mkt rets (do not change this part)
# --------------------------------------------------------
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here