Walk Forward Validation
Yeshwanth Nagaraj
Democratizing Math and Core AI // Levelling playfield for the future
Walk Forward Validation (WFV) is a time-series cross-validation technique used to assess the performance of predictive models. It is particularly useful for time-ordered data where temporal sequence matters, such as stock prices, weather data, or sales figures. WFV is designed to be more realistic in evaluating how well a model will generalize to future, unseen data.
How It Works:
Advantages:
Disadvantages:
Real-World Analogy:
Imagine you're practicing archery, and you want to evaluate your performance. Instead of shooting all arrows at once and then checking how many hit the target, you shoot one arrow, evaluate, adjust your aim, and then shoot the next. This way, you're continually adapting and getting a more realistic assessment of your skills.
领英推荐
Mathematics of Walk Forward Validation
The mathematics behind Walk Forward Validation (WFV) is relatively straightforward. Let's assume you have a time-series dataset D with N observations:
D={(x1,y1),(x2,y2),…,(xN,yN)}
Here, x_{i} represents the feature vector for the i th observation, and y_{i} is the corresponding target value.
The performance metric (e.g., RMSE, MAE) is calculated for each test window and then averaged to get the overall performance of the model.
Python Code Example
Here's a simple Python code example using scikit-learn's Linear Regression model on synthetic time-series data:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Generate synthetic time-series data
N = 100
X = np.linspace(0, 10, N).reshape(-1, 1)
y = 3 * X.squeeze() + np.random.randn(N) * 2
# Initial training window size and test window size
W, T = 20, 5
# Initialize variables to store performance metrics
rmse_list = []
# Walk Forward Validation
for i in range(0, N - W, T):
train_X, train_y = X[i:i+W], y[i:i+W]
test_X, test_y = X[i+W:i+W+T], y[i+W:i+W+T]
# Train model
model = LinearRegression()
model.fit(train_X, train_y)
# Test model
predictions = model.predict(test_X)
rmse = np.sqrt(mean_squared_error(test_y, predictions))
rmse_list.append(rmse)
print(f"Test window {i+W}-{i+W+T}: RMSE = {rmse}")
# Overall performance
print(f"Average RMSE: {np.mean(rmse_list)}")
In this example: