Numerical Python question

2mc mcrider at bigfoot.com
Sun Oct 12 18:50:25 EDT 2003


Carl Banks <imbosol at aerojockey.invalid> wrote in message news:<awhib.4252$fv4.702 at nwrdny02.gnilink.net>...
> Let's say D[i] is the daily high at the end of day i, and N is the
> total number of days in the array.  To calculate 10-day averages, you
> can add ten slices offset by one, then divide the sum by 10:
> 
>     A = zeros(N-9,Float)
>     for j in range(10):
>         A += D[j:N-9+j]
>     A /= 10.0
> 
> Bam, that's it.  Instead of looping over tens of thousands of slices
> of ten, you're now looping over ten slices of tens of thousands.
> 
> This works because, when you add ten slices offset by one, the result
> is that each item contains the sum of ten consecutive numbers from the
> original array.

Thank you very much for this code.  It took me several looks at it,
before I understood it.  I'm still getting the cobwebs out of my brain
over the way I used to do things.

I have 2 questions.  This code snippet runs at "array speed," right? 
In other words, it will run much faster than if I tried to duplicate
this with lists, correct?

Also, can you give me an idea of how you would accomplish the 10 day
standard deviation of prices instead of the 10 day average?  It would
really help me get a handle on this.

Thank you.

Matt




More information about the Python-list mailing list