This commit is contained in:
2024-08-14 01:54:48 +00:00
parent 80319e0301
commit cd5c78d47f
4 changed files with 1055 additions and 227 deletions
@@ -0,0 +1,263 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c7dac550-c0ed-4ec7-846e-8edb2086c9cc",
"metadata": {},
"source": [
"# Augmented Dickey-Fuller Test (ADF)\n",
"Stationarity Test"
]
},
{
"cell_type": "markdown",
"id": "778b9362-37e3-40e0-a20a-1ca5e2cddf05",
"metadata": {},
"source": [
"## Preparing The data"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "998ecc54-aaba-4761-bb98-1eda5c9fa091",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>tstamp</th>\n",
" <th>target</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722470400000000000</td>\n",
" <td>64640.679892</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722470460000000000</td>\n",
" <td>64652.991289</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722470520000000000</td>\n",
" <td>64660.005093</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722470580000000000</td>\n",
" <td>64653.482847</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722470640000000000</td>\n",
" <td>64687.458279</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1372</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722556500000000000</td>\n",
" <td>65439.307663</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1373</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722556560000000000</td>\n",
" <td>65445.733114</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1374</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722556620000000000</td>\n",
" <td>65446.371741</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1375</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722556680000000000</td>\n",
" <td>65420.879478</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1376</th>\n",
" <td>PAIR-BTC-USDT</td>\n",
" <td>1722556740000000000</td>\n",
" <td>65377.032222</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1377 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" id tstamp target\n",
"0 PAIR-BTC-USDT 1722470400000000000 64640.679892\n",
"1 PAIR-BTC-USDT 1722470460000000000 64652.991289\n",
"2 PAIR-BTC-USDT 1722470520000000000 64660.005093\n",
"3 PAIR-BTC-USDT 1722470580000000000 64653.482847\n",
"4 PAIR-BTC-USDT 1722470640000000000 64687.458279\n",
"... ... ... ...\n",
"1372 PAIR-BTC-USDT 1722556500000000000 65439.307663\n",
"1373 PAIR-BTC-USDT 1722556560000000000 65445.733114\n",
"1374 PAIR-BTC-USDT 1722556620000000000 65446.371741\n",
"1375 PAIR-BTC-USDT 1722556680000000000 65420.879478\n",
"1376 PAIR-BTC-USDT 1722556740000000000 65377.032222\n",
"\n",
"[1377 rows x 3 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"from statsmodels.tsa.stattools import adfuller\n",
"import numpy as np\n",
"\n",
"def demo_example_data() -> pd.Series:\n",
" # Generate example time series data\n",
" # np.random.seed(0)\n",
" time_series_data = np.random.randn(100) # Random data for demonstration\n",
" \n",
" # Create a pandas Series\n",
" data = pd.Series(time_series_data)\n",
" \n",
" # Optionally, you can add a datetime index if you have time-indexed data\n",
" dates = pd.date_range(start='2020-01-01', periods=len(time_series_data), freq='D')\n",
" data = pd.Series(time_series_data, index=dates)\n",
"\n",
" # Display the first few rows of the data\n",
" print(data.head())\n",
" return data\n",
"\n",
"def load_df_from_db(file: str, query: str) -> pd.DataFrame:\n",
" import sqlite3 \n",
" \n",
" conn = sqlite3.connect(file)\n",
" df = pd.read_sql_query(query, conn)\n",
" df['timestamp'] = pd.to_datetime(df['tstamp'])\n",
" df.set_index('timestamp', inplace=True)\n",
" return df\n",
"\n",
"file_path = \"/workspace/data/crypto_md/20240801.mktdata.ohlcv.db\"\n",
"instrument_id='PAIR-BTC-USDT'\n",
"query = f\"\"\"\n",
"select \n",
" instrument_id as id, \n",
" tstamp, \n",
" vwap \n",
"from bnbspot_ohlcv_1min \n",
"where instrument_id = '{instrument_id}'\n",
"\"\"\"\n",
"\n",
"df = load_df_from_db(file=file_path, query=query)\n",
"df.rename(columns={'vwap': 'target'}, inplace=True)\n",
"# df[\"tstamp2\"] = df.index\n",
"df = df.reset_index()\n",
"df = df.drop([\"timestamp\"], axis=1) \n",
"df"
]
},
{
"cell_type": "markdown",
"id": "43e43154-5a04-4a1d-977c-7f930d62f241",
"metadata": {},
"source": [
"## Running Test"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b97d357f-787b-4cff-849f-b91b1ec35e7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2020-01-01 -1.652908\n",
"2020-01-02 -0.157302\n",
"2020-01-03 -1.396187\n",
"2020-01-04 0.150374\n",
"2020-01-05 1.048603\n",
"Freq: D, dtype: float64\n",
"ADF Statistic: -9.985535987881171\n",
"p-value: 2.060269774403535e-17\n",
"Critical Values: {'1%': -3.498198082189098, '5%': -2.891208211860468, '10%': -2.5825959973472097}\n"
]
}
],
"source": [
"import pandas as pd\n",
"from statsmodels.tsa.stattools import adfuller\n",
"\n",
"# Example time series data\n",
"data = demo_example_data()\n",
"\n",
"# Perform the ADF test\n",
"result = adfuller(data)\n",
"\n",
"# Extract and print the results\n",
"print('ADF Statistic:', result[0])\n",
"print('p-value:', result[1])\n",
"print('Critical Values:', result[4])\n"
]
}
],
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+30 -53
View File
@@ -10,7 +10,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "6b269e64-be58-43b5-ad60-0fbd1d37861a",
"metadata": {},
"outputs": [
@@ -46,56 +46,26 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 2,
"id": "7313a620-a0eb-4207-a12a-90aeee3cd980",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]',\n",
" environ{'USER': 'oleg',\n",
" 'SSH_CLIENT': '100.102.18.30 39290 22',\n",
" 'XDG_SESSION_TYPE': 'tty',\n",
" 'SHLVL': '1',\n",
" 'MOTD_SHOWN': 'pam',\n",
" 'HOME': '/home/oleg',\n",
" 'OLDPWD': '/home/oleg/.vscode-server',\n",
" 'SSL_CERT_FILE': '/usr/lib/ssl/certs/ca-certificates.crt',\n",
" 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus',\n",
" 'LOGNAME': 'oleg',\n",
" '_': '/home/oleg/.pyenv/python3.10-venv/bin/python',\n",
" 'XDG_SESSION_CLASS': 'user',\n",
" 'VSCODE_CLI_REQUIRE_TOKEN': 'a18f8694-adf8-4fa9-a860-a18b2bdb3d92',\n",
" 'XDG_SESSION_ID': '149',\n",
" 'PATH': '/home/oleg/.pyenv/python3.10-venv/bin:/home/oleg/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin',\n",
" 'VSCODE_AGENT_FOLDER': '/home/oleg/.vscode-server',\n",
" 'XDG_RUNTIME_DIR': '/run/user/1000',\n",
" 'SSL_CERT_DIR': '/usr/lib/ssl/certs',\n",
" 'LANG': 'en_US.UTF-8',\n",
" 'SHELL': '/bin/bash',\n",
" 'PWD': '/home/oleg',\n",
" 'SSH_CONNECTION': '100.102.18.30 39290 100.102.233.115 22',\n",
" 'XDG_DATA_DIRS': '/home/oleg/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share',\n",
" 'VSCODE_HANDLES_SIGPIPE': 'true',\n",
" 'LS_COLORS': '',\n",
" 'LESSCLOSE': '/usr/bin/lesspipe %s %s',\n",
" 'LESSOPEN': '| /usr/bin/lesspipe %s',\n",
" 'LD_LIBRARY_PATH': '/usr/local/cuda/lib64',\n",
" 'VSCODE_AMD_ENTRYPOINT': 'vs/workbench/api/node/extensionHostProcess',\n",
" 'VSCODE_HANDLES_UNCAUGHT_ERRORS': 'true',\n",
" 'VSCODE_NLS_CONFIG': '{\"locale\":\"en\",\"osLocale\":\"en\",\"availableLanguages\":{}}',\n",
" 'BROWSER': '/home/oleg/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/helpers/browser.sh',\n",
" 'VSCODE_CWD': '/home/oleg',\n",
" 'ELECTRON_RUN_AS_NODE': '1',\n",
" 'VSCODE_IPC_HOOK_CLI': '/run/user/1000/vscode-ipc-c5d6268a-2e45-4659-9523-6fcffb529a08.sock',\n",
" 'PYTHONUNBUFFERED': '1',\n",
" 'VIRTUAL_ENV': '/home/oleg/.pyenv/python3.10-venv',\n",
" 'PYTHONIOENCODING': 'utf-8',\n",
" 'VIRTUAL_ENV_PROMPT': '(python3.10-venv) ',\n",
" 'PS1': '(python3.10-venv) ',\n",
" 'PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING': '1',\n",
" 'PYTHON_FROZEN_MODULES': 'on',\n",
"('3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0]',\n",
" environ{'PATH': '/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',\n",
" 'HOSTNAME': '30c1a1a6daca',\n",
" 'JUPYTER_ENABLE_LAB': 'yes',\n",
" 'PYTHONPATH': '/cvtt/prod',\n",
" 'NVIDIA_VISIBLE_DEVICES': 'all',\n",
" 'NVIDIA_DRIVER_CAPABILITIES': 'compute,utility',\n",
" 'LD_LIBRARY_PATH': '/usr/local/nvidia/lib:/usr/local/nvidia/lib64',\n",
" 'PYTORCH_VERSION': '2.2.1',\n",
" 'HOME': '/root',\n",
" 'LC_CTYPE': 'C.UTF-8',\n",
" 'JPY_SESSION_NAME': '/workspace/Testing GPU.ipynb',\n",
" 'JPY_PARENT_PID': '1',\n",
" 'PYDEVD_USE_FRAME_EVAL': 'NO',\n",
" 'TERM': 'xterm-color',\n",
" 'CLICOLOR': '1',\n",
@@ -107,7 +77,7 @@
" 'CUDA_MODULE_LOADING': 'LAZY'})"
]
},
"execution_count": 10,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@@ -116,7 +86,7 @@
"import sys\n",
"import os\n",
"\n",
"sys.path.append(\"/home/oleg/develop/cvtt2\")\n",
"sys.path.append(\"/\")\n",
"sys.version,os.environ\n",
"\n"
]
@@ -131,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 3,
"id": "95d9a2e6-3464-4dbe-9a97-0c2d5eb34193",
"metadata": {},
"outputs": [],
@@ -160,14 +130,14 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"id": "eb38de31-fc19-4515-b08d-9cd7607ea958",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4be0c8cc9c004ef6b32f044018434a85",
"model_id": "54e9ddbe3f4349ca9ebbee5aaec14477",
"version_major": 2,
"version_minor": 0
},
@@ -188,10 +158,10 @@
{
"data": {
"text/plain": [
"1718155463838047267"
"1723585308075979805"
]
},
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -211,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 5,
"id": "f46e46a7-9b57-44aa-9bc9-dcbcf643bc88",
"metadata": {},
"outputs": [
@@ -219,8 +189,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"jupyter-events==0.10.0\n",
"jupyter-lsp==2.2.5\n",
"jupyter_client==8.6.2\n",
"jupyter_core==5.7.2\n",
"jupyter_server==2.14.2\n",
"jupyter_server_terminals==0.5.3\n",
"jupyterlab==4.2.4\n",
"jupyterlab_pygments==0.3.0\n",
"jupyterlab_server==2.27.3\n",
"jupyterlab_widgets==3.0.11\n"
]
}