You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Return Forecasting: Read Historical Daily Yen Futures Data
In this notebook, you will load historical Dollar-Yen exchange rate futures data and apply time series analysis and modeling to determine whether there is any predictable behavior.
# Futures contract on the Yen-dollar exchange rate:# This is the continuous chain of the futures contracts that are 1 month to expirationyen_futures=pd.read_csv(
Path("yen.csv"), index_col="Date", infer_datetime_format=True, parse_dates=True
)
yen_futures.head()
Start by plotting the "Settle" price. Do you see any patterns, long-term and/or short?
# Plot just the "Settle" column from the dataframe:yen_futures.Settle.plot(figsize=(15,8), fontsize=15, colormap='cool', title="Yen Futures Settle Prices")
<matplotlib.axes._subplots.AxesSubplot at 0x7fb94a197610>
Decomposition Using a Hodrick-Prescott Filter
Using a Hodrick-Prescott Filter, decompose the Settle price into a trend and noise.
importstatsmodels.apiassm# Apply the Hodrick-Prescott Filter by decomposing the "Settle" price into two separate series:yen_futures_noise, yen_futures_trend=sm.tsa.filters.hpfilter(yen_futures["Settle"])
# Create a dataframe of just the settle price, and add columns for "noise" and "trend" series from above:data= {'Settle': yen_futures.Settle, 'Noise': yen_futures_noise, 'Trend': yen_futures_trend}
df=pd.DataFrame(data)
df.index
# Plot the Settle Price vs. the Trend for 2015 to the presentdf[['Settle', 'Trend']].loc['2015':].plot(fontsize=15, colormap='cool', figsize=(15,8), title="Trend vs Settle for 2015")
<matplotlib.axes._subplots.AxesSubplot at 0x7fb99213aa90>
# Plot the Settle Noiseyen_futures_noise.plot(figsize=(15,8), colormap='cool_r', title="Settle Price Noise", fontsize=15)
<matplotlib.axes._subplots.AxesSubplot at 0x7fb9482046d0>
Forecasting Returns using an ARMA Model
Using futures Settle Returns, estimate an ARMA model
ARMA: Create an ARMA model and fit it to the returns data. Note: Set the AR and MA ("p" and "q") parameters to p=2 and q=1: order=(2, 1).
Output the ARMA summary table and take note of the p-values of the lags. Based on the p-values, is the model a good fit (p < 0.05)?
Plot the 5-day forecast of the forecasted returns (the results forecast from ARMA model)
# Create a series using "Settle" price percentage returns, drop any nan"s, and check the results:# (Make sure to multiply the pct_change() results by 100)# In this case, you may have to replace inf, -inf values with np.nan"sreturns= (yen_futures[["Settle"]].pct_change() *100)
returns=returns.replace(-np.inf, np.nan).dropna()
returns.head()
importstatsmodels.apiassmfromstatsmodels.tsa.arima_modelimportARMA# Estimate and ARMA model using statsmodels (use order=(2, 1))model=ARMA(returns.values, order=(2,1))
# Fit the model and assign it to a variable called resultsresults=model.fit()
# Output model summary results:results=model.fit()
results.summary()
ARMA Model Results
Dep. Variable:
y
No. Observations:
7514
Model:
ARMA(2, 1)
Log Likelihood
-7894.071
Method:
css-mle
S.D. of innovations
0.692
Date:
Sun, 23 Aug 2020
AIC
15798.142
Time:
19:03:52
BIC
15832.765
Sample:
0
HQIC
15810.030
coef
std err
z
P>|z|
[0.025
0.975]
const
0.0063
0.008
0.804
0.422
-0.009
0.022
ar.L1.y
-0.3059
1.278
-0.239
0.811
-2.810
2.198
ar.L2.y
-0.0019
0.019
-0.099
0.921
-0.040
0.036
ma.L1.y
0.2944
1.278
0.230
0.818
-2.210
2.798
Roots
Real
Imaginary
Modulus
Frequency
AR.1
-3.3382
+0.0000j
3.3382
0.5000
AR.2
-157.3438
+0.0000j
157.3438
0.5000
MA.1
-3.3973
+0.0000j
3.3973
0.5000
# Plot the 5 Day Returns Forecastpd.DataFrame(results.forecast(steps=5)[0]).plot(figsize=(15,8), colormap='cool_r', fontsize=15, title="5 Day Returns Forecast")
<matplotlib.axes._subplots.AxesSubplot at 0x7fb948204190>
Using the raw Yen Settle Price, estimate an ARIMA model.
Set P=5, D=1, and Q=1 in the model (e.g., ARIMA(df, order=(5,1,1))
P= # of Auto-Regressive Lags, D= # of Differences (this is usually =1), Q= # of Moving Average Lags
Output the ARIMA summary table and take note of the p-values of the lags. Based on the p-values, is the model a good fit (p < 0.05)?
Construct a 5 day forecast for the Settle Price. What does the model forecast will happen to the Japanese Yen in the near term?
fromstatsmodels.tsa.arima_modelimportARIMA# Estimate and ARIMA Model:# Hint: ARIMA(df, order=(p, d, q))model=ARIMA(yen_futures.Settle.values, order=(5,1,1))
# Fit the modelresult=model.fit()
# Output model summary results:result.summary()
ARIMA Model Results
Dep. Variable:
D.y
No. Observations:
7514
Model:
ARIMA(5, 1, 1)
Log Likelihood
-41944.619
Method:
css-mle
S.D. of innovations
64.281
Date:
Sun, 23 Aug 2020
AIC
83905.238
Time:
19:03:54
BIC
83960.635
Sample:
1
HQIC
83924.259
coef
std err
z
P>|z|
[0.025
0.975]
const
0.3158
0.700
0.451
0.652
-1.056
1.688
ar.L1.D.y
0.2814
0.699
0.402
0.688
-1.090
1.652
ar.L2.D.y
0.0007
0.016
0.042
0.966
-0.030
0.032
ar.L3.D.y
-0.0127
0.012
-1.032
0.302
-0.037
0.011
ar.L4.D.y
-0.0137
0.015
-0.890
0.374
-0.044
0.016
ar.L5.D.y
-0.0012
0.018
-0.066
0.948
-0.036
0.034
ma.L1.D.y
-0.2964
0.699
-0.424
0.672
-1.667
1.074
Roots
Real
Imaginary
Modulus
Frequency
AR.1
1.8905
-1.3790j
2.3400
-0.1003
AR.2
1.8905
+1.3790j
2.3400
0.1003
AR.3
-2.2637
-3.0253j
3.7785
-0.3522
AR.4
-2.2637
+3.0253j
3.7785
0.3522
AR.5
-10.8643
-0.0000j
10.8643
-0.5000
MA.1
3.3740
+0.0000j
3.3740
0.0000
# Plot the 5 Day Price Forecastpd.DataFrame(result.forecast(steps=5)[0]).plot(figsize=(15,8), colormap='cool_r', fontsize=15, title="5 Day Returns Forecast")
<matplotlib.axes._subplots.AxesSubplot at 0x7fb927da7650>
Rather than predicting returns, let's forecast near-term volatility of Japanese Yen futures returns. Being able to accurately predict volatility will be extremely useful if we want to trade in derivatives or quantify our maximum loss.
Using futures Settle Returns, estimate an GARCH model
GARCH: Create an GARCH model and fit it to the returns data. Note: Set the parameters to p=2 and q=1: order=(2, 1).
Output the GARCH summary table and take note of the p-values of the lags. Based on the p-values, is the model a good fit (p < 0.05)?
Plot the 5-day forecast of the volatility.
fromarchimportarch_model
# Estimate a GARCH model:model=arch_model(returns, mean="Zero", vol="GARCH", p=2, q=1)
# Fit the modelresults=model.fit(disp="off")
# Summarize the model resultsresults.summary()
Zero Mean - GARCH Model Results
Dep. Variable:
Settle
R-squared:
0.000
Mean Model:
Zero Mean
Adj. R-squared:
0.000
Vol Model:
GARCH
Log-Likelihood:
-7461.93
Distribution:
Normal
AIC:
14931.9
Method:
Maximum Likelihood
BIC:
14959.6
No. Observations:
7514
Date:
Sun, Aug 23 2020
Df Residuals:
7510
Time:
19:03:55
Df Model:
4
Volatility Model
coef
std err
t
P>|t|
95.0% Conf. Int.
omega
4.2896e-03
2.057e-03
2.085
3.708e-02
[2.571e-04,8.322e-03]
alpha[1]
0.0381
1.282e-02
2.970
2.974e-03
[1.295e-02,6.321e-02]
alpha[2]
0.0000
1.703e-02
0.000
1.000
[-3.338e-02,3.338e-02]
beta[1]
0.9536
1.420e-02
67.135
0.000
[ 0.926, 0.981]
Covariance estimator: robust
# Find the last day of the datasetlast_day=returns.index.max().strftime('%Y-%m-%d')
last_day
'2019-10-15'
# Create a 5 day forecast of volatilityforecast_horizon=5# Start the forecast using the last_day calculated aboveforecasts=results.forecast(start=last_day, horizon=forecast_horizon)
forecasts
<arch.univariate.base.ARCHModelForecast at 0x7fb92a2d5050>
# Annualize the forecastintermediate=np.sqrt(forecasts.variance.dropna() *252)
intermediate.head()
<matplotlib.axes._subplots.AxesSubplot at 0x7fb92a428d50>
Conclusions
Based on your time series analysis, would you buy the yen now?
Is the risk of the yen expected to increase or decrease?
Based on the model evaluation, would you feel confident in using these models for trading?
Based on your time series analysis, would you buy the yen now?: Overall trend Yen/ USD is upward. Prices are increasing so i would buy Yen.
Is the risk of the yen expected to increase or decrease? - the volatility is increasing so yes the risk is increasing.
Based on the model evaluation, would you feel confident in using these models for trading? - ARMA model is not significant based on the (p > 0.05), so it doesn't allow us to do a good judgement call. ARIMA model (p > 0.05) - I would not use it for the estimations as well. GARCH model (p < 0.05) gives us more confidence to predict volatility but it does not allow to make a buy/sell call. I won't be confident in using these models at least in ARMA / ARIMA (p=2 and q=1 / p=5, d=1, q=1).