This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.py
76 lines (70 loc) · 2.29 KB
/
ts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
__author__ = "Prakash"
import numpy as np
def simple_mv(price_train, price_valid, n = 12):
""" Moving Average Function
Input:
------
Price_train: the train data on which you want to run moving average
Price_valid: mainly to know the length of predictions you need to make. use validation data while validating and test_Data dataframe while Predicting.
alpha: weight parameter
n: the number of previous datapoints to consider.
Function:
--------
F(t+1) = (F(t) + F(t-1) + F(t-2) + .....F(t-n))/n
Returns:
-------
A list with predictions.
"""
if price_valid is None:
total_length = len(price_train)
else:
total_length = len(price_train)+len(price_valid)
pred =[]
for i in range(total_length):
if i < n:
pred.append(0)
elif i <= len(price_train):
x = price_train[i-n:i]
pred.append(np.mean(x))
else:
x = pred[-n:]
pred.append(np.mean(x))
return pred
def ses(price_train, price_valid, alpha =0.2, n = 27):
""" Moving Average Function
Input:
------
Price_train: the train data on which you want to run moving average
Price_valid: mainly to know the length of predictions you need to make. use validation data while validating and test_Data dataframe while Predicting.
alpha: weight parameter
n: the number of previous datapoints to consider.
Function:
--------
F(t+1) = alpha * F(t) + alpha * (1 - aplha) * F(t-1) + alpha * (1 - alpha)^2 * F(t-2) + .....N
Returns:
-------
A list with predictions.
"""
if price_valid is None:
total_length = len(price_train)
else:
total_length = len(price_train)+len(price_valid)
pred =[]
for i in range(total_length):
if i < n:
pred.append(0)
elif i <= len(price_train):
x = price_train[i-n:i]
val = []
for i in range(len(x)):
m = (alpha)*((1-alpha)**i)*(x[-i])
val.append(m)
pred.append(np.sum(val))
else:
x = pred[-n:]
val = []
for i in range(len(x)):
m = (alpha)*((1-alpha)**i)*(x[-i])
val.append(m)
pred.append(np.sum(val))
return pred