Beispiel #1
0
 def construct_hyper_grids(X, n_grid=30):
     grids = dict()
     ssqdev = numpy.var(X) * float(len(X))
     grids['a'] = utils.log_linspace(.1, float(len(X)), n_grid)
     grids['b'] = utils.log_linspace(.1, float(len(X)), n_grid)
     grids['t'] = utils.log_linspace(.1, float(len(X)), n_grid)
     grids['m'] = utils.log_linspace(.0001, max(X), n_grid)
     return grids
Beispiel #2
0
 def construct_hyper_grids(X, n_grid=30):
     N = float(len(X))
     Sx = numpy.sum(X)
     Mx = numpy.sum(1-X)
     grids = {
         'λ' : utils.log_linspace(1/N, N, n_grid),
         'α' : utils.log_linspace(Sx/N, Sx, n_grid),
         'β' : utils.log_linspace(Mx/N, Mx, n_grid),
     }
     return grids
Beispiel #3
0
    def construct_hyper_grids(X, n_grid=30):
        grids = dict()
        ssqdev = numpy.var(X) * float(len(X))
        # assert ssqdev > 0

        grids['m'] = numpy.linspace(min(X), max(X), n_grid)
        grids['r'] = utils.log_linspace(.1, float(len(X)) / 2.0, n_grid)
        grids['s'] = utils.log_linspace(ssqdev / 100.0, ssqdev, n_grid)
        grids['nu'] = utils.log_linspace(.1, float(len(X)) / 2.0, n_grid)

        return grids
Beispiel #4
0
    def construct_hyper_grids(X, n_grid=30):
        grids = dict()
        ssqdev = numpy.var(X) * float(len(X))
        # assert ssqdev > 0

        grids["m"] = numpy.linspace(min(X), max(X), n_grid)
        grids["r"] = utils.log_linspace(0.1, float(len(X)) / 2.0, n_grid)
        grids["s"] = utils.log_linspace(ssqdev / 100.0, ssqdev, n_grid)
        grids["nu"] = utils.log_linspace(0.1, float(len(X)) / 2.0, n_grid)

        return grids
Beispiel #5
0
 def construct_hyper_grids(X,n_grid=30):
     grids = dict()
     ssqdev = numpy.var(X)*float(len(X));
             
     grids['r'] = utils.log_linspace(1.0/float(len(X)),float(len(X)), n_grid)
     grids['s'] = utils.log_linspace(ssqdev/100.0, ssqdev, n_grid)
     grids['nu'] = utils.log_linspace(1.0,float(len(X)), n_grid) # df >= 1
     grids['m'] = numpy.linspace(min(X),max(X), n_grid)
     
     grids['m'] = numpy.linspace(-2.0,2.0, n_grid) # for gewek
     grids['s'] = utils.log_linspace(.01, 3.0, n_grid) # for geweke
     
     return grids
Beispiel #6
0
    def construct_hyper_grids(X, n_grid=30):
        ssx = numpy.sum(numpy.sin(X))
        scx = numpy.sum(numpy.cos(X))
        N = float(len(X))
        k = estimate_kappa(N, ssx, scx)

        grid_interval = TWOPI / n_grid
        grids = dict()
        grids['a'] = utils.log_linspace(1.0 / N, TWOPI, n_grid)
        grids['b'] = numpy.linspace(grid_interval, TWOPI, n_grid)
        grids['shape'] = utils.log_linspace(1.0 / N, N, n_grid)
        grids['scale'] = numpy.linspace(k, k * N * 2, n_grid)

        return grids
