예제 #1
0
    def test_bca(self):
        num_bootstrap = 20
        bs = IIDBootstrap(self.x)
        bs.seed(23456)

        ci_direct = bs.conf_int(self.func, reps=num_bootstrap, method='bca')
        bs.reset()
        base, results = bs._base, bs._results
        p = np.zeros(2)
        p[0] = np.mean(results[:, 0] < base[0])
        p[1] = np.mean(results[:, 1] < base[1])
        b = stats.norm.ppf(p)
        b = b[:, None]
        q = stats.norm.ppf(np.array([0.025, 0.975]))

        base = self.func(self.x)
        nobs = self.x.shape[0]
        jk = _loo_jackknife(self.func, nobs, [self.x], {})
        u = jk.mean() - jk
        u2 = np.sum(u * u, 0)
        u3 = np.sum(u * u * u, 0)
        a = u3 / (6.0 * (u2**1.5))
        a = a[:, None]
        percentiles = 100 * stats.norm.cdf(b + (b + q) / (1 - a * (b + q)))

        ci = np.zeros((2, 2))
        for i in range(2):
            ci[i] = np.percentile(results[:, i], list(percentiles[i]))
        ci = ci.T
        assert_allclose(ci_direct, ci)
예제 #2
0
    def test_bca(self):
        num_bootstrap = 20
        bs = IIDBootstrap(self.x)
        bs.seed(23456)

        def func(y):
            return y.mean(axis=0)

        ci_direct = bs.conf_int(func, reps=num_bootstrap, method='bca')
        bs.reset()
        base, results = bs._base, bs._results
        p = np.zeros(2)
        p[0] = np.mean(results[:, 0] < base[0])
        p[1] = np.mean(results[:, 1] < base[1])
        b = stats.norm.ppf(p)
        b = b[:, None]
        q = stats.norm.ppf(np.array([0.025, 0.975]))

        base = func(self.x)
        nobs = self.x.shape[0]
        jk = _loo_jackknife(func, nobs, [self.x], {})
        u = (nobs - 1) * (jk - base)
        u2 = np.sum(u * u, 0)
        u3 = np.sum(u * u * u, 0)
        a = u3 / (6.0 * (u2 ** 1.5))
        a = a[:, None]
        percentiles = 100 * stats.norm.cdf(b + (b + q) / (1 - a * (b + q)))

        ci = np.zeros((2, 2))
        for i in range(2):
            ci[i] = np.percentile(results[:, i], list(percentiles[i]))
        ci = ci.T
        assert_allclose(ci_direct, ci)
예제 #3
0
    def test_jackknife(self):
        def func(x):
            return x.mean(axis=0)

        x = self.x
        results = _loo_jackknife(func, len(x), (x, ), {})

        direct_results = np.zeros_like(x)
        for i in range(len(x)):
            if i == 0:
                y = x[1:]
            elif i == (len(x) - 1):
                y = x[:-1]
            else:
                temp = list(x[:i])
                temp.extend(list(x[i + 1:]))
                y = np.array(temp)
            direct_results[i] = func(y)
        assert_allclose(direct_results, results)

        x = self.x_df
        results_df = _loo_jackknife(func, len(x), (x, ), {})
        assert_equal(results, results_df)

        y = self.y
        results = _loo_jackknife(func, len(y), (y, ), {})

        direct_results = np.zeros_like(y)
        for i in range(len(y)):
            if i == 0:
                z = y[1:]
            elif i == (len(y) - 1):
                z = y[:-1]
            else:
                temp = list(y[:i])
                temp.extend(list(y[i + 1:]))
                z = np.array(temp)
            direct_results[i] = func(z)
        assert_allclose(direct_results, results)

        y = self.y_series
        results_series = _loo_jackknife(func, len(y), (y, ), {})
        assert_allclose(results, results_series)
예제 #4
0
    def test_jackknife(self):
        def func(x):
            return x.mean(axis=0)

        x = self.x
        results = _loo_jackknife(func, len(x), (x,), {})

        direct_results = np.zeros_like(x)
        for i in range(len(x)):
            if i == 0:
                y = x[1:]
            elif i == (len(x) - 1):
                y = x[:-1]
            else:
                temp = list(x[:i])
                temp.extend(list(x[i + 1:]))
                y = np.array(temp)
            direct_results[i] = func(y)
        assert_allclose(direct_results, results)

        x = self.x_df
        results_df = _loo_jackknife(func, len(x), (x,), {})
        assert_equal(results, results_df)

        y = self.y
        results = _loo_jackknife(func, len(y), (y,), {})

        direct_results = np.zeros_like(y)
        for i in range(len(y)):
            if i == 0:
                z = y[1:]
            elif i == (len(y) - 1):
                z = y[:-1]
            else:
                temp = list(y[:i])
                temp.extend(list(y[i + 1:]))
                z = np.array(temp)
            direct_results[i] = func(z)
        assert_allclose(direct_results, results)

        y = self.y_series
        results_series = _loo_jackknife(func, len(y), (y,), {})
        assert_equal(results, results_series)