I am a mathematician. I am doing some work on simulating probability distribution draws, but there are lots of calculations involved in terms of numerical integration, function inversion. All of this can be vectorized to "speed it up", but I was wondering if tensorizing it, and putting it on CUDA would speed it up more? I can't show my exact code (and I need custom distribution classes for a reason), but it currently looks like: class ProbabilityDistribution: def __init__(self, pdf, cdf, xmin, interp, xmax: Optional[float] = None): self.pdf = pdf self.cdf = cdf self.interp = interp self.xmin = xmin # Find right endpoint for cdf if not xmax: low = self.xmin high = 1e5 if self.cdf(high) < 1: pass else: while high - low > 1e-5: mid = (low + high) / 2 if self.cdf(mid) < 1: low = mid else: high = mid self.xmax = high else: self.xmax = xmax if self.interp: xvals = np.linspace(self.xmin, self.xmax, 10000) self.cdf_interp = scipy.interpolate.interp1d(xvals, [self.cdf(x) for x in xvals], bounds_error=False, fill_value=(0, 1)) def cdf_inv(self, p: float) -> float: if not self.interp: try: v = bisect(lambda x: self.cdf(x) - p, self.xmin, self.xmax, xtol=np.abs(1 - self.cdf(self.xmax))) return v except(ValueError): print(f'Failed with p = {p}, xmin = {self.xmin}, max = 1e5') else: try: return scipy.optimize.brentq(lambda x: self.cdf_interp(x) - p, self.xmin, self.xmax, xtol=np.abs(1 - self.cdf(self.xmax))) except (ValueError): return self.xmax #Too close to upper bound def sample(self, n: int = 1) -> np.ndarray: """Sample from the tempered Pareto-like distribution.""" u = np.random.uniform(0, 1, n) return [self.cdf_inv(p) for p in u] If I change $u$ to a tensor, and then pass the operations onto this as a tensor, would this speed it up? I don't know quite how CUDA works, so please let me know! Continue reading...