Skip to content

Instantly share code, notes, and snippets.

@rgommers
Created August 28, 2013 20:45
Show Gist options
  • Save rgommers/6371025 to your computer and use it in GitHub Desktop.
Save rgommers/6371025 to your computer and use it in GitHub Desktop.
Timing of stats.norminvgauss.ppf
In [16]: %lprun -f _cdf -f _cdf_sc -f _ppf -f _ppf_sc ppf(x, 2, -1)
Timer unit: 1e-06 s
File: /home/rgommers/Code/scipy/scipy/stats/distributions.py
Function: _ppf_single_call at line 1133
Total time: 2.63388 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1133 def _ppf_single_call(self, q, *args):
1134 99 115 1.2 0.0 left = right = None
1135 99 120 1.2 0.0 if self.a > -np.inf:
1136 left = self.a
1137 99 95 1.0 0.0 if self.b < np.inf:
1138 right = self.b
1139
1140 99 77 0.8 0.0 factor = 10.
1141 99 75 0.8 0.0 if not left: # i.e. self.a = -inf
1142 99 101 1.0 0.0 left = -1.*factor
1143 99 171843 1735.8 6.5 while self._ppf_to_solve(left, q,*args) > 0.:
1144 right = left
1145 left *= factor
1146 # left is now such that cdf(left) < q
1147 99 118 1.2 0.0 if not right: # i.e. self.b = inf
1148 99 78 0.8 0.0 right = factor
1149 99 39838 402.4 1.5 while self._ppf_to_solve(right, q,*args) < 0.:
1150 left = right
1151 right *= factor
1152 # right is now such that cdf(right) > q
1153
1154 99 126 1.3 0.0 return optimize.brentq(self._ppf_to_solve,
1155 99 2421293 24457.5 91.9 left, right, args=(q,)+args, xtol=self.xtol)
File: /home/rgommers/Code/scipy/scipy/stats/distributions.py
Function: _cdf at line 5285
Total time: 2.61922 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
5285 def _cdf(self, x, a, b, x_opt=None):
5286 # x_opt is expensive to compute, but we can't cache it easily (no state
5287 # variables that depend on shape parameters can be used) - therefore
5288 # pass it directly to _cdf_single_call
5289 1526 1487 1.0 0.1 if x_opt is None:
5290 x_opt = np.empty(a.shape, dtype=float) # a.shape == b.shape == x.shape
5291 x_opt.fill(self._compute_xopt(a[0], b[0]))
5292
5293 1526 1523 1.0 0.1 self.veccdf.nin = self.numargs + 2 # shape args, x, x_opt
5294 1526 2616207 1714.4 99.9 return self.veccdf(x, a, b, x_opt)
File: /home/rgommers/Code/scipy/scipy/stats/distributions.py
Function: _cdf_single_call at line 5296
Total time: 2.56624 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
5296 def _cdf_single_call(self, x, a, b, x_opt):
5297 1526 1541 1.0 0.1 if x <= x_opt:
5298 841 1792379 2131.2 69.8 return integrate.quad(self._pdf, -np.inf, x, args=(a, b))[0]
5299 else:
5300 685 771524 1126.3 30.1 h = integrate.quad(self._pdf, x, np.inf, args=(a, b))[0]
5301 685 801 1.2 0.0 return 1 - h
File: /home/rgommers/Code/scipy/scipy/stats/distributions.py
Function: _ppf at line 5303
Total time: 2.63817 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
5303 def _ppf(self, q, a, b):
5304 1 27 27.0 0.0 a, b = map(np.atleast_1d, (a, b)) # needed because of _rvs
5305 1 8 8.0 0.0 x_opt = np.empty(a.shape, dtype=float)
5306 1 2940 2940.0 0.1 x_opt.fill(self._compute_xopt(a[0], b[0]))
5307 1 7 7.0 0.0 self.vecfunc.nin = self.numargs + 2
5308 1 2635188 2635188.0 99.9 return self.vecfunc(q, a, b, x_opt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment