コード例 #1
0
ファイル: test_normal_domain.py プロジェクト: zgrey/PSDR
def test_corner():
    dom = NormalDomain([5], [[0.1]], truncate=1e-2)
    print dom.norm_lb
    print dom.norm_ub
    print dom._center()
    print "lower bound", dom.corner([-1])
    print "upper bound", dom.corner([1])
    print dom.sample(10)
コード例 #2
0
def test_sampling(m=5):
    np.random.seed(0)
    mean = np.random.randn(m)
    L = np.random.randn(m, m)
    cov = L.dot(L.T)

    dom = NormalDomain(mean, cov)

    X = dom.sample(1e6)

    mean_est = np.mean(X, axis=0)
    print('mean')
    print(mean)
    print(mean_est)
    print(
        np.isclose(mean,
                   np.mean(X, axis=0),
                   rtol=50 / np.sqrt(X.shape[0]),
                   atol=50 / np.sqrt(X.shape[0])))
    assert np.all(
        np.isclose(mean,
                   np.mean(X, axis=0),
                   rtol=50 / np.sqrt(X.shape[0]),
                   atol=50 / np.sqrt(X.shape[0]))), "Mean not correct"

    print("Covariance")
    cov_est = np.cov(X.T)
    print(cov)
    print(cov_est)
    print(
        np.isclose(cov,
                   cov_est,
                   rtol=100 / np.sqrt(X.shape[0]),
                   atol=100 / np.sqrt(X.shape[0])))
    assert np.all(
        np.isclose(cov,
                   cov_est,
                   rtol=100 / np.sqrt(X.shape[0]),
                   atol=100 / np.sqrt(X.shape[0]))), "Covariance not correct"

    # Now check with a truncation parameter
    truncate = 1e-2
    dom2 = NormalDomain(mean, cov, truncate=truncate)
    print(dom2.clip)
    X2 = dom2.sample(1e5)
    assert np.all(dom2.isinside(
        X2)), "Sampling did not place all points inside the constraints"

    frac_inside = np.sum(dom2.isinside(X)) / len(X)

    assert np.isclose(
        1 - frac_inside, truncate, atol=100 * truncate / np.sqrt(X.shape[0])
    ), "Constraint doesn't seem to get right fraction included"