-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExponential Smoothing By Jolly Madamedon.py
More file actions
151 lines (91 loc) · 2.84 KB
/
Copy pathExponential Smoothing By Jolly Madamedon.py
File metadata and controls
151 lines (91 loc) · 2.84 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
from datetime import timedelta
start_date = pd.to_datetime("2022-01-01")
end_date = start_date + timedelta(days=199)
date_range = pd.date_range(start_date, end_date, freq='D')
data = {
'Date': date_range,
'Value': np.linspace(50, 150, 200) + np.random.normal(scale=10, size=200),
'Feature1': np.random.randint(1, 100, 200),
'Feature2': np.random.uniform(0, 1, 200)
}
df = pd.DataFrame(data)
# In[2]:
df.head()
# In[3]:
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
# In[4]:
df
# In[5]:
df["Date"] = pd.to_datetime(df["Date"])
df.set_index("Date", inplace = True)
# In[6]:
model = SimpleExpSmoothing(df["Value"])
fit_model = model.fit()
# In[7]:
fit_model.summary()
# In[8]:
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6))
plt.plot(df['Value'], label='Value')
plt.title('Time Series Plot of Value')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()
# In[9]:
value_summary = df['Value'].describe()
print(value_summary)
# In[10]:
window_size = 10
df['Moving Average'] = df['Value'].rolling(window=window_size).mean()
plt.figure(figsize=(12, 6))
plt.plot(df['Value'], label='Original Value', alpha=0.5)
plt.plot(df['Moving Average'], label=f'{window_size}-Day Moving Average', color='red')
plt.title('Time Series Plot with Moving Average')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()
# In[11]:
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 7))
plt.plot(df.index, df['Value'], label='Original Data')
df['Fitted'] = fit_model.fittedvalues
plt.plot(df.index, df['Fitted'], label='Fitted Values', color='red')
plt.title('Original Data vs Fitted Values')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()
# In[12]:
forecast_period = 90
forecast = fit_model.forecast(steps=forecast_period)
future_dates = pd.date_range(df.index[-1] + timedelta(days=1), periods=forecast_period)
plt.figure(figsize=(12, 7))
plt.plot(df.index, df['Value'], label='Original Data')
plt.plot(df.index, df['Fitted'], label='Fitted Values', color='red')
plt.plot(future_dates, forecast, label='Forecast', color='green')
plt.title('Original Data, Fitted Values, and Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()
# In[14]:
from statsmodels.tsa.holtwinters import Holt
holt_model = Holt(df['Value']).fit()
holt_forecast = holt_model.forecast(steps=90)
plt.figure(figsize=(12, 6))
plt.plot(df.index, df['Value'], label='Original Data')
plt.plot(df.index, holt_model.fittedvalues, label='Holt Fitted Values', color='orange')
plt.plot(future_dates, holt_forecast, label='Holt Forecast', color='green')
plt.title('Holt’s Linear Trend Model - Original Data, Fitted Values, and Forecast')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()
# In[ ]: