├── 1.jpg ├── 2.jpg ├── Holt_Winters.py └── data1.txt /1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlanConstantine/ExponentialSmoothing/19c22b9abda8ca282df2b2d38012f353bb465216/1.jpg -------------------------------------------------------------------------------- /2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlanConstantine/ExponentialSmoothing/19c22b9abda8ca282df2b2d38012f353bb465216/2.jpg -------------------------------------------------------------------------------- /Holt_Winters.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Date : 2017-04-11 21:27:00 3 | # @Author : Alan Lau (rlalan@outlook.com) 4 | # @Language : Python3.5 5 | 6 | import numpy as np 7 | from matplotlib import pyplot as plt 8 | 9 | 10 | def exponential_smoothing(alpha, s): 11 | s2 = np.zeros(s.shape) 12 | s2[0] = s[0] 13 | for i in range(1, len(s2)): 14 | s2[i] = alpha*s[i]+(1-alpha)*s2[i-1] 15 | return s2 16 | 17 | 18 | def show_data(new_year, pre_year, data, s_pre_double, s_pre_triple): 19 | year, time_id, number = data.T 20 | 21 | plt.figure(figsize=(14, 6), dpi=80) 22 | plt.plot(year, number, color='blue', label="actual value") 23 | plt.plot(new_year[1:], s_pre_double[2:], 24 | color='red', label="double predicted value") 25 | plt.plot(new_year[1:], s_pre_triple[2:], 26 | color='green', label="triple predicted value") 27 | plt.legend(loc='lower right') 28 | plt.title('Projects') 29 | plt.xlabel('year') 30 | plt.ylabel('number') 31 | plt.xticks(new_year) 32 | # plt.colors() 33 | plt.show() 34 | 35 | 36 | def main(): 37 | alpha = .70 38 | pre_year = np.array([2016, 2017]) 39 | data_path = r'data1.txt' 40 | data = np.loadtxt(data_path) 41 | year, time_id, number = data.T 42 | initial_line = np.array([0, 0, number[0]]) 43 | initial_data = np.insert(data, 0, values=initial_line, axis=0) 44 | initial_year, initial_time_id, initial_number = initial_data.T 45 | 46 | s_single = exponential_smoothing(alpha, initial_number) 47 | s_double = exponential_smoothing(alpha, s_single) 48 | 49 | a_double = 2*s_single-s_double 50 | b_double = (alpha/(1-alpha))*(s_single-s_double) 51 | s_pre_double = np.zeros(s_double.shape) 52 | for i in range(1, len(initial_time_id)): 53 | s_pre_double[i] = a_double[i-1]+b_double[i-1] 54 | pre_next_year = a_double[-1]+b_double[-1]*1 55 | pre_next_two_year = a_double[-1]+b_double[-1]*2 56 | insert_year = np.array([pre_next_year, pre_next_two_year]) 57 | s_pre_double = np.insert(s_pre_double, len(s_pre_double), values=np.array( 58 | [pre_next_year, pre_next_two_year]), axis=0) 59 | 60 | s_triple = exponential_smoothing(alpha, s_double) 61 | 62 | a_triple = 3*s_single-3*s_double+s_triple 63 | b_triple = (alpha/(2*((1-alpha)**2)))*((6-5*alpha)*s_single - 64 | 2*((5-4*alpha)*s_double)+(4-3*alpha)*s_triple) 65 | c_triple = ((alpha**2)/(2*((1-alpha)**2)))*(s_single-2*s_double+s_triple) 66 | 67 | s_pre_triple = np.zeros(s_triple.shape) 68 | 69 | for i in range(1, len(initial_time_id)): 70 | s_pre_triple[i] = a_triple[i-1]+b_triple[i-1]*1 + c_triple[i-1]*(1**2) 71 | 72 | pre_next_year = a_triple[-1]+b_triple[-1]*1 + c_triple[-1]*(1**2) 73 | pre_next_two_year = a_triple[-1]+b_triple[-1]*2 + c_triple[-1]*(2**2) 74 | insert_year = np.array([pre_next_year, pre_next_two_year]) 75 | s_pre_triple = np.insert(s_pre_triple, len(s_pre_triple), values=np.array( 76 | [pre_next_year, pre_next_two_year]), axis=0) 77 | 78 | new_year = np.insert(year, len(year), values=pre_year, axis=0) 79 | output = np.array([new_year, s_pre_double, s_pre_triple]) 80 | print(output) 81 | show_data(new_year, pre_year, data, s_pre_double, s_pre_triple) 82 | 83 | 84 | if __name__ == '__main__': 85 | main() 86 | -------------------------------------------------------------------------------- /data1.txt: -------------------------------------------------------------------------------- 1 | #year time_id number 2 | 1994 1 10 3 | 1995 2 3 4 | 1996 3 27 5 | 1997 4 13 6 | 1998 5 12 7 | 1999 6 13 8 | 2000 7 14 9 | 2001 8 23 10 | 2002 9 32 11 | 2003 10 30 12 | 2004 11 36 13 | 2005 12 40 14 | 2006 13 58 15 | 2007 14 51 16 | 2008 15 73 17 | 2009 16 80 18 | 2010 17 106 19 | 2011 18 127 20 | 2012 19 135 21 | 2013 20 161 22 | 2014 21 149 23 | 2015 22 142 --------------------------------------------------------------------------------