Exemple #1
0
def gen(alpha, beta, name):
    """Generate fixture data and write to file.

    # Arguments

    * `alpha`: first shape parameter
    * `alpha`: second shape parameter
    * `name::str`: output filename

    # Examples

    ``` python
    python> alpha = random_sample( 100 ) * 10
    python> beta = random_sample( 100 ) * 10
    python> gen(alpha, beta, './data.json')
    ```
    """
    dist = betaprime(alpha, beta)
    kurtosis = dist.stats(moments="k")

    # Store data to be written to file as a dictionary:
    data = {
        "alpha": alpha.tolist(),
        "beta": beta.tolist(),
        "expected": kurtosis.tolist()
    }

    # Based on the script directory, create an output filepath:
    filepath = os.path.join(DIR, name)

    # Write the data to the output filepath as JSON:
    with open(filepath, "w") as outfile:
        json.dump(data, outfile)
Exemple #2
0
def get_limits(sigma, M, N=1):
    """
    Return the limits on the spectral kurtosis value to exclude the specified confidence
    interval in sigma using a Pearson Type VI distribution (betaprime in scipy.stats world).
    The return value is a two-element tuple of lower limit, upper limit.
    
    .. note::
        This corresponds to Section 3.1 in Nita & Gary (2010, MNRAS 406, L60)
    """

    # Adjust the NumPy error levels so that we know when ppf() may be suspect
    errLevels = numpy.geterr()
    numpy.seterr(all='raise')

    # Convert the sigma to a fraction for the high and low clip levels
    percentClip = (1.0 - erf(sigma / numpy.sqrt(2))) / 2.0

    # Build the Pearson type VI distribution function - parameters then instance
    a = _alpha(M, N)
    b = _beta(M, N)
    d = _delta(M, N)

    rv = betaprime(a, b, loc=d)

    # Try to get a realistic lower limit, giving up if we hit an overflow error
    try:
        lower = rv.ppf(percentClip)
    except FloatingPointError:
        lower = mean(M, N) - sigma * std(M, N)

    # Try to get a realistic upper limit, giving up if we hit an overflow error
    try:
        upper = rv.ppf(1.0 - percentClip)
    except FloatingPointError:
        upper = mean(M, N) + sigma * std(M, N)

    # Restore the NumPy error levels
    numpy.seterr(**errLevels)

    return lower, upper
