You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.3 KiB

4 weeks ago
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import sys
  4. sys.path.append("../")
  5. from model import Kronos, KronosTokenizer, KronosPredictor
  6. def plot_prediction(kline_df, pred_df):
  7. pred_df.index = kline_df.index[-pred_df.shape[0]:]
  8. sr_close = kline_df['close']
  9. sr_pred_close = pred_df['close']
  10. sr_close.name = 'Ground Truth'
  11. sr_pred_close.name = "Prediction"
  12. sr_volume = kline_df['volume']
  13. sr_pred_volume = pred_df['volume']
  14. sr_volume.name = 'Ground Truth'
  15. sr_pred_volume.name = "Prediction"
  16. close_df = pd.concat([sr_close, sr_pred_close], axis=1)
  17. volume_df = pd.concat([sr_volume, sr_pred_volume], axis=1)
  18. fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6), sharex=True)
  19. ax1.plot(close_df['Ground Truth'], label='Ground Truth', color='blue', linewidth=1.5)
  20. ax1.plot(close_df['Prediction'], label='Prediction', color='red', linewidth=1.5)
  21. ax1.set_ylabel('Close Price', fontsize=14)
  22. ax1.legend(loc='lower left', fontsize=12)
  23. ax1.grid(True)
  24. ax2.plot(volume_df['Ground Truth'], label='Ground Truth', color='blue', linewidth=1.5)
  25. ax2.plot(volume_df['Prediction'], label='Prediction', color='red', linewidth=1.5)
  26. ax2.set_ylabel('Volume', fontsize=14)
  27. ax2.legend(loc='upper left', fontsize=12)
  28. ax2.grid(True)
  29. plt.tight_layout()
  30. plt.show()
  31. # 1. Load Model and Tokenizer
  32. tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base")
  33. model = Kronos.from_pretrained("NeoQuasar/Kronos-small")
  34. # 2. Instantiate Predictor
  35. predictor = KronosPredictor(model, tokenizer, device="cuda:0", max_context=512)
  36. # 3. Prepare Data
  37. df = pd.read_csv("./data/XSHG_5min_600977.csv")
  38. df['timestamps'] = pd.to_datetime(df['timestamps'])
  39. lookback = 400
  40. pred_len = 120
  41. x_df = df.loc[:lookback-1, ['open', 'high', 'low', 'close', 'volume', 'amount']]
  42. x_timestamp = df.loc[:lookback-1, 'timestamps']
  43. y_timestamp = df.loc[lookback:lookback+pred_len-1, 'timestamps']
  44. # 4. Make Prediction
  45. pred_df = predictor.predict(
  46. df=x_df,
  47. x_timestamp=x_timestamp,
  48. y_timestamp=y_timestamp,
  49. pred_len=pred_len,
  50. T=1.0,
  51. top_p=0.9,
  52. sample_count=1,
  53. verbose=True
  54. )
  55. # 5. Visualize Results
  56. print("Forecasted Data Head:")
  57. print(pred_df.head())
  58. # Combine historical and forecasted data for plotting
  59. kline_df = df.loc[:lookback+pred_len-1]
  60. # visualize
  61. plot_prediction(kline_df, pred_df)