<jupyter_start><jupyter_text>Regression Analysis: Seasonal Effects with Sklearn Linear RegressionIn this notebook, you will build a SKLearn linear regression model to predict Yen futures ("settle") returns with *lagged* Yen futures returns.<jupyter_code># Futures contract on the Yen-dollar exchange rate:
# This is the continuous chain of the futures contracts that are 1 month to expiration
yen_futures = pd.read_csv(
    Path("yen.csv"), index_col="Date", infer_datetime_format=True, parse_dates=True
)
yen_futures.head()
# Trim the dataset to begin on January 1st, 1990
yen_futures = yen_futures.loc["1990-01-01":, :]
yen_futures.head()<jupyter_output><empty_output><jupyter_text>Data Preparation  Returns<jupyter_code># 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"s
# YOUR CODE HERE!
yen_futures['Return'] = yen_futures.Settle.pct_change() * 100
yen_futures = yen_futures.replace(-np.inf, np.nan).dropna()
yen_futures = yen_futures.dropna()
yen_futures.Return.plot()
print(yen_futures.head())
yen_futures.tail()<jupyter_output>Open    High     Low    Last  Change  Settle    Volume  \
Date                                                                   
2014-02-18  9831.0  9865.0  9734.0  9775.0    42.0  9775.0  203495.0   
2014-02-19  9768.0  9825.0  9760.0  9773.0     2.0  9773.0  129508.0   
2014-02-20  9774.0  9837.0  9765.0  9775.0     2.0  9775.0  160202.0   
2014-02-21  9772.0  9776.0  9725.0  9758.0    20.0  9755.0  103091.0   
2014-02-24  9752.0  9789.0  9740.0  9757.0     2.0  9757.0   90654.0   

            Previous Day Open Interest    Return  
Date                                              
2014-02-18                    196924.0 -0.427829  
2014-02-19                    197197.0 -0.020460  
2014-02-20                    198280.0  0.020465  
2014-02-21                    202990.0 -0.204604  
2014-02-24                    203114.0  0.020502<jupyter_text>Lagged Returns<jupyter_code># Create a lagged return using the shift function
# YOUR CODE HERE!

yen_futures['Lagged_Return'] = yen_futures.Return.shift()
# Drop NaN values
yen_futures = yen_futures.dropna()
yen_futures.tail()
yen_futures.head()<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code># Create a train/test split for the data using 2018-2019 for testing and the rest for training
train = yen_futures[:'2017']
test = yen_futures['2018':]
# Create four dataframes:
# X_train (training set using just the independent variables), X_test (test set of of just the independent variables)
# Y_train (training set using just the "y" variable, i.e., "Futures Return"), Y_test (test set of just the "y" variable):
# YOUR CODE HERE!
X_train = train["Lagged_Return"].to_frame()
X_test = test["Lagged_Return"].to_frame()
y_train = train["Return"]
y_test = test["Return"]
X_train<jupyter_output><empty_output><jupyter_text>Linear Regression Model<jupyter_code># Create a Linear Regression model and fit it to the training data
from sklearn.linear_model import LinearRegression

# Fit a SKLearn linear regression using just the training set (X_train, Y_train):
# YOUR CODE HERE!
model = LinearRegression()
model.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Make predictions using the Testing DataNote: We want to evaluate the model using data that it has never seen before, in this case: X_test.<jupyter_code># Make a prediction of "y" values using just the test dataset
# YOUR CODE HERE!
predictions = model.predict(X_test)
predictions
# Assemble actual y data (Y_test) with predicted y data (from just above) into two columns in a dataframe:
# YOUR CODE HERE!
Results = y_test.to_frame()
Results["Predicted Return"] = predictions
Results.head(10)
# Plot the first 20 predictions vs the true values
# YOUR CODE HERE!
Results[:20].plot(subplots=True)<jupyter_output>C:\Users\chakravartiraghavan\anaconda3\envs\timeseries\lib\site-packages\pandas\plotting\_tools.py:307: MatplotlibDeprecationWarning: 
The rowNum attribute was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use ax.get_subplotspec().rowspan.start instead.
  layout[ax.rowNum, ax.colNum] = ax.get_visible()
C:\Users\chakravartiraghavan\anaconda3\envs\timeseries\lib\site-packages\pandas\plotting\_tools.py:307: MatplotlibDeprecationWarning: 
The colNum attribute was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use ax.get_subplotspec().colspan.start instead.
  layout[ax.rowNum, ax.colNum] = ax.get_visible()
C:\Users\chakravartiraghavan\anaconda3\envs\timeseries\lib\site-packages\pandas\plotting\_tools.py:313: MatplotlibDeprecationWarning: 
The rowNum attribute was deprecated in Matplotlib 3.2 and will be removed two minor releases later. Use ax.get_subplotspec().rowspan.start instead.
  if not layout[ax.rowNum + 1, ax.colNum]:
C:\Users[...]<jupyter_text>Out-of-Sample PerformanceEvaluate the model using "out-of-sample" data (X_test and y_test)<jupyter_code>from sklearn.metrics import mean_squared_error
# Calculate the mean_squared_error (MSE) on actual versus predicted test "y" 
# YOUR CODE HERE!
mse = mean_squared_error(
    Results["Return"],
    Results["Predicted Return"]
)

# Using that mean-squared-error, calculate the root-mean-squared error (RMSE):
# YOUR CODE HERE!
rmse = np.sqrt(mse)
print(f"Out-of-Sample Root Mean Squared Error (RMSE): {rmse}")<jupyter_output>Out-of-Sample Root Mean Squared Error (RMSE): 0.4154832784856737<jupyter_text>In-Sample PerformanceEvaluate the model using in-sample data (X_train and y_train)<jupyter_code># Construct a dataframe using just the "y" training data:
# YOUR CODE HERE!
in_sample_results = y_train.to_frame()

# Add a column of "in-sample" predictions to that dataframe:  
# YOUR CODE HERE!
in_sample_results["In-sample Predictions"] = model.predict(X_train)

# Calculate in-sample mean_squared_error (for comparison to out-of-sample)
# YOUR CODE HERE!
in_sample_mse = mean_squared_error(
    in_sample_results["Return"],
    in_sample_results["In-sample Predictions"]
)

# Calculate in-sample root mean_squared_error (for comparison to out-of-sample)
# YOUR CODE HERE!
in_sample_rmse = np.sqrt(in_sample_mse)
print(f"In-sample Root Mean Squared Error (RMSE): {in_sample_rmse}")<jupyter_output>In-sample Root Mean Squared Error (RMSE): 0.5963660785073426
{'fraction_non_alphanumeric': 0.0968541763520843, 'fraction_numerical': 0.0644661397799473, 'mean_word_length': 4.225910931174089, 'pattern_counts': {'":': 1, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '289956', 'license': 'ADSL', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2180, 'n_words': 611, 'path': 'regression_analysis.ipynb', 'repo_name': 'rchak007/time_series', 'size': '6453'}