ibnummuhammad
commited on
Commit
•
3b9a22e
1
Parent(s):
672c1c4
Add grangers_causation_matrix()
Browse files
granger_causality_testing.ipynb
CHANGED
@@ -2,13 +2,15 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
9 |
"import numpy as np\n",
|
10 |
"import pandas as pd\n",
|
11 |
"import plotly.express as px\n",
|
|
|
|
|
12 |
"from statsmodels.stats.stattools import durbin_watson\n",
|
13 |
"from statsmodels.tsa.api import VAR\n",
|
14 |
"from statsmodels.tsa.stattools import adfuller\n",
|
@@ -7303,6 +7305,78 @@
|
|
7303 |
"grangers_causation_matrix(df_train_transformed, variables = df_train_transformed.columns)"
|
7304 |
]
|
7305 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7306 |
{
|
7307 |
"cell_type": "code",
|
7308 |
"execution_count": null,
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 43,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
9 |
"import numpy as np\n",
|
10 |
"import pandas as pd\n",
|
11 |
"import plotly.express as px\n",
|
12 |
+
"from sklearn.metrics import mean_absolute_error\n",
|
13 |
+
"from sklearn.metrics import mean_squared_error\n",
|
14 |
"from statsmodels.stats.stattools import durbin_watson\n",
|
15 |
"from statsmodels.tsa.api import VAR\n",
|
16 |
"from statsmodels.tsa.stattools import adfuller\n",
|
|
|
7305 |
"grangers_causation_matrix(df_train_transformed, variables = df_train_transformed.columns)"
|
7306 |
]
|
7307 |
},
|
7308 |
+
{
|
7309 |
+
"cell_type": "code",
|
7310 |
+
"execution_count": 39,
|
7311 |
+
"metadata": {},
|
7312 |
+
"outputs": [],
|
7313 |
+
"source": [
|
7314 |
+
"lag_order = results.k_ar\n",
|
7315 |
+
"\n",
|
7316 |
+
"df_input = df_train_transformed.values[-lag_order:]\n",
|
7317 |
+
"df_forecast = results.forecast(y=df_input, steps=n_obs)\n",
|
7318 |
+
"df_forecast = (pd.DataFrame(df_forecast, index=df_test.index, columns=df_test.columns + '_pred'))"
|
7319 |
+
]
|
7320 |
+
},
|
7321 |
+
{
|
7322 |
+
"cell_type": "code",
|
7323 |
+
"execution_count": 40,
|
7324 |
+
"metadata": {},
|
7325 |
+
"outputs": [],
|
7326 |
+
"source": [
|
7327 |
+
"def invert_transformation(df, pred):\n",
|
7328 |
+
" forecast = df_forecast.copy()\n",
|
7329 |
+
" columns = df.columns\n",
|
7330 |
+
" for col in columns:\n",
|
7331 |
+
" forecast[str(col)+'_pred'] = df[col].iloc[-1] + forecast[str(col)+'_pred'].cumsum()\n",
|
7332 |
+
" return forecast"
|
7333 |
+
]
|
7334 |
+
},
|
7335 |
+
{
|
7336 |
+
"cell_type": "code",
|
7337 |
+
"execution_count": 41,
|
7338 |
+
"metadata": {},
|
7339 |
+
"outputs": [],
|
7340 |
+
"source": [
|
7341 |
+
"output = invert_transformation(df_train, df_forecast)\n",
|
7342 |
+
"\n",
|
7343 |
+
"combined = pd.concat([output['apple_pred'], df_test['apple'], output['walmart_pred'], df_test['walmart'], output['tesla_pred'], df_test['tesla']], axis=1)"
|
7344 |
+
]
|
7345 |
+
},
|
7346 |
+
{
|
7347 |
+
"cell_type": "code",
|
7348 |
+
"execution_count": 44,
|
7349 |
+
"metadata": {},
|
7350 |
+
"outputs": [
|
7351 |
+
{
|
7352 |
+
"name": "stdout",
|
7353 |
+
"output_type": "stream",
|
7354 |
+
"text": [
|
7355 |
+
"Forecast accuracy of Apple\n",
|
7356 |
+
"RMSE: 4.37\n",
|
7357 |
+
"MAE: 3.67\n"
|
7358 |
+
]
|
7359 |
+
},
|
7360 |
+
{
|
7361 |
+
"name": "stderr",
|
7362 |
+
"output_type": "stream",
|
7363 |
+
"text": [
|
7364 |
+
"/home/ibnu/miniconda3/envs/py312/lib/python3.12/site-packages/sklearn/metrics/_regression.py:483: FutureWarning:\n",
|
7365 |
+
"\n",
|
7366 |
+
"'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'.\n",
|
7367 |
+
"\n"
|
7368 |
+
]
|
7369 |
+
}
|
7370 |
+
],
|
7371 |
+
"source": [
|
7372 |
+
"rmse = mean_squared_error(combined['apple_pred'], combined['apple'], squared=False)\n",
|
7373 |
+
"mae = mean_absolute_error(combined['apple_pred'], combined['apple'])\n",
|
7374 |
+
"\n",
|
7375 |
+
"print('Forecast accuracy of Apple')\n",
|
7376 |
+
"print('RMSE: ', round(rmse,2))\n",
|
7377 |
+
"print('MAE: ', round(mae,2))"
|
7378 |
+
]
|
7379 |
+
},
|
7380 |
{
|
7381 |
"cell_type": "code",
|
7382 |
"execution_count": null,
|