コード例 #1
0
def test_noise_dbns():

    X = np.random.standard_normal((10, 5))
    Q = X.T.dot(X)
    noises = [randomization.isotropic_gaussian((5,), 1.),
              randomization.laplace((5,), 1.),
              randomization.logistic((5,), 1.),
              randomization.gaussian(Q)]

    for i, noise in enumerate(noises):

        x = np.random.standard_normal(5)
        u = np.random.standard_normal(5)
        noise.log_density(x)
        np.testing.assert_allclose(np.exp(noise.log_density(x)), noise._density(x))
        noise.smooth_objective(x, 'func')
        noise.smooth_objective(x, 'grad')
        noise.smooth_objective(x, 'both')
        noise.gradient(x)

        S = noise.sample()
        if i != 3:
            np.testing.assert_allclose(float(noise.log_density(S)), float(np.log(noise._density(S)).sum()))
        nt.assert_equal(noise.sample().shape, (5,))
        nt.assert_equal(noise.sample().shape, (5,))

        if hasattr(noise, "CGF"):
            val, grad = noise.CGF
            u = np.zeros(5)
            u[:2] = 0.1
            val(u), grad(u)

        if hasattr(noise, "CGF_grad"):
            val, grad = noise.CGF_grad
            val(x), grad(x)
コード例 #2
0
def test_noise_dbns():

    X = np.random.standard_normal((10, 5))
    Q = X.T.dot(X)
    noises = [
        randomization.isotropic_gaussian((5, ), 1.),
        randomization.laplace((5, ), 1.),
        randomization.logistic((5, ), 1.),
        randomization.gaussian(Q)
    ]

    v1, v2 = [], []

    for i, noise in enumerate(noises):

        x = np.random.standard_normal(5)
        u = np.random.standard_normal(5)
        v1.append(np.exp(noise.log_density(x)))
        v2.append(noise._density(x))

        noise.smooth_objective(x, 'func')
        noise.smooth_objective(x, 'grad')
        noise.smooth_objective(x, 'both')
        noise.gradient(x)

        nt.assert_equal(noise.sample().shape, (5, ))
        nt.assert_equal(noise.sample().shape, (5, ))

        if noise.CGF is not None:
            u = np.zeros(5)
            u[:2] = 0.1
            noise.CGF.smooth_objective(u, 'both')

        if noise.CGF_conjugate is not None:
            noise.CGF_conjugate.smooth_objective(x, 'both')
コード例 #3
0
    def __init__(self, X, Y, l_theory, l_min, l_1se, sigma_reid):

        randomized_lasso.__init__(self, X, Y, l_theory, l_min, l_1se,
                                  sigma_reid)
        n, p = X.shape

        ARrho = []
        for s in np.random.sample(100):
            Xr = X[int(s * n)]
            ARrho.append(np.corrcoef(Xr[1:], Xr[:-1])[0, 1])
        ARrho = np.mean(ARrho)
        print("AR parameter", ARrho)

        mean_diag = np.mean((X**2).sum(0))
        randomizer_scale = np.sqrt(mean_diag) * np.std(
            Y) * self.randomizer_scale

        ARcov = ARrho**(np.abs(np.subtract.outer(
            np.arange(p), np.arange(p)))) * randomizer_scale**2
        self._randomizer = randomization.gaussian(ARcov)