Exemplo n.º 1
0
def test_bernstein_distribution_2d():
    grid = dt._Grid([51, 51])

    cop_tr = tra.TransfFrank
    args = (2, )
    ca = ArchimedeanCopula(cop_tr())
    distr1 = stats.uniform
    distr2 = stats.uniform
    cad = CopulaDistribution(ca, [distr1, distr2], cop_args=args)
    cdfv = cad.cdf(grid.x_flat, args)
    cdf_g = cdfv.reshape(grid.k_grid)

    bpd = BernsteinDistribution(cdf_g)

    cdf_bp = bpd.cdf(grid.x_flat)
    assert_allclose(cdf_bp, cdfv, atol=0.005)
    assert_array_less(np.median(np.abs(cdf_bp - cdfv)), 0.001)

    grid_eps = dt._Grid([51, 51], eps=1e-8)
    pdfv = cad.pdf(grid_eps.x_flat)
    pdf_bp = bpd.pdf(grid_eps.x_flat)
    assert_allclose(pdf_bp, pdfv, atol=0.01, rtol=0.04)
    assert_array_less(np.median(np.abs(pdf_bp - pdfv)), 0.05)

    # check marginal cdfs
    # get marginal cdf
    xx = np.column_stack((np.linspace(0, 1, 5), np.ones(5)))
    cdf_m1 = bpd.cdf(xx)
    assert_allclose(cdf_m1, xx[:, 0], atol=1e-13)
    xx = np.column_stack((np.ones(5), np.linspace(0, 1, 5)))
    cdf_m2 = bpd.cdf(xx)
    assert_allclose(cdf_m2, xx[:, 1], atol=1e-13)

    xx_ = np.linspace(0, 1, 5)
    xx = xx_[:, None]  # currently requires 2-dim
    bpd_m1 = bpd.get_marginal(0)
    cdf_m1 = bpd_m1.cdf(xx)
    assert_allclose(cdf_m1, xx_, atol=1e-13)
    pdf_m1 = bpd_m1.pdf(xx)
    assert_allclose(pdf_m1, np.ones(len(xx)), atol=1e-13)

    bpd_m = bpd.get_marginal(1)
    cdf_m = bpd_m.cdf(xx)
    assert_allclose(cdf_m, xx_, atol=1e-13)
    pdf_m = bpd_m.pdf(xx)
    assert_allclose(pdf_m, np.ones(len(xx)), atol=1e-13)
Exemplo n.º 2
0
    def setup_class(cls):
        grid = dt._Grid([91, 101])

        cop_tr = tra.TransfFrank
        args = (2, )
        ca = ArchimedeanCopula(cop_tr())
        distr1 = stats.beta(4, 3)
        distr2 = stats.beta(4, 4)  # (5, 2)
        cad = CopulaDistribution(ca, [distr1, distr2], cop_args=args)
        cdfv = cad.cdf(grid.x_flat, args)
        cdf_g = cdfv.reshape(grid.k_grid)

        cls.grid = grid
        cls.cdfv = cdfv
        cls.distr = cad
        cls.bpd = BernsteinDistribution(cdf_g)
Exemplo n.º 3
0
def test_bernstein_distribution_1d():
    grid = dt._Grid([501])
    loc = grid.x_flat == 0
    grid.x_flat[loc] = grid.x_flat[~loc].min() / 2
    grid.x_flat[grid.x_flat == 1] = 1 - grid.x_flat.min()

    distr = stats.beta(3, 5)
    cdf_g = distr.cdf(np.squeeze(grid.x_flat))
    bpd = BernsteinDistribution(cdf_g)

    cdf_bp = bpd.cdf(grid.x_flat)
    assert_allclose(cdf_bp, cdf_g, atol=0.005)
    assert_array_less(np.median(np.abs(cdf_bp - cdf_g)), 0.001)

    pdfv = distr.pdf(np.squeeze(grid.x_flat))
    pdf_bp = bpd.pdf(grid.x_flat)
    assert_allclose(pdf_bp, pdfv, atol=0.02)
    assert_array_less(np.median(np.abs(pdf_bp - pdfv)), 0.01)

    # compare with UV class
    xf = np.squeeze(grid.x_flat)  # UV returns column if x is column
    bpd1 = BernsteinDistributionUV(cdf_g)
    cdf_bp1 = bpd1.cdf(xf)
    assert_allclose(cdf_bp1, cdf_bp, atol=1e-13)
    pdf_bp1 = bpd1.pdf(xf)
    assert_allclose(pdf_bp1, pdf_bp, atol=1e-13)

    cdf_bp1 = bpd1.cdf(xf, method="beta")
    assert_allclose(cdf_bp1, cdf_bp, atol=1e-13)
    pdf_bp1 = bpd1.pdf(xf, method="beta")
    assert_allclose(pdf_bp1, pdf_bp, atol=1e-13)

    cdf_bp1 = bpd1.cdf(xf, method="bpoly")
    assert_allclose(cdf_bp1, cdf_bp, atol=1e-13)
    pdf_bp1 = bpd1.pdf(xf, method="bpoly")
    assert_allclose(pdf_bp1, pdf_bp, atol=1e-13)

    # check rvs
    # currently smoke test
    rvs = bpd.rvs(100)
    assert len(rvs) == 100
