0% found this document useful (0 votes)
8 views11 pages

Time Series Updated

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Time Series Updated

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

TY_COMP_A_72_KOMAL_SINHA

Apply Time-Series On Nifty-50 Dataset


import pandas as pd
import numpy as p
import warnings
warnings.filterwarnings('ignore')

ts=pd.read_csv("HDFCBANK.csv")

ts.head()

Prev Deliverable
Date Symbol Series Open High Low Last Close VWAP Volume Turnover Trades %Delive
Close Volume

2000-
0 HDFCBANK EQ 157.40 166.00 170.00 166.00 170.00 170.00 169.52 33259 5.638122e+11 NaN NaN
01-03

2000-
1 HDFCBANK EQ 170.00 182.00 183.45 171.00 174.00 173.80 174.99 168710 2.952261e+12 NaN NaN
01-04

2000-
2 HDFCBANK EQ 173.80 170.00 173.90 165.00 168.00 166.95 169.20 159820 2.704094e+12 NaN NaN
01-05

2000-
3 HDFCBANK EQ 166.95 168.00 170.00 165.30 168.95 168.30 168.44 85026 1.432166e+12 NaN NaN
01-06

2000-
4 HDFCBANK EQ 168.30 162.15 171.00 162.15 170.75 168.35 166.79 85144 1.420158e+12 NaN NaN
01-07

ts.shape

(5306, 15)

ts.tail()

Prev Deliver
Date Symbol Series Open High Low Last Close VWAP Volume Turnover Trades
Close

2021-
5301 HDFCBANK EQ 1414.15 1413.00 1429.00 1402.75 1407.55 1404.8 1413.19 15085476 2.131861e+15 291268.0 97918
04-26

2021-
5302 HDFCBANK EQ 1404.80 1407.25 1442.00 1404.80 1435.05 1438.7 1430.40 10296453 1.472810e+15 233200.0 56502
04-27

2021-
5303 HDFCBANK EQ 1438.70 1436.25 1479.00 1431.00 1475.00 1476.8 1463.19 12051970 1.763438e+15 197146.0 71966
04-28

2021-
5304 HDFCBANK EQ 1476.80 1486.20 1503.65 1461.00 1471.65 1472.5 1481.15 12039276 1.783196e+15 252296.0 48185
04-29

2021-
5305 HDFCBANK EQ 1472.50 1445.00 1453.80 1407.50 1412.90 1412.3 1421.13 17616451 2.503529e+15 447876.0 89829
04-30

ts.isnull().sum()

Date 0
Symbol 0
Series 0
Prev Close 0
Open 0
High 0
Low 0
Last 0
Close 0
VWAP 0
Volume 0
Turnover 0
Trades 2850
Deliverable Volume 509
%Deliverble 509
dtype: int64

ts.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5306 entries, 0 to 5305
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 5306 non-null object
1 Symbol 5306 non-null object
2 Series 5306 non-null object
3 Prev Close 5306 non-null float64
4 Open 5306 non-null float64
5 High 5306 non-null float64
6 Low 5306 non-null float64
7 Last 5306 non-null float64
8 Close 5306 non-null float64
9 VWAP 5306 non-null float64
10 Volume 5306 non-null int64
11 Turnover 5306 non-null float64
12 Trades 2456 non-null float64
13 Deliverable Volume 4797 non-null float64
14 %Deliverble 4797 non-null float64
dtypes: float64(11), int64(1), object(3)
memory usage: 621.9+ KB

ts['Date']=pd.to_datetime(ts['Date'])

ts.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5306 entries, 0 to 5305
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 5306 non-null datetime64[ns]
1 Symbol 5306 non-null object
2 Series 5306 non-null object
3 Prev Close 5306 non-null float64
4 Open 5306 non-null float64
5 High 5306 non-null float64
6 Low 5306 non-null float64
7 Last 5306 non-null float64
8 Close 5306 non-null float64
9 VWAP 5306 non-null float64
10 Volume 5306 non-null int64
11 Turnover 5306 non-null float64
12 Trades 2456 non-null float64
13 Deliverable Volume 4797 non-null float64
14 %Deliverble 4797 non-null float64
dtypes: datetime64[ns](1), float64(11), int64(1), object(2)
memory usage: 621.9+ KB

