{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Granger causality Part II: The Movie\nThis is a follow-up notebook to [\"*Granger causality testing for 1 day*\"](https://www.kaggle.com/carlmcbrideellis/granger-causality-testing-for-1-day). The cryptocurrencies on the X-axis are the *leaders*, and on the Y-axis the *followers*. The greater the value, the more they follow. Note that I pre-set the maximum value of the colour scale to be 100 to better allow comparison between days. However, that is not the global maximum value, which for the dates examined below corresponds to the 7th of September 2021 with a great many tests with high values (one reaching a value of over 290).\n### Just press play...","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nfrom matplotlib import rc\nfrom IPython.display import HTML\nimport matplotlib.animation as animation\n\nimport numpy as np \nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n#plt.style.use('fivethirtyeight')\nplt.rcParams.update({'font.size': 14})\n#plt.rcParams[\"figure.figsize\"] = (8, 8)\nimport seaborn as sns\nimport plotly.express as px\n\nimport datetime\nfrom datetime import timedelta, date\n\nimport statsmodels.api as sm\nfrom statsmodels.tsa.stattools import grangercausalitytests\n\n# read in the data\ntrain = pd.read_csv(\"../input/g-research-crypto-forecasting/train.csv\")\nasset_details = pd.read_csv(\"../input/g-research-crypto-forecasting/asset_details.csv\")\ntrain['timestamp'] = pd.to_datetime(train['timestamp'], unit='s')\ntrain = train.set_index('timestamp')\nmapping = dict(asset_details[['Asset_ID', 'Asset_Name']].values)\ntrain[\"Asset_name\"] = train[\"Asset_ID\"].map(mapping)\ncryptocurrencies = asset_details['Asset_Name'].unique()\n\ngranger_matrix = np.zeros((14, 14), float)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-16T07:53:43.943974Z","iopub.execute_input":"2021-11-16T07:53:43.944563Z","iopub.status.idle":"2021-11-16T07:54:48.814391Z","shell.execute_reply.started":"2021-11-16T07:53:43.944428Z","shell.execute_reply":"2021-11-16T07:54:48.813508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_day  = datetime.datetime(2021, 5, 1, 0, 0, 0)  # start from midnight the 1st of May 2021\ndelta_time = datetime.timedelta(days=1) # look at days\nn_steps    = 143  # look at the 143 days up to the 20th of September 2021 \nlag        =   1  # look at the 1 minute lag\ncolour_max = 100  # set the upper limit of the colour scale ","metadata":{"execution":{"iopub.status.busy":"2021-11-16T07:54:48.816966Z","iopub.execute_input":"2021-11-16T07:54:48.817664Z","iopub.status.idle":"2021-11-16T07:54:48.822275Z","shell.execute_reply.started":"2021-11-16T07:54:48.817628Z","shell.execute_reply":"2021-11-16T07:54:48.821493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\nfig, (ax) = plt.subplots(1, figsize = (11, 11));\n\ndef animate(time_stepper):\n    start_time = first_day  + (time_stepper * delta_time)\n    end_time   = start_time + delta_time\n    # now extract this time window from the training data\n    last_day   = train.loc[start_time:end_time]\n    # create columns of the Close values\n    close_columns = pd.pivot_table(last_day,index='timestamp',columns='Asset_ID',values='Close')\n    for i in range(14):\n        for j in range(14):\n            results = grangercausalitytests(close_columns[[i, j]].pct_change().dropna(), [lag], verbose=False)\n            granger_matrix[i,j] = results[lag][0]['ssr_ftest'][0]\n    ax.cla() # clears the annotations rather than overwriting\n    sns.heatmap(ax = ax, data = granger_matrix,cmap='YlOrBr',annot=True, fmt='.1f',cbar=False, vmin=0, vmax=colour_max)\n    plt.title(start_time.strftime(\"%-d %B %Y\") +\" to \"+ end_time.strftime(\"%-d %B %Y\"))\n    plt.yticks(asset_details.Asset_ID.values +0.5, asset_details.Asset_Name.values, rotation='horizontal');\n    plt.xticks(asset_details.Asset_ID.values +0.5, asset_details.Asset_Name.values, rotation='vertical');\n\nanim = animation.FuncAnimation(fig, animate, frames=n_steps, save_count=n_steps, repeat=False)\nHTML(anim.to_jshtml())","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-11-16T07:54:48.834648Z","iopub.execute_input":"2021-11-16T07:54:48.834949Z","iopub.status.idle":"2021-11-16T07:58:48.998427Z","shell.execute_reply.started":"2021-11-16T07:54:48.834924Z","shell.execute_reply":"2021-11-16T07:58:48.997597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HTML(anim.to_jshtml())","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-16T07:58:48.999559Z","iopub.execute_input":"2021-11-16T07:58:48.999763Z","iopub.status.idle":"2021-11-16T07:58:49.307199Z","shell.execute_reply.started":"2021-11-16T07:58:48.999738Z","shell.execute_reply":"2021-11-16T07:58:49.306604Z"},"trusted":true},"execution_count":null,"outputs":[]}]}