python代写-F633
时间:2022-04-18
AcF633: Python Programming Final Individual Project
1.2: 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, 5s, 10s, 15s, 20s, 30s, 40s, 50s, 1 minute (1min), 2min,
3min, 4min, 5min, 6min, 7min, 8min, 9min, 10min, 15min, 20min and 30min.
The require outputs are 3 data frames RVdf, RVdf 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. (5 marks)
1.3: Use results in 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.
0 500 1000 1500
Sampling frequency (secs)
1.0
1.2
1.4
1.6
1.8
Av
er
ag
ed
R
V
(x
10
4 )
RV signature plot
0 500 1000 1500
Sampling frequency (secs)
0.8
1.0
1.2
1.4
1.6
Av
er
ag
ed
B
V
(x
10
4 )
BV signature plot
0 500 1000 1500
Sampling frequency (secs)
0.8
0.9
1.0
1.1
1.2
1.3
Av
er
ag
ed
T
RV
(x
10
4 )
TRV signature plot
(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 AAPL on each date in the sample. Print the test conclusion for each date.
Your jump test should be based on the test statistic zt =
Jt√
v̂ar(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). (5 marks)
Task 2: Return-Volatility Modelling and Forecasting (Σ = 25 marks)
Refer back to the csv data file ‘DowJones-Feb2022.csv’ that lists the con-
stituents of the Dow Jones Industrial Average (DJIA) index as of 9 February
2022 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 DJIA index
excluding stocks DOW and WBA. Import daily Adjusted Close (Adj Close)
prices for both stocks between 01/01/2010 and 31/12/2021 from Yahoo Fi-
nance. 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 Student’s
t errors for the log returns of each stock that minimizes AIC, with m, p, q ∈
3
AcF633: Python Programming Final Individual Project
{1, 2, 3} and o ∈ {0, 1}. Print the best-fitted AR(m)-GJR-GARCH(p, o, q)
output for each stock, and use it to test for the presence of ‘leverage effects’
(i.e. asymmetric responses of the conditional 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. (10 marks)
2.2: Write code to plot a 2-by-4 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 standard normal distribution, (iii) ACF of the standardized
residuals, and (iv) ACF of the squared standardized residuals.
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. (5 marks)
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
Date
4
2
0
2
4
6
AR(3)-GJR-GARCH(1,1,1) Standardized residuals-CRM
4 3 2 1 0 1 2 3 4
0.0
0.1
0.2
0.3
0.4
0.5
De
ns
ity
Distribution of standardized residuals
t(df=3.8)
0 5 10 15 20 25 30 35
1.00
0.75
0.50
0.25
0.00
0.25
0.50
0.75
1.00
ACF of standardized residuals
0 5 10 15 20 25 30 35
1.00
0.75
0.50
0.25
0.00
0.25
0.50
0.75
1.00
ACF of standardized residuals squared
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
Date
6
4
2
0
2
4
6
8
AR(3)-GJR-GARCH(1,1,1) Standardized residuals-INTC
4 3 2 1 0 1 2 3 4
0.0
0.1
0.2
0.3
0.4
0.5
De
ns
ity
Distribution of standardized residuals
t(df=4.8)
0 5 10 15 20 25 30 35
1.00
0.75
0.50
0.25
0.00
0.25
0.50
0.75
1.00
ACF of standardized residuals
0 5 10 15 20 25 30 35
1.00
0.75
0.50
0.25
0.00
0.25
0.50
0.75
1.00
ACF of standardized residuals squared
2.3: Using the remaining data from 01/01/2019 to 31/12/2021 as out-of-sample
data, write code to produce one-step analytic forecasts, together with 95%
confidence interval, for the returns and conditional volatility of each stock
using respective best-fitted AR(m)-GJR-GARCH(p, o, q) model found in Task
2.1. For each stock, the forecast output is a data frame with 4 columns f,
fl and fu, and volf corresponding to the one-step return forecasts, 95% CI
lower bounds and upper bounds for return forecasts, and one-step conditional
volatility forecasts. (5 marks)
2.4: Use results in Task 2.3, write code to plot a 2-by-2 subplot figure showing:
Row 1: (i) the one-step return forecasts against the true values during
the out-of-sample period for both stocks in your sample, plus the 95%
confidence interval of the return forecasts; and (ii) the one-step conditional
volatility forecasts.
Row 2: The same subplots for the second stock.
4
AcF633: Python Programming Final Individual Project
201
9-05
201
9-09
202
0-01
202
0-05
202
0-09
202
1-01
202
1-05
202
1-09
Date
15
10
5
0
5
10
15
20
25
AR(3)-GJR-GARCH(1,1,1) One-step return forecasts - CRM
Observed returns
Forecast returns
95% IC
201
9-05
201
9-09
202
0-01
202
0-05
202
0-09
202
1-01
202
1-05
202
1-09
Date
1
2
3
4
5
6
7
8
AR(3)-GJR-GARCH(1,1,1) One-step volatility forecasts - CRM
Forecast volatility
201
9-05
201
9-09
202
0-01
202
0-05
202
0-09
202
1-01
202
1-05
202
1-09
Date
20
15
10
5
0
5
10
15
AR(3)-GJR-GARCH(1,1,1) One-step return forecasts - INTC
Observed returns
Forecast returns
95% IC
201
9-05
201
9-09
202
0-01
202
0-05
202
0-09
202
1-01
202
1-05
202
1-09
Date
1
2
3
4
5
6
7
8
AR(3)-GJR-GARCH(1,1,1) One-step volatility forecasts - INTC
Forecast volatility
(5 marks)
Task 3: Trading Strategies (Σ = 30 marks)
3.1: You are interested in investing in two stocks in your sample. You have
an investment budget of $10,000 and you would like to split equally between
the two stocks. Consider a buy and hold strategy where you buy $5,000
worth of each stock on the first trading day of the out-of-sample period (i.e.
02/01/2019) using the ‘Adj Close’ price and hold them until the end of that
period (i.e. 31/12/2021), assuming, for simplicity, that you can buy and hold
a non-integer unit of shares of each stock. Write code to plot the value of your
investment in each stock and the total value of your investment in both stocks
during the period. Your figure should look similar to the following.
5
AcF633: Python Programming Final Individual Project
201
9-01
201
9-05
201
9-09
202
0-01
202
0-05
202
0-09
202
1-01
202
1-05
202
1-09
202
2-01
Date
4000
6000
8000
10000
12000
14000
16000
$
Buy-and-Hold Investment Value
CRM
INTC
Total
Evaluate the profitability of the buy-and-hold strategy by producing a BuyHoldProf
data frame of the following format:
Start Value End Value ($) Profit/Loss ($) Profit/Loss (%)
CRM 5000
INTC 5000
Total 10000
(10 marks)
3.2: Now instead of following a buy-and-hold strategy, you perform a more so-
phisticated strategy based on technical analysis. In particular, you use the one-
step return forecasts produced by the best-fitted AR(m)-GJR-GARCH(p, o, q)
model found in Task 2.3 to decide whether you should buy or sell stocks, as
detailed below
- On a particular trading day, if the return forecast of a stock for the next
day is positive (i.e. the stock price is forecast to increase on the next
day), you will purchase the stock at the end of that trading day at the
‘Adj Close’ price using all the remaining cash balance (if any) for the
stock. If there is no cash balance for the stock, you do nothing and carry
on your stock holdings to the next day.
- If the return forecast is negative (i.e. the stock price is forecast to decrease
on the next day), you will sell all your holdings of that stock (if any) for
cash. If your stock holdings are none, you do nothing and carry on the
cash balance for the stock to the next day.
- The value of your investment in each stock on a particular day equals the
value of your holdings of the stock (if any) plus the remaining cash balance
for that stock (if any) on that day.
6
AcF633: Python Programming Final Individual Project
Assume, for simplicity, that you keep the cash balances for the two stocks in
your sample separate so you only purchase a stock using the remaining cash
balance for that stock and not using the cash balance for the other stock. Also
assume that (1) there are no transaction costs or fees associated with buying
and selling stocks, (2) there is no interest earned on cash balances, and (3)
you can buy and sell a non-integer unit of shares of each stock.
Write code to plot the value of your investment in each stock and the total
value of your investment in both stocks during the out-of-sample period based
on the best-fitted AR(m)-GJR-GARCH(p, o, q) model found in Task 2.1. Your
figure should look similar to the following. Evaluate the profitability of the
GARCH-based investment strategy by producing GARCHProf data frame of the
same format as the BuyHoldProf in Task 3.1.
201
9-01
201
9-05
201
9-09
202
0-01
202
0-05
202
0-09
202
1-01
202
1-05
202
1-09
202
2-01
Date
5000
7500
10000
12500
15000
17500
20000
22500
$
AR-GJR-GARCH-based Investment Value
CRM
INTC
Total
(10 marks)
3.3: Now assume that there are fixed transaction costs (including commission
fees, exchange fess and bid-ask spread) of c = 0.1% for each buy and sell order,
regardless of the trading volumes. If you buy $V worth of shares at price p,
you only receive V/(p × (1 + c)) unit of shares instead of V/p as in the case
of no transaction costs. Similarly, if you sell A units of shares at price p, you
only receive $A×p× (1−c). Plot the value of your GARCH-based investment
in each stock and the total value of your investment in this case during the
out-of-sample period. Re-evaluate the profitability of your investment strategy
now by producing GARCHProfWithcost data frame of the same format as the
BuyHoldProf in Task 3.1. (10 marks)
7