コード例 #1
0
def test_random_uniform(vector_array):
    with pytest.raises(Exception):
        vector_array.random(-1)
    for c in (0, 1, 2, 30):
        for low in (-1e-3, 0, 7):
            for high in (0.5, 7):
                if c > 0 and high <= low:
                    with pytest.raises(ValueError):
                        vector_array.random(c, low=low, high=high)
                    continue
                seed = 123
                try:
                    v = vector_array.random(c, low=low, high=high, seed=seed)
                except ValueError as e:
                    if high <= low:
                        continue
                    raise e
                assert v.space == vector_array.space
                assert len(v) == c
                if min(v.dim, c) > 0:
                    assert np.all(v.sup_norm() < max(abs(low), abs(high)))
                try:
                    x = v.to_numpy()
                    assert x.shape == (c, v.dim)
                    assert np.all(x < high)
                    assert np.all(x >= low)
                except NotImplementedError:
                    pass
                vv = vector_array.random(c,
                                         distribution='uniform',
                                         low=low,
                                         high=high,
                                         seed=seed)
                assert np.allclose((v - vv).sup_norm(), 0.)
コード例 #2
0
ファイル: vectorarray.py プロジェクト: pymor/pymor
def test_random_uniform(vector_array):
    with pytest.raises(Exception):
        vector_array.random(-1)
    for c in (0, 1, 2, 30):
        for low in (-1e-3, 0, 7):
            for high in (0.5, 7):
                if c > 0 and high <= low:
                    with pytest.raises(ValueError):
                        vector_array.random(c, low=low, high=high)
                    continue
                seed = 123
                try:
                    v = vector_array.random(c, low=low, high=high, seed=seed)
                except ValueError as e:
                    if high <= low:
                        continue
                    raise e
                assert v.space == vector_array.space
                assert len(v) == c
                if min(v.dim, c) > 0:
                    assert np.all(v.sup_norm() < max(abs(low), abs(high)))
                try:
                    x = v.to_numpy()
                    assert x.shape == (c, v.dim)
                    assert np.all(x < high)
                    assert np.all(x >= low)
                except NotImplementedError:
                    pass
                vv = vector_array.random(c, distribution='uniform', low=low, high=high, seed=seed)
                assert np.allclose((v - vv).sup_norm(), 0.)
コード例 #3
0
def test_random_normal(vector_array):
    with pytest.raises(Exception):
        vector_array.random(-1)
    for c in (0, 1, 2, 30):
        for loc in (-1e-3, 0, 7):
            for scale in (-1, 0.5, 7):
                if c > 0 and scale <= 0:
                    with pytest.raises(ValueError):
                        vector_array.random(c, 'normal', loc=loc, scale=scale)
                    continue
                seed = 123
                try:
                    v = vector_array.random(c,
                                            'normal',
                                            loc=loc,
                                            scale=scale,
                                            seed=seed)
                except ValueError as e:
                    if scale <= 0:
                        continue
                    raise e
                assert v.space == vector_array.space
                assert len(v) == c
                try:
                    x = v.to_numpy()
                    assert x.shape == (c, v.dim)
                    import scipy.stats
                    n = x.size
                    if n == 0:
                        continue
                    # test for expected value
                    norm = scipy.stats.norm()
                    gamma = 1 - 1e-7
                    alpha = 1 - gamma
                    lower = np.sum(x) / n - norm.ppf(1 - alpha /
                                                     2) * scale / np.sqrt(n)
                    upper = np.sum(x) / n + norm.ppf(1 - alpha /
                                                     2) * scale / np.sqrt(n)
                    assert lower <= loc <= upper
                except NotImplementedError:
                    pass
                vv = vector_array.random(c,
                                         'normal',
                                         loc=loc,
                                         scale=scale,
                                         seed=seed)
                assert np.allclose((v - vv).sup_norm(), 0.)
コード例 #4
0
ファイル: vectorarray.py プロジェクト: pymor/pymor
def test_random_normal(vector_array):
    with pytest.raises(Exception):
        vector_array.random(-1)
    for c in (0, 1, 2, 30):
        for loc in (-1e-3, 0, 7):
            for scale in (-1, 0.5, 7):
                if c > 0 and scale <= 0:
                    with pytest.raises(ValueError):
                        vector_array.random(c, 'normal', loc=loc, scale=scale)
                    continue
                seed = 123
                try:
                    v = vector_array.random(c, 'normal', loc=loc, scale=scale, seed=seed)
                except ValueError as e:
                    if scale <= 0:
                        continue
                    raise e
                assert v.space == vector_array.space
                assert len(v) == c
                try:
                    x = v.to_numpy()
                    assert x.shape == (c, v.dim)
                    import scipy.stats
                    n = x.size
                    if n == 0:
                        continue
                    # test for expected value
                    norm = scipy.stats.norm()
                    gamma = 1 - 1e-7
                    alpha = 1 - gamma
                    lower = np.sum(x)/n - norm.ppf(1 - alpha/2) * scale / np.sqrt(n)
                    upper = np.sum(x)/n + norm.ppf(1 - alpha/2) * scale / np.sqrt(n)
                    assert lower <= loc <= upper
                except NotImplementedError:
                    pass
                vv = vector_array.random(c, 'normal', loc=loc, scale=scale, seed=seed)
                assert np.allclose((v - vv).sup_norm(), 0.)