text
stringlengths
0
828
Notes
------
Adapted from Ethan White's macroecology_tools
""""""
if bins == b:
p = 0
else:
BOUNDS = [0, 1]
DIST_FROM_BOUND = 10 ** -15
m = np.array(np.arange(1, np.int(b) + 1))
y = lambda x: np.sum(x ** m / b * bins) - np.sum((x ** m) / m)
p = optim.bisect(y, BOUNDS[0] + DIST_FROM_BOUND,
min((sys.float_info[0] / bins) ** (1 / b), 2),
xtol=1.490116e-08, maxiter=1000)
return p"
3883,"def _expon_solve_lam_from_mu(mu, b):
""""""
For the expon_uptrunc, given mu and b, return lam.
Similar to geom_uptrunc
""""""
def lam_eq(lam, mu, b):
# Small offset added to denominator to avoid 0/0 erors
lam, mu, b = Decimal(lam), Decimal(mu), Decimal(b)
return ( (1 - (lam*b + 1) * np.exp(-lam*b)) /
(lam - lam * np.exp(-lam*b) + Decimal(1e-32)) - mu )
return optim.brentq(lam_eq, -100, 100, args=(mu, b), disp=True)"
3884,"def _make_rank(dist_obj, n, mu, sigma, crit=0.5, upper=10000, xtol=1):
""""""
Make rank distribution using both ppf and brute force.
Setting crit = 1 is equivalent to just using the ppf
Parameters
----------
{0}
""""""
qs = (np.arange(1, n + 1) - 0.5) / n
rank = np.empty(len(qs))
brute_ppf = lambda val, prob: prob - dist_obj.cdf(val, mu, sigma)
qs_less = qs <= crit
ind = np.sum(qs_less)
# Use ppf if qs are below crit
rank[qs_less] = dist_obj.ppf(qs[qs_less], mu, sigma)
# Use brute force if they are above
for i, tq in enumerate(qs[~qs_less]):
j = ind + i
try:
# TODO: Use an adaptable lower bound to increase speed
rank[j] = np.abs(np.ceil(optim.brentq(brute_ppf, -1, upper,
args=(tq,), xtol=xtol)))
except ValueError:
# If it is above the upper bound set all remaining values
# to the previous value
rank[j:] = np.repeat(rank[j - 1], len(rank[j:]))
break
return rank"
3885,"def _mean_var(vals, pmf):
""""""
Calculates the mean and variance from vals and pmf
Parameters
----------
vals : ndarray
Value range for a distribution
pmf : ndarray
pmf values corresponding with vals
Returns
-------
: tuple
(mean, variance)
""""""
mean = np.sum(vals * pmf)
var = np.sum(vals ** 2 * pmf) - mean ** 2
return mean, var"
3886,"def rank(self, n, *args):
""""""{0}""""""
return self.ppf((np.arange(1, n+1) - 0.5) / n, *args)"
3887,"def rvs_alt(self, *args, **kwargs):
""""""{0}""""""
l = kwargs.get('l', 1)
b = kwargs.get('b', 1e5)
size = kwargs.get('size', 1)