[Tutor] I Need Help
Steven D'Aprano
steve at pearwood.info
Wed Aug 24 20:57:43 EDT 2016
On Wed, Aug 24, 2016 at 11:26:07AM +0100, Micheal Emeagi wrote:
> yt = [1,2,3,4,5,6]
> ft = [yt[0],yt[0]]
> alpha = 0.5
> while len(ft) != len(yt) + 1:
> ft.append(ft[1] + alpha * (yt[1] - ft[1]))
> print(ft)
> ft[1] += 1
> yt[1] += 1
>
> print (ft)
I think that your intention is to handle each element of yt, exactly
once each. It is nearly always easiest to use a for loop rather than a
while loop for these sorts of tasks.
So your code then becomes:
yt = [1, 2, 3, 4, 5, 6]
ft = [yt[0], yt[0]]
alpha = 0.5
for y in yt:
# ft[-1] always refers to the LAST element of ft
forecast_value = ft[-1] + alpha*(y - ft[-1])
ft.append(forecast_value)
print(ft)
When I run this code, I get:
[1, 1, 1.0]
[1, 1, 1.0, 1.5]
[1, 1, 1.0, 1.5, 2.25]
[1, 1, 1.0, 1.5, 2.25, 3.125]
[1, 1, 1.0, 1.5, 2.25, 3.125, 4.0625]
[1, 1, 1.0, 1.5, 2.25, 3.125, 4.0625, 5.03125]
Notice that ft has length TWO more than yt, rather than one. That's
because you initialise it with two copies of yt[0]. Are you sure that's
what you want?
--
Steve
More information about the Tutor
mailing list