ts.describe()

Date Prev Close Open High Low Last Close VWAP Volume

count 5306 5306.000000 5306.000000 5306.000000 5306.000000 5306.000000 5306.000000 5306.000000 5.306000e+03

2010-08-18
mean 1007.093884 1007.472767 1019.986939 993.822211 1007.364003 1007.330390 1006.929263 2.102580e+06
21:26:56.132679936

2000-01-03
min 157.400000 162.150000 167.900000 157.000000 163.000000 163.400000 161.400000 1.042000e+03
00:00:00

2005-04-13
25% 479.912500 482.112500 486.912500 473.100000 480.700000 480.200000 479.512500 3.058325e+05
12:00:00

2010-08-17
50% 934.750000 939.350000 953.950000 922.175000 935.600000 935.725000 937.455000 9.444650e+05
12:00:00

2015-12-17
75% 1421.000000 1423.525000 1440.000000 1399.000000 1422.812500 1421.000000 1420.567500 2.123647e+06
18:00:00

2021-04-30
max 2565.800000 2566.000000 2583.300000 2553.700000 2563.000000 2565.800000 2570.700000 1.005650e+08
00:00:00

std NaN 635.757762 635.461516 641.444674 629.502818 635.722752 635.675021 635.481917 3.970048e+06

ts.columns

Index(['Date', 'Symbol', 'Series', 'Prev Close', 'Open', 'High', 'Low', 'Last',


'Close', 'VWAP', 'Volume', 'Turnover', 'Trades', 'Deliverable Volume',
'%Deliverble'],
dtype='object')

numeric_columns = ts.select_dtypes(include=['float64', 'int64']).columns


correlation_matrix = ts[numeric_columns].corr()

correlation_matrix

Prev Deliverable
Open High Low Last Close VWAP Volume Turnover Trades %De
Close Volume

Prev Close 1.000000 0.998585 0.998357 0.998360 0.998259 0.998274 0.998439 0.130454 0.297208 0.141440 0.077712

Open 0.998585 1.000000 0.999660 0.999683 0.999494 0.999509 0.999699 0.131908 0.299158 0.143177 0.079493

High 0.998357 0.999660 1.000000 0.999441 0.999739 0.999746 0.999818 0.133611 0.300377 0.148713 0.080831

Low 0.998360 0.999683 0.999441 1.000000 0.999682 0.999705 0.999815 0.129105 0.296920 0.134617 0.076835

Last 0.998259 0.999494 0.999739 0.999682 1.000000 0.999983 0.999904 0.131446 0.298578 0.141770 0.078862

Close 0.998274 0.999509 0.999746 0.999705 0.999983 1.000000 0.999924 0.131224 0.298390 0.141255 0.078593

VWAP 0.998439 0.999699 0.999818 0.999815 0.999904 0.999924 1.000000 0.131496 0.298866 0.141381 0.078985

Volume 0.130454 0.131908 0.133611 0.129105 0.131446 0.131224 0.131496 1.000000 0.943630 0.886561 0.956175

Turnover 0.297208 0.299158 0.300377 0.296920 0.298578 0.298390 0.298866 0.943630 1.000000 0.862532 0.917001

Trades 0.141440 0.143177 0.148713 0.134617 0.141770 0.141255 0.141381 0.886561 0.862532 1.000000 0.810748

Deliverable
0.077712 0.079493 0.080831 0.076835 0.078862 0.078593 0.078985 0.956175 0.917001 0.810748 1.000000
Volume

%Deliverble -0.170163 -0.169105 -0.170689 -0.164952 -0.167904 -0.167849 -0.168143 -0.225536 -0.223488 -0.294889 -0.077598

ts.dropna(axis=0,inplace=True)

