예제 #1
0
    def ppf(self, x):
        """
        Computes the percent point function of the distribution at the point(s)
        x. It is defined as the inverse of the CDF. y = ppf(x) can be
        interpreted as the argument y for which the value of the cdf(x) is equal
        to y. Essentially that means the random varable y is the place on the
        distribution the CDF evaluates to x.

        Parameters
        ----------
        x: array, dtype=float, shape=(m x n), bounds=(0,1)
            The value(s) at which the user would like the ppf evaluated.
            If an array is passed in, the ppf is evaluated at every point
            in the array and an array of the same size is returned.

        Returns
        -------
        ppf: array, dtype=float, shape=(m x n)
            The ppf at each point in x.
        """
        vals = np.ceil(bdtrik(x, self.n, self.p))
        vals1 = vals - 1
        temp = bdtr(vals1, self.n, self.p)
        ppf = np.where((temp >= x), vals1, vals)
        return ppf
예제 #2
0
    def cdf(self, *X):
        # check array for numpy structure
        X = check_array(X, reduce_args=True, ensure_1d=True)

        # floor X values
        X = np.floor(X)

        return sc.bdtr(X, self.n_trials, self.bias)
예제 #3
0
 def _cdf_single(self, x, n, a, b):
     k = floor(x)
     p = linspace(0, 1, num=10001)
     bta = btdtr(a, b, p)
     p_med = (p[:-1] + p[1:]) / 2
     bta_med = bta[1:] - bta[:-1]
     vals = (bdtr(k, n, p_med) * bta_med).sum(axis=-1)
     return vals
예제 #4
0
    def _cdf(self, x_data, size, prob):
        size = numpy.round(size)
        x_data = x_data - 0.5

        floor = numpy.zeros(x_data.shape)
        indices = x_data >= 0
        floor[indices] = special.bdtr(numpy.floor(x_data[indices]), size, prob)

        ceil = numpy.ones(x_data.shape)
        indices = x_data <= size
        ceil[indices] = special.bdtr(numpy.ceil(x_data[indices]), size, prob)
        ceil[numpy.isnan(ceil)] = 0  # left edge case

        offset = x_data - numpy.floor(x_data)

        return floor * (1 - offset) + ceil * offset

        offset = x_data - x_data_int + 0.5
        assert numpy.all(offset >= 0) and numpy.all(offset <= 1), (
            "somethings up with the offset: %s" % offset)
        x_data_int = numpy.clip(x_data_int, 0, size)
        out = out_lower * (1 - offset) + out_upper * offset
        return out
예제 #5
0
    def quantile(self, *q):
        # check array for numpy structure
        q = check_array(q, reduce_args=True, ensure_1d=True)

        # get the upper value of X (ceiling)
        X_up = np.ceil(sc.bdtrik(q, self.n_trials, self.bias))

        # get the lower value of X (floor)
        X_down = np.maximum(X_up - 1, 0)

        # recompute quantiles to validate transformation
        q_test = sc.bdtr(X_down, self.n_trials, self.bias)

        # when q_test is greater than true, shift output down
        out = np.where(q_test >= q, X_down, X_up).astype(int)

        # return only in-bound values
        return np.where(self.support.contains(out), out, np.nan)
예제 #6
0
    def cdf(self, x):
        """
        Computes the cumulative distribution function of the
        distribution at the point(s) x. The cdf is defined as follows:
            F(x|p) = 1 - (1 - p) ** x

        Parameters
        ----------
        x: array, dtype=float, shape=(m x n)
            The value(s) at which the user would like the cdf evaluated.
            If an array is passed in, the cdf is evaluated at every point
            in the array and an array of the same size is returned.

        Returns
        -------
        cdf: array, dtype=float, shape=(m x n)
            The cdf at each point in x.
        """
        k = np.floor(x)
        cdf = bdtr(k, self.n, self.p)

        return cdf