mean, var, skew, kurt = betaprime.stats(a, b, moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(betaprime.ppf(0.01, a, b),
                betaprime.ppf(0.99, a, b), 100)
ax.plot(x, betaprime.pdf(x, a, b),
       'r-', lw=5, alpha=0.6, label='betaprime 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 = betaprime(a, b)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = betaprime.ppf([0.001, 0.5, 0.999], a, b)
np.allclose([0.001, 0.5, 0.999], betaprime.cdf(vals, a, b))
# True

# Generate random numbers:

r = betaprime.rvs(a, b, size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
Exemple #4
0
 def test_logpdf(self):
     alpha, beta = 267, 1472
     x = np.array([0.2, 0.5, 0.6])
     b = stats.betaprime(alpha, beta)
     assert_(np.isfinite(b.logpdf(x)).all())
     assert_allclose(b.pdf(x), np.exp(b.logpdf(x)))
def geweke_test():
    """
    Create a discrete time Hawkes model and generate from it.

    :return:
    """
    T = 50
    dt = 1.0
    dt_max = 3.0
    network_hypers = {
        'C': 1,
        'p': 0.5,
        'kappa': 3.0,
        'alpha': 3.0,
        'beta': 1.0 / 20.0
    }
    model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(
        K=1, dt=dt, dt_max=dt_max, network_hypers=network_hypers)
    model.generate(T=T)

    # Gibbs sample and then generate new data
    N_samples = 10000
    samples = []
    lps = []
    for itr in xrange(N_samples):
        if itr % 10 == 0:
            print "Iteration: ", itr
        # Resample the model
        model.resample_model()
        samples.append(model.copy_sample())
        lps.append(model.log_probability())

        # Geweke step
        model.data_list.pop()
        model.generate(T=T)

    # Compute sample statistics for second half of samples
    A_samples = np.array([s.weight_model.A for s in samples])
    W_samples = np.array([s.weight_model.W for s in samples])
    g_samples = np.array([s.impulse_model.g for s in samples])
    lambda0_samples = np.array([s.bias_model.lambda0 for s in samples])
    c_samples = np.array([s.network.c for s in samples])
    p_samples = np.array([s.network.p for s in samples])
    v_samples = np.array([s.network.v for s in samples])
    lps = np.array(lps)

    offset = 0
    A_mean = A_samples[offset:, ...].mean(axis=0)
    W_mean = W_samples[offset:, ...].mean(axis=0)
    g_mean = g_samples[offset:, ...].mean(axis=0)
    lambda0_mean = lambda0_samples[offset:, ...].mean(axis=0)

    print "A mean:        ", A_mean
    print "W mean:        ", W_mean
    print "g mean:        ", g_mean
    print "lambda0 mean:  ", lambda0_mean

    # Plot the log probability over iterations
    plt.figure()
    plt.plot(np.arange(N_samples), lps)
    plt.xlabel("Iteration")
    plt.ylabel("Log probability")

    # Plot the histogram of bias samples
    plt.figure()
    p_lmbda0 = gamma(model.bias_model.alpha, scale=1. / model.bias_model.beta)
    _, bins, _ = plt.hist(lambda0_samples[:, 0],
                          bins=20,
                          alpha=0.5,
                          normed=True)
    bincenters = 0.5 * (bins[1:] + bins[:-1])
    plt.plot(bincenters, p_lmbda0.pdf(bincenters), 'r--', linewidth=1)
    plt.xlabel('lam0')
    plt.ylabel('p(lam0)')

    print "Expected p(A):  ", model.network.P
    print "Empirical p(A): ", A_samples.mean(axis=0)

    # Plot the histogram of weight samples
    plt.figure()
    Aeq1 = A_samples[:, 0, 0] == 1
    # p_W1 = gamma(model.network.kappa, scale=1./model.network.v[0,0])

    # The marginal distribution of W under a gamma prior on the scale
    # is a beta prime distribution
    p_W1 = betaprime(model.network.kappa,
                     model.network.alpha,
                     scale=model.network.beta)

    _, bins, _ = plt.hist(W_samples[Aeq1, 0, 0],
                          bins=20,
                          alpha=0.5,
                          normed=True)
    bincenters = 0.5 * (bins[1:] + bins[:-1])
    plt.plot(bincenters, p_W1.pdf(bincenters), 'r--', linewidth=1)
    plt.xlabel('W')
    plt.ylabel('p(W | A=1)')

    # Plot the histogram of impulse samples
    plt.figure()
    for b in range(model.B):
        plt.subplot(1, model.B, b + 1)
        a = model.impulse_model.gamma[b]
        b = model.impulse_model.gamma.sum() - a
        p_beta11b = beta(a, b)

        _, bins, _ = plt.hist(g_samples[:, 0, 0, b],
                              bins=20,
                              alpha=0.5,
                              normed=True)
        bincenters = 0.5 * (bins[1:] + bins[:-1])
        plt.plot(bincenters, p_beta11b.pdf(bincenters), 'r--', linewidth=1)
        plt.xlabel('g_%d' % b)
        plt.ylabel('p(g_%d)' % b)

    # Plot the histogram of weight scale
    plt.figure()
    for c1 in range(model.C):
        for c2 in range(model.C):
            plt.subplot(model.C, model.C, 1 + c1 * model.C + c2)
            p_v = gamma(model.network.alpha, scale=1. / model.network.beta)

            _, bins, _ = plt.hist(v_samples[:, c1, c2],
                                  bins=20,
                                  alpha=0.5,
                                  normed=True)
            bincenters = 0.5 * (bins[1:] + bins[:-1])
            plt.plot(bincenters, p_v.pdf(bincenters), 'r--', linewidth=1)
            plt.xlabel('v_{%d,%d}' % (c1, c2))
            plt.ylabel('p(v)')

    plt.show()
Exemple #6
0
 def Phi_linf(self, prob):
     d = self.dim
     a = self.a
     g = betaprime(d, a + 1 - d).cdf
     ig = betaprime(d, a - d).ppf
     return g(ig(2 * prob)) * (a - d) / 2
Exemple #7
0
 def __init__(self, dim, sigma=None, lambd=None, a=None, device='cpu'):
     self.a = a
     if a is None:
         raise ValueError('Parameter `a` is required.')
     super().__init__(dim, sigma, lambd, device)
     self.beta_dist = betaprime(dim, a - self.dim)
Exemple #8
0
                                                     41.404620910360876)
drivingdistance_model_dict['ncx2'] = st.ncx2(0.30254190304723211,
                                             1.1286538320791935,
                                             14.999999999999998,
                                             8.7361471573932192)
drivingdistance_model_dict['chi'] = st.chi(0.47882729877571095,
                                           14.999999999999996,
                                           44.218301183844645)
drivingdistance_model_dict['recipinvgauss'] = st.recipinvgauss(
    2447246.0546641815, 14.999999999994969, 31.072009722580802)
drivingdistance_model_dict['f'] = st.f(0.85798489720127036, 4.1904554804436929,
                                       14.99998319939356, 21.366492843433996)

drivingduration_model_dict = ct.OrderedDict()
drivingduration_model_dict['betaprime'] = st.betaprime(2.576282082814398,
                                                       9.7247974165209996,
                                                       9.1193851632305201,
                                                       261.3457987967214)
drivingduration_model_dict['exponweib'] = st.exponweib(2.6443841639764942,
                                                       0.89242254172118096,
                                                       10.603640861374947,
                                                       40.28556311444698)
drivingduration_model_dict['gengamma'] = st.gengamma(4.8743515108339581,
                                                     0.61806208678747043,
                                                     9.4649293818479716,
                                                     5.431576919220225)
drivingduration_model_dict['recipinvgauss'] = st.recipinvgauss(
    0.499908918842556, 0.78319699707613699, 28.725450197674746)
drivingduration_model_dict['f'] = st.f(9.8757694313677113, 12.347442183821462,
                                       0.051160749890587665,
                                       73.072591767722287)
 def test_logpdf(self):
     alpha, beta = 267, 1472
     x = np.array([0.2, 0.5, 0.6])
     b = stats.betaprime(alpha, beta)
     assert_(np.isfinite(b.logpdf(x)).all())
     assert_allclose(b.pdf(x), np.exp(b.logpdf(x)))
Exemple #10
0
mlpParams = {
    'hidden_layer_sizes': [(100, ), (200, 200), (500, 500)],
    'activation': ['identity', 'logistic', 'tanh', 'relu'],
    'solver': ['lbfgs', 'sgd', 'adam'],
    'alpha': expon(scale=.0005, loc=.0001),
    'learning_rate': ['constant', 'invscaling', 'adaptive'],
    'power_t': beta(a=2, b=2),
    'momentum': uniform(),
    'early_stopping': [True],
    'beta_1': beta(a=2, b=.5),
    'beta_2': beta(a=2, b=.5),
    'max_iter': [500]
}

passiveAggParams = {
    'C': betaprime(a=5, b=5),  #list(range(0.1,3,0.3)),
    'n_iter': list(range(5, 10)),
    'loss': ['hinge', 'squared_hinge']
}

sgdParams = {
    'loss': [
        'hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron',
        'squared_loss', 'huber', 'epsilon_insensitive',
        'squared_epsilon_insensitive'
    ]
}

baggingParams = {
    'n_estimators': [5, 10, 20],
    'base_estimator': [None],  ####################
Exemple #11
0
def all_dists():
    # dists param were taken from scipy.stats official
    # documentaion examples
    # Total - 89
    return {
        "alpha":
        stats.alpha(a=3.57, loc=0.0, scale=1.0),
        "anglit":
        stats.anglit(loc=0.0, scale=1.0),
        "arcsine":
        stats.arcsine(loc=0.0, scale=1.0),
        "beta":
        stats.beta(a=2.31, b=0.627, loc=0.0, scale=1.0),
        "betaprime":
        stats.betaprime(a=5, b=6, loc=0.0, scale=1.0),
        "bradford":
        stats.bradford(c=0.299, loc=0.0, scale=1.0),
        "burr":
        stats.burr(c=10.5, d=4.3, loc=0.0, scale=1.0),
        "cauchy":
        stats.cauchy(loc=0.0, scale=1.0),
        "chi":
        stats.chi(df=78, loc=0.0, scale=1.0),
        "chi2":
        stats.chi2(df=55, loc=0.0, scale=1.0),
        "cosine":
        stats.cosine(loc=0.0, scale=1.0),
        "dgamma":
        stats.dgamma(a=1.1, loc=0.0, scale=1.0),
        "dweibull":
        stats.dweibull(c=2.07, loc=0.0, scale=1.0),
        "erlang":
        stats.erlang(a=2, loc=0.0, scale=1.0),
        "expon":
        stats.expon(loc=0.0, scale=1.0),
        "exponnorm":
        stats.exponnorm(K=1.5, loc=0.0, scale=1.0),
        "exponweib":
        stats.exponweib(a=2.89, c=1.95, loc=0.0, scale=1.0),
        "exponpow":
        stats.exponpow(b=2.7, loc=0.0, scale=1.0),
        "f":
        stats.f(dfn=29, dfd=18, loc=0.0, scale=1.0),
        "fatiguelife":
        stats.fatiguelife(c=29, loc=0.0, scale=1.0),
        "fisk":
        stats.fisk(c=3.09, loc=0.0, scale=1.0),
        "foldcauchy":
        stats.foldcauchy(c=4.72, loc=0.0, scale=1.0),
        "foldnorm":
        stats.foldnorm(c=1.95, loc=0.0, scale=1.0),
        # "frechet_r": stats.frechet_r(c=1.89, loc=0.0, scale=1.0),
        # "frechet_l": stats.frechet_l(c=3.63, loc=0.0, scale=1.0),
        "genlogistic":
        stats.genlogistic(c=0.412, loc=0.0, scale=1.0),
        "genpareto":
        stats.genpareto(c=0.1, loc=0.0, scale=1.0),
        "gennorm":
        stats.gennorm(beta=1.3, loc=0.0, scale=1.0),
        "genexpon":
        stats.genexpon(a=9.13, b=16.2, c=3.28, loc=0.0, scale=1.0),
        "genextreme":
        stats.genextreme(c=-0.1, loc=0.0, scale=1.0),
        "gausshyper":
        stats.gausshyper(a=13.8, b=3.12, c=2.51, z=5.18, loc=0.0, scale=1.0),
        "gamma":
        stats.gamma(a=1.99, loc=0.0, scale=1.0),
        "gengamma":
        stats.gengamma(a=4.42, c=-3.12, loc=0.0, scale=1.0),
        "genhalflogistic":
        stats.genhalflogistic(c=0.773, loc=0.0, scale=1.0),
        "gilbrat":
        stats.gilbrat(loc=0.0, scale=1.0),
        "gompertz":
        stats.gompertz(c=0.947, loc=0.0, scale=1.0),
        "gumbel_r":
        stats.gumbel_r(loc=0.0, scale=1.0),
        "gumbel_l":
        stats.gumbel_l(loc=0.0, scale=1.0),
        "halfcauchy":
        stats.halfcauchy(loc=0.0, scale=1.0),
        "halflogistic":
        stats.halflogistic(loc=0.0, scale=1.0),
        "halfnorm":
        stats.halfnorm(loc=0.0, scale=1.0),
        "halfgennorm":
        stats.halfgennorm(beta=0.675, loc=0.0, scale=1.0),
        "hypsecant":
        stats.hypsecant(loc=0.0, scale=1.0),
        "invgamma":
        stats.invgamma(a=4.07, loc=0.0, scale=1.0),
        "invgauss":
        stats.invgauss(mu=0.145, loc=0.0, scale=1.0),
        "invweibull":
        stats.invweibull(c=10.6, loc=0.0, scale=1.0),
        "johnsonsb":
        stats.johnsonsb(a=4.32, b=3.18, loc=0.0, scale=1.0),
        "johnsonsu":
        stats.johnsonsu(a=2.55, b=2.25, loc=0.0, scale=1.0),
        "ksone":
        stats.ksone(n=1e03, loc=0.0, scale=1.0),
        "kstwobign":
        stats.kstwobign(loc=0.0, scale=1.0),
        "laplace":
        stats.laplace(loc=0.0, scale=1.0),
        "levy":
        stats.levy(loc=0.0, scale=1.0),
        "levy_l":
        stats.levy_l(loc=0.0, scale=1.0),
        "levy_stable":
        stats.levy_stable(alpha=0.357, beta=-0.675, loc=0.0, scale=1.0),
        "logistic":
        stats.logistic(loc=0.0, scale=1.0),
        "loggamma":
        stats.loggamma(c=0.414, loc=0.0, scale=1.0),
        "loglaplace":
        stats.loglaplace(c=3.25, loc=0.0, scale=1.0),
        "lognorm":
        stats.lognorm(s=0.954, loc=0.0, scale=1.0),
        "lomax":
        stats.lomax(c=1.88, loc=0.0, scale=1.0),
        "maxwell":
        stats.maxwell(loc=0.0, scale=1.0),
        "mielke":
        stats.mielke(k=10.4, s=3.6, loc=0.0, scale=1.0),
        "nakagami":
        stats.nakagami(nu=4.97, loc=0.0, scale=1.0),
        "ncx2":
        stats.ncx2(df=21, nc=1.06, loc=0.0, scale=1.0),
        "ncf":
        stats.ncf(dfn=27, dfd=27, nc=0.416, loc=0.0, scale=1.0),
        "nct":
        stats.nct(df=14, nc=0.24, loc=0.0, scale=1.0),
        "norm":
        stats.norm(loc=0.0, scale=1.0),
        "pareto":
        stats.pareto(b=2.62, loc=0.0, scale=1.0),
        "pearson3":
        stats.pearson3(skew=0.1, loc=0.0, scale=1.0),
        "powerlaw":
        stats.powerlaw(a=1.66, loc=0.0, scale=1.0),
        "powerlognorm":
        stats.powerlognorm(c=2.14, s=0.446, loc=0.0, scale=1.0),
        "powernorm":
        stats.powernorm(c=4.45, loc=0.0, scale=1.0),
        "rdist":
        stats.rdist(c=0.9, loc=0.0, scale=1.0),
        "reciprocal":
        stats.reciprocal(a=0.00623, b=1.01, loc=0.0, scale=1.0),
        "rayleigh":
        stats.rayleigh(loc=0.0, scale=1.0),
        "rice":
        stats.rice(b=0.775, loc=0.0, scale=1.0),
        "recipinvgauss":
        stats.recipinvgauss(mu=0.63, loc=0.0, scale=1.0),
        "semicircular":
        stats.semicircular(loc=0.0, scale=1.0),
        "t":
        stats.t(df=2.74, loc=0.0, scale=1.0),
        "triang":
        stats.triang(c=0.158, loc=0.0, scale=1.0),
        "truncexpon":
        stats.truncexpon(b=4.69, loc=0.0, scale=1.0),
        "truncnorm":
        stats.truncnorm(a=0.1, b=2, loc=0.0, scale=1.0),
        "tukeylambda":
        stats.tukeylambda(lam=3.13, loc=0.0, scale=1.0),
        "uniform":
        stats.uniform(loc=0.0, scale=1.0),
        "vonmises":
        stats.vonmises(kappa=3.99, loc=0.0, scale=1.0),
        "vonmises_line":
        stats.vonmises_line(kappa=3.99, loc=0.0, scale=1.0),
        "wald":
        stats.wald(loc=0.0, scale=1.0),
        "weibull_min":
        stats.weibull_min(c=1.79, loc=0.0, scale=1.0),
        "weibull_max":
        stats.weibull_max(c=2.87, loc=0.0, scale=1.0),
        "wrapcauchy":
        stats.wrapcauchy(c=0.0311, loc=0.0, scale=1.0),
    }