{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "1023f2c1-e45f-4e1c-9a1b-66f59f128196",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Panda Version: 2.2.2\n",
      "Today date is:  2024-06-14\n"
     ]
    }
   ],
   "source": [
    "import sqlite3\n",
    "\n",
    "import os\n",
    "import re\n",
    "\n",
    "import numpy as np\n",
    "# It is apparently officially accepted to explicitly\n",
    "# list all the functions you need from numpy:\n",
    "from numpy import array, zeros, exp, random, dot\n",
    "from numpy import shape, reshape, meshgrid, linspace\n",
    "from numpy import hstack, vstack\n",
    "\n",
    "import pandas as pd\n",
    "print('Panda Version:', pd.__version__)\n",
    "\n",
    "# Set ipython's max row display\n",
    "pd.set_option('display.max_row', 100, 'display.max_columns', 25)\n",
    "\n",
    "import matplotlib.pyplot as plt # for plotting\n",
    "import matplotlib\n",
    "matplotlib.rcParams['figure.dpi'] = 100 # highres display\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow import Variable\n",
    "\n",
    "from tensorflow.keras import Sequential\n",
    "from tensorflow.keras import Model\n",
    "from tensorflow.keras.layers import Dense\n",
    "from tensorflow.keras.layers import Dropout\n",
    "from tensorflow.keras.layers import TimeDistributed, RepeatVector\n",
    "\n",
    "from keras.optimizers import SGD\n",
    "\n",
    "from keras.models import load_model\n",
    "from keras.callbacks import EarlyStopping\n",
    "from keras.callbacks import ModelCheckpoint\n",
    "\n",
    "from keras.layers import LSTM, Dense, Concatenate\n",
    "\n",
    "from keras.optimizers import SGD\n",
    "\n",
    "import collections\n",
    "from collections import Counter\n",
    "\n",
    "import seaborn as sns\n",
    "from statsmodels.graphics.tsaplots import plot_acf, plot_pacf\n",
    "\n",
    "# Import date class from datetime module\n",
    "import time\n",
    "import datetime\n",
    "# import datetime as dt\n",
    "from datetime import date\n",
    "print(\"Today date is: \", date.today())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "c09a37a6-f0d9-48e3-a1d1-65ddaf2c489c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/workspace/leo\n",
      "total 16400\n",
      "drwxrwxr-x 1 1000 1000     648 Jun 13 10:01 .\n",
      "drwxrwxr-x 1 1000 1000      18 Jun  3 23:40 ..\n",
      "-rw------- 1 1000 1000 1449984 Jun  4 00:49 20240601.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1445888 Jun  3 23:44 20240602.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1437696 Jun  4 16:45 20240603.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1269760 Jun  5 10:00 20240604.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1081344 Jun  6 10:00 20240605.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1441792 Jun  7 10:00 20240606.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1445888 Jun  8 10:00 20240607.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1449984 Jun  9 10:00 20240608.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1437696 Jun 10 10:00 20240609.mktdata.ohlcv.db\n",
      "-rw-r--r-- 1 1000 1000       0 Jun 12 15:29 20240609.mktdata.ohlcvdb\n",
      "-rw------- 1 1000 1000 1437696 Jun 11 10:00 20240610.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1449984 Jun 12 10:01 20240611.mktdata.ohlcv.db\n",
      "-rw------- 1 1000 1000 1445888 Jun 13 10:01 20240612.mktdata.ohlcv.db\n"
     ]
    }
   ],
   "source": [
    "!pwd\n",
    "\n",
    "!ls -la /workspace/data/crypto_md/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "8d72d849-e8df-4564-a006-03ab646b9330",
   "metadata": {},
   "outputs": [],
   "source": [
    "# db_conn = sqlite3.connect(mktdata_db_file)\n",
    "# tables_df = pd.read_sql_query(\"select * from sqlite_master where type = 'table'\", db_conn)\n",
    "# print (tables_df_20240601.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4df4b0d4-e92e-42a9-8747-ff60669a4e10",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "5d2aed0b-8c9d-4f5a-9166-785da4811390",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "66803\n"
     ]
    }
   ],
   "source": [
    "directory = \"/workspace/data/crypto_md\"\n",
    "\n",
    "# List of dates\n",
    "dates = ['20240601', \n",
    "         '20240602', \n",
    "         '20240603', \n",
    "         '20240604', \n",
    "         '20240605', \n",
    "         '20240606', \n",
    "         '20240607', \n",
    "         '20240608', \n",
    "         '20240609',\n",
    "         '20240610',\n",
    "         '20240611',\n",
    "         '20240612']        \n",
    "\n",
    "# Create an entirely empty DataFrame\n",
    "df_concat = pd.DataFrame()\n",
    "\n",
    "for date in dates:\n",
    "    # Construct the filename\n",
    "    filename = f\"{directory}/{date}.mktdata.ohlcv.db\"\n",
    "    \n",
    "    # Create the SQLite connection\n",
    "    conn = sqlite3.connect(filename)\n",
    "    \n",
    "    # Read the data into a DataFrame\n",
    "    df = pd.read_sql_query(\"select * from coinbase_ohlcv_1min\", conn)\n",
    "\n",
    "    df_concat = pd.concat([df_concat, df], axis = 0,ignore_index = True)\n",
    "    # Print the combined DataFrame\n",
    "    # print(df_concat.shape[0])\n",
    "    # print(df_concat.shape[1])\n",
    "    conn.close()\n",
    "print(df_concat.shape[0])\n",
    "# print(df_concat.head(2))\n",
    "# print(df_concat.tail(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "cd83027c-eaa1-44d9-98ad-8506099c323d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_concat['tstamp'] = pd.to_datetime(df_concat['tstamp'])\n",
    "# Extract individual components\n",
    "df_concat['year'] = df_concat['tstamp'].dt.year\n",
    "df_concat['month'] = df_concat['tstamp'].dt.month\n",
    "df_concat['day'] = df_concat['tstamp'].dt.day\n",
    "df_concat['hour'] = df_concat['tstamp'].dt.hour\n",
    "df_concat['minute'] = df_concat['tstamp'].dt.minute\n",
    "df_concat['second'] = df_concat['tstamp'].dt.second\n",
    "\n",
    "df_concat['date'] = df_concat['day'].astype(str) + '-' + df_concat['hour'].astype(str) + '-' + df_concat['minute'].astype(str)\n",
    "df_concat = df_concat.sort_values(by = ['day', 'hour', 'minute'])\n",
    "\n",
    "selected_columns = ['date', 'instrument_id', 'close', 'volume', 'vwap']\n",
    "df_concat = df_concat[selected_columns]\n",
    "\n",
    "# instrument_list = df_concat['instrument_id'].unique().tolist()\n",
    "# print(\"Distinct values in column 'instrument_id':\", instrument_list)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "316c0cfb-c73d-4dad-9d49-eb80daa229ec",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'df_concat' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# BTC\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m df_concat_btc \u001b[38;5;241m=\u001b[39m \u001b[43mdf_concat\u001b[49m[df_concat[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minstrument_id\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPAIR-BTC-USD\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[1;32m      4\u001b[0m \u001b[38;5;66;03m# Reset Index\u001b[39;00m\n\u001b[1;32m      5\u001b[0m df_concat_btc \u001b[38;5;241m=\u001b[39m df_concat_btc\u001b[38;5;241m.\u001b[39mreset_index(drop \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'df_concat' is not defined"
     ]
    }
   ],
   "source": [
    "# BTC\n",
    "df_concat_btc = df_concat[df_concat['instrument_id'] == 'PAIR-BTC-USD']\n",
    "\n",
    "# Reset Index\n",
    "df_concat_btc = df_concat_btc.reset_index(drop = True)\n",
    "\n",
    "# Rename Vars\n",
    "df_concat_btc['close_btc'] = df_concat_btc['close']/100000.00\n",
    "df_concat_btc['volume_btc'] = df_concat_btc['volume']\n",
    "df_concat_btc['vwap_btc'] = df_concat_btc['vwap']/100000.00\n",
    "\n",
    "df_concat_btc = df_concat_btc.drop('close', axis = 1)\n",
    "df_concat_btc = df_concat_btc.drop('volume', axis = 1)\n",
    "df_concat_btc = df_concat_btc.drop('vwap', axis = 1)\n",
    "df_concat_btc = df_concat_btc.drop('instrument_id', axis = 1)\n",
    "\n",
    "# print (df_concat_btc.head(3))\n",
    "\n",
    "# LTC\n",
    "df_concat_ltc = df_concat[df_concat['instrument_id'] == 'PAIR-LTC-USD']\n",
    "\n",
    "# Reset Index\n",
    "df_concat_ltc = df_concat_ltc.reset_index(drop = True)\n",
    "\n",
    "# Rename Vars\n",
    "df_concat_ltc['close_ltc'] = df_concat_ltc['close']/100.00\n",
    "df_concat_ltc['volume_ltc'] = df_concat_ltc['volume']\n",
    "df_concat_ltc['vwap_ltc'] = df_concat_ltc['vwap']/100.00\n",
    "\n",
    "df_concat_ltc = df_concat_ltc.drop('close', axis = 1)\n",
    "df_concat_ltc = df_concat_ltc.drop('volume', axis = 1)\n",
    "df_concat_ltc = df_concat_ltc.drop('vwap', axis = 1)\n",
    "df_concat_ltc = df_concat_ltc.drop('instrument_id', axis = 1)\n",
    "\n",
    "# print (df_concat_ltc.head(3))\n",
    "\n",
    "# ETH\n",
    "df_concat_eth = df_concat[df_concat['instrument_id'] == 'PAIR-ETH-USD']\n",
    "\n",
    "# Reset Index\n",
    "df_concat_eth = df_concat_eth.reset_index(drop = True)\n",
    "\n",
    "# Rename Vars\n",
    "df_concat_eth['close_eth'] = df_concat_eth['close']/10000.00\n",
    "df_concat_eth['volume_eth'] = df_concat_eth['volume']\n",
    "df_concat_eth['vwap_eth'] = df_concat_eth['vwap']/10000.00\n",
    "\n",
    "df_concat_eth = df_concat_eth.drop('close', axis = 1)\n",
    "df_concat_eth = df_concat_eth.drop('volume', axis = 1)\n",
    "df_concat_eth = df_concat_eth.drop('vwap', axis = 1)\n",
    "df_concat_eth = df_concat_eth.drop('instrument_id', axis = 1)\n",
    "\n",
    "# print (df_concat_eth.head(3))\n",
    "\n",
    "# XRP\n",
    "df_concat_xrp = df_concat[df_concat['instrument_id'] == 'PAIR-XRP-USD']\n",
    "\n",
    "# Reset Index\n",
    "df_concat_xrp = df_concat_xrp.reset_index(drop = True)\n",
    "\n",
    "# Rename Vars\n",
    "df_concat_xrp['close_xrp'] = df_concat_xrp['close']\n",
    "df_concat_xrp['volume_xrp'] = df_concat_xrp['volume']\n",
    "df_concat_xrp['vwap_xrp'] = df_concat_xrp['vwap']\n",
    "\n",
    "df_concat_xrp = df_concat_xrp.drop('close', axis = 1)\n",
    "df_concat_xrp = df_concat_xrp.drop('volume', axis = 1)\n",
    "df_concat_xrp = df_concat_xrp.drop('vwap', axis = 1)\n",
    "df_concat_xrp = df_concat_xrp.drop('instrument_id', axis = 1)\n",
    "\n",
    "# print (df_concat_xrp.head(3))\n",
    "\n",
    "df_M1 = pd.merge(df_concat_xrp, df_concat_eth, on = 'date', how = 'outer')\n",
    "# print (df_M1.head(3))\n",
    "# print (\"M1:  \", df_M1.shape[0])\n",
    "df_M2 = pd.merge(df_M1, df_concat_ltc, on = 'date', how = 'outer')\n",
    "# print (df_M2.head(3))\n",
    "# print (\"M2:  \", df_M2.shape[0])\n",
    "df_M3 = pd.merge(df_M2, df_concat_btc, on = 'date', how = 'outer')\n",
    "# print (df_M3.head(3))\n",
    "# print (\"M3:  \", df_M3.shape[0])\n",
    "\n",
    "# Drop rows with any NaN value and assign it to a new DataFrame\n",
    "# sample = df_M3.dropna(axis = 0).reset_index(drop = True)\n",
    "print (\"========================================\")\n",
    "print (sample.head(3))\n",
    "sample_size = sample.shape[0]\n",
    "print (\"test sample size: \", sample_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b193e3d7-7907-4137-a63f-4695f76aae21",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'sample' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msample.shape[0] shape:  \u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[43msample\u001b[49m\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m0\u001b[39m])\n",
      "\u001b[0;31mNameError\u001b[0m: name 'sample' is not defined"
     ]
    }
   ],
   "source": [
    "print ('sample.shape[0] shape:  ', sample.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "982cc6d4-28f7-4338-b51d-17666c90541c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "92700c76-8eac-4ebb-86d3-27066486c437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44936 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Create a figure and axis object\n",
    "fig, ax = plt.subplots(2 , 1)\n",
    "\n",
    "# Plot the data\n",
    "ax[0].plot(df_concat_btc['date'], df_concat_btc['close_btc'], 'blue')\n",
    "# ax[0].plot(df_concat_btc['date'], df_concat_btc['vwap_btc'], 'green')\n",
    "\n",
    "# Customize the plot\n",
    "ax[0].set_title('PAIR-BTC-USDT')\n",
    "ax[0].set_xlabel('date')\n",
    "ax[0].set_ylabel('close')\n",
    "\n",
    "ax[1].plot(df_concat_btc['date'], df_concat_btc['vwap_btc'], 'green')\n",
    "# ax[1].plot(df_concat_btc['date'], df_concat_btc['volume_BTC'], 'red')\n",
    "\n",
    "# Show the plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "3c6d80b3-4a46-432b-9b88-6a264f9b7a7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "First part of the DataFrame:\n",
      "          date  close_xrp    volume_xrp  vwap_xrp  close_eth  volume_eth  \\\n",
      "13356  7-23-25     0.4995   8673.755143  0.499557   0.368412   53.830657   \n",
      "13357  7-23-26     0.4996  34403.036949  0.499605   0.368370   89.163024   \n",
      "\n",
      "       vwap_eth  close_ltc  volume_ltc  vwap_ltc  close_btc  volume_btc  \\\n",
      "13356  0.368548     0.8003   74.882298  0.800537    0.69375    1.598973   \n",
      "13357  0.368411     0.8002   30.666566  0.800233    0.69381    1.612419   \n",
      "\n",
      "       vwap_btc  \n",
      "13356  0.693999  \n",
      "13357  0.693838  \n",
      "\n",
      "Second part of the DataFrame:\n",
      "          date  close_xrp    volume_xrp  vwap_xrp  close_eth  volume_eth  \\\n",
      "13358  7-23-27     0.4995   8240.166990  0.499561   0.368369  108.118487   \n",
      "13359  7-23-28     0.4995  21256.045405  0.499585   0.367813  229.373936   \n",
      "\n",
      "       vwap_eth  close_ltc  volume_ltc  vwap_ltc  close_btc  volume_btc  \\\n",
      "13358  0.368353     0.8002   43.931145  0.800134   0.693709    1.765308   \n",
      "13359  0.368127     0.8002   22.408370  0.800188   0.693200    2.643365   \n",
      "\n",
      "       vwap_btc  \n",
      "13358  0.693748  \n",
      "13359  0.693508  \n"
     ]
    }
   ],
   "source": [
    "split_index = round(sample.shape[0] * 0.8)\n",
    "\n",
    "# First part of the DataFrame\n",
    "sample_test = sample.iloc[:split_index]\n",
    "\n",
    "# Second part of the DataFrame\n",
    "sample_valid = sample.iloc[split_index:]\n",
    "\n",
    "print(\"\\nFirst part of the DataFrame:\")\n",
    "print (sample_test.tail(2))\n",
    "\n",
    "print(\"\\nSecond part of the DataFrame:\")\n",
    "print (sample_valid.head(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4edcdbdb-85fb-415e-a6c4-71cc383c4410",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "id": "c3dee439-a3e8-4843-8f8b-33572b4170e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sample_size:  13358\n",
      "[1108]\n",
      "[[5.18409007e-01 1.74656457e-04 3.76978106e-01 1.87974873e-04\n",
      "  8.30976720e-01 4.13691462e-04 6.76322893e-01 1.69435655e-04]\n",
      " [5.18281600e-01 1.74656457e-04 3.76891560e-01 1.87974873e-04\n",
      "  8.30856335e-01 4.13691462e-04 6.76295204e-01 1.69435655e-04]\n",
      " [5.18160992e-01 1.74656457e-04 3.76894853e-01 1.87974873e-04\n",
      "  8.31396694e-01 4.13691462e-04 6.76243830e-01 1.69435655e-04]\n",
      " [5.18395710e-01 1.74656457e-04 3.77093334e-01 1.87974873e-04\n",
      "  8.31512839e-01 4.13691462e-04 6.76356022e-01 1.69435655e-04]\n",
      " [5.18466873e-01 1.74656457e-04 3.77115186e-01 1.87974873e-04\n",
      "  8.31656507e-01 4.13691462e-04 6.76334315e-01 1.69435655e-04]\n",
      " [5.18169063e-01 1.74656457e-04 3.77119085e-01 1.87974873e-04\n",
      "  8.31606805e-01 4.13691462e-04 6.76275342e-01 1.69435655e-04]\n",
      " [5.18300186e-01 1.74656457e-04 3.77137593e-01 1.87974873e-04\n",
      "  8.31608277e-01 4.13691462e-04 6.76244964e-01 1.69435655e-04]\n",
      " [5.18213605e-01 1.74656457e-04 3.77114097e-01 1.87974873e-04\n",
      "  8.31657508e-01 4.13691462e-04 6.76252500e-01 1.69435655e-04]\n",
      " [5.18272282e-01 1.74656457e-04 3.77189791e-01 1.87974873e-04\n",
      "  8.31595503e-01 4.13691462e-04 6.76253699e-01 1.69435655e-04]\n",
      " [5.18303634e-01 1.74656457e-04 3.77154892e-01 1.87974873e-04\n",
      "  8.32082769e-01 4.13691462e-04 6.76271128e-01 1.69435655e-04]\n",
      " [5.18562378e-01 1.74656457e-04 3.77397110e-01 1.87974873e-04\n",
      "  8.30954599e-01 4.13691462e-04 6.76918294e-01 1.69435655e-04]\n",
      " [5.18341398e-01 1.74656457e-04 3.77160454e-01 1.87974873e-04\n",
      "  8.31766697e-01 4.13691462e-04 6.76294450e-01 1.69435655e-04]\n",
      " [5.18497467e-01 1.74656457e-04 3.77261769e-01 1.87974873e-04\n",
      "  8.31990904e-01 4.13691462e-04 6.76269889e-01 1.69435655e-04]\n",
      " [5.18698229e-01 1.74656457e-04 3.77392003e-01 1.87974873e-04\n",
      "  8.31954856e-01 4.13691462e-04 6.76339962e-01 1.69435655e-04]\n",
      " [5.18693630e-01 1.74656457e-04 3.77533283e-01 1.87974873e-04\n",
      "  8.32184637e-01 4.13691462e-04 6.76516962e-01 1.69435655e-04]\n",
      " [5.18672691e-01 1.74656457e-04 3.77517862e-01 1.87974873e-04\n",
      "  8.32276407e-01 4.13691462e-04 6.76558233e-01 1.69435655e-04]]\n",
      "XXXX ====== Actual Input ====== XXXX\n",
      "(1, 16, 8)\n",
      "[[0.51863729 0.37762073 0.83232141 0.6765906 ]]\n",
      "(1, 1, 1)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_28/3826110232.py:15: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  sample_test['Selected'] = 0\n"
     ]
    }
   ],
   "source": [
    "#  06.14.2024\n",
    "# ============================\n",
    "sample_size = sample_test.shape[0]\n",
    "print ('sample_size: ', sample_size)\n",
    "\n",
    "sl = 16  # <--- sequence length\n",
    "batch = 1  # <--- batch size\n",
    "n_features = 8 # <---- no. of variables\n",
    "n_out = 1 # <----- no. of predicted variables\n",
    "frwd = 1  # <--- how many to predict\n",
    "\n",
    "\n",
    "# Trimming Indices !!!\n",
    "# Marking selected rows\n",
    "sample_test['Selected'] = 0\n",
    "\n",
    "# Selecting m random rows without replacement\n",
    "np.random.seed(125)\n",
    "selected_indices = np.random.choice(sample_test.index, size = batch, replace = False)\n",
    "\n",
    "sample_test.loc[selected_indices, 'Selected'] = 1 # print (sample_test[sample_test['Selected'] == 1])\n",
    "chosen_idx1 = sample_test[sample_test['Selected'] == 1].index\n",
    "chosen_idx2 = chosen_idx1[chosen_idx1 > sl]\n",
    "\n",
    "thrown_idx1 = chosen_idx1[chosen_idx1 <= sl]\n",
    "cnt_thrown_idx1 = len(thrown_idx1)\n",
    "\n",
    "chosen_idx3 = chosen_idx2[chosen_idx2 < (sample_size - frwd)]\n",
    "\n",
    "thrown_idx2 = chosen_idx2[chosen_idx2 >= (sample_size - frwd)]\n",
    "cnt_thrown_idx2 = len(thrown_idx2)\n",
    "\n",
    "cnt_thrown_idx = cnt_thrown_idx1 + cnt_thrown_idx2\n",
    "\n",
    "print (selected_indices)\n",
    "\n",
    "all_ts_xrp = np.empty((1, )) # final input/output\n",
    "all_ts_eth = np.empty((1, )) # final input/output\n",
    "all_ts_ltc = np.empty((1, )) # final input/output\n",
    "all_ts_btc = np.empty((1, )) # final input/output\n",
    "\n",
    "stdev_xrp =  np.empty((1, )) # final input/output\n",
    "stdev_eth =  np.empty((1, )) # final input/output\n",
    "stdev_ltc =  np.empty((1, )) # final input/output\n",
    "stdev_btc =  np.empty((1, )) # final input/output\n",
    "\n",
    "aft_ts_xrp = np.empty((1))\n",
    "aft_ts_eth = np.empty((1))\n",
    "aft_ts_ltc = np.empty((1))\n",
    "aft_ts_btc = np.empty((1))\n",
    "\n",
    "for idx in chosen_idx3:\n",
    "  selected_rows_before = sample_test.iloc[max(0, idx-sl): idx]\n",
    "  selected_rows_after = sample_test.iloc[max(0, idx): idx+frwd]\n",
    "\n",
    "  # print ('====================')\n",
    "  sr_before_vwap_xrp = np.array(selected_rows_before['vwap_xrp'])\n",
    "  sr_after_vwap_xrp = np.array(selected_rows_after['vwap_xrp'])    \n",
    "  # print (sr_before_vwap_xrp)\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_xrp = np.std(sr_before_vwap_xrp)\n",
    "  std_dev_xrp_arr = np.repeat(std_dev_xrp, sl)\n",
    "  # print(std_dev_xrp_arr)\n",
    "\n",
    "  sr_before_vwap_eth = np.array(selected_rows_before['vwap_eth'])\n",
    "  sr_after_vwap_eth = np.array(selected_rows_after['vwap_eth'])\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_eth = np.std(sr_before_vwap_eth)\n",
    "  std_dev_eth_arr = np.repeat(std_dev_eth, sl)\n",
    "  # print(std_dev_eth_arr)\n",
    "    \n",
    "\n",
    "  sr_before_vwap_ltc = np.array(selected_rows_before['vwap_ltc'])\n",
    "  sr_after_vwap_ltc = np.array(selected_rows_after['vwap_ltc'])\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_ltc = np.std(sr_before_vwap_ltc)\n",
    "  std_dev_ltc_arr = np.repeat(std_dev_ltc, sl)\n",
    "  # print(std_dev_ltc_arr)\n",
    "    \n",
    "  sr_before_vwap_btc = np.array(selected_rows_before['vwap_btc'])\n",
    "  sr_after_vwap_btc = np.array(selected_rows_after['vwap_btc'])\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_btc = np.std(sr_before_vwap_btc)\n",
    "  std_dev_btc_arr = np.repeat(std_dev_btc, sl)\n",
    "  # print(std_dev_btc_arr)\n",
    "    \n",
    "  all_ts_xrp = np.concatenate((all_ts_xrp, sr_before_vwap_xrp))\n",
    "  stdev_xrp = np.concatenate((stdev_xrp, std_dev_xrp_arr))\n",
    "    \n",
    "  all_ts_eth = np.concatenate((all_ts_eth, sr_before_vwap_eth))\n",
    "  stdev_eth = np.concatenate((stdev_eth, std_dev_eth_arr))\n",
    "\n",
    "  all_ts_ltc = np.concatenate((all_ts_ltc, sr_before_vwap_ltc))\n",
    "  stdev_ltc = np.concatenate((stdev_ltc, std_dev_ltc_arr))\n",
    "\n",
    "  all_ts_btc = np.concatenate((all_ts_btc, sr_before_vwap_btc))\n",
    "  stdev_btc = np.concatenate((stdev_btc, std_dev_btc_arr))\n",
    "\n",
    "  aft_ts_xrp = np.concatenate((aft_ts_xrp, sr_after_vwap_xrp))\n",
    "  aft_ts_eth = np.concatenate((aft_ts_eth, sr_after_vwap_eth))\n",
    "  aft_ts_ltc = np.concatenate((aft_ts_ltc, sr_after_vwap_ltc))\n",
    "  aft_ts_btc = np.concatenate((aft_ts_btc, sr_after_vwap_btc))\n",
    "\n",
    "X_XRP = all_ts_xrp[1:]\n",
    "X_XRP_stdev = stdev_xrp[1:]\n",
    "\n",
    "X_ETH = all_ts_eth[1:]\n",
    "X_ETH_stdev = stdev_eth[1:]\n",
    "\n",
    "X_LTC = all_ts_ltc[1:]\n",
    "X_LTC_stdev = stdev_ltc[1:]\n",
    "\n",
    "X_BTC = all_ts_btc[1:]\n",
    "X_BTC_stdev = stdev_btc[1:]\n",
    "\n",
    "X0 = np.column_stack((X_XRP, X_XRP_stdev, X_ETH, X_ETH_stdev, X_LTC, X_LTC_stdev, X_BTC, X_BTC_stdev))\n",
    "print (X0)\n",
    "\n",
    "# ======== Model Input =========\n",
    "X = X0.reshape(batch-cnt_thrown_idx, sl, n_features)\n",
    "print ('XXXX ====== Actual Input ====== XXXX')\n",
    "print (X.shape)\n",
    "\n",
    "y_XRP = aft_ts_xrp[1:]\n",
    "y_ETH = aft_ts_eth[1:]\n",
    "y_LTC = aft_ts_ltc[1:]\n",
    "y_BTC = aft_ts_btc[1:]\n",
    "\n",
    "y = np.column_stack((y_XRP, y_ETH, y_LTC, y_BTC))\n",
    "print (y)\n",
    "\n",
    "y = y_BTC.reshape(batch-cnt_thrown_idx, frwd, n_out)\n",
    "# print ('YYYY ====== Actual Input ====== YYYY')\n",
    "print (y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c433b265-d167-4612-b26c-d7dad02c7021",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "88e292cc-1244-462a-b311-3e964773c742",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'values' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[253], line 9\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(batch\u001b[38;5;241m-\u001b[39mcnt_thrown_idx):\n\u001b[1;32m      7\u001b[0m \u001b[38;5;66;03m# Plot the autocorrelation function (ACF)    \u001b[39;00m\n\u001b[1;32m      8\u001b[0m     plt\u001b[38;5;241m.\u001b[39mfigure(figsize \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m12\u001b[39m, \u001b[38;5;241m6\u001b[39m))\n\u001b[0;32m----> 9\u001b[0m     plot_acf(\u001b[43mvalues\u001b[49m[i], lags \u001b[38;5;241m=\u001b[39m sl \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m     10\u001b[0m     plt\u001b[38;5;241m.\u001b[39mtitle(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mAutocorrelation Function (ACF)\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m     11\u001b[0m     plt\u001b[38;5;241m.\u001b[39mshow()\n",
      "\u001b[0;31mNameError\u001b[0m: name 'values' is not defined"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 1200x600 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Figure AutoCorrelations and Partial AutoCorrelations\n",
    "import seaborn as sns\n",
    "from statsmodels.graphics.tsaplots import plot_acf, plot_pacf\n",
    "# from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "for i in range(batch-cnt_thrown_idx):\n",
    "# Plot the autocorrelation function (ACF)    \n",
    "    plt.figure(figsize = (12, 6))\n",
    "    plot_acf(values[i], lags = sl - 1)\n",
    "    plt.title('Autocorrelation Function (ACF)')\n",
    "    plt.show()\n",
    "    \n",
    "# Plot the partial autocorrelation function (PACF)\n",
    "    # plt.figure(figsize = (12, 6))\n",
    "    # plot_pacf(values[round(i/2)], lags = 60)\n",
    "    # plt.title('Partial Autocorrelation Function (PACF)')\n",
    "    # plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "4c6cf92b-58fd-4642-81ee-d7af73c3e75f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===================================================\n",
      "sequence length:   16\n",
      "batch size:   1\n",
      "no. of variables:   8\n",
      "no. of predicted variables:   1\n",
      "how many steps forward to predict:   1\n",
      "===================================================\n"
     ]
    }
   ],
   "source": [
    "print (\"===================================================\")\n",
    "print (\"sequence length:  \", sl)\n",
    "print (\"batch size:  \", batch-cnt_thrown_idx)\n",
    "print (\"no. of variables:  \", n_features)\n",
    "print (\"no. of predicted variables:  \", n_out)\n",
    "print (\"how many steps forward to predict:  \", frwd)\n",
    "print (\"===================================================\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "4021e394-635b-4493-8a89-a658e1229325",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"sequential_5\"</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1mModel: \"sequential_5\"\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
       "┃<span style=\"font-weight: bold\"> Layer (type)                    </span>┃<span style=\"font-weight: bold\"> Output Shape           </span>┃<span style=\"font-weight: bold\">       Param # </span>┃\n",
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
       "│ lstm_8 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">LSTM</span>)                   │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)            │     <span style=\"color: #00af00; text-decoration-color: #00af00\">1,067,008</span> │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ repeat_vector_4 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">RepeatVector</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span>)         │             <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ lstm_9 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">LSTM</span>)                   │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1024</span>)        │     <span style=\"color: #00af00; text-decoration-color: #00af00\">6,295,552</span> │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ time_distributed_4              │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1</span>)           │         <span style=\"color: #00af00; text-decoration-color: #00af00\">1,025</span> │\n",
       "│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">TimeDistributed</span>)               │                        │               │\n",
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
       "</pre>\n"
      ],
      "text/plain": [
       "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
       "┃\u001b[1m \u001b[0m\u001b[1mLayer (type)                   \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape          \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m      Param #\u001b[0m\u001b[1m \u001b[0m┃\n",
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
       "│ lstm_8 (\u001b[38;5;33mLSTM\u001b[0m)                   │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m)            │     \u001b[38;5;34m1,067,008\u001b[0m │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ repeat_vector_4 (\u001b[38;5;33mRepeatVector\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m512\u001b[0m)         │             \u001b[38;5;34m0\u001b[0m │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ lstm_9 (\u001b[38;5;33mLSTM\u001b[0m)                   │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m1024\u001b[0m)        │     \u001b[38;5;34m6,295,552\u001b[0m │\n",
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
       "│ time_distributed_4              │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1\u001b[0m, \u001b[38;5;34m1\u001b[0m)           │         \u001b[38;5;34m1,025\u001b[0m │\n",
       "│ (\u001b[38;5;33mTimeDistributed\u001b[0m)               │                        │               │\n",
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">7,363,585</span> (28.09 MB)\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m7,363,585\u001b[0m (28.09 MB)\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">7,363,585</span> (28.09 MB)\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m7,363,585\u001b[0m (28.09 MB)\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/50\n",
      "1/1 - 2s - 2s/step - loss: 0.4657\n",
      "Epoch 2/50\n",
      "1/1 - 0s - 27ms/step - loss: 0.2649\n",
      "Epoch 3/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.1508\n",
      "Epoch 4/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0857\n",
      "Epoch 5/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0486\n",
      "Epoch 6/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0275\n",
      "Epoch 7/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0155\n",
      "Epoch 8/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0087\n",
      "Epoch 9/50\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.11/dist-packages/keras/src/callbacks/early_stopping.py:156: UserWarning: Early stopping conditioned on metric `val_loss` which is not available. Available metrics are: loss\n",
      "  current = self.get_monitor_value(logs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 - 0s - 26ms/step - loss: 0.0049\n",
      "Epoch 10/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0028\n",
      "Epoch 11/50\n",
      "1/1 - 0s - 26ms/step - loss: 0.0015\n",
      "Epoch 12/50\n",
      "1/1 - 0s - 26ms/step - loss: 8.6631e-04\n",
      "Epoch 13/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.8503e-04\n",
      "Epoch 14/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.7142e-04\n",
      "Epoch 15/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.5183e-04\n",
      "Epoch 16/50\n",
      "1/1 - 0s - 26ms/step - loss: 8.4915e-05\n",
      "Epoch 17/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.7482e-05\n",
      "Epoch 18/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.6546e-05\n",
      "Epoch 19/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.4839e-05\n",
      "Epoch 20/50\n",
      "1/1 - 0s - 26ms/step - loss: 8.2946e-06\n",
      "Epoch 21/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.6361e-06\n",
      "Epoch 22/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.5911e-06\n",
      "Epoch 23/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.4481e-06\n",
      "Epoch 24/50\n",
      "1/1 - 0s - 26ms/step - loss: 8.0920e-07\n",
      "Epoch 25/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.5228e-07\n",
      "Epoch 26/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.5271e-07\n",
      "Epoch 27/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.4123e-07\n",
      "Epoch 28/50\n",
      "1/1 - 0s - 26ms/step - loss: 7.8914e-08\n",
      "Epoch 29/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.4095e-08\n",
      "Epoch 30/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.4630e-08\n",
      "Epoch 31/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.3760e-08\n",
      "Epoch 32/50\n",
      "1/1 - 0s - 26ms/step - loss: 7.6875e-09\n",
      "Epoch 33/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.2910e-09\n",
      "Epoch 34/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.4005e-09\n",
      "Epoch 35/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.3437e-09\n",
      "Epoch 36/50\n",
      "1/1 - 0s - 26ms/step - loss: 7.4849e-10\n",
      "Epoch 37/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.1797e-10\n",
      "Epoch 38/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.3283e-10\n",
      "Epoch 39/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.3097e-10\n",
      "Epoch 40/50\n",
      "1/1 - 0s - 26ms/step - loss: 7.3669e-11\n",
      "Epoch 41/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.1439e-11\n",
      "Epoch 42/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.2737e-11\n",
      "Epoch 43/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.2790e-11\n",
      "Epoch 44/50\n",
      "1/1 - 0s - 26ms/step - loss: 7.5175e-12\n",
      "Epoch 45/50\n",
      "1/1 - 0s - 26ms/step - loss: 4.3521e-12\n",
      "Epoch 46/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.4016e-12\n",
      "Epoch 47/50\n",
      "1/1 - 0s - 26ms/step - loss: 1.4211e-12\n",
      "Epoch 48/50\n",
      "1/1 - 0s - 26ms/step - loss: 7.9936e-13\n",
      "Epoch 49/50\n",
      "1/1 - 0s - 26ms/step - loss: 5.1159e-13\n",
      "Epoch 50/50\n",
      "1/1 - 0s - 26ms/step - loss: 2.8777e-13\n"
     ]
    }
   ],
   "source": [
    "# from keras.initializers import glorot_uniform, he_uniform, orthogonal, zeros\n",
    "\n",
    "n_steps = sl\n",
    "\n",
    "model_8f = Sequential()\n",
    "model_8f.add(tf.keras.layers.Input(shape = (n_steps, n_features)))\n",
    "# model_8f.add(tf.keras.layers.LSTM(units = 512, activation = 'relu', return_sequences = False))   \n",
    "\n",
    "\n",
    "model_8f.add(tf.keras.layers.LSTM(\n",
    "    units = 512,\n",
    "    # kernel_initializer = glorot_uniform(),    # Initialize the input weights\n",
    "    # recurrent_initializer = orthogonal(),     # Initialize the recurrent weights\n",
    "    # bias_initializer = zeros(),               # Initialize the biases\n",
    "    activation = 'relu', \n",
    "    return_sequences = False\n",
    "))\n",
    "\n",
    "# model_8f.add(tf.keras.layers.Dropout(0.2)) \n",
    "model_8f.add(tf.keras.layers.RepeatVector(frwd))\n",
    "model_8f.add(tf.keras.layers.LSTM(units = 1024, activation = 'linear', return_sequences = True))\n",
    "# model_8f.add(tf.keras.layers.Dropout(0.2)) \n",
    "model_8f.add(tf.keras.layers.TimeDistributed(Dense(units = n_out)))\n",
    "\n",
    "# Compile the model with MSE loss computed for each time step\n",
    "\n",
    "sgd = SGD(learning_rate = 0.1)\n",
    "model_8f.compile(optimizer = sgd, loss = 'mean_squared_error')\n",
    "# model_8f.compile(optimizer = 'adam', loss = 'mean_squared_error')\n",
    "model_8f.summary()\n",
    "\n",
    "# Define a custom callback to log errors at each time step\n",
    "# class TimeStepErrorLogger(tf.keras.callbacks.Callback):\n",
    "#     def on_epoch_end(self, epoch, logs = None):\n",
    "#         predictions = self.model.predict(X)\n",
    "#         errors = (predictions - y) ** 2  # Squared errors\n",
    "#         time_step_errors = np.mean(errors, axis = 0)  # Mean error for each time step\n",
    "#         print(f\"Epoch {epoch + 1} - Time Step Errors: {time_step_errors}\")   \n",
    "\n",
    "\n",
    "es = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 1, patience = 5)\n",
    "\n",
    "# fit model\n",
    "# The number of epochs is the number of complete passes through the training dataset\n",
    "history = model_8f.fit(X, y, epochs = 50, \n",
    "                       # validation_split = 0.2,\n",
    "                       verbose = 2, callbacks=[es])\n",
    "                       # , callbacks = [TimeStepErrorLogger()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "badacf02-562c-495a-8d62-374545da5804",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'val_loss'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[249], line 6\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[38;5;66;03m# Plot the data\u001b[39;00m\n\u001b[1;32m      5\u001b[0m ax\u001b[38;5;241m.\u001b[39mplot(history\u001b[38;5;241m.\u001b[39mhistory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mloss\u001b[39m\u001b[38;5;124m'\u001b[39m], \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mblue\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 6\u001b[0m ax\u001b[38;5;241m.\u001b[39mplot(\u001b[43mhistory\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhistory\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mval_loss\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mred\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m      8\u001b[0m \u001b[38;5;66;03m# Customize the plot\u001b[39;00m\n\u001b[1;32m      9\u001b[0m ax\u001b[38;5;241m.\u001b[39mset_title(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mModel Loss (blue) vs Val. Loss (red)\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[0;31mKeyError\u001b[0m: 'val_loss'"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13164 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Create a figure and axis object\n",
    "fig, ax = plt.subplots(1, 1)\n",
    "\n",
    "# Plot the data\n",
    "ax.plot(history.history['loss'], 'blue')\n",
    "ax.plot(history.history['val_loss'], 'red')\n",
    "\n",
    "# Customize the plot\n",
    "ax.set_title('Model Loss (blue) vs Val. Loss (red)')\n",
    "ax.set_xlabel('Epoch')\n",
    "ax.set_ylabel('Loss')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "d08d10b3-a984-4dec-b1af-e29a9e937626",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Extract weights from the LSTM layer\n",
    "lstm_layer = model_8f.layers[0]\n",
    "lstm_weights = lstm_layer.get_weights()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "1f7cdb81-3ea1-467d-8492-fbdc469a4039",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kernel shape: (8, 2048)\n",
      "Recurrent Kernel shape: (512, 2048)\n",
      "Bias shape: (2048,)\n"
     ]
    }
   ],
   "source": [
    "kernel, recurrent_kernel, bias = lstm_weights\n",
    "\n",
    "# Print the shapes\n",
    "print(f\"Kernel shape: {kernel.shape}\")\n",
    "print(f\"Recurrent Kernel shape: {recurrent_kernel.shape}\")\n",
    "print(f\"Bias shape: {bias.shape}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "5be4cb34-6a95-426f-b08f-1c26fcfdaa90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kernel weights: [[ 0.05232579  0.01374082  0.01113737 ... -0.03336089  0.03715584\n",
      "  -0.03538815]\n",
      " [ 0.03264289  0.02306121  0.02604442 ...  0.03129478 -0.05207984\n",
      "   0.04096095]\n",
      " [-0.02632057 -0.02589257 -0.02369264 ... -0.03858705 -0.01252202\n",
      "   0.00666459]\n",
      " ...\n",
      " [-0.01920575 -0.00956063  0.02410255 ...  0.01255936  0.04612235\n",
      "   0.05381919]\n",
      " [ 0.04781022  0.01975601 -0.0432153  ...  0.05187599 -0.01336266\n",
      "   0.03635232]\n",
      " [-0.03965205 -0.00055679 -0.03181865 ...  0.0390408  -0.0238521\n",
      "   0.00138673]]\n",
      "Recurrent kernel weights: [[-0.01153879 -0.00017272  0.0282393  ...  0.00996209  0.01181875\n",
      "  -0.01943585]\n",
      " [ 0.01909763  0.00872338  0.02296009 ...  0.01635076  0.01008803\n",
      "  -0.01466706]\n",
      " [ 0.02507331  0.0413265  -0.00896156 ...  0.01102871  0.00432566\n",
      "   0.01624689]\n",
      " ...\n",
      " [-0.00220611 -0.00429316  0.03294532 ... -0.02962208  0.02178407\n",
      "  -0.00670702]\n",
      " [ 0.00095747  0.00812461  0.00659628 ...  0.03368684 -0.05981153\n",
      "   0.00807448]\n",
      " [ 0.00640679  0.01504548  0.01278104 ... -0.00735644 -0.00102678\n",
      "  -0.02216036]]\n",
      "Bias weights: [ 8.6383414e-05  0.0000000e+00  0.0000000e+00 ... -6.8583427e-07\n",
      "  1.6081285e-05  1.9838195e-04]\n"
     ]
    }
   ],
   "source": [
    "# Print the weights\n",
    "print(\"Kernel weights:\", kernel)\n",
    "print(\"Recurrent kernel weights:\", recurrent_kernel)\n",
    "print(\"Bias weights:\", bias)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb2a2814-070a-40e4-b909-a18698b0b848",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "id": "7ab3f50a-cf41-4307-b34b-17b0614504e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sample_size:  3340\n",
      "[2459 1981]\n",
      "[[4.93597160e-01 1.88567599e-04 3.68336025e-01 9.40572538e-05\n",
      "  7.99405968e-01 3.40939455e-04 6.92646161e-01 8.27791498e-05]\n",
      " [4.93609641e-01 1.88567599e-04 3.68221016e-01 9.40572538e-05\n",
      "  7.98633495e-01 3.40939455e-04 6.92502338e-01 8.27791498e-05]\n",
      " [4.93462410e-01 1.88567599e-04 3.68192285e-01 9.40572538e-05\n",
      "  7.98732066e-01 3.40939455e-04 6.92478336e-01 8.27791498e-05]\n",
      " [4.93224118e-01 1.88567599e-04 3.68046015e-01 9.40572538e-05\n",
      "  7.98648803e-01 3.40939455e-04 6.92426662e-01 8.27791498e-05]\n",
      " [4.93080436e-01 1.88567599e-04 3.67989744e-01 9.40572538e-05\n",
      "  7.98699921e-01 3.40939455e-04 6.92436495e-01 8.27791498e-05]\n",
      " [4.93280821e-01 1.88567599e-04 3.68143955e-01 9.40572538e-05\n",
      "  7.99042488e-01 3.40939455e-04 6.92612000e-01 8.27791498e-05]\n",
      " [4.93273371e-01 1.88567599e-04 3.68186350e-01 9.40572538e-05\n",
      "  7.99276127e-01 3.40939455e-04 6.92707897e-01 8.27791498e-05]\n",
      " [4.93912719e-01 1.88567599e-04 3.67940442e-01 9.40572538e-05\n",
      "  7.99959683e-01 3.40939455e-04 6.92539084e-01 8.27791498e-05]\n",
      " [4.93303158e-01 1.88567599e-04 3.68145198e-01 9.40572538e-05\n",
      "  7.99226963e-01 3.40939455e-04 6.92711968e-01 8.27791498e-05]\n",
      " [4.93394588e-01 1.88567599e-04 3.68085665e-01 9.40572538e-05\n",
      "  7.98890779e-01 3.40939455e-04 6.92578699e-01 8.27791498e-05]\n",
      " [4.93484151e-01 1.88567599e-04 3.68108715e-01 9.40572538e-05\n",
      "  7.98869128e-01 3.40939455e-04 6.92501961e-01 8.27791498e-05]\n",
      " [4.93506775e-01 1.88567599e-04 3.68086727e-01 9.40572538e-05\n",
      "  7.98950462e-01 3.40939455e-04 6.92526095e-01 8.27791498e-05]\n",
      " [4.93460716e-01 1.88567599e-04 3.68031436e-01 9.40572538e-05\n",
      "  7.98795847e-01 3.40939455e-04 6.92517939e-01 8.27791498e-05]\n",
      " [4.93580922e-01 1.88567599e-04 3.68036301e-01 9.40572538e-05\n",
      "  7.98821485e-01 3.40939455e-04 6.92509088e-01 8.27791498e-05]\n",
      " [4.93484923e-01 1.88567599e-04 3.68053843e-01 9.40572538e-05\n",
      "  7.98741236e-01 3.40939455e-04 6.92521550e-01 8.27791498e-05]\n",
      " [4.93562354e-01 1.88567599e-04 3.68097200e-01 9.40572538e-05\n",
      "  7.98728388e-01 3.40939455e-04 6.92600633e-01 8.27791498e-05]\n",
      " [4.96013375e-01 8.69266756e-05 3.69958064e-01 1.70093454e-04\n",
      "  8.06885430e-01 4.65473747e-04 6.96517911e-01 2.11211773e-04]\n",
      " [4.96082497e-01 8.69266756e-05 3.69958733e-01 1.70093454e-04\n",
      "  8.07015134e-01 4.65473747e-04 6.96595439e-01 2.11211773e-04]\n",
      " [4.96078576e-01 8.69266756e-05 3.70110112e-01 1.70093454e-04\n",
      "  8.06939231e-01 4.65473747e-04 6.96860974e-01 2.11211773e-04]\n",
      " [4.96055090e-01 8.69266756e-05 3.70135799e-01 1.70093454e-04\n",
      "  8.06903318e-01 4.65473747e-04 6.96754418e-01 2.11211773e-04]\n",
      " [4.96091402e-01 8.69266756e-05 3.70108084e-01 1.70093454e-04\n",
      "  8.06979031e-01 4.65473747e-04 6.96658593e-01 2.11211773e-04]\n",
      " [4.95994443e-01 8.69266756e-05 3.69934927e-01 1.70093454e-04\n",
      "  8.06906337e-01 4.65473747e-04 6.96543349e-01 2.11211773e-04]\n",
      " [4.95899476e-01 8.69266756e-05 3.69912433e-01 1.70093454e-04\n",
      "  8.06999576e-01 4.65473747e-04 6.96474965e-01 2.11211773e-04]\n",
      " [4.95920517e-01 8.69266756e-05 3.69897038e-01 1.70093454e-04\n",
      "  8.06788626e-01 4.65473747e-04 6.96369088e-01 2.11211773e-04]\n",
      " [4.96045006e-01 8.69266756e-05 3.69913218e-01 1.70093454e-04\n",
      "  8.06884572e-01 4.65473747e-04 6.96308858e-01 2.11211773e-04]\n",
      " [4.95929403e-01 8.69266756e-05 3.69922259e-01 1.70093454e-04\n",
      "  8.06274165e-01 4.65473747e-04 6.96246735e-01 2.11211773e-04]\n",
      " [4.95997900e-01 8.69266756e-05 3.69836113e-01 1.70093454e-04\n",
      "  8.06678341e-01 4.65473747e-04 6.96386785e-01 2.11211773e-04]\n",
      " [4.95930560e-01 8.69266756e-05 3.69808869e-01 1.70093454e-04\n",
      "  8.06040310e-01 4.65473747e-04 6.96455563e-01 2.11211773e-04]\n",
      " [4.95965278e-01 8.69266756e-05 3.69787820e-01 1.70093454e-04\n",
      "  8.05964893e-01 4.65473747e-04 6.96416290e-01 2.11211773e-04]\n",
      " [4.95945132e-01 8.69266756e-05 3.69639940e-01 1.70093454e-04\n",
      "  8.06012342e-01 4.65473747e-04 6.96354516e-01 2.11211773e-04]\n",
      " [4.95805449e-01 8.69266756e-05 3.69532449e-01 1.70093454e-04\n",
      "  8.05758002e-01 4.65473747e-04 6.96019279e-01 2.11211773e-04]\n",
      " [4.95804639e-01 8.69266756e-05 3.69598758e-01 1.70093454e-04\n",
      "  8.05796060e-01 4.65473747e-04 6.96123180e-01 2.11211773e-04]]\n",
      "XXXX ====== Actual Input ====== XXXX\n",
      "(2, 16, 8)\n",
      "(2, 1, 1)\n"
     ]
    }
   ],
   "source": [
    "#  Validation Set - 06.14.2024\n",
    "# ============================\n",
    "# Reset Index\n",
    "sample_valid = sample_valid.reset_index(drop = True)\n",
    "sample_size = sample_valid.shape[0]\n",
    "print ('sample_size: ', sample_size)\n",
    "# sample_valid.head()\n",
    "# sl = 8  # <--- sequence length\n",
    "batch_valid = 2  # <--- batch size\n",
    "# n_features = 8 # <---- no. of variables\n",
    "# n_out = 1 # <----- no. of predicted variables\n",
    "# frwd = 1  # <--- how many to predict\n",
    "\n",
    "\n",
    "# Trimming Indices !!!\n",
    "# Marking selected rows\n",
    "sample_valid['Selected'] = 0\n",
    "\n",
    "# Selecting m random rows without replacement\n",
    "np.random.seed(125)\n",
    "selected_indices = np.random.choice(sample_valid.index, size = batch_valid, replace = False)\n",
    "\n",
    "sample_valid.loc[selected_indices, 'Selected'] = 1 # print (sample_valid[sample_valid['Selected'] == 1])\n",
    "chosen_idx1 = sample_valid[sample_valid['Selected'] == 1].index\n",
    "chosen_idx2 = chosen_idx1[chosen_idx1 > sl]\n",
    "\n",
    "thrown_idx1 = chosen_idx1[chosen_idx1 <= sl]\n",
    "cnt_thrown_idx1 = len(thrown_idx1)\n",
    "\n",
    "chosen_idx3 = chosen_idx2[chosen_idx2 < (sample_size - frwd)]\n",
    "\n",
    "thrown_idx2 = chosen_idx2[chosen_idx2 >= (sample_size - frwd)]\n",
    "cnt_thrown_idx2 = len(thrown_idx2)\n",
    "\n",
    "cnt_thrown_idx = cnt_thrown_idx1 + cnt_thrown_idx2\n",
    "\n",
    "print (selected_indices)\n",
    "\n",
    "all_ts_xrp = np.empty((1, )) # final input/output\n",
    "all_ts_eth = np.empty((1, )) # final input/output\n",
    "all_ts_ltc = np.empty((1, )) # final input/output\n",
    "all_ts_btc = np.empty((1, )) # final input/output\n",
    "\n",
    "stdev_xrp =  np.empty((1, )) # final input/output\n",
    "stdev_eth =  np.empty((1, )) # final input/output\n",
    "stdev_ltc =  np.empty((1, )) # final input/output\n",
    "stdev_btc =  np.empty((1, )) # final input/output\n",
    "\n",
    "aft_ts_xrp = np.empty((1))\n",
    "aft_ts_eth = np.empty((1))\n",
    "aft_ts_ltc = np.empty((1))\n",
    "aft_ts_btc = np.empty((1))\n",
    "\n",
    "for idx in chosen_idx3:\n",
    "  selected_rows_before = sample_valid.iloc[max(0, idx-sl): idx]\n",
    "  selected_rows_after = sample_valid.iloc[max(0, idx): idx+frwd]\n",
    "\n",
    "  # print ('====================')\n",
    "  sr_before_vwap_xrp = np.array(selected_rows_before['vwap_xrp'])\n",
    "  sr_after_vwap_xrp = np.array(selected_rows_after['vwap_xrp'])    \n",
    "  # print (sr_before_vwap_xrp)\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_xrp = np.std(sr_before_vwap_xrp)\n",
    "  std_dev_xrp_arr = np.repeat(std_dev_xrp, sl)\n",
    "  # print(std_dev_xrp_arr)\n",
    "\n",
    "  sr_before_vwap_eth = np.array(selected_rows_before['vwap_eth'])\n",
    "  sr_after_vwap_eth = np.array(selected_rows_after['vwap_eth'])\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_eth = np.std(sr_before_vwap_eth)\n",
    "  std_dev_eth_arr = np.repeat(std_dev_eth, sl)\n",
    "  # print(std_dev_eth_arr)\n",
    "    \n",
    "\n",
    "  sr_before_vwap_ltc = np.array(selected_rows_before['vwap_ltc'])\n",
    "  sr_after_vwap_ltc = np.array(selected_rows_after['vwap_ltc'])\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_ltc = np.std(sr_before_vwap_ltc)\n",
    "  std_dev_ltc_arr = np.repeat(std_dev_ltc, sl)\n",
    "  # print(std_dev_ltc_arr)\n",
    "    \n",
    "  sr_before_vwap_btc = np.array(selected_rows_before['vwap_btc'])\n",
    "  sr_after_vwap_btc = np.array(selected_rows_after['vwap_btc'])\n",
    "\n",
    "# Compute standard deviation\n",
    "  std_dev_btc = np.std(sr_before_vwap_btc)\n",
    "  std_dev_btc_arr = np.repeat(std_dev_btc, sl)\n",
    "  # print(std_dev_btc_arr)\n",
    "    \n",
    "  all_ts_xrp = np.concatenate((all_ts_xrp, sr_before_vwap_xrp))\n",
    "  stdev_xrp = np.concatenate((stdev_xrp, std_dev_xrp_arr))\n",
    "    \n",
    "  all_ts_eth = np.concatenate((all_ts_eth, sr_before_vwap_eth))\n",
    "  stdev_eth = np.concatenate((stdev_eth, std_dev_eth_arr))\n",
    "\n",
    "  all_ts_ltc = np.concatenate((all_ts_ltc, sr_before_vwap_ltc))\n",
    "  stdev_ltc = np.concatenate((stdev_ltc, std_dev_ltc_arr))\n",
    "\n",
    "  all_ts_btc = np.concatenate((all_ts_btc, sr_before_vwap_btc))\n",
    "  stdev_btc = np.concatenate((stdev_btc, std_dev_btc_arr))\n",
    "\n",
    "  aft_ts_xrp = np.concatenate((aft_ts_xrp, sr_after_vwap_xrp))\n",
    "  aft_ts_eth = np.concatenate((aft_ts_eth, sr_after_vwap_eth))\n",
    "  aft_ts_ltc = np.concatenate((aft_ts_ltc, sr_after_vwap_ltc))\n",
    "  aft_ts_btc = np.concatenate((aft_ts_btc, sr_after_vwap_btc))\n",
    "\n",
    "X_XRP = all_ts_xrp[1:]\n",
    "X_XRP_stdev = stdev_xrp[1:]\n",
    "\n",
    "X_ETH = all_ts_eth[1:]\n",
    "X_ETH_stdev = stdev_eth[1:]\n",
    "\n",
    "X_LTC = all_ts_ltc[1:]\n",
    "X_LTC_stdev = stdev_ltc[1:]\n",
    "\n",
    "X_BTC = all_ts_btc[1:]\n",
    "X_BTC_stdev = stdev_btc[1:]\n",
    "\n",
    "X0_valid = np.column_stack((X_XRP, X_XRP_stdev, X_ETH, X_ETH_stdev, X_LTC, X_LTC_stdev, X_BTC, X_BTC_stdev))\n",
    "print (X0_valid)\n",
    "\n",
    "# ======== Model Input =========\n",
    "X_valid = X0_valid.reshape(batch_valid-cnt_thrown_idx, sl, n_features)\n",
    "print ('XXXX ====== Actual Input ====== XXXX')\n",
    "print (X_valid.shape)\n",
    "\n",
    "y_XRP = aft_ts_xrp[1:]\n",
    "y_ETH = aft_ts_eth[1:]\n",
    "y_LTC = aft_ts_ltc[1:]\n",
    "y_BTC = aft_ts_btc[1:]\n",
    "\n",
    "# y_valid = np.column_stack((y_XRP, y_ETH, y_LTC, y_BTC))\n",
    "# print (y_valid)\n",
    "\n",
    "y_valid = y_BTC.reshape(batch_valid-cnt_thrown_idx, frwd, n_out)\n",
    "# print ('YYYY ====== Actual Input ====== YYYY')\n",
    "print (y_valid.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "263e1cab-3b94-49e2-a278-fa0336867310",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 666ms/step\n",
      "y_valid\n",
      "<class 'numpy.ndarray'>\n",
      "(2, 1, 1)\n",
      "[[[0.69274031]]\n",
      "\n",
      " [[0.69615353]]]\n",
      "y_hat\n",
      "<class 'numpy.ndarray'>\n",
      "(2, 1)\n",
      "[[0.6757623]\n",
      " [0.6760601]]\n"
     ]
    }
   ],
   "source": [
    "y_hat = model_8f.predict(X_valid, verbose = 1).reshape(batch_valid-cnt_thrown_idx, frwd)\n",
    "# print (sl)\n",
    "\n",
    "print ('y_valid')\n",
    "print (type(y_valid))\n",
    "print (y_valid.shape)\n",
    "print (y_valid)\n",
    "# print (y_valid[0:10])\n",
    "\n",
    "print ('y_hat')\n",
    "print (type(y_hat))\n",
    "print (y_hat.shape)\n",
    "print (y_hat)\n",
    "# y_ht1 = y_hat.reshape(batch-cnt_thrown_idx, frwd)\n",
    "# # print (y_hat[0:10])\n",
    "# print (y_ht1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "e1969a05-91ff-40df-a466-01edd42f1b40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "0\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 46272 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39948 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in range(batch_valid-cnt_thrown_idx):\n",
    "    print ('\\n')  \n",
    "    print (i)\n",
    "    X_resh = X_valid[i:i+1, :, 6].flatten()\n",
    "    # print (X_resh)\n",
    "    # print ('\\n')\n",
    "    y_resh = y_valid[i:i+1].flatten()\n",
    "    # print (y_resh)\n",
    "    y_est_resh = y_hat[i:i+1].flatten()\n",
    "    # print (y_est_resh)\n",
    "    # print ('\\n')\n",
    "    actual = np.concatenate((X_resh, y_resh)) * 100000.00\n",
    "    # print (actual)\n",
    "    # print ('\\n')\n",
    "    estimate = np.concatenate((X_resh, y_est_resh)) * 100000.00\n",
    "    # print (estimate)\n",
    "\n",
    "    dates = list(range(sl+frwd))\n",
    "    \n",
    "    # Create a pandas DataFrame\n",
    "    data = pd.DataFrame({'Date': dates, 'Actuals': actual, 'Forecasts': estimate})\n",
    "    \n",
    "    # Set the 'Date' column as the index\n",
    "    data.set_index('Date', inplace=True)\n",
    "    \n",
    "    # Create a figure and axis object using object-oriented style\n",
    "    fig, ax = plt.subplots(figsize = (10, 6))\n",
    "    \n",
    "    # Plot actuals vs. forecasts\n",
    "    ax.plot(data.index, data['Forecasts'], label = 'Forecasts', marker = 'x')\n",
    "    ax.plot(data.index, data['Actuals'], label = 'Actuals', marker = 'o')\n",
    "    \n",
    "    # Set labels and title\n",
    "    ax.set_xlabel('Date')\n",
    "    ax.set_ylabel('Value')\n",
    "    ax.set_title('Actuals vs. Forecasts')\n",
    "\n",
    "    ax.legend() # Add legend\n",
    "\n",
    "    ax.grid(True) # Add grid\n",
    "    \n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4acc8461-7c33-4b38-91c7-422f89d7d5b0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75c0ee52-c740-434a-92bd-4f46f9ce290a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "7a9e8c46-d24e-4ce9-b20a-232b4a86dab6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    date  close_xrp    volume_xrp  vwap_xrp  close_eth  volume_eth  vwap_eth  \\\n",
      "0  1-0-0     0.5173  28809.165339  0.517217   0.376204   37.497964  0.376108   \n",
      "\n",
      "   close_ltc  volume_ltc  vwap_ltc  close_btc  volume_btc  vwap_btc  Selected  \n",
      "0     0.8317  110.056253  0.831662    0.67506    2.045049  0.674969         0  \n"
     ]
    }
   ],
   "source": [
    "print (sample_test.head(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14eb03d2-1dbe-4834-908f-5a520a4a28c5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "598f83ca-cbb1-44f8-a1c6-7ce84f2243d7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39259ac7-422b-48dc-aecd-2c9799ae3066",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'sample_test' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 5\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# variables are close and volume\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;66;03m# ==============================\u001b[39;00m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;66;03m#  Test Sample --- 06.14.2024\u001b[39;00m\n\u001b[1;32m      4\u001b[0m \u001b[38;5;66;03m# ==============================\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m sample_size \u001b[38;5;241m=\u001b[39m \u001b[43msample_test\u001b[49m\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msample_size: \u001b[39m\u001b[38;5;124m'\u001b[39m, sample_size)\n\u001b[1;32m      8\u001b[0m sl \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m16\u001b[39m  \u001b[38;5;66;03m# <--- sequence length\u001b[39;00m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'sample_test' is not defined"
     ]
    }
   ],
   "source": [
    "# variables are volume and volume\n",
    "# ==============================\n",
    "#  Test Sample --- 06.14.2024\n",
    "# ==============================\n",
    "sample_size = sample_test.shape[0]\n",
    "print ('sample_size: ', sample_size)\n",
    "\n",
    "sl = 16  # <--- sequence length\n",
    "batch = 1  # <--- batch size\n",
    "n_features = 8 # <---- no. of variables\n",
    "n_out = 1 # <----- no. of predicted variables\n",
    "frwd = 1  # <--- how many to predict\n",
    "\n",
    "\n",
    "# Trimming Indices !!!\n",
    "# Marking selected rows\n",
    "sample_test['Selected'] = 0\n",
    "\n",
    "# Selecting m random rows without replacement\n",
    "np.random.seed(125)\n",
    "selected_indices = np.random.choice(sample_test.index, size = batch, replace = False)\n",
    "\n",
    "sample_test.loc[selected_indices, 'Selected'] = 1 # print (sample_test[sample_test['Selected'] == 1])\n",
    "chosen_idx1 = sample_test[sample_test['Selected'] == 1].index\n",
    "chosen_idx2 = chosen_idx1[chosen_idx1 > sl]\n",
    "\n",
    "thrown_idx1 = chosen_idx1[chosen_idx1 <= sl]\n",
    "cnt_thrown_idx1 = len(thrown_idx1)\n",
    "\n",
    "chosen_idx3 = chosen_idx2[chosen_idx2 < (sample_size - frwd)]\n",
    "\n",
    "thrown_idx2 = chosen_idx2[chosen_idx2 >= (sample_size - frwd)]\n",
    "cnt_thrown_idx2 = len(thrown_idx2)\n",
    "\n",
    "cnt_thrown_idx = cnt_thrown_idx1 + cnt_thrown_idx2\n",
    "\n",
    "print (selected_indices)\n",
    "\n",
    "# all_ts_xrp = np.empty((1, )) # final input/output\n",
    "# all_ts_eth = np.empty((1, )) # final input/output\n",
    "# all_ts_ltc = np.empty((1, )) # final input/output\n",
    "# all_ts_btc = np.empty((1, )) # final input/output\n",
    "\n",
    "\n",
    "all_ts_close_xrp = np.empty((1, )) # final input/output\n",
    "all_ts_close_eth = np.empty((1, )) # final input/output\n",
    "all_ts_close_ltc = np.empty((1, )) # final input/output\n",
    "all_ts_close_btc = np.empty((1, )) # final input/output\n",
    "\n",
    "all_ts_volume_xrp = np.empty((1, )) # final input/output\n",
    "all_ts_volume_eth = np.empty((1, )) # final input/output\n",
    "all_ts_volume_ltc = np.empty((1, )) # final input/output\n",
    "all_ts_volume_btc = np.empty((1, )) # final input/output\n",
    "\n",
    "stdev_xrp =  np.empty((1, )) # final input/output\n",
    "stdev_eth =  np.empty((1, )) # final input/output\n",
    "stdev_ltc =  np.empty((1, )) # final input/output\n",
    "stdev_btc =  np.empty((1, )) # final input/output\n",
    "\n",
    "\n",
    "aft_ts_xrp = np.empty((1))\n",
    "aft_ts_eth = np.empty((1))\n",
    "aft_ts_ltc = np.empty((1))\n",
    "aft_ts_btc = np.empty((1))\n",
    "\n",
    "for idx in chosen_idx3:\n",
    "  selected_rows_before = sample_test.iloc[max(0, idx-sl): idx]\n",
    "  selected_rows_after = sample_test.iloc[max(0, idx): idx+frwd]\n",
    "\n",
    "  # xrp\n",
    "  sr_before_close_xrp = np.array(selected_rows_before['close_xrp'])\n",
    "  sr_after_close_xrp = np.array(selected_rows_after['close_xrp'])    \n",
    "  # print (sr_before_close_xrp)\n",
    "  sr_before_volume_xrp = np.array(selected_rows_before['volume_xrp'])\n",
    "  sr_after_volume_xrp = np.array(selected_rows_after['volume_xrp'])    \n",
    "  # print (sr_before_volume_xrp)  \n",
    "    # Compute standard deviation\n",
    "  std_dev_xrp = np.std(sr_before_close_xrp)\n",
    "  std_dev_xrp_arr = np.repeat(std_dev_xrp, sl)\n",
    "  # print(std_dev_xrp_arr)\n",
    "    \n",
    "# eth\n",
    "  sr_before_close_eth = np.array(selected_rows_before['close_eth'])\n",
    "  sr_after_close_eth = np.array(selected_rows_after['close_eth'])\n",
    "  sr_before_volume_eth = np.array(selected_rows_before['volume_eth'])\n",
    "  sr_after_volume_eth = np.array(selected_rows_after['volume_eth'])\n",
    "    # Compute standard deviation\n",
    "  std_dev_eth = np.std(sr_before_close_eth)\n",
    "  std_dev_eth_arr = np.repeat(std_dev_eth, sl)\n",
    "  # print(std_dev_eth_arr)\n",
    "    \n",
    "# ltc\n",
    "  sr_before_close_ltc = np.array(selected_rows_before['close_ltc'])\n",
    "  sr_after_close_ltc = np.array(selected_rows_after['close_ltc'])\n",
    "  sr_before_volume_ltc = np.array(selected_rows_before['volume_ltc'])\n",
    "  sr_after_volume_ltc = np.array(selected_rows_after['volume_ltc'])\n",
    "# Compute standard deviation\n",
    "  std_dev_ltc = np.std(sr_before_close_ltc)\n",
    "  std_dev_ltc_arr = np.repeat(std_dev_ltc, sl)\n",
    "  # print(std_dev_ltc_arr)\n",
    "\n",
    "# btc\n",
    "  sr_before_close_btc = np.array(selected_rows_before['close_btc'])\n",
    "  sr_after_close_btc = np.array(selected_rows_after['close_btc'])\n",
    "  sr_before_volume_btc = np.array(selected_rows_before['volume_btc'])\n",
    "  sr_after_volume_btc = np.array(selected_rows_after['volume_btc'])\n",
    "    # Compute standard deviation\n",
    "  std_dev_btc = np.std(sr_before_close_btc)\n",
    "  std_dev_btc_arr = np.repeat(std_dev_btc, sl)\n",
    "  # print(std_dev_btc_arr)\n",
    "\n",
    "# xrp\n",
    "  all_ts_close_xrp = np.concatenate((all_ts_close_xrp, sr_before_close_xrp))\n",
    "  all_ts_volume_xrp = np.concatenate((all_ts_volume_xrp, sr_before_volume_xrp))\n",
    "  stdev_xrp = np.concatenate((stdev_xrp, std_dev_xrp_arr))\n",
    "# eth\n",
    "  all_ts_close_eth = np.concatenate((all_ts_close_eth, sr_before_close_eth))\n",
    "  all_ts_volume_eth = np.concatenate((all_ts_volume_eth, sr_before_volume_eth))\n",
    "  stdev_eth = np.concatenate((stdev_eth, std_dev_eth_arr))\n",
    "# ltc\n",
    "  all_ts_close_ltc = np.concatenate((all_ts_close_ltc, sr_before_close_ltc))\n",
    "  all_ts_volume_ltc = np.concatenate((all_ts_volume_ltc, sr_before_volume_tc))\n",
    "  stdev_ltc = np.concatenate((stdev_ltc, std_dev_ltc_arr))\n",
    "# btc\n",
    "  all_ts_close_btc = np.concatenate((all_ts_close_btc, sr_before_close_btc))\n",
    "  all_ts_volume_btc = np.concatenate((all_ts_volume_btc, sr_before_volume_btc))\n",
    "  stdev_btc = np.concatenate((stdev_btc, std_dev_btc_arr))\n",
    "\n",
    "\n",
    "  aft_ts_close_xrp = np.concatenate((aft_ts_close_xrp, sr_after_close_xrp))\n",
    "  aft_ts_close_eth = np.concatenate((aft_ts_close_eth, sr_after_close_eth))\n",
    "  aft_ts_close_ltc = np.concatenate((aft_ts_close_ltc, sr_after_close_ltc))\n",
    "  aft_ts_close_btc = np.concatenate((aft_ts_close_btc, sr_after_close_btc))\n",
    "\n",
    "\n",
    "X_XRP_close = all_ts_close_xrp[1:]\n",
    "X_XRP_volume = all_ts_volume_xrp[1:]\n",
    "X_XRP_stdev = stdev_xrp[1:]\n",
    "\n",
    "\n",
    "X_ETH_close = all_ts_close_eth[1:]\n",
    "X_ETH_volume = all_ts_volume_eth[1:]\n",
    "X_ETH_stdev = stdev_eth[1:]\n",
    "\n",
    "X_LTC_close = all_ts_close_ltc[1:]\n",
    "X_LTC_volume = all_ts_volume_ltc[1:]\n",
    "X_LTC_stdev = stdev_ltc[1:]\n",
    "\n",
    "X_BTC_close = all_ts_close_btc[1:]\n",
    "X_BTC_volume = all_ts_volume_btc[1:]\n",
    "X_BTC_stdev = stdev_btc[1:]\n",
    "\n",
    "X0 = np.column_stack((X_XRP_close, X_XRP_volume, \n",
    "                      X_ETH_close, X_ETH_volume, \n",
    "                      X_LTC_close, X_LTC_volume, \n",
    "                      X_BTC_close, X_BTC_volume))\n",
    "print (X0)\n",
    "\n",
    "# ======== Model Input =========\n",
    "X = X0.reshape(batch-cnt_thrown_idx, sl, n_features)\n",
    "print ('XXXX ====== Actual Input ====== XXXX')\n",
    "print (X.shape)\n",
    "\n",
    "y_XRP = aft_ts_xrp[1:]\n",
    "y_ETH = aft_ts_eth[1:]\n",
    "y_LTC = aft_ts_ltc[1:]\n",
    "y_BTC = aft_ts_btc[1:]\n",
    "\n",
    "y = np.column_stack((y_XRP, y_ETH, y_LTC, y_BTC))\n",
    "print (y)\n",
    "\n",
    "y = y_BTC.reshape(batch-cnt_thrown_idx, frwd, n_out)\n",
    "# print ('YYYY ====== Actual Input ====== YYYY')\n",
    "print (y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47d6927d-9021-4a56-9e52-ee2061bd5979",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
