09.10.2023 Views

Advanced Data Analytics Using Python_ With Machine Learning, Deep Learning and NLP Examples ( 2023)

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6

Time Series

Here’s the example code for a moving average model:

import numpy as np

def running_mean(l, N):

# Also works for the(strictly invalid) cases when N is even.

if (N//2)*2 == N:

N = N - 1

front = np.zeros(N//2)

back = np.zeros(N//2)

for i in range(1, (N//2)*2, 2):

front[i//2] = np.convolve(l[:i], np.ones((i,))/i, mode

= 'valid')

for i in range(1, (N//2)*2, 2):

back[i//2] = np.convolve(l[-i:], np.ones((i,))/i, mode

= 'valid')

return np.concatenate([front, np.convolve(l,

np.ones((N,))/N, mode = 'valid'), back[::-1]])

print running_mean(2,21)

Autoregressive Processes

Suppose {Z t } is a purely random process with mean 0 and variance σ z2 .

After that, a process {X t } is said to be of autoregressive process of order p if

you have this:

x = a x +¼ a x + z , or

t 1 t-1

p t-p t

x = a x + z

t

p

å

i=

1

i i-1

t

133

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!