1

I am new to using the numpy class and I am having problems with manipulating the contents of the array. Here is the code:

# finance equation to apply to each element of array
for row in cum_ret:
   for col in row:
       if sum(row)!=0:
           row[col] = prev_row[col]*(1+row[col])
       else:
           row[col] = 1

       cum_ret[row][col] = row[col]
   prev_row = row
# see changed contents
for row in cum_ret:
print row

Now I am getting an error saying that array indices used must be of integer or boolean type. I get it because the 'row' value is also an array so it can't index an array object. So what is the correct syntax for doing this, or is their a method I am supposed to use?

Thank in advance

The cum_ret array is a 2d ndarray of float64s and is the array I want to modify. Here is a short snippet of the output:

[[ 0.          0.          0.          0.          0.        ]
 [ 0.00046187  0.00836672  0.00020435 -0.00048292  0.00342209]
 [-0.07633505 -0.00514199 -0.04133778 -0.02450642 -0.01865075]
 ..., 
 [ 0.01229435  0.00175341  0.00709808  0.00213371  0.0061171 ]
 [-0.0118614  -0.00994933 -0.00557095 -0.00141945 -0.00347423]
 [ 0.01214725 -0.00502466  0.00537611 -0.00035537 -0.00101685]]

And here is were it is occurring:

Traceback (most recent call last):
File "qstk1.py", line 37, in <module>
   cum_ret[row][col] = row[col]

IndexError: arrays used as indices must be of integer (or boolean) type
1
  • What is the data type and shape of cum_ret? Where exactly is the error occurring? What's the stack trace? Commented Sep 16, 2011 at 13:11

1 Answer 1

6

If cum_ret is the array, you can use numpy.sum(cum_ret, axis=1) to get the row-sum and numpy.sum(cum_ret, axis=1) != 0 to generate your test on the whole array at once. Then you can use numpy.select() to apply your conditions.

If you put a print row statement at the beginning of your loop you will notice that it is not integer, rather it is a numpy array... That is causing your error. BTW, you shouldn't need any such looping to perform this operation.

Another consideration: what happens on the first row? What is considered the previous row?

EDIT:

After reading your comments, I think you want something like this:

import numpy
cum_ret = numpy.array([[0,0,0,0],[-0.234,-0.365,-0.634,-0.453], [-0.334,-0.465,-0.534,-0.653],[-0.134,-0.265,-0.334,-0.453]])
b = cum_ret + 1
c = numpy.cumprod(b, axis=0)

No looping required, and no need to check your condition for rows of zero.

Sign up to request clarification or add additional context in comments.

6 Comments

The first row will have all zero entries always. It is a special case so I just add 1, otherwise everything would be 0. The equation is recursive so row 1's new contents depends on row 0's values. Don't you need a loop if you are going to change every single element in the array?
So are there rows with all 0's elsewhere?
Does it really need to be a recursive function? Can you provide more details about what you are calculating?
cumulative daily returns is what I am calculating and that is the equation needed to do that. So the rows are the time period and columns are daily returns of different stocks during that time. So the cumulative return for row 2 is cumulative return for row 1 * ( 1+ daily_return of row(2) ).
@Aladdin: see my edit. You can apply the +1 first, then take the cumulative product using numpy.cumprod().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.