예제 #1
0
def main(num_samples, burn, lag, w):
    
    alpha = 1.0
    N = 25
    Z, Nk, K = utils.crp_gen(N, alpha)

    # crp with gamma prior
    log_prior_fun = lambda a: -a
    log_pdf_lambda = lambda a : utils.unorm_lcrp_post(a, N, K, log_prior_fun)
    proposal_fun = lambda : gamrnd(1.0,1.0)
    D = (0, float('Inf'))

    samples = su.slice_sample(proposal_fun, log_pdf_lambda, D, num_samples=num_samples, burn=burn, lag=lag, w=w)

    minval = min(samples)
    maxval = max(samples)
    xvals = numpy.linspace(minval, maxval, 100)

    yvals = numpy.array([ math.exp(log_pdf_lambda(x)) for x in xvals])
    yvals /= trapz(xvals, yvals)



    ax=pylab.subplot(2,1,1)
    pylab.hist(X,50,normed=True)

    ax_1=pylab.subplot(2,1,2)
    pylab.hist(samples,100,normed=True)
    pylab.plot(xvals,-yvals,c='red',lw=3, alpha=.8)
    pylab.xlim(ax.get_xlim())
    pylab.ylim(ax.get_ylim())

    pylab.show()
예제 #2
0
def gen_partition_crp(n_rows, n_cols, n_views, alphas):
    Zv = [v for v in range(n_views)]
    for _ in range(n_cols-n_views):
        Zv.append(random.randrange(n_views))
    random.shuffle(Zv)

    Zc = []
    for v in range(n_views):
        Zc.append(utils.crp_gen(n_rows, alphas[v])[0])

    return Zv, Zc
예제 #3
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        
예제 #4
0
파일: cc_view.py 프로젝트: CoDaS-Lab/BaxCat
    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
예제 #5
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