Hello, while running the below code, I have got the following error “TypeError: float() argument must be a string or a real number, not ‘Period’”
→ I tried to change the index with pd.DatetimeIndex or to_timestamp but was unsuccessful.
would appreciate any idea
thanks in advance !
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import pandas_datareader as pdr
import statsmodels.api as sm
from matplotlib.pyplot import plot, scatter, show, xlabel, ylabel
import yfinance as yf
from statsmodels.tsa.ar_model import AutoReg
from datetime import datetime
from datetime import timedelta
start = ‘2021-01-01’
end = “2024-03-22”
stock1 = yf.Ticker(‘AAPL’)
stock1_data = stock1.history(interval=‘1d’, start= start, end= end)
stock2 = yf.Ticker(‘AMZN’)
stock2_data = stock2.history(interval=‘1d’, start= start, end= end)
X = np.log(stock1_data[“Close”])
Y = np.log(stock2_data[“Close”])
X = sm.add_constant(X)
model = sm.OLS(Y,X)
results = model.fit()
alpha = results.params.values[0]
beta = results.params.values[1]
errors = Y - (alpha + X[“Close”]*beta)
errors.index = pd.DatetimeIndex(errors.index).to_period(‘D’)
train_end = datetime(2023,4,1)
test_end = datetime(2024,4,1)
train_data = errors[:train_end]
test_data = errors[train_end+timedelta(days=1):test_end]
model2 = AutoReg(train_data, lags=3)
model2_fit = model2.fit()
pred_start_date = test_data.index[0]
pred_end_date = test_data.index[-1]
predictions = model2_fit.predict(start=pred_start_date, end=pred_end_date)
plt.plot(predictions)