Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Final Individual Project

AcF 633 - Python Programming for Data Analysis

Final Individual Project

24th March 2023 noon/12pm to 24th April 2023 noon/12pm (UK time)

This assignment contains one question worth 100 marks and constitutes 60% of the total marks for this course.

You are required to submit to Moodle a SINGLE .zip folder containing a single Jupyter Notebook .ipynb file OR a single Python script .py file, together with any     supporting .csv files (e.g. input data files - Do NOT include the ‘GOOG 202001.csv.gz’ data file) AND a signed coursework coversheet.  The name of this folder should be     your student ID or library card number (e.g. 12345678.zip, where 12345678 is your     student ID).

In your answer script, either Jupyter Notebook .ipynb file or Python .py file, you  do not have to retype the question for each task. However, you must clearly label  which task (e.g.  1.1, 1.2, etc) your subsequent code is related to, either by using a  markdown cell (for .ipynb file) or by using the comments (e.g.  #1 .1 or ‘‘‘1 .1’’’ for .py file).  Provide only ONE answer to each task.  If you have more than one  method to answer a task, choose one that you think is best and most efficient.  If multiple answers are provided for a task, only the first answer will be marked.

Your submission .zip folder MUST be submitted electronically via Moodle by the 24th April 2023 noon/12pm (UK time). Email submissions will NOT be con- sidered. If you have any issues with uploading and submitting your work to Moodle, please email Carole Holroyd at [email protected] BEFORE the deadline for assistance with your submission.

The following penalties will be applied to all coursework that is submitted after the specified submission date:

Up to 3 days late - deduction of 10 marks

Beyond 3 days late - no marks awarded

Good Luck!

Question 1:

Task 1: High-frequency Finance (Σ = 35 marks)

The data file ‘GOOG 202001.csv.gz’contains the tick-by-tick transaction data

for stock GOOG in January 2020, with the following information:

Fields

Definitions

DATE TIME M     SYM ROOT EX

SIZE

PRICE

NBO

NBB

NBOqty

NBBqty

BuySell

Date of transaction

Time of transaction (seconds since mid-night) Security symbol root

Exchange where the transaction was executed Transaction size

Transaction price

Ask price (National Best Offer)

Bid price (National Best Bid)

Ask size

Bid size

Buy/Sell indicator (1 for buys, -1 for sells)

Import the data file into Python and perform the following tasks:

1.1: Write code to perform the filtering steps below in the following order:

(15 marks)

F1: Remove entries with either transaction price, transaction size, ask price,

ask size, bid price or bid size ≤ 0

F2: Remove entries with bid-ask spread (i.e. ask price - bid price) ≤ 0

F3: Aggregate entries that are (a) executed at the same date time (i.e. same

‘DATE’ and ‘TIME M’), (b) executed on the same exchange, and (c) of the same buy/sell indicator, into a single transaction with the median transaction price, median ask price, median bid price, sum transaction size, sum ask size and sum bid size.

F4: Remove entries for which the bid-ask spread is more that 50 times the

median bid-ask spread on each day

F5: Remove entries with the transaction price that is either above the ask

price plus the bid-ask spread, or below the bid price minus the bid-ask spread

Create a data frame called summary of the following format that shows the number and proportion of entries removed by each of the above filtering steps. The proportions (in %) are calculated as the number of entries removed divided by the original number of entries (before any filtering).

F1

F2

F3

F4

F5

Number

Proportion

Here, F1, F2, F3, F4 and F5 are the columns corresponding to the above 5 filtering rules, and Number and Proportion are the row indices of the data frame.

1.2: Using the cleaned data from Task 1.1, write code to compute Realized Volatility (RV), Bipower Variation (BV) and Truncated Realized Volatility (TRV) measures (defined in the lectures) for each trading day in the sample using different sampling frequencies including 1 second (1s), 2s, 3s, 4s, 5s, 10s, 15s, 20s, 30s, 40s, 50s, 1 minute (1min), 2min, 3min, 4min, 5min, 6min, 7min, 8min, 9min, 10min, 15min, 20min and 30min.  The required outputs are 3 data frames RVdf, BVdf and TRVdf (for Realized Volatility, Bipower Variation and Truncated Realized Volatility respectively), each having columns being the above sampling frequencies and row index being the unique dates in the

sample.                                                                                                              (10 marks)

1.3: Use results in Task 1.2, write code to produce a 1-by-3 subplot figure that shows the‘volatility signature plot’for RV, BV and TRV. Scale (i.e. multiply)

the RVs, BVs and TRVs by 104  when making the plots.  Your figure should look similar to the following.

4

3

2

1

RV signature plot

0 500             1000            1500

Sampling frequency (secs)

2.5

2.0

1.5

1.0

BV signature plot

1500

2.0

1.5

1.0

0.5

TRV signature plot

0 500             1000            1500

Sampling frequency (secs)

(5 marks)

1.4: Using a 5min sampling frequency and a 5% significance level, write code to conduct a jump test to test whether or not there are jumps in the prices of

GOOG on each date in the sample.  Your jump test should be based on the

test statistic zt  = ^v一ar(Jt)(Jt), where Jt  = max(RVt BVt , 0) is an estimate of

the jump variation on day t. If there are no jumps on day t, zt ∼ N(0, 1) (see the lecture slides on high-frequency finance for more details). Store the output  in a data frame called jumpdf that has row indices being the unique dates in the sample and columns including‘RV’, ‘BV’,‘J’, ‘jump’, which respectively  capture the RV, BV, jump variation, and whether there are jumps (i.e. ‘jump’

is‘Yes’) or not (i.e. ‘jump’is‘No’) on each date.                                               (5 marks)

Task 2: Return-Volatility Modelling (Σ = 30 marks)

Refer back to the csv data file ‘SP100-Feb2023.csv’that lists the constituents of the S&P100 index as of 8 February 2023 that was investigated in the group projects 1 and 2. Import the data file into Python.

Using your student ID or library card number (e.g.  12345678) as a random seed, draw a random sample of 2 stocks (i.e. tickers) from the S&P100 index excluding stocks ABBV, AVGO, CHTR, DOW, GM, KHC, META, PYPL and TSLA.1 Import daily Adjusted Close (Adj Close) prices for both stocks between 01/01/2009 and 31/12/2022 from Yahoo Finance. Compute the log daily returns (in %) for both stocks and drop days with NaN returns. Perform the following tasks.

2.1: Using data between 01/01/2010 and 31/12/2018 as in-sample data, write

code to find the best-fitted  AR(m)-GJR-GARCH(p,o,q)  model with  Stu- dent’s t errors for the log returns of each stock that minimizes AIC, with m,q ∈ {0, 1, 2, 3}, p ∈ {1, 2, 3} and o ≤ p. Print the best-fitted AR(m)-GJR- GARCH(p,o,q) output for each stock and a statement similar to the following for your stock sample.

Best-fitted AR(m)-GJR-GARCH(p,o,q) model  for GILD: AR(3)-GJR-GARCH(1,1,1)

- AIC =  9545 .9989

Best-fitted AR(m)-GJR-GARCH(p,o,q) model  for GOOG: AR(3)-GJR-GARCH(2,2,2)

- AIC =  8661 .8138                                                                                                       (7 marks)

2.2: Use the best-fitted AR(m)-GJR-GARCH(p,o,q) model in Task 2.1 to test for the presence of‘leverage effects’ (i.e.  asymmetric responses of the condi- tional variance to the positive and negative shocks) in the return series of each

stock. Draw and print your test conclusion using a 5% significance level.          (5 marks)

2.3: Write code to plot a 2-by-5 subplot figure that includes the following diag-

nostics for the best-fitted AR(m)-GJR-GARCH(p,o,q) model found in Task 2.1:

Row 1: (i) Time series plot of the standardized residuals, (ii) histogram of the standardized residuals, fitted with a kernel density estimate and the density of a fitted Student t distribution, (iii) ACF of the standardized residuals, (iv) ACF of the squared standardized residuals, and (v) time

series of the fitted conditional volatility.

Row 2: The same subplots for the second stock.

Your figure should look similar to the following for your sample of stocks.

Comment on what you observe from the plots.                                                  (8 marks)

AR(3)-GJR-GARCH(1,1,1) Standardized residuals-GILD

7.5

5.0

2.5

0.0

2.5

5.0

7.5

10.0

Date

AR(3)-GJR-GARCH(2,2,2) Standardized residuals-GOOG

12.5

10.0

7.5

5.0

2.5

0.0

2.5

5.0

7.5

Date

Distribution of standardized residuals-GILD

0.5

0.4

0.3

0.2

0.1

0.0

Distribution of standardized residuals-GOOG

0.6

0.5

0.4

0.3

0.2

0.1

0.0

1.00

0.75

0.50

0.25

0.00

0.25

0.50

0.75

1.00

1.00

0.75

0.50

0.25

0.00

0.25

0.50

0.75

1.00

of standardized residuals-

10          15          20 25          30          35

ACF