예제 #7
0
    def cdf(self, x):
        """
        Computes the cumulative distribution function of the
        distribution at the point(s) x. The cdf is defined as follows:
            F(x|p) = 1 - (1 - p) ** x

        Parameters
        ----------
        x: array, dtype=float, shape=(m x n)
            The value(s) at which the user would like the cdf evaluated.
            If an array is passed in, the cdf is evaluated at every point
            in the array and an array of the same size is returned.

        Returns
        -------
        cdf: array, dtype=float, shape=(m x n)
            The cdf at each point in x.
        """
        k = np.floor(x)
        cdf = bdtr(k, self.n, self.p)

        return cdf
예제 #8
0
 def test_bdtr_bdtri_roundtrip(self):
     bdtr_vals = sc.bdtr([0, 1, 2], 2, 0.5)
     roundtrip_vals = sc.bdtri([0, 1, 2], 2, bdtr_vals)
     assert_allclose(roundtrip_vals, [0.5, 0.5, np.nan])
예제 #9
0
 def test(self):
     val = sc.bdtr(0, 1, 0.5)
     assert_allclose(val, 0.5)
예제 #10
0
 def test_bdtr_bdtrc_sum_to_one(self):
     bdtr_vals = sc.bdtr([0, 1, 2], 2, 0.5)
     bdtrc_vals = sc.bdtrc([0, 1, 2], 2, 0.5)
     vals = bdtr_vals + bdtrc_vals
     assert_allclose(vals, [1.0, 1.0, 1.0])
예제 #11
0
 def test_domain(self):
     val = sc.bdtr(-1.1, 1, 0.5)
     assert np.isnan(val)
예제 #12
0
 def test_inf(self, k, n, p):
     with suppress_warnings() as sup:
         sup.filter(DeprecationWarning)
         val = sc.bdtr(k, n, p)
     assert np.isnan(val)
예제 #13
0
 def test_rounding(self):
     double_val = sc.bdtr([0.1, 1.1, 2.1], 2, 0.5)
     int_val = sc.bdtr([0, 1, 2], 2, 0.5)
     assert_array_equal(double_val, int_val)
예제 #14
0
 def ref(self, x, y, z):
     return special.bdtr(x, y, z)
예제 #15
0
 def _cdf(self, x, n, p):
     k = floor(x)
     vals = special.bdtr(k, n, p)
     return vals
예제 #16
0
 def _cdf(self, x, n, p):
     k = floor(x)
     vals = special.bdtr(k, n, p)
     return vals
예제 #17
0
def addsubtract(a, b):
    if a > b:
        return a - b
    else:
        return a + b

vec_addsubtract = np.vectorize(addsubtract)
r = vec_addsubtract([0, 3, 6, 9], [1, 3, 5, 7])
print(r)

np.cast['f'](np.pi)

x = np.r_[-2:3]
y = np.select([x > 3, x >= 0], [0, x + 2])
print(y)

print(bdtr(-1, 10, 0.3))


# 1-D interpolation (interp1d)
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2 / 9.0)
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')

xnew = np.linspace(0, 10, num=41, endpoint=True)
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.show()
예제 #18
0
 def _ppf(self, q, n, p):
     vals = ceil(special.bdtrik(q, n, p))
     vals1 = np.maximum(vals - 1, 0)
     temp = special.bdtr(vals1, n, p)
     return np.where(temp >= q, vals1, vals)
예제 #19
0
 def _cdf(self, x_data, size, prob):
     return special.bdtr(numpy.floor(x_data), numpy.floor(size), prob)
예제 #20
0
 def _ppf(self, q, n, p):
     vals = ceil(special.bdtrik(q, n, p))
     vals1 = np.maximum(vals - 1, 0)
     temp = special.bdtr(vals1, n, p)
     return np.where(temp >= q, vals1, vals)
예제 #21
0
 def test_sum_is_one(self):
     val = sc.bdtr([0, 1, 2], 2, 0.5)
     assert_array_equal(val, [0.25, 0.75, 1.0])