[Tutor] I Need Help

Alan Gauld alan.gauld at yahoo.co.uk
Wed Aug 24 13:28:00 EDT 2016


On 24/08/16 11:26, Micheal Emeagi wrote:

> forcast values. Ft is the forcast list whose length should be one element
> greater than the Yt list. I want the elements of ft to increment by one
> while I use it to generate the subsequent element. But for some reasons I
> don't understand, it keeps changing the second element in Ft.

You have hard coded an index of 1, so it will always append a
calculation based on that list item..

> Could please help me figure what is going wrong with the while iteration.

I don;t understand your algorithm so don;t know what you are tryingt to
do, but the easiest way to deal with this kind of issue is usually to
manually step through the code listing each variable on each iteration.
Sometimes on paper or you could just use print statements.

Like this:

> yt = [1,2,3,4,5,6]
> ft = [yt[0],yt[0]]

This sets the first two values to 1, is that what you wanted?

> alpha = 0.5
> while len(ft) != len(yt) + 1:
>     ft.append(ft[1] + alpha * (yt[1] - ft[1]))

yt = [1,2,3,4,5,6]
ft = [1,1] -> [1,1,(1+0.5(2-1)] -> [1,1,1.5]

>     ft[1] += 1
>     yt[1] += 1

yt = [1,3,3,4,5,6]
ft = [1,2,1.5]
len(ft) != len(yt+1 -> True

Notice that this is where you always change the same index value.
I'm guessing you maybe need an index variable that you
increment round about here. But I'm not sure since I
don't really understand your algorithm. Continuing...

Next iteration:

>     ft.append(ft[1] + alpha * (yt[1] - ft[1]))

yt = [1,3,3,4,5,6]
ft = [1,2,1.5,] -> [1,2,1.5,(2+0.5(3-2)) -> [1,2,1.5,2.5]

>     ft[1] += 1
>     yt[1] += 1

yt = [1,4,3,4,5,6]
ft = [1,3,1.5,2.5]
len(ft) != len(yt)+1 -> True

Next iteration:

> ft.append(ft[1] + alpha * (yt[1] - ft[1]))

yt = [1,4,3,4,5,6]
ft = [1,3,1.5,2.5] -> [1,3,1.5,2.5,(3+0.5(4-3)) -> [1,3,1.5,2.5,3.5]

> ft[1] += 1
> yt[1] += 1

yt = [1,5,3,4,5,6]
ft = [1,4,1.5,2.5,3.5]
len(ft) != len(yt)+1 -> True

etc...

I'm assuming that's not what you were looking for but since
I don't know what you expected I can't say any more, other
than that, if it is, there are easier ways to do it!

I'm guessing you might want something like:

yt = [1,2,3,4,5,6]
ft = [yt[0]]
alpha = 0.5
for idx,n in enumerate(yt):
   ft.append(ft[idx] + alpha*(n-ft[idx]))

Which gives an ft of:

[1, 1.0, 1.5, 2.25, 3.125, 4.0625, 5.03125]

But that's just a wild guess at what you might be
trying to do. But maybe it will give you some
ideas.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list