0

I need to complete the function exponentialPDF but get an error:

'IndexError: index 0 is out of bounds for axis 0 with size 0'

The function looks like this:

def uniformPDF(x,a=0.0,b=4.0):
    p = 1.0/(b-a)*ones((len(x),))
    p[x<a] = 0.0
    p[x>b] = 0.0
    return(p)

def exponentialPDF(x,a=1.0):
"""
Call:
   p = exponentialPDF(x,a)
Input argument:
   vals: float (array)
Output argument:
   p: float (array)
Examples:
   In[1]:  exponentialPDF([1,2],3)
   Out[1]: array([ 0.14936121,  0.03332699])
"""
    p = a * exp(-a*x)
    p[x<0] = 0.0
    return(p)

Can someone help me with the error?

Amirshk
  • 8,170
  • 2
  • 35
  • 64
Veysel
  • 23
  • 5

1 Answers1

0

Sounds like the list you are passing in to your function is empty. Read about python lists and see the following post: IndexError: list assignment index out of range

I found the function works using numpy array, e.g. p = exponentialPDF(np.array([1,2]),3). Hope this help, you should check out the SO homework post and ask again if you're still stuck.

EDIT: As you are using numpy, I would add an explicit convert to numpy array in the function as follows:

def exponentialPDF(x,a=1.0):
    """
    Call:
       p = exponentialPDF(x,a)
    Input argument:
       vals: float (array)
    Output argument:
        p: float (array)
    Examples:
       In[1]:  exponentialPDF([1,2],3)
       Out[1]: array([ 0.14936121,  0.03332699])
    """
    if type(x) is list:
        x = array(x)

    p = a * exp(-a*x)
    p[x<0] = 0.0
    return(p)

Hopefully this will fix your problem and you can use the function as needed (assuming returning a numpy array from the function is okay).

Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • First, thank you for your answer. I tried it with the first post you mentioned but it still won't work. And yes, you're right that p = exponentialPDF(np.array([1,2]),3) works, I already wrote an email to the assistants if they wrote the function wrong. We have on top of our function these and are not allowed to import more: from numpy import * from scipy import stats from scipy.special import gammaln from pylab import * Is there maybe another way to solve this problem? Thanks also for the hint concerning the homework post. – Veysel Mar 21 '15 at 18:22
  • In which case, I would suggest adding a conversion to numpy array in the function, I've edited my answer. Also, I know it's not your code but generic, import * syntax is not recommended as you can get conflicts between modules – Ed Smith Mar 21 '15 at 18:25
  • Thanks a lot! They also have a mistake in their example, because if I try exponentialPDF(([1,2]),3) then the right answer comes out, but if I try it without the brackets as given in the example: exponentialPDF([1,2],3), then the values are slightly different. – Veysel Mar 22 '15 at 08:10
  • Great, glad it worked! Typically in python, using (1,2) gives tuples and [1,2] lists. It's strange, using ([]) appears to give a different answer for you -- I get the same list from ([1,2]) as [1,2] and neither form works for me with your original function. It may be that () has been redefined to convert to a numpy array in your case... – Ed Smith Mar 22 '15 at 18:14