{ "cells": [ { "cell_type": "markdown", "id": "9ddfd4df", "metadata": {}, "source": [ "# Neuro-Symbolic AI Forecasting — Symbolic Seam\n", "**Research Prototype: Constraint-Aware Forecasting with Neural + Symbolic Integration**\n", "\n", "## Research Objective\n", "\n", "Demonstrate how symbolic constraints (knowledge rules) can reduce unrealistic forecasts from a neural model under market stress, while documenting the accuracy tradeoff.**Pipeline:** Data → Knowledge Graph → Neural Baseline (System 1) → Domain Constraints (System 2) → Symbolic Projection → Stress Test → Evaluation\n", "\n", "\n", "**Key Finding:** The symbolic layer achieves 95% violation reduction under stress but at the cost of slightly higher RMSE on normal data. This is a safety-first design, not an accuracy-first design." ] }, { "cell_type": "markdown", "id": "c4882cfc", "metadata": {}, "source": [ "## 1. Setup & GPU Config" ] }, { "cell_type": "code", "execution_count": null, "id": "2d14c2df", "metadata": { "execution": { "iopub.execute_input": "2026-07-03T20:14:02.426722Z", "iopub.status.busy": "2026-07-03T20:14:02.426341Z", "iopub.status.idle": "2026-07-03T20:14:12.193955Z", "shell.execute_reply": "2026-07-03T20:14:12.193310Z", "shell.execute_reply.started": "2026-07-03T20:14:02.426693Z" }, "trusted": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Device: cuda | GPUs available: 2\n" ] } ], "source": [ "%pip install yfinance cvxpy -q\n", "\n", "import torch, torch.nn as nn\n", "import numpy as np, pandas as pd\n", "import matplotlib.pyplot as plt\n", "import cvxpy as cp\n", "import warnings, os\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "# Use both GPUs if available (Kaggle T4 x2)\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "n_gpus = torch.cuda.device_count()\n", "print(f\"Device: {device} | GPUs available: {n_gpus}\")\n", "\n", "SEED = 42\n", "np.random.seed(SEED); torch.manual_seed(SEED)\n", "\n", "os.makedirs(\"data\", exist_ok=True)\n", "os.makedirs(\"models\", exist_ok=True)\n", "os.makedirs(\"results\", exist_ok=True)" ] }, { "cell_type": "markdown", "id": "68d552da", "metadata": {}, "source": [ "## 2. Data Exploration (Notebook 01)" ] }, { "cell_type": "code", "execution_count": 2, "id": "69738e6b", "metadata": { "execution": { "iopub.execute_input": "2026-07-03T20:14:18.467620Z", "iopub.status.busy": "2026-07-03T20:14:18.467026Z", "iopub.status.idle": "2026-07-03T20:14:19.898040Z", "shell.execute_reply": "2026-07-03T20:14:19.897301Z", "shell.execute_reply.started": "2026-07-03T20:14:18.467589Z" }, "trusted": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[*********************100%***********************] 1 of 1 completed" ] }, { "name": "stdout", "output_type": "stream", "text": [ "(1006, 5)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "data": { "text/html": [ "
| Price | \n", "Open | \n", "High | \n", "Low | \n", "Close | \n", "Volume | \n", "
|---|---|---|---|---|---|
| Ticker | \n", "AAPL | \n", "AAPL | \n", "AAPL | \n", "AAPL | \n", "AAPL | \n", "
| Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " |
| 2023-12-22 | \n", "192.995392 | \n", "193.222829 | \n", "190.810137 | \n", "191.433090 | \n", "37149600 | \n", "
| 2023-12-26 | \n", "191.442997 | \n", "191.719862 | \n", "190.671728 | \n", "190.889267 | \n", "28919300 | \n", "
| 2023-12-27 | \n", "190.335512 | \n", "191.334202 | \n", "188.951173 | \n", "190.988113 | \n", "48087700 | \n", "
| 2023-12-28 | \n", "191.967044 | \n", "192.481228 | \n", "191.007900 | \n", "191.413315 | \n", "34049900 | \n", "
| 2023-12-29 | \n", "191.729722 | \n", "192.224126 | \n", "189.584012 | \n", "190.375061 | \n", "42672100 | \n", "