Python scipy.stats.beta() Examples

The following are 30 code examples of scipy.stats.beta(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.stats , or try the search function .
Example #1
Source File: beta_distribution.py    From minimal-streamlit-example with MIT License 6 votes vote down vote up
def plot_dist(alpha_value: float, beta_value: float, data: np.ndarray = None):
    beta_dist = beta(alpha_value, beta_value)

    xs = np.linspace(0, 1, 1000)
    ys = beta_dist.pdf(xs)

    fig, ax = plt.subplots(figsize=(7, 3))
    ax.plot(xs, ys)
    ax.set_xlim(0, 1)
    ax.set_xlabel("x")
    ax.set_ylabel("P(x)")

    if data is not None:
        likelihoods = beta_dist.pdf(data)
        sum_log_likelihoods = np.sum(beta_dist.logpdf(data))
        ax.vlines(data, ymin=0, ymax=likelihoods)
        ax.scatter(data, likelihoods, color="black")
        st.write(
            f"""
_Under your alpha={alpha_slider:.2f} and beta={beta_slider:.2f},
the sum of log likelihoods is {sum_log_likelihoods:.2f}_
"""
        )
    st.pyplot(fig) 
Example #2
Source File: test_abc_smc_algorithm.py    From pyABC with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_beta_binomial_two_identical_models_adaptive(db_path, sampler):
    binomial_n = 5

    def model_fun(args):
        return {"result": st.binom(binomial_n, args.theta).rvs()}

    models = [model_fun for _ in range(2)]
    models = list(map(SimpleModel, models))
    population_size = AdaptivePopulationSize(800)
    parameter_given_model_prior_distribution = [
        Distribution(theta=st.beta(1, 1)) for _ in range(2)]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistance(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(.1),
                 sampler=sampler)
    abc.new(db_path, {"result": 2})

    minimum_epsilon = .2
    history = abc.run(minimum_epsilon, max_nr_populations=3)
    mp = history.get_model_probabilities(history.max_t)
    assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08 
Example #3
Source File: beta.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 6 votes vote down vote up
def __init__(self, lower=None, upper=None, shape_A=None, shape_B=None):
        self.shape_A = shape_A
        self.shape_B = shape_B
        self.lower = lower
        self.upper = upper
        if (self.shape_A is not None) and (self.shape_B is not None):
            if self.shape_A >= 1. and self.shape_B >= 1.0:
                self.mean = (self.shape_A) / (self.shape_A + self.shape_B)
                self.variance = (self.shape_A * self.shape_B) / ( (self.shape_A + self.shape_B)**2 * (self.shape_A + self.shape_B + 1.0) )
                self.skewness = 2.0 * (self.shape_B - self.shape_A) * np.sqrt(self.shape_A + self.shape_B + 1.0) / ( (self.shape_A + self.shape_B + 2.0) * np.sqrt(self.shape_A * self.shape_B) )
                self.kurtosis = 6.0 * ((self.shape_A - self.shape_B)**2 * (self.shape_A + self.shape_B + 1.0) - self.shape_A * self.shape_B * (self.shape_A + self.shape_B + 2.0)  ) /( (self.shape_A * self.shape_B) * (self.shape_A + self.shape_B + 2.0) * (self.shape_A + self.shape_B + 3.0)) + 3.0
                self.bounds = np.array([0, 1])
                self.shape_parameter_A = self.shape_B - 1.0
                self.shape_parameter_B = self.shape_A - 1.0
                self.parent = beta(self.shape_A, self.shape_B)
        if (self.lower is not None) and (self.upper is not None):
            self.x_range_for_pdf = np.linspace(self.lower, self.upper, RECURRENCE_PDF_SAMPLES) 
Example #4
Source File: test_abc_smc_algorithm.py    From pyABC with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_all_in_one_model(db_path, sampler):
    models = [AllInOneModel() for _ in range(2)]
    population_size = ConstantPopulationSize(800)
    parameter_given_model_prior_distribution = [Distribution(theta=RV("beta",
                                                                      1, 1))
                                                for _ in range(2)]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistance(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(.1),
                 sampler=sampler)
    abc.new(db_path, {"result": 2})

    minimum_epsilon = .2
    history = abc.run(minimum_epsilon, max_nr_populations=3)
    mp = history.get_model_probabilities(history.max_t)
    assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08 
Example #5
Source File: test_abc_smc_algorithm.py    From pyABC with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_beta_binomial_two_identical_models(db_path, sampler):
    binomial_n = 5

    def model_fun(args):
        return {"result": st.binom(binomial_n, args.theta).rvs()}

    models = [model_fun for _ in range(2)]
    models = list(map(SimpleModel, models))
    population_size = ConstantPopulationSize(800)
    parameter_given_model_prior_distribution = [Distribution(theta=st.beta(
                                                                      1, 1))
                                                for _ in range(2)]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistance(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(.1),
                 sampler=sampler)
    abc.new(db_path, {"result": 2})

    minimum_epsilon = .2
    history = abc.run(minimum_epsilon, max_nr_populations=3)
    mp = history.get_model_probabilities(history.max_t)
    assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08 
Example #6
Source File: clt3d.py    From QuantEcon.lectures.code with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def gen_x_draws(k):
    """
    Returns a flat array containing k independent draws from the
    distribution of X, the underlying random variable.  This distribution is
    itself a convex combination of three beta distributions.
    """
    bdraws = beta_dist.rvs((3, k))
    # == Transform rows, so each represents a different distribution == #
    bdraws[0, :] -= 0.5
    bdraws[1, :] += 0.6
    bdraws[2, :] -= 1.1
    # == Set X[i] = bdraws[j, i], where j is a random draw from {0, 1, 2} == #
    js = np.random.random_integers(0, 2, size=k)
    X = bdraws[js, np.arange(k)]
    # == Rescale, so that the random variable is zero mean == #
    m, sigma = X.mean(), X.std()
    return (X - m) / sigma 
Example #7
Source File: test_distributions.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_beta(self):
        # case with finite support interval
        v = stats.beta.expect(lambda x: (x-19/3.)*(x-19/3.), args=(10, 5),
                              loc=5, scale=2)
        assert_almost_equal(v, 1./18., decimal=13)

        m = stats.beta.expect(lambda x: x, args=(10, 5), loc=5., scale=2.)
        assert_almost_equal(m, 19/3., decimal=13)

        ub = stats.beta.ppf(0.95, 10, 10, loc=5, scale=2)
        lb = stats.beta.ppf(0.05, 10, 10, loc=5, scale=2)
        prob90 = stats.beta.expect(lambda x: 1., args=(10, 10), loc=5.,
                                   scale=2., lb=lb, ub=ub, conditional=False)
        assert_almost_equal(prob90, 0.9, decimal=13)

        prob90c = stats.beta.expect(lambda x: 1, args=(10, 10), loc=5,
                                    scale=2, lb=lb, ub=ub, conditional=True)
        assert_almost_equal(prob90c, 1., decimal=13) 
Example #8
Source File: test_distributions.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_pickling(self):
        # test that a frozen instance pickles and unpickles
        # (this method is a clone of common_tests.check_pickling)
        beta = stats.beta(2.3098496451481823, 0.62687954300963677)
        poiss = stats.poisson(3.)
        sample = stats.rv_discrete(values=([0, 1, 2, 3],
                                           [0.1, 0.2, 0.3, 0.4]))

        for distfn in [beta, poiss, sample]:
            distfn.random_state = 1234
            distfn.rvs(size=8)
            s = pickle.dumps(distfn)
            r0 = distfn.rvs(size=8)

            unpickled = pickle.loads(s)
            r1 = unpickled.rvs(size=8)
            assert_equal(r0, r1)

            # also smoke test some methods
            medians = [distfn.ppf(0.5), unpickled.ppf(0.5)]
            assert_equal(medians[0], medians[1])
            assert_equal(distfn.cdf(medians[0]),
                         unpickled.cdf(medians[1])) 
Example #9
Source File: beta_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testBetaSample(self):
    with self.test_session():
      a = 1.
      b = 2.
      beta = tf.contrib.distributions.Beta(a, b)
      n = tf.constant(100000)
      samples = beta.sample(n)
      sample_values = samples.eval()
      self.assertEqual(sample_values.shape, (100000,))
      self.assertFalse(np.any(sample_values < 0.0))
      self.assertLess(
          stats.kstest(
              # Beta is a univariate distribution.
              sample_values, stats.beta(a=1., b=2.).cdf)[0],
          0.01)
      # The standard error of the sample mean is 1 / (sqrt(18 * n))
      self.assertAllClose(sample_values.mean(axis=0),
                          stats.beta.mean(a, b),
                          atol=1e-2)
      self.assertAllClose(np.cov(sample_values, rowvar=0),
                          stats.beta.var(a, b),
                          atol=1e-1)

  # Test that sampling with the same seed twice gives the same results. 
Example #10
Source File: beta_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testBetaVariance(self):
    with tf.Session():
      a = [1., 2, 3]
      b = [2., 4, 1.2]
      expected_variance = stats.beta.var(a, b)
      dist = tf.contrib.distributions.Beta(a, b)
      self.assertEqual(dist.variance().get_shape(), (3,))
      self.assertAllClose(expected_variance, dist.variance().eval()) 
Example #11
Source File: beta.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 5 votes vote down vote up
def get_description(self):
        """
        A description of the beta distribution.

        :param Beta self:
            An instance of the beta class.
        :return:
            A string describing the beta distribution.
        """
        text = "is a beta distribution is defined over a support; given here as "+str(self.lower)+", to "+str(self.upper)+". It has two shape parameters, given here to be "+str(self.shape_A)+" and "+str(self.shape_B)+"."
        return text 
Example #12
Source File: beta_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testBetaLogCdf(self):
    with self.test_session():
      shape = (30, 40, 50)
      for dt in (np.float32, np.float64):
        a = 10. * np.random.random(shape).astype(dt)
        b = 10. * np.random.random(shape).astype(dt)
        x = np.random.random(shape).astype(dt)
        actual = tf.exp(tf.contrib.distributions.Beta(a, b).log_cdf(x)).eval()
        self.assertAllEqual(np.ones(shape, dtype=np.bool), 0. <= x)
        self.assertAllEqual(np.ones(shape, dtype=np.bool), 1. >= x)
        self.assertAllClose(stats.beta.cdf(x, a, b), actual, rtol=1e-4, atol=0) 
Example #13
Source File: beta_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testBetaCdf(self):
    with self.test_session():
      shape = (30, 40, 50)
      for dt in (np.float32, np.float64):
        a = 10. * np.random.random(shape).astype(dt)
        b = 10. * np.random.random(shape).astype(dt)
        x = np.random.random(shape).astype(dt)
        actual = tf.contrib.distributions.Beta(a, b).cdf(x).eval()
        self.assertAllEqual(np.ones(shape, dtype=np.bool), 0. <= x)
        self.assertAllEqual(np.ones(shape, dtype=np.bool), 1. >= x)
        self.assertAllClose(stats.beta.cdf(x, a, b), actual, rtol=1e-4, atol=0) 
Example #14
Source File: beta_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testBetaEntropy(self):
    with tf.Session():
      a = [1., 2, 3]
      b = [2., 4, 1.2]
      expected_entropy = stats.beta.entropy(a, b)
      dist = tf.contrib.distributions.Beta(a, b)
      self.assertEqual(dist.entropy().get_shape(), (3,))
      self.assertAllClose(expected_entropy, dist.entropy().eval()) 
Example #15
Source File: beta.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 5 votes vote down vote up
def get_recurrence_coefficients(self, order):
        """
        Recurrence coefficients for the beta distribution.

        :param Beta self:
            An instance of the Beya class.
        :param array order:
            The order of the recurrence coefficients desired.
        :return:
            Recurrence coefficients associated with the beta distribution.
        """
        ab =  jacobi_recurrence_coefficients(self.shape_parameter_A, self.shape_parameter_B, self.lower, self.upper, order)
        return ab 
Example #16
Source File: beta_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testBetaMean(self):
    with tf.Session():
      a = [1., 2, 3]
      b = [2., 4, 1.2]
      expected_mean = stats.beta.mean(a, b)
      dist = tf.contrib.distributions.Beta(a, b)
      self.assertEqual(dist.mean().get_shape(), (3,))
      self.assertAllClose(expected_mean, dist.mean().eval()) 
Example #17
Source File: dirichlet_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testDirichletSample(self):
    with self.test_session():
      alpha = [1., 2]
      dirichlet = tf.contrib.distributions.Dirichlet(alpha)
      n = tf.constant(100000)
      samples = dirichlet.sample(n)
      sample_values = samples.eval()
      self.assertEqual(sample_values.shape, (100000, 2))
      self.assertTrue(np.all(sample_values > 0.0))
      self.assertLess(
          stats.kstest(
              # Beta is a univariate distribution.
              sample_values[:, 0], stats.beta(a=1., b=2.).cdf)[0],
          0.01) 
Example #18
Source File: beta.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 5 votes vote down vote up
def get_pdf(self, points=None):
        """
        A beta probability density function.

        :param Beta self:
            An instance of the Beta class.
        :return:
            Probability density values along the support of the Beta distribution.
        """
        if points is not None:
            return self.parent.pdf(points)
        else:
            raise ValueError( 'Please specify an input for getPDF method') 
Example #19
Source File: beta.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 5 votes vote down vote up
def get_cdf(self, points=None):
        """
        A beta cumulative density function.

        :param Beta self:
            An instance of the Beta class.
        :return:
            An array of N equidistant values over the support of the distribution.
        :return:
            Cumulative density values along the support of the Gamma distribution.
        """
        if points is not None:
                return self.parent.cdf(points)
        else:
            raise ValueError( 'Please digit an input for getCDF method') 
Example #20
Source File: test_probscale.py    From mpl-probscale with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_the_scale_beta():
    fig, ax = plt.subplots(figsize=(4, 8))
    ax.set_yscale('prob', as_pct=True, dist=stats.beta(3, 2))
    ax.set_ylim(1, 99)
    fig.tight_layout()
    return fig 
Example #21
Source File: variations.py    From trials with MIT License 5 votes vote down vote up
def __init__(self, alpha=0.5, beta=0.5):
        """Create a variation."""
        self.prior_alpha = alpha
        self.prior_beta = beta

        self.alpha = alpha
        self.beta = beta 
Example #22
Source File: variations.py    From trials with MIT License 5 votes vote down vote up
def update(self, successes, failures):
        """Update variation state with observations."""
        self.alpha += successes
        self.beta += failures 
Example #23
Source File: variations.py    From trials with MIT License 5 votes vote down vote up
def posterior(self):
        """Return posterior as scipy frozen random variable."""
        return stats.beta(self.alpha, self.beta) 
Example #24
Source File: estimators.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def momentcondquant(distfn, params, mom2, quantile=None, shape=None):
    '''moment conditions for estimating distribution parameters by matching
    quantiles, defines as many moment conditions as quantiles.

    Returns
    -------
    difference : array
        difference between theoretical and empirical quantiles

    Notes
    -----
    This can be used for method of moments or for generalized method of
    moments.

    '''
    #this check looks redundant/unused know
    if len(params) == 2:
        loc, scale = params
    elif len(params) == 3:
        shape, loc, scale = params
    else:
        #raise NotImplementedError
        pass #see whether this might work, seems to work for beta with 2 shape args

    #mom2diff = np.array(distfn.stats(*params)) - mom2
    #if not quantile is None:
    pq, xq = quantile
    #ppfdiff = distfn.ppf(pq, alpha)
    cdfdiff = distfn.cdf(xq, *params) - pq
    #return np.concatenate([mom2diff, cdfdiff[:1]])
    return cdfdiff
    #return mom2diff 
Example #25
Source File: test_distributions.py    From numpyro with Apache License 2.0 5 votes vote down vote up
def test_beta_binomial_log_prob(total_count, shape):
    concentration0 = np.exp(np.random.normal(size=shape))
    concentration1 = np.exp(np.random.normal(size=shape))
    value = jnp.arange(1 + total_count)

    num_samples = 100000
    probs = np.random.beta(concentration1, concentration0, size=(num_samples,) + shape)
    log_probs = dist.Binomial(total_count, probs).log_prob(value)
    expected = logsumexp(log_probs, 0) - jnp.log(num_samples)

    actual = dist.BetaBinomial(concentration1, concentration0, total_count).log_prob(value)
    assert_allclose(actual, expected, rtol=0.02) 
Example #26
Source File: other_strats.py    From Probabilistic-Programming-and-Bayesian-Methods-for-Hackers with MIT License 5 votes vote down vote up
def ucb_bayes( self ):
	C = 0
	n = 10000
	alpha =1 - 1./( (self.N+1) )
	return np.argmax( beta.ppf( alpha,
							   1 + self.wins, 
							   1 + self.trials - self.wins ) ) 
Example #27
Source File: test_abc_smc_algorithm.py    From pyABC with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_beta_binomial_different_priors(db_path, sampler):
    binomial_n = 5

    def model(args):
        return {"result": st.binom(binomial_n, args['theta']).rvs()}

    models = [model for _ in range(2)]
    models = list(map(SimpleModel, models))
    population_size = ConstantPopulationSize(800)
    a1, b1 = 1, 1
    a2, b2 = 10, 1
    parameter_given_model_prior_distribution = [Distribution(theta=RV("beta",
                                                                      a1, b1)),
                                                Distribution(theta=RV("beta",
                                                                      a2, b2))]
    abc = ABCSMC(models, parameter_given_model_prior_distribution,
                 MinMaxDistance(measures_to_use=["result"]),
                 population_size,
                 eps=MedianEpsilon(.1),
                 sampler=sampler)
    n1 = 2
    abc.new(db_path, {"result": n1})

    minimum_epsilon = .2
    history = abc.run(minimum_epsilon, max_nr_populations=3)
    mp = history.get_model_probabilities(history.max_t)

    def B(a, b):
        return gamma(a) * gamma(b) / gamma(a + b)

    def expected_p(a, b, n1):
        return binom(binomial_n, n1) * B(a + n1, b + binomial_n - n1) / B(a, b)

    p1_expected_unnormalized = expected_p(a1, b1, n1)
    p2_expected_unnormalized = expected_p(a2, b2, n1)
    p1_expected = p1_expected_unnormalized / (p1_expected_unnormalized +
                                              p2_expected_unnormalized)
    p2_expected = p2_expected_unnormalized / (p1_expected_unnormalized +
                                              p2_expected_unnormalized)
    assert abs(mp.p[0] - p1_expected) + abs(mp.p[1] - p2_expected) < .08 
Example #28
Source File: bootstrap.py    From resample with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _fit_parametric_family(dist: stats.rv_continuous, sample: np.ndarray) -> Tuple:
    if dist == stats.multivariate_normal:
        # has no fit method...
        return np.mean(sample, axis=0), np.cov(sample.T, ddof=1)

    if dist == stats.t:
        fit_kwd = {"fscale": 1}
    elif dist in {stats.f, stats.beta}:
        fit_kwd = {"floc": 0, "fscale": 1}
    elif dist in (stats.gamma, stats.lognorm, stats.invgauss, stats.pareto):
        fit_kwd = {"floc": 0}
    else:
        fit_kwd = {}

    return dist.fit(sample, **fit_kwd) 
Example #29
Source File: test_multivariate.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_2D_dirichlet_is_beta(self):
        np.random.seed(2846)

        alpha = np.random.uniform(10e-10, 100, 2)
        d = dirichlet(alpha)
        b = beta(alpha[0], alpha[1])

        num_tests = 10
        for i in range(num_tests):
            x = np.random.uniform(10e-10, 100, 2)
            x /= np.sum(x)
            assert_almost_equal(b.pdf(x), d.pdf([x]))

        assert_almost_equal(b.mean(), d.mean()[0])
        assert_almost_equal(b.var(), d.var()[0]) 
Example #30
Source File: estimators.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def momentcondquant(distfn, params, mom2, quantile=None, shape=None):
    '''moment conditions for estimating distribution parameters by matching
    quantiles, defines as many moment conditions as quantiles.

    Returns
    -------
    difference : array
        difference between theoretical and empirical quantiles

    Notes
    -----
    This can be used for method of moments or for generalized method of
    moments.

    '''
    #this check looks redundant/unused know
    if len(params) == 2:
        loc, scale = params
    elif len(params) == 3:
        shape, loc, scale = params
    else:
        #raise NotImplementedError
        pass #see whether this might work, seems to work for beta with 2 shape args

    #mom2diff = np.array(distfn.stats(*params)) - mom2
    #if not quantile is None:
    pq, xq = quantile
    #ppfdiff = distfn.ppf(pq, alpha)
    cdfdiff = distfn.cdf(xq, *params) - pq
    #return np.concatenate([mom2diff, cdfdiff[:1]])
    return cdfdiff
    #return mom2diff