Certainly! Here's a more detailed suggestion for a better alternative to address the "Ignores intra-horizon moves" issue:
Instead of evaluating only the final predicted price at time step T, you should evaluate the entire predicted trajectory over the horizon T. This ensures that the model is correctly capturing the dynamics of the price movement throughout the entire period, not just at the endpoint.
-
Define the Target Labels for the Entire Horizon:
For each sample in your dataset, calculate the target label based on the entire trajectory from time 0 to time T.
- Label 1 (Up): If the price touches
Start Price + N before it touches Start Price - N within the horizon T.
- Label -1 (Down): If the price touches
Start Price - N before it touches Start Price + N within the horizon T.
- Label 0 (Flat): If neither barrier is touched within the horizon T.
Here's a function to compute these labels:
def compute_target_labels(targets, threshold, start_prices):
"""
Compute target labels based on the entire trajectory.
Args:
targets: Array of target prices (samples, prediction_horizon)
threshold: Threshold value N
start_prices: Array of starting prices for each sample
Returns:
Array of target labels (samples,)
"""
targets_diff = targets - start_prices[:, None] # Compute price changes
up_barrier = threshold
down_barrier = -threshold
# Check if the up or down barrier is hit first within the horizon
up_hit = jnp.any(targets_diff >= up_barrier, axis=1)
down_hit = jnp.any(targets_diff <= down_barrier, axis=1)
# Determine the label based on which barrier is hit first
labels = jnp.where(up_hit & ~down_hit, 1,
jnp.where(down_hit & ~up_hit, -1, 0))
return labels
-
Define the Prediction Labels for the Entire Horizon:
Apply the same logic to the predicted trajectory to determine the prediction labels.
def compute_prediction_labels(preds, threshold, start_prices):
"""
Compute prediction labels based on the entire trajectory.
Args:
preds: Array of predicted prices (samples, prediction_horizon)
threshold: Threshold value N
start_prices: Array of starting prices for each sample
Returns:
Array of prediction labels (samples,)
"""
preds_diff = preds - start_prices[:, None] # Compute price changes
up_barrier = threshold
down_barrier = -threshold
# Check if the up or down barrier is hit first within the horizon
up_hit = jnp.any(preds_diff >= up_barrier, axis=1)
down_hit = jnp.any(preds_diff <= down_barrier, axis=1)
# Determine the label based on which barrier is hit first
labels = jnp.where(up_hit & ~down_hit, 1,
jnp.where(down_hit & ~up_hit, -1, 0))
return labels
-
Evaluate the Model:
Compare the predicted labels with the target labels to compute metrics such as accuracy, precision, recall, and F1 score.
def evaluate_directional_metrics(preds, targets, thresholds=None, start_prices=None):
"""
Evaluate directional accuracy and macro-F1 at multiple thresholds.
Args:
preds: Predictions array (samples, prediction_horizon)
targets: Targets array (samples, prediction_horizon)
thresholds: Optional sequence of thresholds
start_prices: Array of starting prices for each sample
Returns:
Dictionary of metrics for each threshold
"""
if thresholds is None:
thresholds = [20, 40, 60, 80, 100]
metrics = {}
for threshold in thresholds:
# Compute target and prediction labels
target_labels = compute_target_labels(targets, threshold, start_prices)
prediction_labels = compute_prediction_labels(preds, threshold, start_prices)
# Calculate metrics
accuracy = jnp.mean(prediction_labels == target_labels)
# Calculate per-class metrics
classes = jnp.array([-1, 0, 1])
def class_metrics(c):
tp = jnp.sum((prediction_labels == c) & (target_labels == c))
fp = jnp.sum((prediction_labels == c) & (target_labels != c))
fn = jnp.sum((prediction_labels != c) & (target_labels == c))
tn = jnp.sum((prediction_labels != c) & (target_labels != c))
precision = safe_divide(tp, tp + fp)
recall = safe_divide(tp, tp + fn)
f1 = safe_divide(2 * precision * recall, precision + recall)
return precision, recall, f1
precision, recall, f1s = jax.vmap(class_metrics)(classes)
macro_f1 = jnp.mean(f1s)
metrics[threshold] = {
'accuracy': accuracy,
'precision': precision,
'recall': recall,
'macro_f1': macro_f1
}
return metrics
This approach ensures that the model is evaluated based on its ability to predict the occurrence of a move of size N within the entire horizon T, rather than just at the final time step. This more accurately reflects the goal of predicting significant price movements over a specified time period.