ibnummuhammad commited on
Commit
1671916
1 Parent(s): 32b2ff4

Add granger_causality_testing.ipynb

Browse files
Files changed (1) hide show
  1. granger_causality_testing.ipynb +446 -3
granger_causality_testing.ipynb CHANGED
@@ -199,13 +199,456 @@
199
  },
200
  {
201
  "cell_type": "code",
202
- "execution_count": null,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  "metadata": {},
204
  "outputs": [],
205
  "source": [
206
- "df = pd.merge(df_apple[['Date', 'Adj Close']], df_walmart[['Date', 'Adj Close']], on='Date', how='right').rename(columns = {'Adj Close_x':'apple', 'Adj Close_y':'walmart'})\n",
207
- "df = df.merge(df_tesla[['Date', 'Adj Close']], on='Date', how='right').rename(columns={'Adj Close':'tesla'})"
208
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  }
210
  ],
211
  "metadata": {
 
199
  },
200
  {
201
  "cell_type": "code",
202
+ "execution_count": 13,
203
+ "metadata": {},
204
+ "outputs": [],
205
+ "source": [
206
+ "df = pd.merge(df_apple[['Date', 'Price']], df_walmart[['Date', 'Price']], on='Date', how='right').rename(columns = {'Adj Close_x':'apple', 'Adj Close_y':'walmart'})\n",
207
+ "df = df.merge(df_tesla[['Date', 'Price']], on='Date', how='right').rename(columns={'Adj Close':'tesla'})"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": 14,
213
+ "metadata": {},
214
+ "outputs": [
215
+ {
216
+ "data": {
217
+ "text/html": [
218
+ "<div>\n",
219
+ "<style scoped>\n",
220
+ " .dataframe tbody tr th:only-of-type {\n",
221
+ " vertical-align: middle;\n",
222
+ " }\n",
223
+ "\n",
224
+ " .dataframe tbody tr th {\n",
225
+ " vertical-align: top;\n",
226
+ " }\n",
227
+ "\n",
228
+ " .dataframe thead th {\n",
229
+ " text-align: right;\n",
230
+ " }\n",
231
+ "</style>\n",
232
+ "<table border=\"1\" class=\"dataframe\">\n",
233
+ " <thead>\n",
234
+ " <tr style=\"text-align: right;\">\n",
235
+ " <th></th>\n",
236
+ " <th>Date</th>\n",
237
+ " <th>Price_x</th>\n",
238
+ " <th>Price_y</th>\n",
239
+ " <th>Price</th>\n",
240
+ " </tr>\n",
241
+ " </thead>\n",
242
+ " <tbody>\n",
243
+ " <tr>\n",
244
+ " <th>0</th>\n",
245
+ " <td>02/01/2024</td>\n",
246
+ " <td>182.52</td>\n",
247
+ " <td>175.56</td>\n",
248
+ " <td>191.97</td>\n",
249
+ " </tr>\n",
250
+ " <tr>\n",
251
+ " <th>1</th>\n",
252
+ " <td>01/01/2024</td>\n",
253
+ " <td>184.40</td>\n",
254
+ " <td>165.25</td>\n",
255
+ " <td>187.29</td>\n",
256
+ " </tr>\n",
257
+ " <tr>\n",
258
+ " <th>2</th>\n",
259
+ " <td>12/01/2023</td>\n",
260
+ " <td>192.53</td>\n",
261
+ " <td>157.65</td>\n",
262
+ " <td>248.48</td>\n",
263
+ " </tr>\n",
264
+ " <tr>\n",
265
+ " <th>3</th>\n",
266
+ " <td>11/01/2023</td>\n",
267
+ " <td>189.95</td>\n",
268
+ " <td>155.69</td>\n",
269
+ " <td>240.08</td>\n",
270
+ " </tr>\n",
271
+ " <tr>\n",
272
+ " <th>4</th>\n",
273
+ " <td>10/01/2023</td>\n",
274
+ " <td>170.77</td>\n",
275
+ " <td>163.41</td>\n",
276
+ " <td>200.84</td>\n",
277
+ " </tr>\n",
278
+ " <tr>\n",
279
+ " <th>...</th>\n",
280
+ " <td>...</td>\n",
281
+ " <td>...</td>\n",
282
+ " <td>...</td>\n",
283
+ " <td>...</td>\n",
284
+ " </tr>\n",
285
+ " <tr>\n",
286
+ " <th>159</th>\n",
287
+ " <td>11/01/2010</td>\n",
288
+ " <td>11.11</td>\n",
289
+ " <td>54.09</td>\n",
290
+ " <td>2.36</td>\n",
291
+ " </tr>\n",
292
+ " <tr>\n",
293
+ " <th>160</th>\n",
294
+ " <td>10/01/2010</td>\n",
295
+ " <td>10.75</td>\n",
296
+ " <td>54.17</td>\n",
297
+ " <td>1.46</td>\n",
298
+ " </tr>\n",
299
+ " <tr>\n",
300
+ " <th>161</th>\n",
301
+ " <td>09/01/2010</td>\n",
302
+ " <td>10.13</td>\n",
303
+ " <td>53.52</td>\n",
304
+ " <td>1.36</td>\n",
305
+ " </tr>\n",
306
+ " <tr>\n",
307
+ " <th>162</th>\n",
308
+ " <td>08/01/2010</td>\n",
309
+ " <td>8.68</td>\n",
310
+ " <td>50.14</td>\n",
311
+ " <td>1.30</td>\n",
312
+ " </tr>\n",
313
+ " <tr>\n",
314
+ " <th>163</th>\n",
315
+ " <td>07/01/2010</td>\n",
316
+ " <td>9.19</td>\n",
317
+ " <td>51.19</td>\n",
318
+ " <td>1.33</td>\n",
319
+ " </tr>\n",
320
+ " </tbody>\n",
321
+ "</table>\n",
322
+ "<p>164 rows × 4 columns</p>\n",
323
+ "</div>"
324
+ ],
325
+ "text/plain": [
326
+ " Date Price_x Price_y Price\n",
327
+ "0 02/01/2024 182.52 175.56 191.97\n",
328
+ "1 01/01/2024 184.40 165.25 187.29\n",
329
+ "2 12/01/2023 192.53 157.65 248.48\n",
330
+ "3 11/01/2023 189.95 155.69 240.08\n",
331
+ "4 10/01/2023 170.77 163.41 200.84\n",
332
+ ".. ... ... ... ...\n",
333
+ "159 11/01/2010 11.11 54.09 2.36\n",
334
+ "160 10/01/2010 10.75 54.17 1.46\n",
335
+ "161 09/01/2010 10.13 53.52 1.36\n",
336
+ "162 08/01/2010 8.68 50.14 1.30\n",
337
+ "163 07/01/2010 9.19 51.19 1.33\n",
338
+ "\n",
339
+ "[164 rows x 4 columns]"
340
+ ]
341
+ },
342
+ "execution_count": 14,
343
+ "metadata": {},
344
+ "output_type": "execute_result"
345
+ }
346
+ ],
347
+ "source": [
348
+ "df"
349
+ ]
350
+ },
351
+ {
352
+ "cell_type": "code",
353
+ "execution_count": 15,
354
+ "metadata": {},
355
+ "outputs": [],
356
+ "source": [
357
+ "df['Date'] = pd.to_datetime(df['Date'])"
358
+ ]
359
+ },
360
+ {
361
+ "cell_type": "code",
362
+ "execution_count": 16,
363
+ "metadata": {},
364
+ "outputs": [
365
+ {
366
+ "data": {
367
+ "text/html": [
368
+ "<div>\n",
369
+ "<style scoped>\n",
370
+ " .dataframe tbody tr th:only-of-type {\n",
371
+ " vertical-align: middle;\n",
372
+ " }\n",
373
+ "\n",
374
+ " .dataframe tbody tr th {\n",
375
+ " vertical-align: top;\n",
376
+ " }\n",
377
+ "\n",
378
+ " .dataframe thead th {\n",
379
+ " text-align: right;\n",
380
+ " }\n",
381
+ "</style>\n",
382
+ "<table border=\"1\" class=\"dataframe\">\n",
383
+ " <thead>\n",
384
+ " <tr style=\"text-align: right;\">\n",
385
+ " <th></th>\n",
386
+ " <th>Date</th>\n",
387
+ " <th>Price_x</th>\n",
388
+ " <th>Price_y</th>\n",
389
+ " <th>Price</th>\n",
390
+ " </tr>\n",
391
+ " </thead>\n",
392
+ " <tbody>\n",
393
+ " <tr>\n",
394
+ " <th>0</th>\n",
395
+ " <td>2024-02-01</td>\n",
396
+ " <td>182.52</td>\n",
397
+ " <td>175.56</td>\n",
398
+ " <td>191.97</td>\n",
399
+ " </tr>\n",
400
+ " <tr>\n",
401
+ " <th>1</th>\n",
402
+ " <td>2024-01-01</td>\n",
403
+ " <td>184.40</td>\n",
404
+ " <td>165.25</td>\n",
405
+ " <td>187.29</td>\n",
406
+ " </tr>\n",
407
+ " <tr>\n",
408
+ " <th>2</th>\n",
409
+ " <td>2023-12-01</td>\n",
410
+ " <td>192.53</td>\n",
411
+ " <td>157.65</td>\n",
412
+ " <td>248.48</td>\n",
413
+ " </tr>\n",
414
+ " <tr>\n",
415
+ " <th>3</th>\n",
416
+ " <td>2023-11-01</td>\n",
417
+ " <td>189.95</td>\n",
418
+ " <td>155.69</td>\n",
419
+ " <td>240.08</td>\n",
420
+ " </tr>\n",
421
+ " <tr>\n",
422
+ " <th>4</th>\n",
423
+ " <td>2023-10-01</td>\n",
424
+ " <td>170.77</td>\n",
425
+ " <td>163.41</td>\n",
426
+ " <td>200.84</td>\n",
427
+ " </tr>\n",
428
+ " <tr>\n",
429
+ " <th>...</th>\n",
430
+ " <td>...</td>\n",
431
+ " <td>...</td>\n",
432
+ " <td>...</td>\n",
433
+ " <td>...</td>\n",
434
+ " </tr>\n",
435
+ " <tr>\n",
436
+ " <th>159</th>\n",
437
+ " <td>2010-11-01</td>\n",
438
+ " <td>11.11</td>\n",
439
+ " <td>54.09</td>\n",
440
+ " <td>2.36</td>\n",
441
+ " </tr>\n",
442
+ " <tr>\n",
443
+ " <th>160</th>\n",
444
+ " <td>2010-10-01</td>\n",
445
+ " <td>10.75</td>\n",
446
+ " <td>54.17</td>\n",
447
+ " <td>1.46</td>\n",
448
+ " </tr>\n",
449
+ " <tr>\n",
450
+ " <th>161</th>\n",
451
+ " <td>2010-09-01</td>\n",
452
+ " <td>10.13</td>\n",
453
+ " <td>53.52</td>\n",
454
+ " <td>1.36</td>\n",
455
+ " </tr>\n",
456
+ " <tr>\n",
457
+ " <th>162</th>\n",
458
+ " <td>2010-08-01</td>\n",
459
+ " <td>8.68</td>\n",
460
+ " <td>50.14</td>\n",
461
+ " <td>1.30</td>\n",
462
+ " </tr>\n",
463
+ " <tr>\n",
464
+ " <th>163</th>\n",
465
+ " <td>2010-07-01</td>\n",
466
+ " <td>9.19</td>\n",
467
+ " <td>51.19</td>\n",
468
+ " <td>1.33</td>\n",
469
+ " </tr>\n",
470
+ " </tbody>\n",
471
+ "</table>\n",
472
+ "<p>164 rows × 4 columns</p>\n",
473
+ "</div>"
474
+ ],
475
+ "text/plain": [
476
+ " Date Price_x Price_y Price\n",
477
+ "0 2024-02-01 182.52 175.56 191.97\n",
478
+ "1 2024-01-01 184.40 165.25 187.29\n",
479
+ "2 2023-12-01 192.53 157.65 248.48\n",
480
+ "3 2023-11-01 189.95 155.69 240.08\n",
481
+ "4 2023-10-01 170.77 163.41 200.84\n",
482
+ ".. ... ... ... ...\n",
483
+ "159 2010-11-01 11.11 54.09 2.36\n",
484
+ "160 2010-10-01 10.75 54.17 1.46\n",
485
+ "161 2010-09-01 10.13 53.52 1.36\n",
486
+ "162 2010-08-01 8.68 50.14 1.30\n",
487
+ "163 2010-07-01 9.19 51.19 1.33\n",
488
+ "\n",
489
+ "[164 rows x 4 columns]"
490
+ ]
491
+ },
492
+ "execution_count": 16,
493
+ "metadata": {},
494
+ "output_type": "execute_result"
495
+ }
496
+ ],
497
+ "source": [
498
+ "df"
499
+ ]
500
+ },
501
+ {
502
+ "cell_type": "code",
503
+ "execution_count": 17,
504
  "metadata": {},
505
  "outputs": [],
506
  "source": [
507
+ "df = df.set_index('Date').rename_axis('company', axis=1)"
 
508
  ]
509
+ },
510
+ {
511
+ "cell_type": "code",
512
+ "execution_count": 18,
513
+ "metadata": {},
514
+ "outputs": [
515
+ {
516
+ "data": {
517
+ "text/html": [
518
+ "<div>\n",
519
+ "<style scoped>\n",
520
+ " .dataframe tbody tr th:only-of-type {\n",
521
+ " vertical-align: middle;\n",
522
+ " }\n",
523
+ "\n",
524
+ " .dataframe tbody tr th {\n",
525
+ " vertical-align: top;\n",
526
+ " }\n",
527
+ "\n",
528
+ " .dataframe thead th {\n",
529
+ " text-align: right;\n",
530
+ " }\n",
531
+ "</style>\n",
532
+ "<table border=\"1\" class=\"dataframe\">\n",
533
+ " <thead>\n",
534
+ " <tr style=\"text-align: right;\">\n",
535
+ " <th>company</th>\n",
536
+ " <th>Price_x</th>\n",
537
+ " <th>Price_y</th>\n",
538
+ " <th>Price</th>\n",
539
+ " </tr>\n",
540
+ " <tr>\n",
541
+ " <th>Date</th>\n",
542
+ " <th></th>\n",
543
+ " <th></th>\n",
544
+ " <th></th>\n",
545
+ " </tr>\n",
546
+ " </thead>\n",
547
+ " <tbody>\n",
548
+ " <tr>\n",
549
+ " <th>2024-02-01</th>\n",
550
+ " <td>182.52</td>\n",
551
+ " <td>175.56</td>\n",
552
+ " <td>191.97</td>\n",
553
+ " </tr>\n",
554
+ " <tr>\n",
555
+ " <th>2024-01-01</th>\n",
556
+ " <td>184.40</td>\n",
557
+ " <td>165.25</td>\n",
558
+ " <td>187.29</td>\n",
559
+ " </tr>\n",
560
+ " <tr>\n",
561
+ " <th>2023-12-01</th>\n",
562
+ " <td>192.53</td>\n",
563
+ " <td>157.65</td>\n",
564
+ " <td>248.48</td>\n",
565
+ " </tr>\n",
566
+ " <tr>\n",
567
+ " <th>2023-11-01</th>\n",
568
+ " <td>189.95</td>\n",
569
+ " <td>155.69</td>\n",
570
+ " <td>240.08</td>\n",
571
+ " </tr>\n",
572
+ " <tr>\n",
573
+ " <th>2023-10-01</th>\n",
574
+ " <td>170.77</td>\n",
575
+ " <td>163.41</td>\n",
576
+ " <td>200.84</td>\n",
577
+ " </tr>\n",
578
+ " <tr>\n",
579
+ " <th>...</th>\n",
580
+ " <td>...</td>\n",
581
+ " <td>...</td>\n",
582
+ " <td>...</td>\n",
583
+ " </tr>\n",
584
+ " <tr>\n",
585
+ " <th>2010-11-01</th>\n",
586
+ " <td>11.11</td>\n",
587
+ " <td>54.09</td>\n",
588
+ " <td>2.36</td>\n",
589
+ " </tr>\n",
590
+ " <tr>\n",
591
+ " <th>2010-10-01</th>\n",
592
+ " <td>10.75</td>\n",
593
+ " <td>54.17</td>\n",
594
+ " <td>1.46</td>\n",
595
+ " </tr>\n",
596
+ " <tr>\n",
597
+ " <th>2010-09-01</th>\n",
598
+ " <td>10.13</td>\n",
599
+ " <td>53.52</td>\n",
600
+ " <td>1.36</td>\n",
601
+ " </tr>\n",
602
+ " <tr>\n",
603
+ " <th>2010-08-01</th>\n",
604
+ " <td>8.68</td>\n",
605
+ " <td>50.14</td>\n",
606
+ " <td>1.30</td>\n",
607
+ " </tr>\n",
608
+ " <tr>\n",
609
+ " <th>2010-07-01</th>\n",
610
+ " <td>9.19</td>\n",
611
+ " <td>51.19</td>\n",
612
+ " <td>1.33</td>\n",
613
+ " </tr>\n",
614
+ " </tbody>\n",
615
+ "</table>\n",
616
+ "<p>164 rows × 3 columns</p>\n",
617
+ "</div>"
618
+ ],
619
+ "text/plain": [
620
+ "company Price_x Price_y Price\n",
621
+ "Date \n",
622
+ "2024-02-01 182.52 175.56 191.97\n",
623
+ "2024-01-01 184.40 165.25 187.29\n",
624
+ "2023-12-01 192.53 157.65 248.48\n",
625
+ "2023-11-01 189.95 155.69 240.08\n",
626
+ "2023-10-01 170.77 163.41 200.84\n",
627
+ "... ... ... ...\n",
628
+ "2010-11-01 11.11 54.09 2.36\n",
629
+ "2010-10-01 10.75 54.17 1.46\n",
630
+ "2010-09-01 10.13 53.52 1.36\n",
631
+ "2010-08-01 8.68 50.14 1.30\n",
632
+ "2010-07-01 9.19 51.19 1.33\n",
633
+ "\n",
634
+ "[164 rows x 3 columns]"
635
+ ]
636
+ },
637
+ "execution_count": 18,
638
+ "metadata": {},
639
+ "output_type": "execute_result"
640
+ }
641
+ ],
642
+ "source": [
643
+ "df"
644
+ ]
645
+ },
646
+ {
647
+ "cell_type": "code",
648
+ "execution_count": null,
649
+ "metadata": {},
650
+ "outputs": [],
651
+ "source": []
652
  }
653
  ],
654
  "metadata": {