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

(x > xm)be taking care of it? I would expect that theifcondition is evaluated first.ifs in your code. I don't have numpy experience, butx > xmwill 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.