Beispiel #7
0
    def construct_hyper_grids(X, n_grid=30):
        grids = dict()
        ssqdev = numpy.var(X) * float(len(X))

        grids['r'] = utils.log_linspace(1.0 / float(len(X)), float(len(X)),
                                        n_grid)
        grids['s'] = utils.log_linspace(ssqdev / 100.0, ssqdev, n_grid)
        grids['nu'] = utils.log_linspace(1.0, float(len(X)), n_grid)  # df >= 1
        grids['m'] = numpy.linspace(min(X), max(X), n_grid)

        grids['m'] = numpy.linspace(-2.0, 2.0, n_grid)  # for gewek
        grids['s'] = utils.log_linspace(.01, 3.0, n_grid)  # for geweke

        return grids
Beispiel #8
0
    def construct_hyper_grids(X,n_grid=30):
        ssx = numpy.sum(numpy.sin(X))
        scx = numpy.sum(numpy.cos(X))
        N = float(len(X))
        k = estimate_kappa(N, ssx, scx)

        grid_interval = TWOPI/n_grid
        grids = dict()
        grids['a'] = utils.log_linspace(1.0/N, TWOPI, n_grid)
        grids['b'] = numpy.linspace(grid_interval, TWOPI, n_grid)
        grids['shape'] = utils.log_linspace(1.0/N, N, n_grid)
        grids['scale'] = numpy.linspace(k,k*N*2, n_grid)
    
        return grids
Beispiel #9
0
 def construct_hyper_grids(X,n_grid=30):
     grids = dict()
     # only use integers for a so we can nicely draw from a negative binomial
     # in predictive_draw
     grids['a'] = numpy.unique(numpy.round(numpy.linspace(1,len(X),n_grid)))
     grids['b'] = utils.log_linspace(.1,float(len(X)), n_grid)
     return grids
Beispiel #10
0
 def construct_hyper_grids(X, n_grid=30):
     grids = dict()
     # only use integers for a so we can nicely draw from a negative binomial
     # in predictive_draw
     grids['a'] = numpy.unique(
         numpy.round(numpy.linspace(1, len(X), n_grid)))
     grids['b'] = utils.log_linspace(.1, float(len(X)), n_grid)
     return grids
Beispiel #11
0
    def __init__(self, dims, alpha=None, Z=None, n_grid=30):
        """
        Constructor
        input arguments:
        -- dims: a list of cc_dim objects
        optional arguments:
        -- alpha: crp concentration parameter. If none, is selected from grid.
        -- Z: starting partiton of rows to categories. If nonde, is intialized 
        from CRP(alpha)
        -- n_grid: number of grid points in the hyperparameter grids
        """

        N = dims[0].N
        self.N = N

        # generate alpha
        self.alpha_grid = utils.log_linspace(1.0/self.N, self.N, n_grid)
        
        if alpha is None:
            alpha = random.choice(self.alpha_grid)
        else:
            assert alpha > 0.0

        if Z is None:
            Z, Nk, K = utils.crp_gen(N, alpha)
        else:
            assert len(Z) == dims[0].X.shape[0]
            Nk = utils.bincount(Z)
            K = len(Nk)

        assert sum(Nk) == N
        assert K == len(Nk)

        self.dims = dict()
        for dim in dims:
            dim.reassign(Z)
            self.dims[dim.index] = dim

        self.alpha = alpha
        self.Z = numpy.array(Z)
        self.K = K
        self.Nk = Nk        
Beispiel #12
0
    def __init__(self, dims, alpha=None, Z=None, n_grid=30):
        """
        Constructor
        input arguments:
        -- dims: a list of cc_dim objects
        optional arguments:
        -- alpha: crp concentration parameter. If none, is selected from grid.
        -- Z: starting partiton of rows to categories. If nonde, is intialized 
        from CRP(alpha)
        -- n_grid: number of grid points in the hyperparameter grids
        """

        N = dims[0].N
        self.N = N

        # generate alpha
        self.alpha_grid = utils.log_linspace(1.0 / self.N, self.N, n_grid)

        if alpha is None:
            alpha = random.choice(self.alpha_grid)
        else:
            assert alpha > 0.0

        if Z is None:
            Z, Nk, K = utils.crp_gen(N, alpha)
        else:
            assert len(Z) == dims[0].X.shape[0]
            Nk = utils.bincount(Z)
            K = len(Nk)

        assert sum(Nk) == N
        assert K == len(Nk)

        self.dims = dict()
        for dim in dims:
            dim.reassign(Z)
            self.dims[dim.index] = dim

        self.alpha = alpha
        self.Z = numpy.array(Z)
        self.K = K
        self.Nk = Nk
