| 517 | return preds |
| 518 | |
| 519 | def predict(self, df, x_timestamp, y_timestamp, pred_len, T=1.0, top_k=0, top_p=0.9, sample_count=1, verbose=True): |
| 520 | |
| 521 | if not isinstance(df, pd.DataFrame): |
| 522 | raise ValueError("Input must be a pandas DataFrame.") |
| 523 | |
| 524 | if not all(col in df.columns for col in self.price_cols): |
| 525 | raise ValueError(f"Price columns {self.price_cols} not found in DataFrame.") |
| 526 | |
| 527 | df = df.copy() |
| 528 | if self.vol_col not in df.columns: |
| 529 | df[self.vol_col] = 0.0 # Fill missing volume with zeros |
| 530 | df[self.amt_vol] = 0.0 # Fill missing amount with zeros |
| 531 | if self.amt_vol not in df.columns and self.vol_col in df.columns: |
| 532 | df[self.amt_vol] = df[self.vol_col] * df[self.price_cols].mean(axis=1) |
| 533 | |
| 534 | if df[self.price_cols + [self.vol_col, self.amt_vol]].isnull().values.any(): |
| 535 | raise ValueError("Input DataFrame contains NaN values in price or volume columns.") |
| 536 | |
| 537 | x_time_df = calc_time_stamps(x_timestamp) |
| 538 | y_time_df = calc_time_stamps(y_timestamp) |
| 539 | |
| 540 | x = df[self.price_cols + [self.vol_col, self.amt_vol]].values.astype(np.float32) |
| 541 | x_stamp = x_time_df.values.astype(np.float32) |
| 542 | y_stamp = y_time_df.values.astype(np.float32) |
| 543 | |
| 544 | x_mean, x_std = np.mean(x, axis=0), np.std(x, axis=0) |
| 545 | |
| 546 | x = (x - x_mean) / (x_std + 1e-5) |
| 547 | x = np.clip(x, -self.clip, self.clip) |
| 548 | |
| 549 | x = x[np.newaxis, :] |
| 550 | x_stamp = x_stamp[np.newaxis, :] |
| 551 | y_stamp = y_stamp[np.newaxis, :] |
| 552 | |
| 553 | preds = self.generate(x, x_stamp, y_stamp, pred_len, T, top_k, top_p, sample_count, verbose) |
| 554 | |
| 555 | preds = preds.squeeze(0) |
| 556 | preds = preds * (x_std + 1e-5) + x_mean |
| 557 | |
| 558 | pred_df = pd.DataFrame(preds, columns=self.price_cols + [self.vol_col, self.amt_vol], index=y_timestamp) |
| 559 | return pred_df |
| 560 | |
| 561 | |
| 562 | def predict_batch(self, df_list, x_timestamp_list, y_timestamp_list, pred_len, T=1.0, top_k=0, top_p=0.9, sample_count=1, verbose=True): |