I’m converting a Python
code to Julia
, I’m newbie to both in real, and got slight different in the output, which could be related to the indexes difference between the 2 languages but not sure:
Python
code is:
def triple_exponential_smoothing(series, slen, alpha, beta, gamma, n_preds):
result = []
seasonals = initial_seasonal_components(series, slen)
for i in range(len(series)+n_preds):
if i == 0: # initial values
smooth = series[0]
trend = initial_trend(series, slen)
result.append(series[0])
continue
if i >= len(series): # we are forecasting
m = i - len(series) + 1
result.append((smooth + m*trend) + seasonals[i%slen])
else:
val = series[i]
last_smooth, smooth = smooth, alpha*(val-seasonals[i%slen]) + (1-alpha)*(smooth+trend)
trend = beta * (smooth-last_smooth) + (1-beta)*trend
seasonals[i%slen] = gamma*(val-smooth) + (1-gamma)*seasonals[i%slen]
result.append(smooth+trend+seasonals[i%slen])
return result
Equivalent Julia
code is:
function triple_exponential_smoothing(series, slen, α, β, γ, n_preds)
result = []
smooth = 0.0
trend = 0.0
seasonals = initial_seasonal_components(series, slen)
println("The seasonalities are: $seasonals")
for i in (1:length(series) + n_preds + 1)
if i == 1 # iniial value
smooth = series[i]
trend = initial_trend(series, slen)
println("The initial_trend is: $trend")
push!(result, series[i])
elseif i >= length(series) # we are forecasting
m = i - length(series) + 1
push!(result, (smooth + m * trend) + seasonals[i % slen + 1])
else # we are simulating history
val = series[i]
last_smooth = smooth
smooth = α * (val - seasonals[i % slen + 1]) +
(1.0 - α)*(smooth + trend)
trend = β * (smooth - last_smooth) + (1.0 - β) * trend
seasonals[i % slen + 1] = γ * (val - smooth) +
(1.0 - γ) * seasonals[i % slen + 1]
push!(result, smooth + trend + seasonals[i % slen + 1])
end
end
println("The forecast is:")
result
return result
end
Output of Python
is:
The forecast is: [
30.0,
20.344492,
28.410053,
30.438124,
39.46682,
⋮
41.15883,
31.517647,
33.275066,
28.828945,
32.61863,
]
The output of Juia
is:
97-element Array{Any,1}:
30
20.1974145
28.31508149221089
30.25227323256118
39.31840215208643
⋮
30.541983724030448
31.02710824538603
26.624311736837914
29.164185056307655
19.070532032623255
My full code, to help testing, is:
initial_trend(series, slen) = sum(
map(i -> (last(i) - first(i)) / slen,
zip(series[1:slen], series[slen+1:2*slen])
)
) / slen
function initial_seasonal_components(series, slen)
season_averages = map(i -> sum(i) / length(i) ,Iterators.partition(series,12) |> collect)
return map(i -> begin
sum_of_vals_over_avg = 0.0
map(j -> # for j in (0:6-1)
sum_of_vals_over_avg += series[i + j * 12] - season_averages[j+1]
, (0:6-1)) #end
sum_of_vals_over_avg / 6
end
, (1:12))
end
function triple_exponential_smoothing(series, slen, α, β, γ, n_preds)
result = []
smooth = 0.0
trend = 0.0
seasonals = initial_seasonal_components(series, slen)
println("The seasonalities are: $seasonals")
for i in (1:length(series) + n_preds + 1)
if i == 1 # iniial value
smooth = series[i]
trend = initial_trend(series, slen)
println("The initial_trend is: $trend")
push!(result, series[i])
elseif i >= length(series) # we are forecasting
m = i - length(series) + 1
push!(result, (smooth + m * trend) + seasonals[i % slen + 1])
else # we are simulating history
val = series[i]
last_smooth = smooth
smooth = α * (val - seasonals[i % slen + 1]) +
(1.0 - α)*(smooth + trend)
trend = β * (smooth - last_smooth) + (1.0 - β) * trend
seasonals[i % slen + 1] = γ * (val - smooth) +
(1.0 - γ) * seasonals[i % slen + 1]
push!(result, smooth + trend + seasonals[i % slen + 1])
end
end
println("The forecast is:")
result
return result
end
series = [30,21,29,31,40,48,53,47,37,39,31,29,17,9,20,24,27,35,41,38,
27,31,27,26,21,13,21,18,33,35,40,36,22,24,21,20,17,14,17,19,
26,29,40,31,20,24,18,26,17,9,17,21,28,32,46,33,23,28,22,27,
18,8,17,21,31,34,44,38,31,30,26,32];
triple_exponential_smoothing(series, 12, 0.716, 0.029, 0.993, 24)