Beispiel #13
0
    def __init__(self, X, cctypes, distargs, n_grid=30, Zv=None, Zrcv=None, hypers=None, seed=None):
        """
        cc_state constructor

        input arguments:
        -- X: a list of numpy data columns.
        -- cctypes: a list of strings where each entry is the data type for 
        each column.
        -- distargs: a list of distargs appropriate for each type in cctype.
        For details on distrags see the documentation for each data type.

        optional arguments:
        -- n_grid: number of bins for hyperparameter grids. Default = 30.
        -- Zv: The assignment of columns to views. If not specified, a 
        partition is generated randomly
        -- Zrcv: The assignment of rows to clusters for each view
        -- ct_kernel: which column transition kenerl to use. Default = 0 (Gibbs)
        -- seed: seed the random number generator. Default = system time.

        example:
        >>> import numpy
        >>> n_rows = 100
        >>> X = [numpy.random.normal(n_rows), numpy.random.normal(n_rows)]
        >>> State = cc_state(X, ['normal', 'normal'], [None, None])
        """

        if seed is not None:
            random.seed(seed)
            numpy.random.seed(seed)

        self.n_rows = len(X[0])
        self.n_cols = len(X)
        self.n_grid = n_grid

        # construct the dims
        self.dims = []
        for col in range(self.n_cols):
            Y = X[col] 
            cctype = cctypes[col]
            if _is_uncollapsed[cctype]:
                dim = cc_dim_uc(Y, _cctype_class[cctype], col, n_grid=n_grid, distargs=distargs[col])
            else:
                dim = cc_dim(Y, _cctype_class[cctype], col, n_grid=n_grid, distargs=distargs[col])
            self.dims.append(dim)

        # set the hyperparameters in the dims
        if hypers is not None:
            for d in range(self.n_cols):
                self.dims[d].set_hypers(hypers[d])

        # initialize CRP alpha  
        self.alpha_grid = utils.log_linspace(1.0/self.n_cols, self.n_cols, self.n_grid)
        self.alpha = random.choice(self.alpha_grid)

        assert len(self.dims) == self.n_cols

        if Zrcv is not None:
            assert Zv is not None
            assert len(Zv) == self.n_cols
            assert len(Zrcv) == max(Zv)+1
            assert len(Zrcv[0]) == self.n_rows

        # construct the view partition
        if Zv is None:
            Zv, Nv, V = utils.crp_gen(self.n_cols, self.alpha)
        else:
            Nv = utils.bincount(Zv)
            V = len(Nv)

        # construct views
        self.views = []
        for view in range(V):
            indices = [i for i in range(self.n_cols) if Zv[i] == view]
            dims_view = []
            for index in indices:
                dims_view.append(self.dims[index])

            if Zrcv is None:
                self.views.append(cc_view(dims_view, n_grid=n_grid))
            else:
                self.views.append(cc_view(dims_view, Z=numpy.array(Zrcv[view]), n_grid=n_grid))

        self.Zv = numpy.array(Zv)
        self.Nv = Nv
        self.V = V
Beispiel #14
0
 def construct_hyper_grids(X, n_grid=30):
     grids = dict()
     grids['alpha'] = utils.log_linspace(1.0 / float(len(X)), float(len(X)),
                                         n_grid)
     return grids
 def construct_hyper_grids(X,n_grid=30):
     grids = dict()
     grids['alpha'] = utils.log_linspace(1.0/float(len(X)), float(len(X)), n_grid)
     return grids