Numerical Python question

Carl Banks imbosol at aerojockey.invalid
Sun Oct 12 19:53:19 EDT 2003


2mc wrote:
> 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?

Yes, it should be far faster.  The main benefit of Numeric arrays is
they can do all kinds of array operations without having to wite a
loop in Python.

The main advantage of this method over the method using "sum" is that
this method operates on large arrays while iterating 10 times in
Python.  The method using "sum" operates on 10-element arrays while
iterating 100 thousand or so times in Python.  It's easy to see that
the former method puts Numeric is put to better use.


> 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.

Well, I don't remember the exact formula for standard deviation--but
here is an example that does something to that effect (it uses the
average calculated above):

    S = zeros(N-9,Float)
    for j in range(10):
        S += sqrt(D[j:N-9+j] - A)
    S /= 10.0

The key is to think of the array slices as if they were regular scalar
values.  The above method calculates the SD (not really) pretty much
the same way one would calculate the SD using regular old numbers.

Note that sqrt is Numeric.sqrt, not math.sqrt.


-- 
CARL BANKS                   http://www.aerojockey.com/software

As the newest Lady Turnpot descended into the kitchen wrapped only in
her celery-green dressing gown, her creamy bosom rising and falling
like a temperamental souffle, her tart mouth pursed in distaste, the
sous-chef whispered to the scullery boy, "I don't know what to make of
her." 
          --Laurel Fortuner, Montendre, France 
            1992 Bulwer-Lytton Fiction Contest Winner




More information about the Python-list mailing list