0

I am frequently using the following construct to implement the Heaviside step function

def f(x,xm):   
   1 * (xm > c)

I used the same to implement the CDF of the Pareto distribution.

def pareto_cum(x,xm,a):
     return (1.-(xm/x)**a) * (x > xm)

Unfortunately this produces an error message in python 3. I don't understand why division by zero is a problem here, since always xm>0. That is, x > 0 should be guaranteed by default.

import numpy as np
import matplotlib.pyplot as plt


x=np.linspace(0,10,100000)
def pareto_cum(x,xm,a):
     return (1.-(xm/x)**a) * (x > xm)

plt.plot(x,pareto_cum(x,2,3))
__main__:2: RuntimeWarning: divide by zero encountered in true_divide
__main__:2: RuntimeWarning: invalid value encountered in multiply
[<matplotlib.lines.Line2D object at 0x7fdf026770b8>]

CDF of the Pareto distribution https://en.wikipedia.org/wiki/Pareto_distribution

6
  • 1
    Why would it be "guaranteed by default"? You never check to make sure it's non-zero. Commented May 27, 2017 at 18:44
  • Perhaps I misunderstand something, but shouldn't (x > xm) be taking care of it? I would expect that the if condition is evaluated first. Commented May 27, 2017 at 18:46
  • 1
    I don't see any ifs in your code. I don't have numpy experience, but x > xm will evaluate to a boolean value, then it's cast to a number since you're multiplying it. All it will do is force everything to either evaluate to 0 (if x is less than xm), or do nothing otherwise. Commented May 27, 2017 at 18:50
  • You're just doing math. There are no checks here. Commented May 27, 2017 at 18:52
  • Deleted my answer since I was over my head. Hopefully Importance's answer can help you. Commented May 27, 2017 at 19:07

1 Answer 1

1

It's not an error message, it's a warning. So you can ignore it if you like. The only effect this has it that the first point of the result is nan. The code runs fine otherwise.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,10,100000)
def pareto_cum(x,xm,a):
     return (1.-(xm/x)**a) * (x > xm)

plt.plot(x,pareto_cum(x,2,3))
plt.show()

produces as expected:

enter image description here

You can get rid of the warning by not letting 0 be part of the array, e.g.

x=np.linspace(0,10,100000)
x[0] = 1e-12

You can also check inside the function

def pareto_cum(x,xm,a):
     x[x==0] = 1e-12
     return (1.-(xm/x)**a) * (x > xm)
Sign up to request clarification or add additional context in comments.

4 Comments

x[x==0] = 1e-12 is passed by value I guess. i.e. this will not change the actual data.
Of course it will change the data. Every item in x that is equal to zero will afterwards be 1e-12. But of course we only change the local variable x here. The global x outside the function has nothing do do with it (they just have the same name but python wouldn't know about that anyways).
OK, pass by value it is.
Well, python does not have the concept of pass-by-value, but the behaviour as seen by the programmer is mostly the same.

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.