0

this is how I got the two arrays (array 1 and array2) for my function:

x = np.arange(-5, 5,0.01)
prob=stats.norm.pdf(x,0,1)
prob_array=numpy.array(prob).reshape(1000,1) #array1
x_tran=m.transpose()
x_tran_array=array(x_tran)
mu_array=array(mu)           # mu is stock return 
mu_array1=numpy.array(mu_array).reshape(54966,1)
sigma_array=array(sigma)     #sigma is the historical volatility
sigma_array1=numpy.array(sigma_array).reshape(54966,1)
mu1_mat=mat(ones((1,1000)))  #for matrix calculation
original_x=mu_array1*mu1_mat+sigma_array1*x_tran_array #array2

I defined a function:

def TK_value(z,p):
    if z >= 0:
        utility=z**0.88
        prob=(p**0.61)/(p**0.61+(1-p)**0.61)**(1/0.61)
    else:
        utility= -2.25*(-z)**0.88
        prob=(p**0.69)/(p**0.69+(1-p)**0.69)**(1/0.69)
    return utility*prob


tks=TK_value(original_x,prob_array)

I have two arrays with original_x with shape((54966, 1000) and prob_array with shape (1000,1). I want to use original_x as z and prob_array as p in this function.

But the error is :

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

phipsgabler
  • 20,535
  • 4
  • 40
  • 60
nancyyin
  • 3
  • 2
  • Is `z` an array? – alani Aug 11 '20 at 16:16
  • How are you calling the function, what data are you supplying to `z` and are you sure that you actually are supplying what you think you are? The error seems to be for the comparison of `if z>=0:` and apparently at this point `z` is an array with more than one element. – Peteris Aug 11 '20 at 16:17
  • You should paste the full error message with the line of code that it refers to, but I suspect that the problem is in the `if z>=0:` line. You are also going to have problems with the `-2.25(-z)` bit (float is not callable) because you have presumably missed out an intended `*` operator. – alani Aug 11 '20 at 16:18
  • Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Trenton McKinney Aug 11 '20 at 17:59
  • @nancyyin You're using Numpy, aren't you? If yes, that should be mentioned in the question (and tagged as such). – phipsgabler Aug 11 '20 at 18:00
  • You're making us guess where the error is. Please update the question to include the full error traceback message. – John Gordon Aug 11 '20 at 18:03
  • sorry, I just edited my question – nancyyin Aug 11 '20 at 18:04

1 Answers1

0

Welcome to SO! The problem seems to be this line: if z >= 0: If you use the '>'/'<' operator on an array it will return the following:

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> a > 2
array([False, False,  True])

This array can't be converted to bool by default, you have to be more specific, for example by using any() to test if atleast one element falls under the given condition. Numpy arrays can do it like this: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.any.html.

Josef
  • 2,869
  • 2
  • 22
  • 23