<jupyter_start><jupyter_text>Coded by : Andi-Nov<jupyter_code>import numpy as np
import pandas as pd
from pathlib import Path
%matplotlib inline
yen_futures = pd.read_csv(
    Path("yen.csv"), index_col="Date", infer_datetime_format=True, parse_dates=True
)
yen_futures.head()
yen_futures = yen_futures.loc["1990-01-01":, :]
print(yen_futures.head())
yen_settle  = pd.DataFrame(yen_futures["Settle"])
yen_settle.plot()
# Using a Hodrick-Prescott Filter, decompose the Settle price into a trend and noise.
import statsmodels.api as sm
yen_settle_noise, yen_settle_trend = sm.tsa.filters.hpfilter(yen_settle)
yen_settle["noise"] = yen_settle_noise
yen_settle["trend"] = yen_settle_trend
yen_settle.head()
yen_settle_2015 = yen_settle.loc["2015-1-1":]
yen_settle_2015 = yen_settle_2015.drop("noise", axis=1)
yen_settle_2015.plot()
yen_settle["noise"].plot()
returns = (yen_futures[["Settle"]].pct_change() * 100)
returns = returns.replace(-np.inf, np.nan).dropna()
returns.tail()
from statsmodels.tsa.arima_model import ARMA
model = ARMA(returns.values, order=(2,1))
results = model.fit()
print(results.summary())
print(pd.DataFrame(results.forecast(steps=5)[0]).plot(title="5 Day Returns Forecast"))
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(yen_futures["Settle"], order=(5, 1, 2))
results = model.fit()
results.summary()
pd.DataFrame(results.forecast(steps=5)[0]).plot(title="Forecast Prediction")
from arch import arch_model
model = arch_model(returns, mean="Zero", vol="GARCH", p=2, q=1)
results = model.fit(disp="off")
results.summary()
last_day = returns.index.max().strftime('%Y-%m-%d')
print(last_day)
forecast_horizon = 5
forecasts = results.forecast(start=last_day, horizon=forecast_horizon)
forecasts
intermediate = np.sqrt(forecasts.variance.dropna() * 252)
intermediate # Only one date!
final = intermediate.dropna().T
print(final.head())
# Plot the forecast!
fig = final.plot(title="Volatility Forecast")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14926133469179828, 'fraction_numerical': 0.02547121752419766, 'mean_word_length': 7.576419213973799, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '30401', 'license': 'Apache-2.0', 'n_tokens_mistral': 789, 'n_tokens_neox': 744, 'n_words': 134, 'path': 'Python_Nub/ARMA_TimeSeries/TimeSeries_ARMA.ipynb', 'repo_name': 'Nub-Team/Noob_Computation', 'size': '1963'}