Exemplo n.º 1
0
 def setUp(self):
     self.N = 15
     self.D = 2
     self.df = Inverse(c=0.5)
     self.lm = GaussianLikelihoodModel(mu0=np.zeros(self.D),
                                       sigma0=np.eye(self.D),
                                       sigma=np.eye(self.D)).compile()
     self.ddt = DirichletDiffusionTree(self.df, self.lm)
     self.sampler = MetropolisHastingsSampler(self.ddt, np.zeros((self.N, self.D)))
     self.sampler.initialize_assignments()
Exemplo n.º 2
0
X = X[idx]
y = y[idx]

constraints = set()

for constraint in interactor.current_interactions:
    a, b, c = constraint
    if a not in idx_map or b not in idx_map or c not in idx_map:
        continue
    constraints.add(tuple(map(lambda x: idx_map[x], constraint)))

logging.info("Interactions: %s", constraints)
logging.info("Interactions: %u", len(constraints))

N, D = X.shape

df = Inverse(c=2)
cov = np.cov(X.T) / 4.0 + np.eye(D) * 0.001
lm = GaussianLikelihoodModel(sigma=cov, sigma0=np.eye(D) / 2.0, mu0=X.mean(axis=0)).compile()

model = DirichletDiffusionTree(df=df,
                               likelihood_model=lm,
                               constraints=constraints)
sampler = MetropolisHastingsSampler(model, X)
sampler.initialize_assignments()

def iterate(iters):
    for i in tqdm(xrange(iters)):
        sampler.sample()
Exemplo n.º 3
0
        data, _ = simulator.gen(n, p)
        #data = np.random.uniform(0,1,(n, 2))
        all_dat[i] = data
        
        #data2, tree2 = simulator2.gen(n, p)
        #all_dat2[i] = data2.ravel()
        #true_lik2.append(tree2.calculate_marg_log_likelihood(c=10))
    print("Finish Simulating Data")

    ######
    #D = 2 
    #N = 100
    #X = np.random.multivariate_normal(mean=np.zeros(D), cov=np.eye(D), size=N).astype(np.float32)
    #print(X.shape)
    df = Inverse()
    lm = GaussianLikelihoodModel(sigma=np.eye(p), mu0=np.zeros(p), sigma0=np.eye(p))
    print("Get initial likelihood")
    #df2 = Inverse()
    #lm2 = GaussianLikelihoodModel(sigma=np.eye(p), mu0=np.zeros(p), sigma0=np.eye(p))
    #print("Get initial likelihood")

    ddt = DirichletDiffusionTree(df=df, likelihood_model=lm)
    #ddt2 = DirichletDiffusionTree(df=df2, likelihood_model=lm2)


    print("Initialize Dirichlet Diffusion Tree")
    tree_list = []
    #tree_list2 = []

    """"""
    for i in range(n_samples):
Exemplo n.º 4
0
import logging
logging.basicConfig(level=logging.INFO)
import numpy as np
from trees.util import plot_tree, plot_tree_2d
from trees.ddt import DirichletDiffusionTree, Inverse, GaussianLikelihoodModel
from trees.mcmc import MetropolisHastingsSampler
from tqdm import tqdm

if __name__ == "__main__":
    D = 2
    N = 100
    X = np.random.multivariate_normal(mean=np.zeros(D), cov=np.eye(D),
                                      size=N).astype(np.float32)
    df = Inverse(c=1)
    lm = GaussianLikelihoodModel(sigma=np.eye(D) / 4.0,
                                 mu0=np.zeros(D),
                                 sigma0=np.eye(D))
    ddt = DirichletDiffusionTree(df=df, likelihood_model=lm)
    mh = MetropolisHastingsSampler(ddt, X)
    mh.initialize_assignments()

    for _ in tqdm(range(1000)):
        mh.sample()

    plt.figure()
    plt.plot(mh.likelihoods)

    #plt.figure()
    #plot_tree(mh.tree)

    #plt.figure()
Exemplo n.º 5
0
class TestDDT(unittest.TestCase):

    def setUp(self):
        self.N = 15
        self.D = 2
        self.df = Inverse(c=0.5)
        self.lm = GaussianLikelihoodModel(mu0=np.zeros(self.D),
                                          sigma0=np.eye(self.D),
                                          sigma=np.eye(self.D)).compile()
        self.ddt = DirichletDiffusionTree(self.df, self.lm)
        self.sampler = MetropolisHastingsSampler(self.ddt, np.zeros((self.N, self.D)))
        self.sampler.initialize_assignments()

    def test_choice(self):
        stay_prob = 1.0 / self.ddt.root.tree_size

        self.assertEqual(self.ddt.uniform_index(stay_prob / 2, ignore_depth=0)[0], self.ddt.root)

        left_size, right_size = self.ddt.root.left.tree_size, self.ddt.root.right.tree_size

        remainder = 1 - stay_prob
        left_prob, right_prob = left_size / float(left_size + right_size) * remainder, \
            right_size / float(left_size + right_size) * remainder

        self.assertEqual(stay_prob + left_prob + right_prob, 1)

        p = 1.0 / left_size / 2

        self.assertEqual(self.ddt.uniform_index(stay_prob + p * left_prob, ignore_depth=0)[0], self.ddt.root.left)

        p = 1.0 / right_size / 2
        self.assertEqual(self.ddt.uniform_index(stay_prob + left_prob + p * right_prob, ignore_depth=0)[0], self.ddt.root.right)

    def test_get_node(self):
        self.assertEqual(self.ddt[()], self.ddt.root)

    def test_point_index(self):
        def find_point(i):
            for node in self.ddt.dfs():
                if {i} == node.points():
                    return node
        for i in xrange(self.N):
            self.assertEqual(self.ddt.point_index(i), find_point(i))

    def test_detach_node(self):
        self.assertRaises(AssertionError, lambda: self.ddt.root.detach_node())
        self.assertRaises(AssertionError, lambda: self.ddt.root.left.detach_node())
        self.assertRaises(AssertionError, lambda: self.ddt.root.right.detach_node())
        for i in xrange(self.N):
            node = self.ddt.point_index(0)
            if not node.parent is self.ddt.root:
                break


        old_parent = node.parent
        sibling = node.parent.other_child(node)
        old_grandparent = old_parent.parent

        parent = node.detach_node()

        self.assertEqual(old_parent, parent)
        self.assertEqual(node.parent, parent)
        self.assertEqual(node.parent, old_parent)
        self.assertEqual(node.parent, old_parent)
        self.assertEqual(node.parent.parent, None)
        self.assertEqual(len(node.parent.children), 1)
        self.assertTrue(sibling in old_grandparent.children)

    def test_sample_assignment(self):
        for _ in xrange(1000):
            assignment, log_prob = self.ddt.sample_assignment()
            self.assertAlmostEqual(log_prob, self.ddt.log_prob_assignment(assignment), places=5)

    def test_gaussian(self):
        mu = np.zeros(self.D)
        x = np.ones(self.D)

        for t in np.arange(0, 1, 0.01):
            diff = 1 - t
            self.assertAlmostEqual(self.lm.calculate_transition(x, mu, 1, t),
                             stats.multivariate_normal(mean=mu, cov=np.eye(self.D) * diff).logpdf(x))