Exemplo n.º 4
0
    def test_basic(self):
        bpd = self.bpd
        grid = self.grid
        cdfv = self.cdfv
        distr = self.distr

        if grid.x_flat.shape[0] < 51**2:
            cdf_bp = bpd.cdf(grid.x_flat)
            assert_allclose(cdf_bp, cdfv, atol=0.05)
            assert_array_less(np.median(np.abs(cdf_bp - cdfv)), 0.01)

        grid_eps = dt._Grid([51, 51], eps=1e-2)
        cdfv = distr.cdf(grid_eps.x_flat)
        cdf_bp = bpd.cdf(grid_eps.x_flat)
        assert_allclose(cdf_bp, cdfv, atol=0.01, rtol=0.01)
        assert_array_less(np.median(np.abs(cdf_bp - cdfv)), 0.05)

        pdfv = distr.pdf(grid_eps.x_flat)
        pdf_bp = bpd.pdf(grid_eps.x_flat)
        assert_allclose(pdf_bp, pdfv, atol=0.06, rtol=0.1)
        assert_array_less(np.median(np.abs(pdf_bp - pdfv)), 0.05)
Exemplo n.º 5
0
def test_grid_class():

    res = {
        'k_grid': [3, 5],
        'x_marginal':
        [np.array([0., 0.5, 1.]),
         np.array([0., 0.25, 0.5, 0.75, 1.])],
        'idx_flat.T':
        np.array([[0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2.],
                  [0., 1., 2., 3., 4., 0., 1., 2., 3., 4., 0., 1., 2., 3.,
                   4.]])
    }
    gg = dt._Grid([3, 5])
    assert_equal(gg.k_grid, res["k_grid"])
    assert gg.x_marginal, res["x_marginal"]
    assert_allclose(gg.idx_flat, res["idx_flat.T"].T, atol=1e-12)
    assert_allclose(gg.x_flat, res["idx_flat.T"].T / [2, 4], atol=1e-12)

    gg = dt._Grid([3, 5], eps=0.001)
    assert_allclose(gg.x_flat.min(), 0.001, atol=1e-12)
    assert_allclose(gg.x_flat.max(), 0.999, atol=1e-12)
    xmf = np.concatenate(gg.x_marginal)
    assert_allclose(xmf.min(), 0.001, atol=1e-12)
    assert_allclose(xmf.max(), 0.999, atol=1e-12)

    # 1-dim
    gg = dt._Grid([5], eps=0.001)
    res = {
        'k_grid': [5],
        'x_marginal': [np.array([0.001, 0.25, 0.5, 0.75, 0.999])],
        'idx_flat.T': np.array([[0., 1., 2., 3., 4.]])
    }
    assert_equal(gg.k_grid, res["k_grid"])
    assert gg.x_marginal, res["x_marginal"]
    assert_allclose(gg.idx_flat, res["idx_flat.T"].T, atol=1e-12)
    # x_flat is 2-dim even if grid is 1-dim, TODO: maybe change
    assert_allclose(gg.x_flat, res["x_marginal"][0][:, None], atol=1e-12)

    # 3-dim
    gg = dt._Grid([3, 3, 2], eps=0.)
    res = {
        'k_grid': [3, 3, 2],
        'x_marginal':
        [np.array([0., 0.5, 1.]),
         np.array([0., 0.5, 1.]),
         np.array([0., 1.])],
        'idx_flat.T':
        np.array([[
            0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2.,
            2.
        ],
                  [
                      0., 0., 1., 1., 2., 2., 0., 0., 1., 1., 2., 2., 0., 0.,
                      1., 1., 2., 2.
                  ],
                  [
                      0., 1., 0., 1., 0., 1., 0., 1., 0., 1., 0., 1., 0., 1.,
                      0., 1., 0., 1.
                  ]])
    }
    assert_equal(gg.k_grid, res["k_grid"])
    assert gg.x_marginal, res["x_marginal"]
    assert_allclose(gg.idx_flat, res["idx_flat.T"].T, atol=1e-12)
    assert_allclose(gg.x_flat, res["idx_flat.T"].T / [2, 2, 1], atol=1e-12)
Exemplo n.º 6
0
 def __init__(self, cdf_grid):
     self.cdf_grid = cdf_grid = np.asarray(cdf_grid)
     self.k_dim = cdf_grid.ndim
     self.k_grid = cdf_grid.shape
     self.k_grid_product = np.product([i-1 for i in self.k_grid])
     self._grid = _Grid(self.k_grid)