def test_inv_cdf(self): mu = Variable(torch.randn(100)) s = torch.exp(Variable(torch.randn(100))) value = Variable(torch.rand(100)) dist = Logistic(mu, s) # test inverse cdf res1 = dist.inv_cdf(value).data res2 = logistic.ppf(value.data.numpy(), mu.data.numpy(), s.data.numpy()) self.assertEqual(res1, res2)
# gamma a = 1.99 mean, var, skew, kurt = gamma.stats(a, moments = 'mvsk') x = np.linspace(gamma.ppf(0.01, a), gamma.ppf(0.99, a), 100) ax1.plot(x, gamma.pdf(x, a), 'r-', lw=5, alpha=0.6, label='gamma pdf') ax1.set_title('gamma pdf') ax2.plot(x, gamma.cdf(x, a), 'r-', lw=5, alpha=0.6, label='gamma cdf') ax2.set_title('gamma cdf') # logistic b = 0.5 mean, var, skew, kurt = logistic.stats(b, moments = 'mvsk') x = np.linspace(logistic.ppf(0.01, b), logistic.ppf(0.99, b), 100) ax3.plot(x, logistic.pdf(x, b), 'g-', lw=5, alpha=0.6, label='gamma pdf') ax3.set_title('logistic pdf') ax4.plot(x, logistic.cdf(x, b), 'g-', lw=5, alpha=0.6, label='gamma cdf') ax4.set_title('logistic cdf') # exponential a = 1.99 mean, var, skew, kurt = expon.stats(a, moments = 'mvsk') x = np.linspace(expon.ppf(0.01, a), expon.ppf(0.99, a), 100) ax5.plot(x, expon.pdf(x, a),
import numpy as np import matplotlib.pyplot as plt from scipy.stats import logistic from sat_exp import saturating_exp F = lambda x, (a,b,l): 0.5 + (1-0.5-l)*logistic.cdf(x, loc=a, scale=b) Finv = lambda thresh_val, (a,b,l): logistic.ppf((thresh_val - 0.5)/(1-0.5-l), loc=a, scale=b) def color_list(n, cmap=None): cm = plt.get_cmap("RdYlGn" if cmap is None else cmap) colors = [cm(i) for i in np.linspace(0, 1, n)] return colors*(n/len(colors)) + colors[:n%len(colors)] def plot_pmf(cohs, pcor, res): cmap = color_list(len(res)+1, 'cool') xs = np.array(cohs) xsf = np.linspace(min(xs), max(xs), 50) for i, (theta, thresh) in enumerate(res): plt.scatter(cohs, pcor[i, :]/100.0, color=cmap[i]) plt.plot(xsf, F(xsf, theta), color=cmap[i], linestyle='-') plt.xlim([0.01, None]) plt.ylim([0.45, 1.05]) plt.xscale('log') plt.xlabel('signal strength') plt.ylabel('accuracy') plt.show() def plot_pmf_thresh(reses): cmap = color_list(len(reses)+1, 'Greens') for i, res in enumerate(reses): durs = np.arange(1, len(res)+1)
def poly_log(z, s, method='default', log=False) -> Union[float, np.ndarray]: r""" Computes the polylogarithm function. Current implementation only takes care of :math:`s \leq 0` .. math:: L_s(z) = \sum_{k=1}^\infty \frac{z^k}{k^s} Parameters ---------- z: {array_like, scalar} Numeric or complex vector s: {array_like, scalar} Complex number method: {'default', 'neg-stirling', 'neg-eulerian'} Algorithm used for calculating poly logarithms log: bool If True, returns the log of poly logarithms Returns ------- {array_like, scalar} Poly logarithms """ if isinstance(z, (complex, int, float)): z = np.ravel(z) method = method.lower() assert method in ('default', 'neg-stirling', 'neg-eulerian') if s == 2 and method == 'default': return dilog_complex(z) if np.any(np.iscomplex(z)) else dilog(z) elif method == 'default': method = 'neg-stirling' assert float(s).is_integer() and s <= 1 if s == 1: r = -np.log1p(-z) return np.log(r) if log else r iz = 1 - z n = abs(int(s)) if method == 'neg-stirling': r = z / iz f = np.cumprod([1, *range(1, n + 1)]) s = stirling_second_all(n + 1) p = polyn_eval(f * s, r) if log: res = np.log(p) + logis.ppf(z) else: res = r * p else: # method == 'neg-eulerian' p = polyn_eval(eulerian_all(n), z) if log: res = np.log(p) + np.log(z) - (n + 1) * np.log1p(-z) else: res = z * p / iz**(n + 1) return res.item(0) if res.size == 1 else res
def logistic_thresholds(n_cats): thresholds = [ logistic.ppf((cat + 1) / n_cats) for cat in range(n_cats - 1) ] return np.asarray(thresholds, dtype=np.float32)
from scipy.stats import logistic import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: mean, var, skew, kurt = logistic.stats(moments='mvsk') # Display the probability density function (``pdf``): x = np.linspace(logistic.ppf(0.01), logistic.ppf(0.99), 100) ax.plot(x, logistic.pdf(x), 'r-', lw=5, alpha=0.6, label='logistic pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = logistic() ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = logistic.ppf([0.001, 0.5, 0.999]) np.allclose([0.001, 0.5, 0.999], logistic.cdf(vals)) # True # Generate random numbers: r = logistic.rvs(size=1000)
# python3 import math import random import numpy as np from scipy.stats import logistic from matplotlib import pyplot as plt def inv_logistic_cdf(u): return math.log(u / (1 - u)) random.seed(1001) # random samples from Unif(0, 1) random_numbers = [random.random() for _ in range(12000)] # random samples of Logistic Dist. through the inverse transform random_numbers_from_logistic = [inv_logistic_cdf(random_number) for random_number in random_numbers] x = np.linspace(logistic.ppf(0.001), logistic.ppf(0.999), 100) plt.hist(random_numbers_from_logistic, 60, facecolor='green', normed=True, alpha=0.6, label='random numbers') plt.plot(x, logistic.pdf(x), lw=2, alpha=0.7, label='logistic pdf') plt.legend(loc='best')