Spaces:
Paused
Paused
Tonic commited on
Commit ·
9268db8
1
Parent(s): 5cb366f
simplify passing information to the model
Browse files
app.py
CHANGED
|
@@ -303,13 +303,8 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
| 303 |
# Use Close prices for prediction
|
| 304 |
prices = df['Close'].values
|
| 305 |
|
| 306 |
-
# Calculate returns for additional context
|
| 307 |
-
returns = np.diff(prices) / prices[:-1]
|
| 308 |
-
returns = np.insert(returns, 0, 0) # Add 0 for first day
|
| 309 |
-
|
| 310 |
# Normalize the data using MinMaxScaler
|
| 311 |
normalized_prices = scaler.fit_transform(prices.reshape(-1, 1)).flatten()
|
| 312 |
-
normalized_returns = scaler.fit_transform(returns.reshape(-1, 1)).flatten()
|
| 313 |
|
| 314 |
# Ensure we have enough data points and pad if necessary
|
| 315 |
min_data_points = 64 # Minimum required by Chronos
|
|
@@ -317,18 +312,12 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
| 317 |
# Pad the data with the last value
|
| 318 |
padding = np.full(min_data_points - len(normalized_prices), normalized_prices[-1])
|
| 319 |
normalized_prices = np.concatenate([padding, normalized_prices])
|
| 320 |
-
padding_returns = np.full(min_data_points - len(normalized_returns), normalized_returns[-1])
|
| 321 |
-
normalized_returns = np.concatenate([padding_returns, normalized_returns])
|
| 322 |
elif len(normalized_prices) > min_data_points:
|
| 323 |
# Take the most recent data points
|
| 324 |
normalized_prices = normalized_prices[-min_data_points:]
|
| 325 |
-
normalized_returns = normalized_returns[-min_data_points:]
|
| 326 |
-
|
| 327 |
-
# Combine price and returns data
|
| 328 |
-
combined_data = np.column_stack((normalized_prices, normalized_returns))
|
| 329 |
|
| 330 |
-
# Reshape for Chronos (batch_size=1, sequence_length, features=
|
| 331 |
-
context = torch.tensor(
|
| 332 |
|
| 333 |
# Make prediction with GPU acceleration
|
| 334 |
pipe = load_pipeline()
|
|
@@ -367,11 +356,20 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
| 367 |
if len(context_tensor.shape) != 3:
|
| 368 |
raise ValueError(f"Expected 3D tensor, got shape {context_tensor.shape}")
|
| 369 |
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
|
| 376 |
if quantiles is None or mean is None:
|
| 377 |
raise ValueError("Chronos returned empty prediction")
|
|
@@ -398,11 +396,20 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
| 398 |
# Convert context to the correct format
|
| 399 |
context_tensor = context.to(device=pipe.model.device, dtype=torch.float16)
|
| 400 |
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
if prediction is None:
|
| 408 |
raise ValueError("Chronos predict returned None")
|
|
|
|
| 303 |
# Use Close prices for prediction
|
| 304 |
prices = df['Close'].values
|
| 305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
# Normalize the data using MinMaxScaler
|
| 307 |
normalized_prices = scaler.fit_transform(prices.reshape(-1, 1)).flatten()
|
|
|
|
| 308 |
|
| 309 |
# Ensure we have enough data points and pad if necessary
|
| 310 |
min_data_points = 64 # Minimum required by Chronos
|
|
|
|
| 312 |
# Pad the data with the last value
|
| 313 |
padding = np.full(min_data_points - len(normalized_prices), normalized_prices[-1])
|
| 314 |
normalized_prices = np.concatenate([padding, normalized_prices])
|
|
|
|
|
|
|
| 315 |
elif len(normalized_prices) > min_data_points:
|
| 316 |
# Take the most recent data points
|
| 317 |
normalized_prices = normalized_prices[-min_data_points:]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
# Reshape for Chronos (batch_size=1, sequence_length, features=1)
|
| 320 |
+
context = torch.tensor(normalized_prices.reshape(1, -1, 1), dtype=torch.float32)
|
| 321 |
|
| 322 |
# Make prediction with GPU acceleration
|
| 323 |
pipe = load_pipeline()
|
|
|
|
| 356 |
if len(context_tensor.shape) != 3:
|
| 357 |
raise ValueError(f"Expected 3D tensor, got shape {context_tensor.shape}")
|
| 358 |
|
| 359 |
+
# Try with default configuration first
|
| 360 |
+
try:
|
| 361 |
+
quantiles, mean = pipe.predict_quantiles(
|
| 362 |
+
context=context_tensor,
|
| 363 |
+
prediction_length=actual_prediction_length
|
| 364 |
+
)
|
| 365 |
+
except Exception as e:
|
| 366 |
+
print(f"Default predict_quantiles failed: {str(e)}")
|
| 367 |
+
# Try with explicit quantile levels
|
| 368 |
+
quantiles, mean = pipe.predict_quantiles(
|
| 369 |
+
context=context_tensor,
|
| 370 |
+
prediction_length=actual_prediction_length,
|
| 371 |
+
quantile_levels=[0.1, 0.5, 0.9] # 10th, 50th, and 90th percentiles
|
| 372 |
+
)
|
| 373 |
|
| 374 |
if quantiles is None or mean is None:
|
| 375 |
raise ValueError("Chronos returned empty prediction")
|
|
|
|
| 396 |
# Convert context to the correct format
|
| 397 |
context_tensor = context.to(device=pipe.model.device, dtype=torch.float16)
|
| 398 |
|
| 399 |
+
# Try with default configuration first
|
| 400 |
+
try:
|
| 401 |
+
prediction = pipe.predict(
|
| 402 |
+
context=context_tensor,
|
| 403 |
+
prediction_length=actual_prediction_length
|
| 404 |
+
)
|
| 405 |
+
except Exception as e:
|
| 406 |
+
print(f"Default predict failed: {str(e)}")
|
| 407 |
+
# Try with explicit num_samples
|
| 408 |
+
prediction = pipe.predict(
|
| 409 |
+
context=context_tensor,
|
| 410 |
+
prediction_length=actual_prediction_length,
|
| 411 |
+
num_samples=100
|
| 412 |
+
)
|
| 413 |
|
| 414 |
if prediction is None:
|
| 415 |
raise ValueError("Chronos predict returned None")
|