ts

Prev Delive
Date Symbol Series Open High Low Last Close VWAP Volume Turnover Trades
Close

2011-
2850 HDFCBANK EQ 2399.55 2390.00 2407.00 2374.75 2404.95 2398.20 2397.60 657167 1.575623e+14 32536.0
06-01

2011-
2851 HDFCBANK EQ 2398.20 2355.05 2366.70 2345.00 2361.00 2360.30 2356.74 672276 1.584380e+14 33279.0
06-02

2011-
2852 HDFCBANK EQ 2360.30 2359.95 2370.85 2348.50 2364.00 2356.75 2358.69 598853 1.412506e+14 26661.0
06-03

2011-
2853 HDFCBANK EQ 2356.75 2345.30 2390.00 2345.30 2390.00 2381.25 2366.03 584280 1.382424e+14 22785.0
06-06

2011-
2854 HDFCBANK EQ 2381.25 2365.10 2390.70 2363.80 2374.60 2376.50 2377.48 406347 9.660803e+13 11760.0
06-07

... ... ... ... ... ... ... ... ... ... ... ... ... ...

2021-
5301 HDFCBANK EQ 1414.15 1413.00 1429.00 1402.75 1407.55 1404.80 1413.19 15085476 2.131861e+15 291268.0 9791
04-26

2021-
5302 HDFCBANK EQ 1404.80 1407.25 1442.00 1404.80 1435.05 1438.70 1430.40 10296453 1.472810e+15 233200.0 5650
04-27

2021-
5303 HDFCBANK EQ 1438.70 1436.25 1479.00 1431.00 1475.00 1476.80 1463.19 12051970 1.763438e+15 197146.0 7196
04-28

2021-
5304 HDFCBANK EQ 1476.80 1486.20 1503.65 1461.00 1471.65 1472.50 1481.15 12039276 1.783196e+15 252296.0 4818
04-29

2021-
5305 HDFCBANK EQ 1472.50 1445.00 1453.80 1407.50 1412.90 1412.30 1421.13 17616451 2.503529e+15 447876.0 8982
04-30

2456 rows × 15 columns

ts.set_index(ts['Date'],inplace=True)

ts
Prev Deliv
Date Symbol Series Open High Low Last Close VWAP Volume Turnover Trades
Close

Date

2011- 2011-
HDFCBANK EQ 2399.55 2390.00 2407.00 2374.75 2404.95 2398.20 2397.60 657167 1.575623e+14 32536.0
06-01 06-01

2011- 2011-
HDFCBANK EQ 2398.20 2355.05 2366.70 2345.00 2361.00 2360.30 2356.74 672276 1.584380e+14 33279.0
06-02 06-02

2011- 2011-
HDFCBANK EQ 2360.30 2359.95 2370.85 2348.50 2364.00 2356.75 2358.69 598853 1.412506e+14 26661.0
06-03 06-03

2011- 2011-
HDFCBANK EQ 2356.75 2345.30 2390.00 2345.30 2390.00 2381.25 2366.03 584280 1.382424e+14 22785.0
06-06 06-06

2011- 2011-
HDFCBANK EQ 2381.25 2365.10 2390.70 2363.80 2374.60 2376.50 2377.48 406347 9.660803e+13 11760.0
06-07 06-07

... ... ... ... ... ... ... ... ... ... ... ... ... ...

2021- 2021-
HDFCBANK EQ 1414.15 1413.00 1429.00 1402.75 1407.55 1404.80 1413.19 15085476 2.131861e+15 291268.0 979
04-26 04-26

2021- 2021-
HDFCBANK EQ 1404.80 1407.25 1442.00 1404.80 1435.05 1438.70 1430.40 10296453 1.472810e+15 233200.0 565
04-27 04-27

2021- 2021-
HDFCBANK EQ 1438.70 1436.25 1479.00 1431.00 1475.00 1476.80 1463.19 12051970 1.763438e+15 197146.0 719
04-28 04-28

