--- tags: - time-series-forecasting - stock-prediction - nse - card - transformer license: apache-2.0 --- # CARD NSE Stock Price Predictor **Based on:** [CARD: Channel Aligned Robust Blend Transformer for Time Series Forecasting](https://arxiv.org/abs/2305.12095) (ICLR 2024) ## Model Description This model uses the CARD (Channel Aligned Robust Blend) Transformer architecture to predict NSE (National Stock Exchange of India) stock prices. Key innovations from the paper: - **Dual Attention**: Channel-aligned attention (cross-stock) + Temporal attention (over time) with EMA smoothing - **Token Blend**: Multi-scale token representations via head-merging - **Signal Decay Loss**: Weighted MSE that prioritizes near-future accuracy (l^{-1} weighting for stocks) - **RevIN**: Instance normalization for handling non-stationary financial data - **Dynamic Projection**: Efficient channel attention via softmax-normalized projection ## Architecture Details | Parameter | Value | |-----------|-------| | Lookback Window | 96 trading days | | Prediction Horizon | 5 trading days | | Hidden Dim (d_model) | 64 | | FFN Dim (d_ff) | 128 | | Attention Heads | 8 | | Encoder Layers | 2 | | Patch Length | 16 | | Stride | 8 | | Token Blend Size | 2 | | EMA Alpha | 0.9 | | Dynamic Projection Rank | 8 | | Dropout | 0.3 | ## Test Results | Metric | Value | |--------|-------| | MSE | 0.039362 | | MAE | 0.132980 | | RMSE | 0.198398 | ## Stocks Covered RELIANCE, TCS, HDFCBANK, INFY, ICICIBANK, HINDUNILVR, SBIN, BHARTIARTL, KOTAKBANK, ITC, LT, AXISBANK, BAJFINANCE, ASIANPAINT, MARUTI, HCLTECH, SUNPHARMA, TITAN, ULTRACEMCO, WIPRO ## Training Details - **Data**: NIFTY 50 stocks via yfinance (Close prices) - **Split**: 70% train / 15% validation / 15% test - **Optimizer**: Adam with cosine LR decay + linear warmup - **Loss**: Signal Decay Loss (l^{-1} weighting) - **Learning Rate**: 0.0001 - **Epochs**: 100 (with early stopping, patience=10) - **Batch Size**: 128 ## Usage ```python import torch, json from card_nse_predictor import CARD, CARDConfig # Load config with open("config.json") as f: cfg = CARDConfig(**json.load(f)) # Load model model = CARD(cfg) model.load_state_dict(torch.load("model.pt", map_location="cpu")) model.eval() # Input: (batch, n_stocks, lookback_days) — raw close prices # Output: (batch, n_stocks, pred_len) — predicted close prices x = torch.randn(1, 20, 96) pred = model(x) print(pred.shape) # (1, 20, 5) ``` ## Citation ```bibtex @inproceedings{xue2024card, title={CARD: Channel Aligned Robust Blend Transformer for Time Series Forecasting}, author={Xue, Wang and Zhou, Tian and Wen, Qingsong and Gao, Jinyang and Ding, Bolin and Jin, Rong}, booktitle={International Conference on Learning Representations}, year={2024} } ```