<jupyter_start><jupyter_text>Holt’s linear trend method<jupyter_code>import pandas as pd
import warnings 
warnings.filterwarnings('ignore')


col_names =  ['Day', 'Value']
data  = pd.DataFrame(columns = col_names)

data['Day'] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
data['Value'] = [39,44,40,45,41,46,42,47,43,48,44,48,45,50]
data<jupyter_output><empty_output><jupyter_text>**Holt (1957) extended simple exponential smoothing to allow the forecasting of data with a trend. This method involves a forecast equation and two smoothing equations (one for the level and one for the trend):**\begin{align*}  \text{Forecast equation}&& \hat{y}_{t+h|t} &= \ell_{t} + hb_{t} \\  \text{Level equation}   && \ell_{t} &= \alpha y_{t} + (1 - \alpha)(\ell_{t-1} + b_{t-1})\\  \text{Trend equation}   && b_{t}    &= \beta^*(\ell_{t} - \ell_{t-1}) + (1 -\beta^*)b_{t-1},\end{align*}where  ℓt  denotes an estimate of the level of the series at time  t ,  bt  denotes an estimate of the trend (slope) of the series at time  t ,  α  is the smoothing parameter for the level,  0≤α≤1 , and  β∗  is the smoothing parameter for the trend,  0≤β∗≤1<jupyter_code>import seaborn as sns
import matplotlib.pyplot as plt
sns.pointplot(x='Day', y='Value', data=data)
plt.show()<jupyter_output><empty_output><jupyter_text>There is an overall **increasing trend** and hence we can make use of Holts method We normally obtain the initial level and slope values by **MINIMIZING SUM SQUARED ERROR OVER PERIODS T (here 14)**Obtain initial estimates l0 and b0 by fitting a least squares trend line to the historical data.** y-intercept = l0 ; slope = b0**<jupyter_code>### FINDING THE SLOPE AND GRADIENT

x_mean = sum(data['Day'])/len(data)
y_mean = sum(data['Value'])/len(data)

numerator = []
denominator = []
for i in range(1,len(data)):
    x_val = (data['Day'][i] - x_mean)
    y_val = (data['Value'][i] - y_mean)
    numerator.append(x_val * y_val)
    denominator.append(x_val * x_val)
b0 = sum(numerator)/sum(denominator)
l0 = y_mean - (b0*x_mean)<jupyter_output><empty_output><jupyter_text>** Equation :**     y = 0.484(x) + 40.796<jupyter_code>print("SLOPE IS b0:",    b0)
print("INTERCEPT IS l0:",l0)
#### CONSIDER THE PARAMETERS ALPHA = 0.2 AND BETA = 0.1 ####

alpha = 0.2
beta = 0.1
l = [0] * (len(data)+1)
b = [0] * (len(data)+1)
l[0] = (l0)
b[0] = (b0)
forecast = []

for i in range(0,len(data)):
    forecast.append(b[i] + l[i])
    l[i+1] = (alpha*data['Value'][i]) + (1-alpha)*(b[i]+l[i])
    b[i+1] = beta*(l[i+1] - l[i]) + (1-beta)*b[i]
data['Level Lt'] = l[1:]
data['Slope Bt'] = b[1:]
data['Forecast'] = forecast<jupyter_output><empty_output><jupyter_text>Applying Holt’s linear method with  α =0.2  and  β =0.1<jupyter_code>data.loc[-1] = [0, 'NA' ,40.796414112203585, 0.4842876421823789,'NA' ]  # adding a row
data.index = data.index + 1  # shifting index
data = data.sort_index()  # sorting by index
data<jupyter_output><empty_output><jupyter_text>Lets calculate the error<jupyter_code>data = data[1:]
error = []
error_sq = []
for i in range(1,len(data)+1):
    e = data['Value'][i] - data['Forecast'][i]
    error.append(e)
    error_sq.append(e*e)
data['Error'] = error
data['Error_Square'] = error_sq
data
sum(data['Error_Square'])/ len(data)
fig, ax = plt.subplots()
sns.pointplot(x='Day', y='Value', data=data, ax=ax)
sns.pointplot(x='Day', y='Forecast', data=data, ax=ax, color='r')
sns.plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17987715706346885, 'fraction_numerical': 0.044457443696987425, 'mean_word_length': 5.031746031746032, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '608877', 'license': 'Apache-2.0', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1372, 'n_words': 330, 'path': 'Holts linear trend method.ipynb', 'repo_name': 'BrittGeek/Time-Series-Forecasting', 'size': '3419'}