2021- 2021-
HDFCBANK EQ 1476.80 1486.20 1503.65 1461.00 1471.65 1472.50 1481.15 12039276 1.783196e+15 252296.0 481
04-29 04-29

2021- 2021-
HDFCBANK EQ 1472.50 1445.00 1453.80 1407.50 1412.90 1412.30 1421.13 17616451 2.503529e+15 447876.0 898
04-30 04-30

2456 rows × 15 columns

ts.describe()

Date Prev Close Open High Low Last Close VWAP Volume

count 2456 2456.000000 2456.000000 2456.000000 2456.000000 2456.000000 2456.000000 2456.000000 2.456000e+03

2016-05-12
mean 1205.632492 1205.485525 1216.206454 1193.575570 1205.299369 1205.230517 1205.046344 3.931527e+06
15:18:45.732899328

2011-06-01
min 406.000000 406.000000 413.000000 400.250000 402.400000 406.000000 405.950000 4.632200e+04
00:00:00

2013-11-13
25% 687.800000 687.562500 693.700000 679.950000 687.150000 687.800000 686.850000 1.343126e+06
18:00:00

2016-05-16
50% 1081.650000 1080.850000 1092.450000 1069.500000 1081.000000 1081.650000 1081.000000 2.173722e+06
12:00:00

2018-11-05
75% 1583.087500 1582.500000 1601.487500 1564.400000 1586.400000 1581.800000 1578.280000 3.937254e+06
06:00:00

2021-04-30
max 2565.800000 2566.000000 2583.300000 2553.700000 2563.000000 2565.800000 2570.700000 1.005650e+08
00:00:00

std NaN 558.743721 558.179064 561.419369 555.032094 558.238493 558.239334 558.320810 5.239172e+06

import matplotlib.pyplot as plt

plt.figure(figsize=(15,4))
plt.plot(ts['Close'],color='r')
plt.xlabel("Year's")
plt.ylabel("stockPrice")

Text(0, 0.5, 'stockPrice')


plt.figure(figsize=(10,5))
plt.plot(ts['Open'])
plt.xlabel("Year's")
plt.ylabel("stockPrice")

Text(0, 0.5, 'stockPrice')

plt.figure(figsize=(15,4))
plt.plot(ts['Volume'])
plt.xlabel("Year's")
plt.ylabel("stockPrice")

Text(0, 0.5, 'stockPrice')


plt.figure(figsize=(15,4))
plt.plot(ts['VWAP'])
plt.xlabel("Year's")
plt.ylabel("stockPrice")

Text(0, 0.5, 'stockPrice')

from statsmodels.tsa.stattools import adfuller

