Spaces:
Paused
Paused
Tonic commited on
Commit ·
027a49e
1
Parent(s): fab022d
implement chronos with predict quantiles directly
Browse files
app.py
CHANGED
|
@@ -353,96 +353,30 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
| 353 |
if len(context_tensor.shape) != 3:
|
| 354 |
raise ValueError(f"Expected 3D tensor, got shape {context_tensor.shape}")
|
| 355 |
|
| 356 |
-
#
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
# Calculate mean and std
|
| 381 |
-
mean_pred = scaler.inverse_transform(prediction_np.reshape(-1, 1)).flatten()
|
| 382 |
-
std_pred = np.std(prediction_np) * (scaler.data_max_ - scaler.data_min_)
|
| 383 |
-
|
| 384 |
-
except Exception as e:
|
| 385 |
-
print(f"Base configuration failed: {str(e)}")
|
| 386 |
-
print("Falling back to predict_quantiles...")
|
| 387 |
-
|
| 388 |
-
# Try with predict_quantiles
|
| 389 |
-
try:
|
| 390 |
-
quantiles, mean = pipe.predict_quantiles(
|
| 391 |
-
context=context_tensor,
|
| 392 |
-
prediction_length=actual_prediction_length,
|
| 393 |
-
quantile_levels=[0.1, 0.5, 0.9]
|
| 394 |
-
)
|
| 395 |
-
|
| 396 |
-
if quantiles is None or mean is None:
|
| 397 |
-
raise ValueError("Chronos returned empty prediction")
|
| 398 |
-
|
| 399 |
-
print(f"Quantiles shape: {quantiles.shape}, Mean shape: {mean.shape}")
|
| 400 |
-
|
| 401 |
-
# Convert to numpy arrays
|
| 402 |
-
quantiles = quantiles.detach().cpu().numpy()
|
| 403 |
-
mean = mean.detach().cpu().numpy()
|
| 404 |
-
|
| 405 |
-
# Denormalize predictions
|
| 406 |
-
mean_pred = scaler.inverse_transform(mean.reshape(-1, 1)).flatten()
|
| 407 |
-
lower_bound = scaler.inverse_transform(quantiles[0, :, 0].reshape(-1, 1)).flatten()
|
| 408 |
-
upper_bound = scaler.inverse_transform(quantiles[0, :, 2].reshape(-1, 1)).flatten()
|
| 409 |
-
|
| 410 |
-
# Calculate standard deviation from quantiles
|
| 411 |
-
std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
|
| 412 |
-
|
| 413 |
-
except Exception as e:
|
| 414 |
-
print(f"predict_quantiles failed: {str(e)}")
|
| 415 |
-
print("Falling back to predict...")
|
| 416 |
-
|
| 417 |
-
# Final fallback to predict
|
| 418 |
-
prediction = pipe.predict(
|
| 419 |
-
context=context_tensor,
|
| 420 |
-
prediction_length=actual_prediction_length,
|
| 421 |
-
num_samples=100
|
| 422 |
-
)
|
| 423 |
-
|
| 424 |
-
if prediction is None:
|
| 425 |
-
raise ValueError("Chronos predict returned None")
|
| 426 |
-
|
| 427 |
-
prediction = prediction.detach().cpu().numpy()
|
| 428 |
-
|
| 429 |
-
if prediction.size == 0:
|
| 430 |
-
raise ValueError("Chronos predict returned empty array")
|
| 431 |
-
|
| 432 |
-
print(f"Prediction shape: {prediction.shape}")
|
| 433 |
-
|
| 434 |
-
# Calculate mean and std from samples
|
| 435 |
-
mean_pred = scaler.inverse_transform(prediction.mean(axis=0).reshape(-1, 1)).flatten()
|
| 436 |
-
std_pred = prediction.std(axis=0) * (scaler.data_max_ - scaler.data_min_)
|
| 437 |
|
| 438 |
-
# If we had to limit the prediction length, extend the prediction
|
| 439 |
-
if actual_prediction_length < prediction_days:
|
| 440 |
-
last_pred = mean_pred[-1]
|
| 441 |
-
last_std = std_pred[-1]
|
| 442 |
-
extension = np.array([last_pred * (1 + np.random.normal(0, last_std, prediction_days - actual_prediction_length))])
|
| 443 |
-
mean_pred = np.concatenate([mean_pred, extension])
|
| 444 |
-
std_pred = np.concatenate([std_pred, np.full(prediction_days - actual_prediction_length, last_std)])
|
| 445 |
-
|
| 446 |
except Exception as e:
|
| 447 |
print(f"Chronos prediction error: {str(e)}")
|
| 448 |
print(f"Error type: {type(e)}")
|
|
|
|
| 353 |
if len(context_tensor.shape) != 3:
|
| 354 |
raise ValueError(f"Expected 3D tensor, got shape {context_tensor.shape}")
|
| 355 |
|
| 356 |
+
# Use predict_quantiles directly
|
| 357 |
+
quantiles, mean = pipe.predict_quantiles(
|
| 358 |
+
context=context_tensor,
|
| 359 |
+
prediction_length=actual_prediction_length,
|
| 360 |
+
quantile_levels=[0.1, 0.5, 0.9]
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
if quantiles is None or mean is None:
|
| 364 |
+
raise ValueError("Chronos returned empty prediction")
|
| 365 |
+
|
| 366 |
+
print(f"Quantiles shape: {quantiles.shape}, Mean shape: {mean.shape}")
|
| 367 |
+
|
| 368 |
+
# Convert to numpy arrays
|
| 369 |
+
quantiles = quantiles.detach().cpu().numpy()
|
| 370 |
+
mean = mean.detach().cpu().numpy()
|
| 371 |
+
|
| 372 |
+
# Denormalize predictions
|
| 373 |
+
mean_pred = scaler.inverse_transform(mean.reshape(-1, 1)).flatten()
|
| 374 |
+
lower_bound = scaler.inverse_transform(quantiles[0, :, 0].reshape(-1, 1)).flatten()
|
| 375 |
+
upper_bound = scaler.inverse_transform(quantiles[0, :, 2].reshape(-1, 1)).flatten()
|
| 376 |
+
|
| 377 |
+
# Calculate standard deviation from quantiles
|
| 378 |
+
std_pred = (upper_bound - lower_bound) / (2 * 1.645) # 90% confidence interval
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
except Exception as e:
|
| 381 |
print(f"Chronos prediction error: {str(e)}")
|
| 382 |
print(f"Error type: {type(e)}")
|