def adf_test(series):
result=adfuller(series)
print('ADF Statistics: {}'.format(result[0]))
print('p- value: {}'.format(result[1]))
if result[1] <= 0.05:
print("strong evidence against the null hypothesis, reject the null hypothesis. Data has no unit root and is
else:
print("weak evidence against null hypothesis, time series has a unit root, indicating it is non-stationary "

adf_test(ts['Open'])

ADF Statistics: -2.684389607711623


p- value: 0.07677872161055674
weak evidence against null hypothesis, time series has a unit root, indicating it is non-stationary

ts['Open'].shift(1)
Date
2011-06-01 NaN
2011-06-02 2390.00
2011-06-03 2355.05
2011-06-06 2359.95
2011-06-07 2345.30
...
2021-04-26 1409.00
2021-04-27 1413.00
2021-04-28 1407.25
2021-04-29 1436.25
2021-04-30 1486.20
Name: Open, Length: 2456, dtype: float64

ts['Open_1']=ts['Open']-ts['Open'].shift(1)

adf_test(ts['Open_1'].dropna())

ADF Statistics: -49.3669300564303


p- value: 0.0
strong evidence against the null hypothesis, reject the null hypothesis. Data has no unit root and is stationary

plt.plot(ts['Open_1'])

[<matplotlib.lines.Line2D at 0x1b610b848d0>]

plt.plot(ts['Open'])

[<matplotlib.lines.Line2D at 0x1b610bded90>]

ts['Open_2']=ts['Open']-ts['Open'].shift(12)

adf_test(ts['Open_2'].dropna())
ADF Statistics: -11.480493062985683
p- value: 5.008423821587318e-21
strong evidence against the null hypothesis, reject the null hypothesis. Data has no unit root and is stationary

plt.plot(ts['Open_2'])

[<matplotlib.lines.Line2D at 0x1b610a93ed0>]

ts.dropna(axis=0,inplace=True)

ts

Prev Deliv
Date Symbol Series Open High Low Last Close VWAP Volume Turnover Trades
Close

Date

2011- 2011-
HDFCBANK EQ 2343.00 2350.50 2366.10 2333.65 2350.00 2349.30 2350.16 710674 1.670199e+14 15745.0
06-17 06-17

2011- 2011-
HDFCBANK EQ 2349.30 2342.15 2361.45 2271.95 2300.10 2299.00 2301.09 781285 1.797806e+14 21737.0
06-20 06-20

2011- 2011-
HDFCBANK EQ 2299.00 2302.00 2344.00 2302.00 2327.65 2325.55 2328.93 507886 1.182832e+14 23828.0
06-21 06-21

2011- 2011-
HDFCBANK EQ 2325.55 2332.45 2347.80 2312.40 2320.00 2322.70 2326.28 309158 7.191893e+13 14680.0
06-22 06-22

2011- 2011-
HDFCBANK EQ 2322.70 2319.00 2339.45 2310.00 2339.00 2333.75 2326.89 413379 9.618860e+13 20115.0
06-23 06-23

... ... ... ... ... ... ... ... ... ... ... ... ... ...

2021- 2021-
HDFCBANK EQ 1414.15 1413.00 1429.00 1402.75 1407.55 1404.80 1413.19 15085476 2.131861e+15 291268.0 979
04-26 04-26

2021- 2021-
HDFCBANK EQ 1404.80 1407.25 1442.00 1404.80 1435.05 1438.70 1430.40 10296453 1.472810e+15 233200.0 565
04-27 04-27

2021- 2021-
HDFCBANK EQ 1438.70 1436.25 1479.00 1431.00 1475.00 1476.80 1463.19 12051970 1.763438e+15 197146.0 719
04-28 04-28

2021- 2021-
HDFCBANK EQ 1476.80 1486.20 1503.65 1461.00 1471.65 1472.50 1481.15 12039276 1.783196e+15 252296.0 481
04-29 04-29

2021- 2021-
HDFCBANK EQ 1472.50 1445.00 1453.80 1407.50 1412.90 1412.30 1421.13 17616451 2.503529e+15 447876.0 898
04-30 04-30

2444 rows × 17 columns

ts["Open_1"].plot(figsize=(15,4),color="r",ylim=[0,160] )

<Axes: xlabel='Date'>
ts["Open_2"].plot(figsize=(15,4),color="r",ylim=[0,250] )

<Axes: xlabel='Date'>

from statsmodels.graphics.tsaplots import plot_acf,plot_pacf

acf = plot_acf(ts["Open_1"]) #shift(1)


pacf = plot_pacf(ts["Open_1"])
acf = plot_acf(ts["Open_2"]) #shift(12)
pacf = plot_pacf(ts["Open_2"])
from statsmodels.tsa.arima_model import ARMA

import statsmodels.api as sm
model = sm.tsa.ARIMA(ts['Open'], order=(1, 0, 1))

import statsmodels.api as sm
p = 1 # autoregressive (AR) order
d = 0 # differencing (I) order
q = 1 # moving average (MA) order
ARMAmodel = sm.tsa.ARIMA(ts['Open'], order=(p, d, q))
ARmodel_fit = model.fit()

ypredicted = ARmodel_fit.predict(len(ts), len(ts)+2)


print(ypredicted)

2444 1443.909107
2445 1443.038651
2446 1442.171299
Name: predicted_mean, dtype: float64

Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy