コード例 #1
0
    def test_covariate_shift(self):
        n_sample = 100
        # Biased training
        var_bias = .5**2
        mean_bias = .7
        x_train = SP.random.randn(n_sample) * SP.sqrt(var_bias) + mean_bias
        y_train = self.complete_sample(x_train)

        # Unbiased test set
        var = .3**2
        mean = 0

        x_test = SP.random.randn(n_sample) * SP.sqrt(var) + mean
        x_complete = SP.hstack((x_train, x_test))

        kernel = utils.getQuadraticKernel(x_complete, d=1) +\
            10 * SP.dot(x_complete.reshape(-1, 1), x_complete.reshape(1, -1))
        kernel = utils.scale_K(kernel)
        kernel_train = kernel[SP.ix_(SP.arange(x_train.size),
                                     SP.arange(x_train.size))]
        kernel_test = kernel[SP.ix_(SP.arange(x_train.size, x_complete.size),
                                    SP.arange(x_train.size))]

        mf = MF(n_estimators=100,
                kernel=kernel_train,
                min_depth=0,
                subsampling=False)
        mf.fit(x_train.reshape(-1, 1), y_train.reshape(-1, 1))
        response_gp = mf.predict(x_test.reshape(-1, 1), kernel_test, depth=0)
        self.assertTrue(((response_gp - self.polynom(x_test))**2).sum() < 2.4)
コード例 #2
0
ファイル: test_lmm_forest.py プロジェクト: PMBio/limix
    def test_covariate_shift(self):
        n_sample = 100
        # Biased training
        var_bias = .5**2
        mean_bias = .7
        x_train = SP.random.randn(n_sample)*SP.sqrt(var_bias) + mean_bias
        y_train = self.complete_sample(x_train)

        # Unbiased test set
        var = .3**2
        mean = 0

        x_test = SP.random.randn(n_sample)*SP.sqrt(var) + mean
        x_complete = SP.hstack((x_train, x_test))

        kernel = utils.getQuadraticKernel(x_complete, d=1) +\
            10 * SP.dot(x_complete.reshape(-1, 1), x_complete.reshape(1, -1))
        kernel = utils.scale_K(kernel)
        kernel_train = kernel[SP.ix_(SP.arange(x_train.size),
                                     SP.arange(x_train.size))]
        kernel_test = kernel[SP.ix_(SP.arange(x_train.size, x_complete.size),
                             SP.arange(x_train.size))]

        mf = MF(n_estimators=100, kernel=kernel_train, min_depth=0,
                subsampling=False)
        mf.fit(x_train.reshape(-1, 1), y_train.reshape(-1, 1))
        response_gp = mf.predict(x_test.reshape(-1, 1), kernel_test, depth=0)
        self.assertTrue(((response_gp - self.polynom(x_test))**2).sum() < 2.4)
コード例 #3
0
 def test_toy_data_rand(self):
     y_conf = self.data['y_conf'].value
     kernel = self.data['kernel'].value
     X = self.data['X'].value
     # This is a non-random cross validation
     (training, test) = utils.crossValidationScheme(2, y_conf.size)
     lm_forest = MF(kernel=kernel[SP.ix_(training, training)],
                    sampsize=.5,
                    verbose=0,
                    n_estimators=100)
     lm_forest.fit(X[training], y_conf[training])
     response_tot = lm_forest.predict(X[test],
                                      kernel[SP.ix_(test, training)])
     random_forest = MF(kernel='iid')
     random_forest.fit(X[training], y_conf[training])
     response_iid = random_forest.predict(X[test])
     response_fixed = lm_forest.predict(X[test])
     feature_scores_lmf = lm_forest.log_importance
     feature_scores_rf = random_forest.log_importance
     # All consistency checks
     err = (feature_scores_lmf -
            self.data['feature_scores_lmf'].value).sum()
     self.assertTrue(SP.absolute(err) < 10)
     err = (feature_scores_rf - self.data['feature_scores_rf'].value).sum()
     self.assertTrue(SP.absolute(err) < 10)
     err = SP.absolute(self.data['response_tot'] - response_tot).sum()
     self.assertTrue(SP.absolute(err) < 2)
     err = SP.absolute(self.data['response_fixed'] - response_fixed).sum()
     self.assertTrue(SP.absolute(err) < 4)
     err = SP.absolute(self.data['response_iid'] - response_iid).sum()
     self.assertTrue(SP.absolute(err) < 8)
コード例 #4
0
ファイル: test_lmm_forest.py プロジェクト: PMBio/limix
 def test_toy_data_rand(self):
     y_conf = self.data['y_conf'].value
     kernel = self.data['kernel'].value
     X = self.data['X'].value
     # This is a non-random cross validation
     (training, test) = utils.crossValidationScheme(2, y_conf.size)
     lm_forest = MF(kernel=kernel[SP.ix_(training, training)],
                    sampsize=.5, verbose=0, n_estimators=100)
     lm_forest.fit(X[training], y_conf[training])
     response_tot = lm_forest.predict(X[test],
                                      kernel[SP.ix_(test, training)])
     random_forest = MF(kernel='iid')
     random_forest.fit(X[training], y_conf[training])
     response_iid = random_forest.predict(X[test])
     response_fixed = lm_forest.predict(X[test])
     feature_scores_lmf = lm_forest.log_importance
     feature_scores_rf = random_forest.log_importance
     # All consistency checks
     err = (feature_scores_lmf-self.data['feature_scores_lmf'].value).sum()
     self.assertTrue(SP.absolute(err) < 10)
     err = (feature_scores_rf-self.data['feature_scores_rf'].value).sum()
     self.assertTrue(SP.absolute(err) < 10)
     err = SP.absolute(self.data['response_tot'] - response_tot).sum()
     self.assertTrue(SP.absolute(err) < 2)
     err = SP.absolute(self.data['response_fixed'] - response_fixed).sum()
     self.assertTrue(SP.absolute(err) < 4)
     err = SP.absolute(self.data['response_iid'] - response_iid).sum()
     self.assertTrue(SP.absolute(err) < 8)
コード例 #5
0
ファイル: loopy_bp.py プロジェクト: lingjie/tree-hmm
def bp_update_params_new(args):
    lmds, pis = args.lmds, args.pis
    vert_parent, vert_children = args.vert_parent, args.vert_children
    #print  pis[0].shape
    I, T, L = args.X.shape
    K = args.gamma.shape[0]
    theta, alpha, beta, gamma, emit_probs, X = (args.theta, args.alpha, args.beta, args.gamma, args.emit_probs,
                                        args.X)
    evidence = args.evidence #evid_allchild(lmds, vert_children)
    emit_probs_mat = sp.exp(args.log_obs_mat)
    #emit_probs_mat /= emit_probs_mat.max(axis=2).reshape(I, T, 1)
    gamma_p = copy.copy(gamma)
    alpha_p = copy.copy(alpha)
    beta_p =  copy.copy(beta)
    theta_p = copy.copy(theta)
    emit_probs_p = copy.copy(emit_probs)
    
    theta[:] = args.pseudocount
    alpha[:] = args.pseudocount
    beta[:] = args.pseudocount
    gamma[:] = args.pseudocount
    emit_probs[:] = args.pseudocount

    #evidence = evid_allchild(lmds, args.vert_children)
    ##support = casual_support(pis)
    
    emit_sum = sp.zeros(K)
    for i in xrange(I):
        vp = vert_parent[i]
        for t in xrange(T):
            if i==0 and t==0:
                gamma += emit_probs_mat[i, t, :]*evidence[i,t,:]
                Q = emit_probs_mat[i, t, :]*evidence[i,t,:]
            else:
                if i == 0:
                    tmp1, tmp2 = sp.ix_(pis[i][-1,t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    tmp = alpha_p * (tmp1*tmp2)
                    #tmp /= tmp.sum()
                    Q = tmp.sum(axis=0)
                    alpha += tmp/tmp.sum()
                elif t == 0:
                    tmp1, tmp2 = sp.ix_(pis[vp][i-1,t,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    tmp = beta_p *(tmp1*tmp2)
                    Q = tmp.sum(axis=0)
                    beta += tmp/tmp.sum()
                else:
                    tmp1, tmp2, tmp3 = sp.ix_(pis[vp][i-1,t,:], pis[i][-1, t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    tmp = theta_p *(tmp1*tmp2 *tmp3)
                    Q = (tmp.sum(axis=0)).sum(axis=0)
                    theta += tmp/tmp.sum()
                
            Q /= Q.sum()
            for l in xrange(L):
                if X[i,t,l]:
                    emit_probs[:, l] += Q
            emit_sum += Q
    normalize_trans(theta, alpha, beta, gamma)
    emit_probs[:] = sp.dot(sp.diag(1./emit_sum), emit_probs)
    args.emit_sum = emit_sum
    make_log_obs_matrix(args)
コード例 #6
0
ファイル: ps1.py プロジェクト: jrnold/psc585
    def _p(self, s):
        """Transition probability function

        Parameters
        -----------
        s : int
            Integer representing the province

        Returns
        -----------
        sprime : list
           Each element is a tuple with the transition state
           and the probability of transitioning to that state.
           
        """

        # I enumerate states such that
        # 0 = [0, ...., 0]
        # 1 = [1, 0, 0, 0, ... 0 ]
        # 2 = [0, 1, 0, 0, ... 0 ]
        # 2^k = [0, 0, ..., 0, 1]
        # 2^(k+1) - 1 = [1, 1, ..., 1, 1]
        sbinary = int2binary(s, width=self.k)[::-1]
        # indices of Revolting provinces
        R = sbinary.nonzero()[0].tolist()

        # indices of Non-revolting provinces
        C = sp.logical_not(sbinary).nonzero()[0]
        P = [1]
        newstate = [s]
        for i, si in enumerate(sbinary):
            ## Calculate
            Ci = list(set(C) - set([i]))
            if Ci:
                dminc = self.D[sp.ix_([i], Ci)].min()
            else:
                dminc = self.dmax

            Ri = list(set(R) - set([i]))
            if Ri: 
                dminr = sp.c_[self.D[sp.ix_([i], Ri)]].min()
            else:
                dminr = self.dmax
            # if i in revolt
            if si:
                Si = s - 2**i
                Pi = dminr / dminc
            # if i not in revolt
            else:
                Si = s + 2**i
                Pi = dminc / dminr
            P.append(Pi)
            newstate.append(Si)
        newstate = sp.array(newstate)
        P = sp.array(P)
        P /= P.sum()
        return (P.tolist(), newstate.tolist())
コード例 #7
0
	def set_invcovariance(self,xmask,invertcovariance=[],scalecovariance=None):

		del self.params['xmask']
		xmaskall = scipy.concatenate(xmask)

		if self.scale_data_covariance is not None:
			self.logger.info('Scaling covariance by {:.4f}.'.format(scalecovariance))
			self.covariance *= scalecovariance
		self.stddev = scipy.diag(self.covariance[scipy.ix_(xmaskall,xmaskall)])

		error_message = 'The covariance matrix is ill-conditionned. You may want to try the option sliced.'
		if 'sliced' in self.invert_covariance:
			self.logger.info('Slicing covariance.')
			self.covariance = self.covariance[scipy.ix_(xmaskall,xmaskall)]
			error_message = 'The covariance matrix is ill-conditionned. You may want to try the option block.'

		self.covariance = self.covariance.astype(scipy.float64) #make sure we have enough precision

		if 'diagonal' in self.invert_covariance:
			self.logger.info('Inverting diagonal matrix/blocks.')
			def inv(A):
				return scipy.diag(1./scipy.diag(A))
		elif 'cholesky' in self.invert_covariance:
			self.logger.info('Inverting using Choleskys decomposition.')
			def inv(A):
				c = linalg.inv(linalg.cholesky(A)) #using Cholesky's decomposition
				return scipy.dot(c.T,c)
		else:
			self.logger.info('Inverting using linalg inversion.')
			def inv(A):
				return linalg.inv(A)

		if 'block' in self.invert_covariance:
			self.logger.info('Inverting by block.')
			if 'sliced' in self.invert_covariance: blocksize = scipy.cumsum([0] + map(scipy.sum,xmask))
			else: blocksize = scipy.cumsum([0] + map(len,xmask))
			blocks = [[self.covariance[i1:i2,j1:j2] for j1,j2 in zip(blocksize[:-1],blocksize[1:])] for i1,i2 in zip(blocksize[:-1],blocksize[1:])]
			self.invcovariance = utils.blockinv(blocks,inv=inv)
			error_message = 'The covariance matrix is ill-conditionned. You have to provide a better estimate.'
		else:
			self.invcovariance = inv(self.covariance)

		diff = self.covariance.dot(self.invcovariance)-scipy.eye(self.covariance.shape[0])
		diff = scipy.absolute(diff).max()
		self.logger.info('Inversion computed to absolute precision {:.4g}.'.format(diff))
		if diff > 1.: raise LinAlgError(error_message)

		if 'sliced' not in self.invert_covariance:
			self.logger.info('Slicing covariance.')
			self.invcovariance = self.invcovariance[scipy.ix_(xmaskall,xmaskall)]
コード例 #8
0
    def test_d2k1_2by2(self):
        #1-form innerproduct for 2x2 unit square grid
        bitmap = ones((2, 2), dtype='bool')

        K_local = array([[1.0 / 3.0, 0, 1.0 / 6.0, 0],
                         [0, 1.0 / 3.0, 0, 1.0 / 6.0],
                         [1.0 / 6.0, 0, 1.0 / 3.0, 0],
                         [0, 1.0 / 6.0, 0, 1.0 / 3.0]])

        K_correct = zeros((12, 12))

        K_correct[ix_(
            [0, 1, 2, 6],
            [0, 1, 2, 6
             ])] = K_correct[ix_([0, 1, 2, 6], [0, 1, 2, 6])] + K_local
        K_correct[ix_(
            [5, 6, 7, 10],
            [5, 6, 7, 10
             ])] = K_correct[ix_([5, 6, 7, 10], [5, 6, 7, 10])] + K_local
        K_correct[ix_(
            [2, 3, 4, 8],
            [2, 3, 4, 8
             ])] = K_correct[ix_([2, 3, 4, 8], [2, 3, 4, 8])] + K_local
        K_correct[ix_(
            [7, 8, 9, 11],
            [7, 8, 9, 11
             ])] = K_correct[ix_([7, 8, 9, 11], [7, 8, 9, 11])] + K_local

        assert_almost_equal(K_correct, self.get_K(bitmap, 1))
コード例 #9
0
def parse_ldblk(ldblk_dir, sst_dict, chrom):
    print('... parse reference LD on chromosome %d ...' % chrom)

    if '1kg' in os.path.basename(ldblk_dir):
        chr_name = ldblk_dir + '/ldblk_1kg_chr' + str(chrom) + '.hdf5'
    elif 'ukbb' in os.path.basename(ldblk_dir):
        chr_name = ldblk_dir + '/ldblk_ukbb_chr' + str(chrom) + '.hdf5'

    hdf_chr = h5py.File(chr_name, 'r')
    n_blk = len(hdf_chr)
    ld_blk = [sp.array(hdf_chr['blk_'+str(blk)]['ldblk']) for blk in range(1,n_blk+1)]

    snp_blk = []
    for blk in range(1,n_blk+1):
        snp_blk.append([bb.decode("UTF-8") for bb in list(hdf_chr['blk_'+str(blk)]['snplist'])])

    blk_size = []
    mm = 0
    for blk in range(n_blk):
        idx = [ii for (ii, snp) in enumerate(snp_blk[blk]) if snp in sst_dict['SNP']]
        blk_size.append(len(idx))
        if idx != []:
            idx_blk = range(mm,mm+len(idx))
            flip = [sst_dict['FLP'][jj] for jj in idx_blk]
            ld_blk[blk] = ld_blk[blk][sp.ix_(idx,idx)]*sp.outer(flip,flip)

            _, s, v = linalg.svd(ld_blk[blk])
            h = sp.dot(v.T, sp.dot(sp.diag(s), v))
            ld_blk[blk] = (ld_blk[blk]+h)/2            

            mm += len(idx)
        else:
            ld_blk[blk] = sp.array([])

    return ld_blk, blk_size
コード例 #10
0
ファイル: parse_genet.py プロジェクト: sparkler0323/PRScs
def parse_ldblk(ldblk_dir, sst_dict, chrom):
    print('... parse reference LD on chromosome %d ...' % chrom)

    chr_name = ldblk_dir + '/ldblk_1kg_chr' + str(chrom) + '.hdf5'
    hdf_chr = h5py.File(chr_name, 'r')
    n_blk = len(hdf_chr)
    ld_blk = [
        sp.array(hdf_chr['blk_' + str(blk)]['ldblk'])
        for blk in range(1, n_blk + 1)
    ]
    snp_blk = [
        list(hdf_chr['blk_' + str(blk)]['snplist'])
        for blk in range(1, n_blk + 1)
    ]

    snp_sst = set(sst_dict['SNP'])
    blk_size = []
    for blk in range(n_blk):
        idx = [ii for (ii, snp) in enumerate(snp_blk[blk]) if snp in snp_sst]
        blk_size.append(len(idx))
        if idx != []:
            ld_blk[blk] = ld_blk[blk][sp.ix_(idx, idx)]
        else:
            ld_blk[blk] = sp.array([])

    return ld_blk, blk_size
コード例 #11
0
ファイル: DataSource.py プロジェクト: Sciumo/ProximityForest
 def test_play_vid(self, tnsr, window="KTH Action", size=None, delay=35):
     dims = tnsr.shape
     print dims
     for v in range(dims[0]):
         fMat = tnsr[sp.ix_([v],range(dims[1]), range(dims[2]))].copy()
         img = pv.Image(sp.mat(fMat))
         img.show(window=window, size=size, delay=delay)
コード例 #12
0
 def __transformation(self, coords):
     Gamma = np.zeros((2*self.rank, 2*self.rank))
     if self.rank == 2:
         dx_dy = coords[1, :] - coords[0, :] # [x2-x1 y2-y1]
         i_bar = dx_dy/norm(dx_dy) 
         j_bar = i_bar.dot([[0, 1], [-1, 0]]) # Rotate i_bar by 90 degrees
         Gamma[0, 0:2] = Gamma[2, 2:4] = i_bar
         Gamma[1, 0:2] = Gamma[3, 2:4] = j_bar
         return Gamma, norm(dx_dy)
     elif self.rank == 3:
         i_bar = coords[1, :] - coords[0, :]  # [dx, dy, dz]
         j_bar = np.array([0, 1, 0])  # Assume j-bar points upwards
         k_bar = np.cross(i_bar, j_bar)
         Gamma[np.ix_([0,1,2],[0,1,2])] = gram_schmidt(i_bar, j_bar, k_bar)
         Gamma[np.ix_([3,4,5],[3,4,5])] = Gamma[np.ix_([0, 1, 2], [0, 1, 2])]
         return Gamma, norm(i_bar)
コード例 #13
0
    def plotDeformed(self, disp, scale, rank=2):
        """ Input:  disp = displacement vector
                    scale = 
                    rank = number of dimensions """

        # Craft deformed coordinates
        deformed = self.getCoords()
        for inod in range(len(self.coords)):
            idofs = self.getDofIndices(inod)
            if idofs:  # idofs is not empty
                x = self.getCoords(inod)
                u = np.array(disp[idofs]) * scale
                deformed[inod, :] = u + x

        # Create figure
        if rank == 1 or rank == 2:
            fig = plt.figure(figsize=(6, 6))
            ax = fig.add_subplot(111)
        elif rank == 3:
            fig = plt.figure(figsize=(6, 6))
            ax = fig.add_subplot(111, projection='3d')

        # Plot deformed mesh
        for connect in self.connectivity:
            iele = connect + [connect[0]]
            coords = deformed[np.ix_(iele), :][0]
            ax.plot(coords[:, 0], coords[:, 1], linewidth=0.5, color='k')

        plt.show()
コード例 #14
0
ファイル: DataSource.py プロジェクト: Sciumo/ProximityForest
 def play_tensor(self, tnsr, window="UCF Action", size=(96,96), delay=35):
     dims = tnsr.shape
     print dims
     for v in range(dims[0]):
         fMat = tnsr[sp.ix_([v],range(dims[1]), range(dims[2]))].copy()
         img = pv.Image(sp.mat(fMat))
         img.show(window=window, size=size, delay=delay)
コード例 #15
0
def parse_ldblk(ldblk_dir, sst_dict, pop, chrom, ref):
    print('... parse %s reference LD on chromosome %d ...' % (pop.upper(), chrom))

    if ref == '1kg':
        chr_name = ldblk_dir + '/ldblk_1kg_' + pop.lower() + '/ldblk_1kg_chr' + str(chrom) + '.hdf5'
    elif ref == 'ukbb':
        chr_name = ldblk_dir + '/ldblk_ukbb_' + pop.lower() + '/ldblk_ukbb_chr' + str(chrom) + '.hdf5'

    hdf_chr = h5py.File(chr_name, 'r')
    n_blk = len(hdf_chr)
    ld_blk = [sp.array(hdf_chr['blk_'+str(blk)]['ldblk']) for blk in range(1,n_blk+1)]

    snp_blk = []
    for blk in range(1,n_blk+1):
         snp_blk.append([bb.decode("UTF-8") for bb in list(hdf_chr['blk_'+str(blk)]['snplist'])])

    blk_size = []
    mm = 0
    for blk in range(n_blk):
        idx = [ii for (ii,snp) in enumerate(snp_blk[blk]) if snp in sst_dict['SNP']]
        blk_size.append(len(idx))
        if idx != []:
            idx_blk = range(mm,mm+len(idx))
            flip = [sst_dict['FLP'][jj] for jj in idx_blk]
            ld_blk[blk] = ld_blk[blk][sp.ix_(idx,idx)]*sp.outer(flip,flip)
            mm += len(idx)
        else:
            ld_blk[blk] = sp.array([])

    return ld_blk, blk_size
コード例 #16
0
    def test_delta_updating(self):
        n_sample = 100
        # A 20 x 2 random integer matrix
        X = SP.empty((n_sample, 2))
        X[:, 0] = SP.arange(0, 1, 1.0 / n_sample)
        X[:, 1] = SP.random.rand(n_sample)
        sd_noise = .5
        sd_conf = .5
        noise = SP.random.randn(n_sample, 1) * sd_noise

        # print 'true delta equals', (sd_noise**2)/(sd_conf**2)
        # Here, the observed y is just a linear function of the first column
        # in X and # a little independent gaussian noise
        y_fixed = (X[:, 0:1] > .5) * 1.0
        y_fn = y_fixed + noise

        # Divide into training and test sample using 2/3 of data for training
        training_sample = SP.zeros(n_sample, dtype='bool')
        training_sample[SP.random.permutation(n_sample)
                        [:SP.int_(.66 * n_sample)]] = True
        test_sample = ~training_sample

        kernel = utils.getQuadraticKernel(X[:, 0], d=0.0025) +\
            1e-3*SP.eye(n_sample)
        # The confounded version of y_lin is computed as
        y_conf = sd_conf * SP.random.multivariate_normal(
            SP.zeros(n_sample), kernel, 1).reshape(-1, 1)
        y_tot = y_fn + y_conf
        # Selects rows and columns
        kernel_train = kernel[SP.ix_(training_sample, training_sample)]
        kernel_test = kernel[SP.ix_(test_sample, training_sample)]
        lm_forest = MF(kernel=kernel_train,
                       update_delta=False,
                       max_depth=1,
                       verbose=0)
        # Returns prediction for random effect
        lm_forest.fit(X[training_sample], y_tot[training_sample])
        response_lmf = lm_forest.predict(X[test_sample], k=kernel_test)

        # print 'fitting forest (delta-update)'
        # earn random forest, not accounting for the confounding
        random_forest = MF(kernel=kernel_train,
                           update_delta=True,
                           max_depth=5,
                           verbose=0)
        random_forest.fit(X[training_sample], y_tot[training_sample])
        response_rf = random_forest.predict(X[test_sample], k=kernel_test)
コード例 #17
0
def uniqueVectors(v, tol=1.0e-12):
    """
    Sort vectors and discard duplicates.

      USAGE:

          uvec = uniqueVectors(vec, tol=1.0e-12)

    v   --
    tol -- (optional) comparison tolerance

    D. E. Boyce 2010-03-18
    """

    vdims = v.shape

    iv = zeros(vdims)
    iv2 = zeros(vdims, dtype="bool")
    bsum = zeros((vdims[1], ), dtype="bool")
    for row in range(vdims[0]):
        tmpord = num.argsort(v[row, :]).tolist()
        tmpsrt = v[ix_([row], tmpord)].squeeze()
        tmpcmp = abs(tmpsrt[1:] - tmpsrt[0:-1])
        indep = num.hstack([True, tmpcmp > tol])  # independent values
        rowint = indep.cumsum()
        iv[ix_([row], tmpord)] = rowint
        pass

    #
    #  Dictionary sort from bottom up
    #
    iNum = num.lexsort(iv)
    ivSrt = iv[:, iNum]
    vSrt = v[:, iNum]

    ivInd = zeros(vdims[1], dtype='int')
    nUniq = 1
    ivInd[0] = 0
    for col in range(1, vdims[1]):
        if any(ivSrt[:, col] != ivSrt[:, col - 1]):
            ivInd[nUniq] = col
            nUniq += 1
            pass
        pass

    return vSrt[:, ivInd[0:nUniq]]
コード例 #18
0
ファイル: matrixutil.py プロジェクト: donald-e-boyce/hexrd
def uniqueVectors(v, tol=1.0e-12):
    """
    Sort vectors and discard duplicates.

      USAGE:

          uvec = uniqueVectors(vec, tol=1.0e-12)

    v   --
    tol -- (optional) comparison tolerance

    D. E. Boyce 2010-03-18
    """

    vdims = v.shape

    iv    = zeros(vdims)
    iv2   = zeros(vdims, dtype="bool")
    bsum  = zeros((vdims[1], ), dtype="bool")
    for row in range(vdims[0]):
        tmpord = num.argsort(v[row, :]).tolist()
        tmpsrt = v[ix_([row], tmpord)].squeeze()
        tmpcmp = abs(tmpsrt[1:] - tmpsrt[0:-1])
        indep  = num.hstack([True, tmpcmp > tol]) # independent values
        rowint = indep.cumsum()
        iv[ix_([row], tmpord)] = rowint
        pass

    #
    #  Dictionary sort from bottom up
    #
    iNum = num.lexsort(iv)
    ivSrt = iv[:, iNum]
    vSrt = v[:, iNum]

    ivInd = zeros(vdims[1], dtype='int')
    nUniq = 1; ivInd[0] = 0
    for col in range(1, vdims[1]):
        if any(ivSrt[:, col] != ivSrt[:, col -1]):
            ivInd[nUniq] = col
            nUniq += 1
            pass
        pass

    return vSrt[:, ivInd[0:nUniq]]
コード例 #19
0
def transform_voxel(J_vox, use_rotation=True, use_inversion=True):

    # Apply a random element of the cube isometry group (Oh) to the J
    # specifying a unit cube (voxel.) This consists of selecting a
    # random rotation (from 24) followed by inversion (reflection
    # through the origin.) If both are active, this implements
    # reflections as well.

    # Assumes vertex indices translate to (m,n,l) with m fastest, i.e.
    # 0 -> [0,0,0], 1 -> [1,0,0], 2 -> [0,1,0], 3 -> [1,1,0], etc.

    # All 48 possible transformations appear uniformly, though
    # depending on J_vox there may be fewer distinct outcomes of
    # course.

    # This is to pad with zeros and ensure length 3
    format_str = "{0:0" + str(3) + "b}"

    U = sp.zeros((3, 8), dtype=sp.int64)
    for i in range(8):
        u_str = format_str.format(i)
        U[:, i] = [int(u) for u in u_str]
    U = sp.flipud(U)

    # Translate to [-1,1] vertices
    V = 2 * U - 1

    if use_rotation:
        # Random rotation matrix
        R = get_random_rot()
    else:
        # No rotation
        R = sp.eye(len(V))

    V_prime = R.dot(V)
    U_prime = (V_prime + 1) / 2

    # This is to apply the full octahedral transformation group
    # (i.e. including reflections!)  Inversion just "reflects through
    # the origin", but then translate back so corner (-1,-1,-1) is at
    # the origin.
    if use_inversion is True:
        if sp.rand() < 0.5:
            U_prime *= -1
            U_prime += sp.ones((3, 1)).dot(sp.ones((1, 8)))

    # Get the mapped variable indices, i.e. variable i maps to
    # corresponding
    I_prime = sp.array([[1, 2, 4]]).dot(U_prime)

    I_prime = sp.int64(I_prime[0, :])

    # Need to invert the map
    I_P_inv = sp.argsort(I_prime)
    J_vox_prime = J_vox[sp.ix_(I_P_inv, I_P_inv)]

    return J_vox_prime
コード例 #20
0
ファイル: test_lmm_forest.py プロジェクト: PMBio/limix
    def test_delta_updating(self):
        n_sample = 100
        # A 20 x 2 random integer matrix
        X = SP.empty((n_sample, 2))
        X[:, 0] = SP.arange(0, 1, 1.0/n_sample)
        X[:, 1] = SP.random.rand(n_sample)
        sd_noise = .5
        sd_conf = .5
        noise = SP.random.randn(n_sample, 1)*sd_noise

        # print 'true delta equals', (sd_noise**2)/(sd_conf**2)
        # Here, the observed y is just a linear function of the first column
        # in X and # a little independent gaussian noise
        y_fixed = (X[:, 0:1] > .5)*1.0
        y_fn = y_fixed + noise

        # Divide into training and test sample using 2/3 of data for training
        training_sample = SP.zeros(n_sample, dtype='bool')
        training_sample[
            SP.random.permutation(n_sample)[:SP.int_(.66*n_sample)]] = True
        test_sample = ~training_sample

        kernel = utils.getQuadraticKernel(X[:, 0], d=0.0025) +\
            1e-3*SP.eye(n_sample)
        # The confounded version of y_lin is computed as
        y_conf = sd_conf*SP.random.multivariate_normal(SP.zeros(n_sample),
                                                       kernel, 1).reshape(-1, 1)
        y_tot = y_fn + y_conf
        # Selects rows and columns
        kernel_train = kernel[SP.ix_(training_sample, training_sample)]
        kernel_test = kernel[SP.ix_(test_sample, training_sample)]
        lm_forest = MF(kernel=kernel_train, update_delta=False, max_depth=1,
                       verbose=0)
        # Returns prediction for random effect
        lm_forest.fit(X[training_sample], y_tot[training_sample])
        response_lmf = lm_forest.predict(X[test_sample], k=kernel_test)

        # print 'fitting forest (delta-update)'
        # earn random forest, not accounting for the confounding
        random_forest = MF(kernel=kernel_train, update_delta=True, max_depth=5,
                           verbose=0)
        random_forest.fit(X[training_sample], y_tot[training_sample])
        response_rf = random_forest.predict(X[test_sample], k=kernel_test)
コード例 #21
0
ファイル: dksvd.py プロジェクト: iusami/LC-KSVD
  def initialization4LCKSVD(self,training_feats,H_train,dictsize,iterations,sparsitythres,tol=1e-4):
    """
    Initialization for Label consistent KSVD algorithm
    Inputs
          training_feats  -training features
          H_train         -label matrix for training feature 
          dictsize        -number of dictionary items
          iterations      -iterations
          sparsitythres   -sparsity threshold
          tol             -tolerance when performing the approximate KSVD
    Outputs
          Dinit           -initialized dictionary
          Tinit           -initialized linear transform matrix
          Winit           -initialized classifier parameters
          Q               -optimal code matrix for training features 
    """

    numClass = H_train.shape[0] # number of objects
    numPerClass = round(dictsize/float(numClass)) # initial points from each class
    Dinit = sp.empty((training_feats.shape[0],numClass*numPerClass)) # for LC-Ksvd1 and LC-Ksvd2
    dictLabel = sp.zeros((numClass,numPerClass))

    runKsvd = ApproximateKSVD(numPerClass, max_iter=iterations, tol=tol, transform_n_nonzero_coefs=sparsitythres)
    for classid in range(numClass):

      col_ids = sp.logical_and(H_train[classid,:]==1,sp.sum(training_feats**2, axis=1) > 1e-6)

      #  Initilization for LC-KSVD (perform KSVD in each class)
      Dpart = training_feats[:,col_ids][:,sp.random.choice(col_ids.sum(),numPerClass,replace=False)]
      Dpart = Dpart/splin.norm(Dpart,axis=0)
      para_data = training_feats[:,col_ids]
    
      # ksvd process
      runKsvd.fit(training_feats[:,col_ids])
      Dinit[:,numPerClass*classid:numPerClass*(classid+1)] = runKsvd.components_
    
      dictLabel[classid,numPerClass*classid:numPerClass*(classid+1)] =  1.

    T = sp.eye(dictsize) # scale factor
    Q = sp.zeros((dictsize,training_feats.shape[1])) # energy matrix
    for frameid in range(training_feats.shape[1]):
      for itemid in range(Dinit.shape[1]):
        Q[sp.ix_(dictLabel==itemid,H_train==frameid)] =1.

    # ksvd process
    runKsvd.fit(training_feats,Dinit=Dinit)
    Xtemp = runKsvd.gamma_

    # learning linear classifier parameters
    Winit = splin.pinv(Xtemp.dot(Xtemp.T)+sp.eye(Xtemp.shape[0])).dot(Xtemp).dot(H_train.T)
    Tinit = splin.pinv(Xtemp.dot(Xtemp.T)+sp.eye(Xtemp.shape[0])).dot(Xtemp).dot(Q.T)

    return Dinit,Tinit.T,Winit.T,Q
コード例 #22
0
ファイル: BLUP.py プロジェクト: mattions/limix
    def crossvalidate_delta(self, folds):
        import utils
        cv_scheme = utils.crossValidationScheme(folds, self.nTrain)
        ldeltas = SP.arange(-3, -1.5, .01)
        Ss = []
        Us = []
        Uys = []
        UCs = []
        err = 0.0
        errs = []
        for ldelta in ldeltas:
            for test_set in cv_scheme:
                train_set = ~test_set
                K_sub = self.kernel[SP.ix_(train_set, train_set)]
                K_cross = self.kernel[SP.ix_(~train_set, train_set)]
                # print LA.inv((K_sub + SP.eye(train_set.sum())*self.delta))
                Core = SP.dot(
                    K_cross,
                    LA.inv((K_sub + SP.eye(train_set.sum()) * SP.exp(ldelta))))
                diff = self.yTrain[test_set] -\
                    SP.dot(Core, self.yTrain[train_set])
                err += (diff**2).sum() / diff.size
                S, U = LA.eigh(self.kernel[SP.ix_(train_set, train_set)])
                Ss.append(S)
                Us.append(U)
                Uys.append(SP.dot(U.T, self.yTrain[train_set]))
                UCs.append(SP.dot(U.T, SP.ones_like(self.yTrain[train_set])))
            errs.append(err / len(cv_scheme))
            err = 0.0

        nll_scores = []
        for ldelta in ldeltas:
            # print 'ldelta equals', ldelta
            score = 0.0
            for i in xrange(len(cv_scheme)):
                score += lmm_fast.nLLeval(ldelta, (Uys[i])[:, 0], UCs[i],
                                          Ss[i])
            nll_scores.append(score / len(cv_scheme))
        print 'best ldelta found ll', ldeltas[SP.argmin(nll_scores)]
        return ldeltas[SP.argmin(errs)]
コード例 #23
0
ファイル: blup.py プロジェクト: jeffhsu3/limix
    def crossvalidate_delta(self, folds):
        import utils
        cv_scheme = utils.crossValidationScheme(folds, self.nTrain)
        ldeltas = SP.arange(-3, -1.5, .01)
        Ss = []
        Us = []
        Uys = []
        UCs = []
        err = 0.0
        errs = []
        for ldelta in ldeltas:
            for test_set in cv_scheme:
                train_set = ~test_set
                K_sub = self.kernel[SP.ix_(train_set, train_set)]
                K_cross = self.kernel[SP.ix_(~train_set, train_set)]
                # print LA.inv((K_sub + SP.eye(train_set.sum())*self.delta))
                Core = SP.dot(K_cross, LA.inv((K_sub + SP.eye(train_set.sum()) *
                                               SP.exp(ldelta))))
                diff = self.yTrain[test_set] -\
                    SP.dot(Core, self.yTrain[train_set])
                err += (diff**2).sum()/diff.size
                S, U = LA.eigh(self.kernel[SP.ix_(train_set, train_set)])
                Ss.append(S)
                Us.append(U)
                Uys.append(SP.dot(U.T, self.yTrain[train_set]))
                UCs.append(SP.dot(U.T, SP.ones_like(self.yTrain[train_set])))
            errs.append(err/len(cv_scheme))
            err = 0.0

        nll_scores = []
        for ldelta in ldeltas:
            # print 'ldelta equals', ldelta
            score = 0.0
            for i in xrange(len(cv_scheme)):
                score += lmm_fast.nLLeval(ldelta, (Uys[i])[:, 0], UCs[i], Ss[i])
            nll_scores.append(score/len(cv_scheme))
        print 'best ldelta found ll', ldeltas[SP.argmin(nll_scores)]
        return ldeltas[SP.argmin(errs)]
コード例 #24
0
    def test_d2k1_2by2(self):
        #1-form innerproduct for 2x2 unit square grid
        bitmap = ones((2,2),dtype='bool')

        K_local = array([[ 1.0/3.0,        0,  1.0/6.0,        0],
                         [       0,  1.0/3.0,        0,  1.0/6.0],
                         [ 1.0/6.0,        0,  1.0/3.0,        0],
                         [       0,  1.0/6.0,        0,  1.0/3.0]])

        K_correct = zeros((12,12))

        K_correct[ix_([0,1,2,6],[0,1,2,6])]   = K_correct[ix_([0,1,2,6],[0,1,2,6])]   + K_local
        K_correct[ix_([5,6,7,10],[5,6,7,10])] = K_correct[ix_([5,6,7,10],[5,6,7,10])] + K_local
        K_correct[ix_([2,3,4,8],[2,3,4,8])]   = K_correct[ix_([2,3,4,8],[2,3,4,8])]   + K_local
        K_correct[ix_([7,8,9,11],[7,8,9,11])] = K_correct[ix_([7,8,9,11],[7,8,9,11])] + K_local

        assert_almost_equal(K_correct,self.get_K(bitmap,1))
コード例 #25
0
def bp_marginal_onenode(lmds, pis, args):
    """calculate marginal dist. of node i,t"""
    I, T, L = args.X.shape
    K = args.gamma.shape[0]
    marginal = sp.ones((I, T, K))
    emit_probs_mat = sp.exp(args.log_obs_mat)
    emit_probs_mat /= emit_probs_mat.max(axis=2).reshape(I, T, 1)
    theta, alpha, beta, gamma, emit_probs, X = (args.theta, args.alpha,
                                                args.beta, args.gamma,
                                                args.emit_probs, args.X)
    evidence = evid_allchild(lmds, args.vert_children)
    for i in xrange(I):
        vp = args.vert_parent[i]

        for t in xrange(T):
            if i == 0 and t == 0:
                m = gamma * (emit_probs_mat[i, t, :] * evidence[i, t, :])
                #tmp1, tmp2 = sp.ix_(pis[i][-1,0,:], evidence[i,t+1,:]*emit_probs_mat[i, t+1, :])
                #m = (tmp1*tmp2 * alpha).sum(axis=1)
                #m /= m.sum()
                #breakpoint()
            else:
                if i == 0:
                    #tmp1, tmp2 = sp.ix_(pis[i][-1,t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    #tmp = alpha *(tmp1*tmp2)
                    #m = tmp.sum(axis=1)
                    #print m
                    tmp = sp.dot(pis[i][-1, t - 1, :], alpha)
                    m = tmp * emit_probs_mat[i, t, :] * evidence[i, t, :]

                elif t == 0:
                    tmp = sp.dot(pis[vp][i - 1, t, :], beta)
                    m = tmp * emit_probs_mat[i, t, :] * evidence[i, t, :]

                    #tmp1, tmp2 = sp.ix_(pis[vp][i-1,t,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    #tmp = beta *(tmp1*tmp2)
                    #m = tmp.sum(axis=0)
                else:
                    tmp1, tmp2, tmp3 = sp.ix_(
                        pis[vp][i - 1, t, :], pis[i][-1, t - 1, :],
                        emit_probs_mat[i, t, :] * evidence[i, t, :])
                    tmp = theta * (tmp1 * tmp2 * tmp3)
                    m = (tmp.sum(axis=0)).sum(axis=0)

            m /= m.sum()
            marginal[i, t, :] = m
    return marginal
コード例 #26
0
    def solve(self, A, x, b, hbw=None):
        """ Solves Ax = b """

        A = A[np.ix_(self.fdof, self.fdof)]
        b = b[self.fdof]

        if self.method == "rtfreechol":
            x[self.fdof] = rtfreechol(A, b, hbw)[0]
        elif self.method == "gauss_seidel":
            x[self.fdof] = gauss_seidel(A, b)
        elif self.method == "cholesky":
            raise NotImplementedError()
        elif self.method == "solve":
            x[self.fdof] = solve(A, b)
        elif self.method == "lstsq":
            x[self.fdof] = lstsq(A, b)[0]
        return x
コード例 #27
0
def generate_2D_problem(L, p1, p2, p3):

    # p1,p2,p3: probabilities of generating a plaquette (on the
    # checkerboard sublattice) with 1,2,3 GSs, modulo Z2, and
    # including the FM. p4 = 1-(p1+p2+p3).

    if L % 2 != 0 or L < 4:
        raise Exception("L must be even and >= 4!")

    # For coding clarity
    M = L
    N = L
    num_nodes = L**2

    M_skip = 2

    M_max = M
    N_max = N

    J = sp.zeros((num_nodes, num_nodes))
    h = sp.zeros((num_nodes, ))

    for n in range(N_max):
        M_offset = n % 2
        for m in range(M_offset, M_max, M_skip):

            # Get plaqutte vertices
            curr_node = m + M * n
            nbr_node_M = (m + 1) % M + M * n
            nbr_node_N = m + M * ((n + 1) % N)
            nbr_node_MN = (m + 1) % M + M * ((n + 1) % N)

            plaquette_vars = [curr_node, nbr_node_M, nbr_node_N, nbr_node_MN]

            # print plaquetteVars
            J_plaq = sample_plaquette(p1, p2, p3)
            # FIX!! This does *not* break the degeneracy, but it makes
            # things more difficult for SA.
            #J_plaq = sp.rand()*J_plaq

            # Put into problem
            J[sp.ix_(plaquette_vars, plaquette_vars)] = J_plaq

    return J
コード例 #28
0
ファイル: loopy_bp.py プロジェクト: daler/tree-hmm
def bp_marginal_onenode(lmds, pis, args):
    """calculate marginal dist. of node i,t"""
    I, T, L = args.X.shape
    K = args.gamma.shape[0]
    marginal = sp.ones((I, T, K))
    emit_probs_mat  = sp.exp(args.log_obs_mat)
    emit_probs_mat /= emit_probs_mat.max(axis=2).reshape(I, T, 1)
    theta, alpha, beta, gamma, emit_probs, X = (args.theta, args.alpha, args.beta, args.gamma, args.emit_probs,
                                        args.X)
    evidence = evid_allchild(lmds, args.vert_children)
    for i in xrange(I):
        vp = args.vert_parent[i]

        for t in xrange(T):
            if i==0 and t==0:
                m = gamma *(emit_probs_mat[i, t, :] *evidence[i,t,:])
                #tmp1, tmp2 = sp.ix_(pis[i][-1,0,:], evidence[i,t+1,:]*emit_probs_mat[i, t+1, :])
                #m = (tmp1*tmp2 * alpha).sum(axis=1)
                #m /= m.sum()
                #breakpoint()
            else:
                if i == 0:
                    #tmp1, tmp2 = sp.ix_(pis[i][-1,t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    #tmp = alpha *(tmp1*tmp2)
                    #m = tmp.sum(axis=1)
                    #print m
                    tmp = sp.dot(pis[i][-1,t-1,:], alpha)
                    m = tmp * emit_probs_mat[i, t, :]*evidence[i,t,:]
             
                elif t == 0:
                    tmp = sp.dot(pis[vp][i-1,t,:], beta)
                    m = tmp* emit_probs_mat[i, t, :]*evidence[i,t,:]
                    
                    #tmp1, tmp2 = sp.ix_(pis[vp][i-1,t,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    #tmp = beta *(tmp1*tmp2)
                    #m = tmp.sum(axis=0)
                else:
                    tmp1, tmp2, tmp3 = sp.ix_(pis[vp][i-1,t,:], pis[i][-1, t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                    tmp= theta *(tmp1*tmp2*tmp3)
                    m = (tmp.sum(axis=0)).sum(axis=0)
                
            m /= m.sum()
            marginal[i,t,:] = m
    return marginal
コード例 #29
0
    def __init__(self, conf, props):
        myProps = props.getProps("material")
        myConf = conf.makeProps("material")

        E = myProps.get("young")
        nu = myProps.get("poisson")

        myConf.set("young", E)
        myConf.set("poisson", nu)

        # Calculate the Lamé parameters
        self.la = nu * E / ((1 + nu) * (1 - 2 * nu))
        self.mu = E / (2 * (1 + nu))

        # Create the hookean matrix
        self.H = np.zeros((6, 6))
        self.H[np.ix_([0, 1, 2], [0, 1, 2])] = self.la
        self.H[[0, 1, 2], [0, 1, 2]] = self.la + 2 * self.mu
        self.H[[3, 4, 5], [3, 4, 5]] = self.mu
コード例 #30
0
ファイル: test_lmm_forest.py プロジェクト: PMBio/limix
 def test_depth_building(self):
     self.setUp(m=10)
     X = self.x.copy()
     X -= X.mean(axis=0)
     X /= X.std(axis=0)
     kernel = SP.dot(X, X.T)
     train = SP.where(self.train)[0]
     test = SP.where(~self.train)[0]
     model = MF(fit_optimal_depth=True, max_depth=3,
                kernel=kernel[SP.ix_(train, train)])
     model.fit(self.x[self.train], self.y[self.train],
               fit_optimal_depth=True)
     prediction_1 = model.predict(X[test], k=kernel[test, train],
                                  depth=model.opt_depth)
     # Grow to end
     model.further()
     # Prediction again
     prediction_2 = model.predict(X[test], k=kernel[test, train],
                                  depth=model.opt_depth)
     self.assertEqual((prediction_1 - prediction_2).sum(), 0.0)
コード例 #31
0
 def test_depth_building(self):
     self.setUp(m=10)
     X = self.x.copy()
     X -= X.mean(axis=0)
     X /= X.std(axis=0)
     kernel = SP.dot(X, X.T)
     train = SP.where(self.train)[0]
     test = SP.where(~self.train)[0]
     model = MF(fit_optimal_depth=True,
                max_depth=3,
                kernel=kernel[SP.ix_(train, train)])
     model.fit(self.x[self.train],
               self.y[self.train],
               fit_optimal_depth=True)
     prediction_1 = model.predict(X[test],
                                  k=kernel[test, train],
                                  depth=model.opt_depth)
     # Grow to end
     model.further()
     # Prediction again
     prediction_2 = model.predict(X[test],
                                  k=kernel[test, train],
                                  depth=model.opt_depth)
     self.assertEqual((prediction_1 - prediction_2).sum(), 0.0)
コード例 #32
0
def bp_update_params_new(args, renormalize=True):
    lmds, pis = args.lmds, args.pis
    vert_parent, vert_children = args.vert_parent, args.vert_children
    #print  pis[0].shape
    I, T, L = args.X.shape
    K = args.gamma.shape[0]
    theta, alpha, beta, gamma, emit_probs, X = (args.theta, args.alpha,
                                                args.beta, args.gamma,
                                                args.emit_probs, args.X)
    evidence = args.evidence  #evid_allchild(lmds, vert_children)
    emit_probs_mat = sp.exp(args.log_obs_mat)
    #emit_probs_mat /= emit_probs_mat.max(axis=2).reshape(I, T, 1)
    gamma_p = copy.copy(gamma)
    alpha_p = copy.copy(alpha)
    beta_p = copy.copy(beta)
    theta_p = copy.copy(theta)
    #    emit_probs_p = copy.copy(emit_probs)

    theta[:] = args.pseudocount
    alpha[:] = args.pseudocount
    beta[:] = args.pseudocount
    gamma[:] = args.pseudocount
    emit_probs[:] = args.pseudocount

    #evidence = evid_allchild(lmds, args.vert_children)
    ##support = casual_support(pis)

    emit_sum = sp.zeros((K, L))
    for i in xrange(I):
        vp = vert_parent[i]
        if i != 0:
            idx_i = vert_children[vp].tolist().index(i)
        for t in xrange(T):
            if i == 0 and t == 0:
                gamma += emit_probs_mat[i, t, :] * evidence[i, t, :]
                Q = emit_probs_mat[i, t, :] * evidence[i, t, :]
            elif i == 0:
                tmp1, tmp2 = sp.ix_(
                    pis[i][-1, t - 1, :],
                    emit_probs_mat[i, t, :] * evidence[i, t, :])
                tmp = alpha_p * (tmp1 * tmp2)  # belief
                #tmp /= tmp.sum()
                Q = tmp.sum(axis=0)
                alpha += tmp / tmp.sum()
            elif t == 0:
                tmp1, tmp2 = sp.ix_(pis[vp][idx_i,
                                            t, :], emit_probs_mat[i, t, :] *
                                    evidence[i, t, :])  # i-1->0
                tmp = beta_p * (tmp1 * tmp2)  # belief
                Q = tmp.sum(axis=0)
                beta += tmp / tmp.sum()
            else:
                tmp1, tmp2, tmp3 = sp.ix_(
                    pis[vp][idx_i, t, :], pis[i][-1, t - 1, :],
                    emit_probs_mat[i, t, :] * evidence[i, t, :])
                tmp = theta_p * (tmp1 * tmp2 * tmp3)
                Q = (tmp.sum(axis=0)).sum(axis=0)
                theta += tmp / tmp.sum()

            Q /= Q.sum()
            for l in xrange(L):
                if args.mark_avail[i, l] and X[i, t, l]:
                    emit_probs[:, l] += Q
                emit_sum[:, l] += Q
    if renormalize:
        normalize_trans(theta, alpha, beta, gamma)
        emit_probs[:] = sp.dot(sp.diag(1. / emit_sum), emit_probs)
    args.emit_sum = emit_sum
    make_log_obs_matrix(args)
コード例 #33
0
ファイル: ps3.py プロジェクト: jrnold/psc585
    def func(self, y, t=1):
        """ Function values

        Parameters
        --------------
        y : ndarray, shape (155, )
            Parameter vector
        t : float, optional
            Value of t between 0 and 1

        Returns
        ----------
        F : ndarray, shape (155, )
            Function values at y
        JF : ndarray, shape (155, 155)
            Jacobian of the function evaluated at y
        JFt : ndarray
            Derivative of the function as an implicit function of t
        
        """
        ## Coalition specific payoffs
        Cs = t * self.Cs1 + (1 - t) * self.Cs0

        # number of parameters
        npars = {'v' : 5, 'lam' : 30, 'x' : 60, 'mu' : 60 }
        # Indices for each parameter
        idxs = {}
        idxs['v'] = sp.arange(0, npars['v'])
        idxs['lam'] = sp.arange(idxs['v'].max() + 1,
                                idxs['v'].max() + 1 + npars['lam'])
        idxs['x'] = sp.arange(idxs['lam'].max() + 1,
                              idxs['lam'].max() + 1 + npars['x'])
        idxs['mu'] = sp.arange(idxs['x'].max() + 1,
                              idxs['x'].max() + 1 + npars['mu'])

        ## Split parameters 
        v = y[idxs['v']]
        lam = y[idxs['lam']]
        x = y[idxs['x']]
        mu = y[idxs['mu']]

        ## Function values
        F = sp.zeros(155, )
        ## Lambda
        sig = sp.maximum(0, lam) ** 2
        ## Mu values as a matrix
        ## TODO: check that rows and values are correct
        ## Matlab reshapes by Fortran (column order)
        mmu = sp.reshape(sp.maximum(0, mu), (30, 2), order='C')
        ## Lambda as a matrix
        mlam = sp.maximum(0, -lam) ** 2
        ## proposals to a 30 x 2 matrix
        X = sp.reshape(x, (30, 2), order='C')

        ## utilities for proposal 
        U = sp.zeros((30, 5))
        for i in range(U.shape[0]):
            u1 = - (X[i, 0] - self.ideals[:, 0])**2
            u2 = - (X[i, 1] - self.ideals[:, 1])**2
            U[i, : ] = u1 + u2 + Cs[i, :] + self.K

        ## \bar{u}.
        ## Utility to each player for status quo of (0,0)
        Usq = (-(self.ideals[self.parts, 0])**2
               - (self.ideals[self.parts, 1])**2
               + self.K)
        ## Derivatives of utilities
        DU1 = sp.zeros((30, 5))
        for i in range(DU1.shape[0]):
            DU1[i, ] = - 2 * (X[i, 0] - self.ideals[:, 0])
        DU2 = sp.zeros((30, 5))
        for i in range(DU2.shape[0]):
            DU2[i, ] = - 2 * (X[i, 1] - self.ideals[:, 1])
        ## Equation 7
        F[idxs['v']] = v - (U.T.dot(sig * sp.kron(self.p, sp.ones(6))))

        ## Equation 8
        ## For all players
        for i in range(5):
            ## Indices of player i
            ii = i * 6 + sp.arange(0, 6)
            u = sig[ii].T.dot(U[ii, i]) - U[ii, i] - mlam[ii]
            F[idxs['lam'].min() + ii] = u

        ## Equation 9
        Fx1 = (DU1[sp.r_[0:30], self.prop]
              + DU1[sp.r_[0:30], self.part1] * (mmu[:, 0] ** 2)
              + DU1[sp.r_[0:30], self.part2] * (mmu[:, 1] ** 2))
        Fx2 = (DU2[sp.r_[0:30], self.prop]
              + DU2[sp.r_[0:30], self.part1] * (mmu[:, 0] ** 2)
              + DU2[sp.r_[0:30], self.part2] * (mmu[:, 1] ** 2))
        F[idxs['x']] = sp.reshape(sp.column_stack((Fx1, Fx2)), (60, 1))

        # Equation 10
        F[idxs['mu']] = (U[self.pols, self.parts] - (1 - self.d) * Usq
                         - self.d * v[self.parts] - sp.maximum(0, -mu)**2)

        # Jacobian
        JF = sp.zeros((155, 155))
        # Equation 7
        # with respect to v
        JF[sp.r_[:5], sp.r_[:5]] = sp.ones(5)
        # with respect to lambda
        JF[sp.ix_(idxs['v'], idxs['lam'])] = \
        (-U * (2 * sp.maximum(0, lam) * self.p.repeat(6))[: , sp.newaxis]).T
        # with respect to x
        JF[:5, 35:95:2] = -(DU1 * (sig * self.p.repeat(6))[:, sp.newaxis]).T
        JF[:5, 36:96:2] = -(DU2 * (sig * self.p.repeat(6))[:, sp.newaxis]).T

        # Equation 8
        # with respect to lambda
        for i in range(5):
            ii = i * 6 + sp.r_[0:6]
            foo = (sp.tile(2 * sp.maximum(0, lam[ii]) * U[ii, i], (6, 1)) + 
                   sp.eye(6) * (2 * sp.maximum(0, -lam[ii])))
            JF[sp.ix_(npars['v'] + ii, npars['v'] + ii)] = foo
        # with respect to x
        for i in range(5):
            for m in range(6):
                minlam = idxs['lam'].min()
                minx = idxs['x'].min()
                # range of lambda pars for player i
                ii = i * 6 + sp.r_[0:6]
                # Indices
                JFi0 = minlam + (i * 6) + m
                JFi10 = minx + i * 12 + sp.r_[0:12:2]
                JFi11 = minx + i * 12 + sp.r_[0:12:2] + 1
                #
                JF[JFi0, JFi10] = DU1[ii, i] * sig[ii]
                JF[JFi0, JFi11] = DU2[ii, i] * sig[ii]
                JF[JFi0, minx + i * 12 + m * 2] -= DU1[i * 6 + m, i]
                JF[JFi0, minx + i * 12 + m * 2 + 1] -= DU2[i * 6 + m, i]

        # Equation 9
        # with respect to x
        JF[idxs['x'], idxs['x']] = -2 * (sp.ones(60) + sp.kron((mmu ** 2), sp.ones((2, 1))).sum(1))
        # with respect to mu
        JF[sp.r_[35:95:2], sp.r_[95:155:2]] = 2 * DU1[sp.r_[0:30], self.part1] * mmu[:, 0]
        JF[sp.r_[35:95:2] + 1, sp.r_[95:155:2]] = 2 * DU2[sp.r_[0:30], self.part1] * mmu[:, 0]
        JF[sp.r_[35:95:2], sp.r_[95:155:2] + 1] = 2 * DU1[sp.r_[0:30], self.part2] * mmu[:, 1]
        JF[sp.r_[35:95:2] + 1, sp.r_[95:155:2] + 1] = 2 * DU2[sp.r_[0:30], self.part2] * mmu[:, 1]

        # Equation 10
        # with respect to v
        JF[sp.ix_(idxs['mu'], idxs['v'])] = -self.d * self.COAL
        # with respect to x
        JF[sp.r_[95:155:2], sp.r_[35:95:2]] = DU1[sp.r_[0:30], self.part1]
        JF[sp.r_[95:155:2], sp.r_[35:95:2] + 1] = DU2[sp.r_[0:30], self.part1]
        JF[sp.r_[95:155:2] + 1, sp.r_[35:95:2]] = DU1[sp.r_[0:30], self.part2]
        JF[sp.r_[95:155:2] + 1, sp.r_[35:95:2] + 1] = DU2[sp.r_[0:30], self.part2]
        # with respect to mu
        JF[idxs['mu'], idxs['mu']] = 2 * sp.maximum(0, -mu)

        # Derivatives of H(y, y(t))
        JFt = sp.zeros(155, )
        JFt[idxs['v']] =  -(self.Cs1 - self.Cs0).T.dot(sig * self.p.repeat(6))

        for i in range(5):
            ii = i * 6 + sp.arange(0, 6)
            cv = self.Cs1[ii, i] - self.Cs0[ii, i]
            JFt[idxs['lam'].min() + ii] = sig[ii].T.dot(cv) - cv
        jft1 = self.d * (self.Cs1[sp.arange(0, 30), self.part1]
                          - self.Cs0[sp.arange(0, 30), self.part1])
        jft2 = self.d * (self.Cs1[sp.arange(0, 30), self.part2]
                          - self.Cs0[sp.arange(0, 30), self.part2])
        JFt[idxs['mu']] = sp.reshape(sp.column_stack((jft1, jft2)), (60, 1))

        return (F, JF, JFt)
コード例 #34
0
def bp_bethe_free_energy(args):
    lmds, pis = args.lmds, args.pis
    vert_parent, vert_children = args.vert_parent, args.vert_children
    theta, alpha, beta, gamma, emit_probs, X = (args.theta, args.alpha,
                                                args.beta, args.gamma,
                                                args.emit_probs, args.X)
    I, T, L = X.shape
    #    K = gamma.shape[0]
    free_e = 0.
    #entp = 0.
    log_theta, log_alpha, log_beta, log_gamma = sp.log(theta), sp.log(
        alpha), sp.log(beta), sp.log(gamma)
    emit_probs_mat = sp.exp(args.log_obs_mat)
    evidence = evid_allchild(lmds, vert_children)  #args.evidence

    ### replace start here
    #Q = bp_marginal_onenode(lmds, pis, args) # args.Q
    #Q_clq = sp.zeros((K,K))
    #Q_clq3 = sp.zeros((K,K,K))

    #log_emit_probs_mat = sp.zeros((K,T))
    for i in xrange(I):
        vp = vert_parent[i]
        if i != 0:
            idx_i = vert_children[vp].tolist().index(i)
            print vp, idx_i
        log_probs_mat_i = args.log_obs_mat[i, :, :].T
        if i == 0:
            Qt = gamma * emit_probs_mat[i, 0, :] * evidence[i, 0, :]
            Qt /= Qt.sum()
            free_e -= (Qt * log_gamma).sum() + (Qt *
                                                log_probs_mat_i[:, 0]).sum()
            free_e -= (Qt * sp.log(Qt)).sum()
            #for k in range(K):
            #    free_e -= Q[i,0,k]*(log_gamma[k] + log_probs_mat_i[k, 0] + log(Q[i,0,k]))

            for t in xrange(1, T):
                tmp1, tmp2 = sp.ix_(
                    pis[i][-1, t - 1, :],
                    emit_probs_mat[i, t, :] * evidence[i, t, :])
                Q_clq = alpha * (tmp1 * tmp2)
                Q_clq /= Q_clq.sum()
                Qt = Q_clq.sum(axis=0)
                #breakpoint()
                #free_e -= (Q_clq * log_alpha).sum() +(Qt*log_probs_mat_i[:, t]).sum()
                #free_e += (Q_clq * sp.log(Q_clq)).sum() -2.*(Qt*sp.log(Qt)).sum()

                free_e -= (Qt *
                           (log_probs_mat_i[:, t] + 2. * sp.log(Qt))).sum()
                free_e += (Q_clq * (sp.log(Q_clq) - log_alpha)).sum()
                #entp += (Q_clq * sp.log(Q_clq)).sum() -2.*(Q*sp.log(Q)).sum()
                #Q_clq_sum = 0.
                #for k1 in range(K):
                #    for k2 in range(K):
                #        Q_clq[k1,k2] = alpha[k1,k2]* pis[i][-1,t-1,k1] * emit_probs_mat[i,t,k2]*evidence[i,t,k2]
                #        Q_clq_sum += Q_clq[k1,k2]
                #
                #Q_clq /= Q_clq_sum
                #
                #for k1 in range(K):
                #    free_e -= Q[i,t,k1]*log_probs_mat_i[k1, t] +2.*Q[i,t,k1]*log(Q[i,t,k1])
                #    for k2 in range(K):
                #        free_e -= Q_clq[k1,k2] * log_alpha[k1,k2]
                #        free_e += Q_clq[k1,k2] * log(Q_clq[k1,k2])
        else:

            tmp1, tmp2 = sp.ix_(pis[vp][idx_i, 0, :],
                                emit_probs_mat[i, 0, :] * evidence[i, 0, :])
            Q_clq = beta * (tmp1 * tmp2)
            Q_clq /= Q_clq.sum()
            Qt = Q_clq.sum(axis=0)
            free_e -= (Q_clq * log_beta).sum() + (Qt *
                                                  log_probs_mat_i[:, 0]).sum()
            free_e += (Q_clq * sp.log(Q_clq)).sum()
            free_e -= (Qt * sp.log(Qt)).sum()

            #Q_clq_sum = 0.
            #for k1 in xrange(K):
            #    for k2 in xrange(K):
            #        Q_clq[k1,k2] = beta[k1,k2]* pis[vp][i-1,0,k1] * emit_probs_mat[i,0,k2]*evidence[i,0,k2]
            #        Q_clq_sum += Q_clq[k1,k2]
            #Q_clq /= Q_clq_sum
            #for k1 in xrange(K):
            #     free_e -= Q[i,0,k1] * (log_probs_mat_i[k1, 0] + log(Q[i,0,k1]) )
            #     for k2 in xrange(K):
            #         free_e += Q_clq[k1,k2] * (log(Q_clq[k1,k2]) - log_beta[k1,k2])

            #entp += (Q_clq * sp.log(Q_clq)).sum()-(Q * sp.log(Q)).sum()
            for t in xrange(1, T):
                tmp1, tmp2, tmp3 = sp.ix_(
                    pis[vp][idx_i, t, :], pis[i][-1, t - 1, :],
                    emit_probs_mat[i, t, :] * evidence[i, t, :])
                Q_clq3 = theta * (tmp1 * tmp2 * tmp3)
                Q_clq3 /= Q_clq3.sum()
                Qt = (Q_clq3.sum(axis=0)).sum(axis=0)
                #breakpoint()
                free_e -= (Q_clq3 * log_theta).sum()
                free_e += (Q_clq3 * sp.log(Q_clq3)).sum()  ###!!
                free_e -= (Qt * (sp.log(Qt) + log_probs_mat_i[:, t])).sum()
                #entp += (Q_clq3 * sp.log(Q_clq3)).sum() -(Qt * sp.log(Qt)).sum()
                #Q_clq3_sum = 0
                #for k1 in range(K):
                #    for k2 in xrange(K):
                #        for k3 in xrange(K):
                #            Q_clq3[k1,k2, k3] = theta[k1,k2, k3] * pis[vp][i-1,t,k1] * pis[i][-1, t-1,k2] * emit_probs_mat[i,t,k3]*evidence[i,t,k3]
                #            Q_clq3_sum += Q_clq3[k1,k2, k3]
                #Q_clq3 /=  Q_clq3_sum
                #free_e -= (Q_clq3 * log_theta).sum() +(Q[i,t,:] * log_probs_mat_i[:, t] ).sum()
                #free_e += (Q_clq3 * sp.log(Q_clq3)).sum() ###!!
                #free_e -= (Q[i,t,:]*sp.log(Q[i,t,:])).sum()
                #for k1 in xrange(K):
                #    free_e -= Q[i,t,k1] *(log_probs_mat_i[k1,t] + log(Q[i,t,k1]))
                #    for k2 in xrange(K):
                #        for k3 in xrange(K):
                #            free_e -= Q_clq3[k1,k2,k3] * (log_theta[k1,k2,k3] - log(Q_clq3[k1,k2,k3]))

    #print 'free energy:', free_e
    return free_e
コード例 #35
0
def bp_update_msg_new(args):
    '''now assume the tree are denoted as vert_PARENTS {1:Null, 2:1, 3:2, ...}, vert_Children {1:2, 2:3 , ...}'''
    print 'loopy2'
    lmds, pis = args.lmds, args.pis
    vert_children = args.vert_children
    vert_parent = args.vert_parent
    I, T, L = args.X.shape
    #print I, T, L
    #print  pis[0].shape
    K = args.gamma.shape[0]
    emit_probs_mat = sp.exp(args.log_obs_mat)
    emit_probs_mat /= emit_probs_mat.max(axis=2).reshape(I, T, 1)
    theta, alpha, beta, gamma, emit_probs = args.theta, args.alpha, args.beta, args.gamma, args.emit_probs

    lmds_prev = args.lmds_prev = copy.deepcopy(lmds)  #sp.copy(lmds)
    pis_prev = args.pis_prev = copy.deepcopy(pis)  #sp.copy(pis)
    if ~hasattr(args, 'evidence'):
        evidence = args.evidence = evid_allchild(lmds_prev, vert_children)
    else:
        evidence = args.evidence

    for i in range(I):
        lmds[i][:] = args.pseudocount
        pis[i][:] = args.pseudocount
    #breakpoint()
    for i in xrange(I):  # can be parallelized
        vcs = vert_children[i]
        vp = vert_parent[i]
        if i != 0:
            idx_i = vert_children[vp].tolist().index(i)
        for t in xrange(T):
            #print i, T
            #print pis[i][0,t]
            #print pis_prev[i][0,t]

            if i == 0 and t == 0:
                # msg to vertical child
                for id_vc, vc in enumerate(vcs):
                    pis[i][id_vc, t, :] += gamma * emit_probs_mat[
                        0, t, :] * evidence[i, t] / lmds_prev[vc][0, t, :]
                # msg to horizontal child
                pis[i][-1, t, :] += gamma * emit_probs_mat[0, t, :] * evidence[
                    i, t] / lmds_prev[i][1, t + 1, :]
                # no lambda meessage in this case

            elif i == 0:  # different than t=0 case : now there is a parent, need to sum over; also there is lambda message
                tmp1, tmp2 = sp.ix_(
                    pis_prev[i][-1, t - 1, :],
                    emit_probs_mat[i, t, :] * evidence[i, t, :])
                BEL = alpha * (tmp1 * tmp2)
                # msg to (iterate over) vertical child
                for id_vc, vc in enumerate(vcs):
                    pis[i][id_vc, t, :] += sp.dot(
                        BEL, diag(1. / lmds_prev[vc][0, t, :])).sum(axis=0)
#                    tmp = BEL.sum(axis=0) /lmds_prev[vc][0,t,:]
#                    print pis[i][id_vc, t,:]
#                    print tmp
#                    breakpoint()
# msg to horizontal child
                pis[i][-1, t, :] += sp.dot(
                    BEL, diag(1. / lmds_prev[i][1, t + 1, :])).sum(axis=0)

                # msg to horizontal parent
                lmds[i][1, t, :] += sp.dot(diag(1 / pis_prev[i][-1, t - 1, :]),
                                           BEL).sum(axis=1)
                #breakpoint()

            elif t == 0:
                #                    tmp1, tmp2 = sp.ix_(pis_prev[vp][idx_i,t,:],
                #                    BEL =
                if len(vcs) == 0:
                    pis[i][-1,
                           t, :] += sp.dot(pis_prev[vp][idx_i, t, :],
                                           beta) * (emit_probs_mat[i, t, :])
                else:
                    pis[i][-1,
                           t, :] += sp.dot(pis_prev[vp][idx_i, t, :], beta) * (
                               emit_probs_mat[i, t, :] * evidence[i, t] /
                               lmds_prev[i][-1, t + 1, :]
                           )  # only  change: i-1 -> 0
                    for id_vc, vc in enumerate(vcs):
                        pis[i][id_vc, t, :] += sp.dot(
                            pis_prev[vp][idx_i, t, :], beta) * emit_probs_mat[
                                i, t, :] * evidence[i, t] / lmds_prev[vc][0,
                                                                          t, :]
    #                    pis[i][0,t,:] += sp.dot(pis_prev[vp][idx_i,t,:], beta) * (emit_probs_mat[i,t,:]*lmds_prev[i][-1, t+1,:])
    # in general, should iterate over parents
                lmds[i][0, t, :] += sp.dot(
                    beta, emit_probs_mat[i, t, :] * evidence[i, t])
                lmds[i][1, t, :] += sp.ones(
                    K)  #  doesn't matter for there is no horizontal parent

                #tmp1, tmp2 = sp.ix_(pis_prev[vp][i-1,t,:], emit_probs_mat[i,t,:]*evidence[i,t,:])
                #BEL = beta * (tmp1 * tmp2)
                ## in general, should iterate over children
                #pis[i][-1,t,:] += sp.dot(BEL, 1/lmds_prev[i][1,t+1,:]).sum(axis=0)
                ##pis[i][-1,t,:] += (BEL/lmds_prev[i][1,t+1,:].reshape(1,K)).sum(axis=0)
                ## in general, should iterate over parents
                #lmds[i][0,t,:] += sp.dot(diag(1/pis_prev[vp][i-1, t,:]), BEL).sum(axis=1)  # The index i-1 is a hand-waiving way to do it, in principle should match the index of child species i in pis
                ##lmds[i][0,t,:] += (BEL//pis_prev[vp][i-1, t,:].reshape(K,1)).sum(axis=1)
                #lmds[i][1,t,:] += sp.ones(K) #  doesn't matter for there is no horizontal parent

            else:
                #tmp = sp.zeros(K)
                #for k1 in range(K):
                #    for k2 in range(K):
                #        tmp += theta[k1, k2, :] * pis_prev[vp][i-1,t,k1] * pis_prev[i][-1, t-1, k2] * emit_probs_mat[i,t,:]
                #pis[i][-1,t,:] = tmp
                tmp1, tmp2, tmp3 = sp.ix_(
                    pis_prev[vp][idx_i, t, :], pis_prev[i][-1, t - 1, :],
                    emit_probs_mat[i, t, :] * evidence[i, t, :])
                BEL = theta * (tmp1 * tmp2 * tmp3)
                if len(vcs) == 0:
                    pis[i][-1, t, :] += (BEL.sum(axis=0)).sum(
                        axis=0) / lmds_prev[i][1, t + 1, :]
#                    breakpoint()
                else:
                    pis[i][-1, t, :] += (BEL.sum(axis=0)).sum(
                        axis=0) / lmds_prev[i][1, t + 1, :]
                    #                    pis[i][-1,t,:] += (sp.dot(BEL, 1/lmds_prev[i][1,t+1,:]).sum(axis=0)).sum(axis=0)
                    for id_vc, vc in enumerate(vcs):
                        #                        pis[i][id_vc,t,:] += (sp.dot(BEL, 1/lmds_prev[vc][0,t,:]).sum(axis=0)).sum(axis=0)
                        pis[i][id_vc, t, :] += (BEL.sum(axis=0)).sum(
                            axis=0) / lmds_prev[vc][0, t, :]

                #tmp_lmd1= sp.zeros(K)
                #tmp_lmd2= sp.zeros(K)
                #for k1 in range(K):
                #    for k2 in range(K):
                #        tmp_lmd1 += theta[:,k1, k2] * pis_prev[i][0, t-1, k2]* emit_probs_mat[i,t,k2] * evidence[i,t,k2]
                #        tmp_lmd2 += theta[k1,:,k2] * pis_prev[vp][i-1, t, k2]* emit_probs_mat[i,t,k2] * evidence[i,t,k2]
                #print tmp_lmd1
                #print tmp_lmd2

                ##lmds[i][0,t,:] += tmp_lmd1
                ##lmds[i][1,t,:] += tmp_lmd2


#                tmp_lmd1 = ((BEL / pis_prev[vp][idx_i, t, :].reshape(K,1,1)).sum(axis=1)).sum(axis=1)
#                tmp_lmd2 = ((BEL / pis_prev[i][-1, t-1, :].reshape(1,K,1)).sum(axis=0)).sum(axis=1)     # t=T is not used
                lmds[i][0, t, :] += (BEL.sum(axis=1)).sum(
                    axis=1) / pis_prev[vp][idx_i, t, :]
                lmds[i][1, t, :] += (BEL.sum(axis=0)).sum(
                    axis=1) / pis_prev[i][-1, t - 1, :]
                #print lmds[i][0,t,:]
                #checked, same as above version (from line 353)
                #breakpoint()

    normalize_msg(lmds, pis)
    #    for i in range(I):
    #        print pis[i].shape
    #        print pis[i][0,t,:]
    #        print lmds[i].shape
    #        print lmds[i][0,t,:]
    args.evidence = evid_allchild(lmds, vert_children)
コード例 #36
0
def check_predictors(X, noderange, rmind):
    Xout = X[SP.ix_(noderange, rmind)]
    X_sum = SP.sum(Xout, 0)
    indexes = (X_sum != Xout.shape[0]) & (X_sum != 0)
    return rmind[indexes]
コード例 #37
0
ファイル: py_splitting_core.py プロジェクト: PMBio/limix
def check_predictors(X, noderange, rmind):
    Xout = X[SP.ix_(noderange, rmind)]
    X_sum = SP.sum(Xout,0)
    indexes = (X_sum != Xout.shape[0]) & (X_sum != 0)
    return rmind[indexes]
コード例 #38
0
 def addBlock(self, idofs, jdofs, block):
     """ Input:  idofs = list of row indices
                 jdofs = list of col indices
                 block = block to be added to K[idofs,jdofs] """
     self.K[np.ix_(idofs, jdofs)] += block
コード例 #39
0
 def setBlock(self, idofs, jdofs, block):
     """ Input:  idofs = list of row indices
                 jdofs = list of col indices
                 block = block to be set in K[idofs,jdofs] """
     self.K[np.ix_(idofs, jdofs)] = block
コード例 #40
0
def generate_3D_problem(L, p2FP=0.05, p4FP=0.05):

    # Make a voxel problem on an LxLxL lattice with periodic BC.

    # 1. Define three types of voxel having "+" as a G.S. The three
    # types have 2, 4, or 6 frustrated facet plaquettes. There are 2
    # subtypes within the set of 2 and 4 FP voxels, and 1 within the
    # 6FP set.

    # 2. Define a probability distribution over the voxel classes,
    # p2FP, p4FP, and of course, p6FP = 1-p2FP-p4FP

    # L must be even to have periodic boundary under this partition
    # scheme

    # Current hardest regime seems to be to set p6FP = 1, i.e. 2,4=0.

    if L % 2 != 0 or L < 4:
        raise Exception("L must be even and >= 4!")

    # For coding clarity
    M = L
    N = L
    num_nodes = L**3

    M_skip = 2
    N_skip = 2

    M_max = M
    N_max = N
    L_max = L

    J = sp.zeros((num_nodes, num_nodes))

    for l in range(L_max):
        M_offset = l % 2
        N_offset = M_offset
        for n in range(N_offset, N_max, N_skip):
            for m in range(M_offset, M_max, M_skip):

                # Voxel vertices
                curr_node = m + M * n + M * N * l
                nbr_node_M = (m + 1) % M + M * n + M * N * l
                nbr_node_N = m + M * ((n + 1) % N) + M * N * l
                nbr_node_L = m + M * n + M * N * ((l + 1) % L)
                nbr_node_MN = (m + 1) % M + M * ((n + 1) % N) + M * N * l
                nbr_node_ML = (m + 1) % M + M * n + M * N * ((l + 1) % L)
                nbr_node_NL = m + M * ((n + 1) % N) + M * N * ((l + 1) % L)
                nbr_node_MNL = (m + 1) % M + M * ((n + 1) % N) + M * N * (
                    (l + 1) % L)

                voxel_vars = [
                    curr_node, nbr_node_M, nbr_node_N, nbr_node_MN, nbr_node_L,
                    nbr_node_ML, nbr_node_NL, nbr_node_MNL
                ]

                # print voxel_vars
                J_vox = sample_voxel(p2FP, p4FP)

                # Put into problem
                J[sp.ix_(voxel_vars, voxel_vars)] = J_vox

    return J
コード例 #41
0
ファイル: lmm_forest.py プロジェクト: xypan1232/limix
 def get_cross_kernel(self, oob, subsample):
     return (self.forest.kernel +
             self.forest.delta*SP.eye(self.forest.n))[SP.ix_(oob, subsample)]
コード例 #42
0
ファイル: lmm_forest.py プロジェクト: xypan1232/limix
 def get_kernel(self):
     return self.forest.kernel[SP.ix_(self.subsample, self.subsample)]
コード例 #43
0
ファイル: loopy_bp.py プロジェクト: lingjie/tree-hmm
def bp_bethe_free_energy(args):
    lmds, pis = args.lmds, args.pis
    vert_parent, vert_children = args.vert_parent, args.vert_children
    theta, alpha, beta, gamma, emit_probs, X = (args.theta, args.alpha, args.beta, args.gamma, args.emit_probs,
                        args.X)
    I, T, L = X.shape
    K = gamma.shape[0]
    free_e = 0.
    #entp = 0.
    log_theta, log_alpha, log_beta, log_gamma = sp.log(theta), sp.log(alpha), sp.log(beta), sp.log(gamma)
    emit_probs_mat  = sp.exp(args.log_obs_mat)
    evidence = evid_allchild(lmds, vert_children) #args.evidence
    
    
    ### replace start here
    #Q = bp_marginal_onenode(lmds, pis, args) # args.Q
    #Q_clq = sp.zeros((K,K))
    #Q_clq3 = sp.zeros((K,K,K))
    
    #log_emit_probs_mat = sp.zeros((K,T))
    for i in xrange(I):
        vp = vert_parent[i]
        log_probs_mat_i = args.log_obs_mat[i,:,:].T
        if i==0:
            Qt =  gamma * emit_probs_mat[i,0,:]*evidence[i,0,:]
            Qt /= Qt.sum()
            free_e -= (Qt*log_gamma).sum() + (Qt*log_probs_mat_i[:, 0]).sum()
            free_e -= (Qt*sp.log(Qt)).sum()
            #for k in range(K):
            #    free_e -= Q[i,0,k]*(log_gamma[k] + log_probs_mat_i[k, 0] + log(Q[i,0,k]))
            
            for t in xrange(1 ,T):
                tmp1, tmp2 = sp.ix_(pis[i][-1,t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                Q_clq = alpha * (tmp1*tmp2)
                Q_clq /= Q_clq.sum()
                Qt = Q_clq.sum(axis=0)
                #breakpoint()
                #free_e -= (Q_clq * log_alpha).sum() +(Qt*log_probs_mat_i[:, t]).sum()
                #free_e += (Q_clq * sp.log(Q_clq)).sum() -2.*(Qt*sp.log(Qt)).sum()
                
                free_e -= (Qt * (log_probs_mat_i[:, t]+ 2.*sp.log(Qt))).sum()
                free_e += (Q_clq * (sp.log(Q_clq) -log_alpha)).sum()
                #entp += (Q_clq * sp.log(Q_clq)).sum() -2.*(Q*sp.log(Q)).sum()
                #Q_clq_sum = 0.
                #for k1 in range(K):
                #    for k2 in range(K):
                #        Q_clq[k1,k2] = alpha[k1,k2]* pis[i][-1,t-1,k1] * emit_probs_mat[i,t,k2]*evidence[i,t,k2]
                #        Q_clq_sum += Q_clq[k1,k2]
                #
                #Q_clq /= Q_clq_sum
                #
                #for k1 in range(K):
                #    free_e -= Q[i,t,k1]*log_probs_mat_i[k1, t] +2.*Q[i,t,k1]*log(Q[i,t,k1])
                #    for k2 in range(K):
                #        free_e -= Q_clq[k1,k2] * log_alpha[k1,k2]
                #        free_e += Q_clq[k1,k2] * log(Q_clq[k1,k2])
        else:
            
            tmp1, tmp2 = sp.ix_(pis[vp][i-1,0,:], emit_probs_mat[i, 0, :]*evidence[i,0,:])
            Q_clq = beta *(tmp1*tmp2)
            Q_clq /= Q_clq.sum()
            Qt = Q_clq.sum(axis=0)
            free_e -= (Q_clq * log_beta).sum() +(Qt * log_probs_mat_i[:, 0]).sum()
            free_e += (Q_clq * sp.log(Q_clq)).sum()
            free_e -= (Qt * sp.log(Qt)).sum()
            
            #Q_clq_sum = 0.
            #for k1 in xrange(K):   
            #    for k2 in xrange(K):
            #        Q_clq[k1,k2] = beta[k1,k2]* pis[vp][i-1,0,k1] * emit_probs_mat[i,0,k2]*evidence[i,0,k2]
            #        Q_clq_sum += Q_clq[k1,k2]
            #Q_clq /= Q_clq_sum
            #for k1 in xrange(K):
            #     free_e -= Q[i,0,k1] * (log_probs_mat_i[k1, 0] + log(Q[i,0,k1]) )
            #     for k2 in xrange(K):
            #         free_e += Q_clq[k1,k2] * (log(Q_clq[k1,k2]) - log_beta[k1,k2])
            
            
            #entp += (Q_clq * sp.log(Q_clq)).sum()-(Q * sp.log(Q)).sum()
            for t in xrange(1 ,T):
                tmp1, tmp2, tmp3 = sp.ix_(pis[vp][i-1,t,:], pis[i][-1, t-1,:], emit_probs_mat[i, t, :]*evidence[i,t,:])
                Q_clq3 = theta *(tmp1*tmp2*tmp3)
                Q_clq3 /= Q_clq3.sum()
                Qt = (Q_clq3.sum(axis=0)).sum(axis=0)
                #breakpoint()
                free_e -= (Q_clq3 * log_theta).sum()
                free_e += (Q_clq3 * sp.log(Q_clq3)).sum() ###!!
                free_e -= (Qt * (sp.log(Qt)+log_probs_mat_i[:, t])).sum()
                #entp += (Q_clq3 * sp.log(Q_clq3)).sum() -(Qt * sp.log(Qt)).sum()
                #Q_clq3_sum = 0
                #for k1 in range(K):
                #    for k2 in xrange(K):
                #        for k3 in xrange(K):
                #            Q_clq3[k1,k2, k3] = theta[k1,k2, k3] * pis[vp][i-1,t,k1] * pis[i][-1, t-1,k2] * emit_probs_mat[i,t,k3]*evidence[i,t,k3]
                #            Q_clq3_sum += Q_clq3[k1,k2, k3]
                #Q_clq3 /=  Q_clq3_sum
                #free_e -= (Q_clq3 * log_theta).sum() +(Q[i,t,:] * log_probs_mat_i[:, t] ).sum()
                #free_e += (Q_clq3 * sp.log(Q_clq3)).sum() ###!!
                #free_e -= (Q[i,t,:]*sp.log(Q[i,t,:])).sum()
                #for k1 in xrange(K):    
                #    free_e -= Q[i,t,k1] *(log_probs_mat_i[k1,t] + log(Q[i,t,k1]))
                #    for k2 in xrange(K):
                #        for k3 in xrange(K):
                #            free_e -= Q_clq3[k1,k2,k3] * (log_theta[k1,k2,k3] - log(Q_clq3[k1,k2,k3]))
            
    #print 'free energy:', free_e
    return free_e
コード例 #44
0
 def getBlock(self, idofs, jdofs):
     """ Input:  idofs = list of row indices
                 jdofs = list of col indices
         Output: matrix block K[idofs,jdofs] """
     return self.K[np.ix_(idofs, jdofs)].todense()
コード例 #45
0
    import pylab as pl
    pl.ion()

    N = 100; S = 10
    G = 1. * (sp.rand(N, S)<0.2)
    G-= G.mean(0); G/= G.std(0); G /= sp.sqrt(S)
    Ie = sp.rand(N)<0.5
    Cr = FreeFormCov(2, jitter=0.)
    C = CategoricalLR(Cr, G, Ie)
    C.setRandomParams()

    # calc WW using W
    WW = sp.dot(C.W(), C.W().T)
    # calc WW directly
    Cr_big = sp.zeros((N,N))
    Cr_big[sp.ix_(Ie, Ie)]  = Cr.K()[0,0]
    Cr_big[sp.ix_(Ie, ~Ie)] = Cr.K()[0,1]
    Cr_big[sp.ix_(~Ie, Ie)] = Cr.K()[1,0]
    Cr_big[sp.ix_(~Ie, ~Ie)]= Cr.K()[1,1]
    WW1 = Cr_big * sp.dot(G, G.T)
    print(('WW:', ((WW1-WW)**2).mean()))

    pdb.set_trace()
    
    for i in range(3):
        # calc D(WW)
        _WW_grad = sp.dot(C.W_grad_i(i), C.W().T)
        WW_grad = _WW_grad + _WW_grad.T
        # calc WW directly
        Cr_big = sp.zeros((N,N))
        Cr_big[sp.ix_(Ie, Ie)]  = Cr.K_grad_i(i)[0,0]
コード例 #46
0
    def _calc_observable(pb=0.0, kex=0.0, dw=0.0, r_nz=1.5, r_nxy=0.0,
                         dr_nxy=0.0, cs=0.0):
        """
        Calculate the intensity in presence of exchange after a CEST block assuming
        initial intensity of 1.0.

        Parameters
        ----------
        pb : float
            Fractional population of state B,
            0.0 for 0%, 1.0 for 100%
        kex : float
            Exchange rate between state A and B in /s.
        dw : float
            Chemical shift difference between states A and B in rad/s.
        r_nz : float
            Longitudinal relaxation rate of state {a,b} in /s.
        r_nxy : float
            Transverse relaxation rate of state a in /s.
        dr_nxy : float
            Transverse relaxation rate difference between states a and b in /s.
        cs : float
            Resonance position in rad/s.

        Returns
        -------
        out : float
            Intensity after the CEST block

        """

        if abs(b1_offset) >= 10000.0:

            magz_a = 1.0 - pb

        else:

            dw *= ppm_to_rads

            exchange_induced_shift, _ = correct_chemical_shift(
                pb=pb,
                kex=kex,
                dw=dw,
                r_ixy=r_nxy,
                dr_ixy=dr_nxy
            )

            wg = (
                (cs - carrier) * ppm_to_rads -
                exchange_induced_shift -
                w1_offset
            )

            magz_eq = sc.asarray([[1 - pb], [pb]])
            magz_a = 0.0

            for j, weight in multiplet:
                liouvillian = compute_liouvillian(
                    pb=pb,
                    kex=kex,
                    dw=dw,
                    r_nxy=r_nxy,
                    dr_nxy=dr_nxy,
                    r_nz=r_nz,
                    cs_offset=(wg + j),
                    w1=w1
                )

                s, vr = eig(liouvillian)
                vri = inv(vr)

                sl1 = [2, 5]
                sl2 = [i for i, w in enumerate(s.imag) if abs(w) < 1.0e-6]
                sl3 = [2]

                vri = vri[sc.ix_(sl2, sl1)].real
                t = diag(exp(s[sl2].real * time_t1))
                vr = vr[sc.ix_(sl3, sl2)].real

                magz_a += (
                    weight *
                    dot(dot(dot(vr, t), vri), magz_eq)[0, 0]
                )

        return magz_a
コード例 #47
0
ファイル: markov.py プロジェクト: jrnold/psc585
def kosaraju(P):
    """ Kosaraju's Algorithm for Strongly Connected Components
    
    Parameters
    ----------
    
    P : array, shape (n, n)
        Must be bool or integer.

    Returns
    ------------

    list : list
      Each element of the list is a component of the graph. Each
      component is a list of length two.  The first element in the
      component is a list of the states in that component.  The second
      element in the component is a `boolean` indicating whether the
      component is an ergodic set.

    Notes
    -----------
    
    The typical Kosaraju algorithm is modified to return the ergodic
    sets and transient set of a Markov chain transition matrix.
    
    """

    # shape is the dimension of P
    n = P.shape[0]

    def visit(i, F, E):
        """ depth-first-search
        
        Parameters
        -----------
        
        i : int
           State index.
        F : list
           First visit time
        E : list
           Last visit time

        Returns
        ------------

        F : list
           First visit times
        E : list
           Last visit times

        P and n are contained in the enclosing environment.
        """
        # Mark i as visited
        F[i] = 1

        # For all states
        for j in range(n):
            # if edge from i to j
            # and j has not been visited
            if P[i, j] and not F[j]:
                ## Add j to list of nodes in component
                E[-1][0].append(j)
                F, E = visit(j, F, E)
        return (F, E)

    ## P.T is transpose of P
    L = dfs(P.T)[1]
    ## Get order of L (yes, it's kind of round about)
    order = sorted(range(L.shape[0]),
                   key=lambda i: L[i],
                   reverse=True)
    ## List of length 0 where all elements are 0
    F = [0] * n
    ## a python list like a matlab cell
    E = []
    # I don't use a counter. Instead I append to the list
    # and always work with the last element in the list.
    for i in order:
        if not F[i]:
            # add new component to E with root i
            E.append([[i], False])
            # Find descendents of i
            F, E = visit(i, F, E)
            # states not in current component
            not_in_comp = [j for j in range(n) if j not in E[-1][0]]
            # If all edges to states outside component
            # are 0, then it is a strongly connected component
            if not_in_comp:
                outside_edges = sp.any(P[sp.ix_(E[-1][0], not_in_comp)] > 0)
                if not outside_edges:
                    E[-1][1] = True
            else:
                E[-1][1] = True
    return E
コード例 #48
0
ファイル: loopy_bp.py プロジェクト: lingjie/tree-hmm
def bp_update_msg_new(args):
    lmds, pis = args.lmds, args.pis
    vert_children = args.vert_children
    vert_parent = args.vert_parent
    I, T, L = args.X.shape
    #print I, T, L
    #print  pis[0].shape
    K = args.gamma.shape[0]
    emit_probs_mat = sp.exp(args.log_obs_mat)
    emit_probs_mat /= emit_probs_mat.max(axis=2).reshape(I, T, 1)
    theta, alpha, beta, gamma, emit_probs = args.theta, args.alpha, args.beta, args.gamma, args.emit_probs 
    
    lmds_prev = args.lmds_prev = copy.deepcopy(lmds) #sp.copy(lmds)
    pis_prev = args.pis_prev = copy.deepcopy(pis) #sp.copy(pis)
    if ~hasattr(args, 'evidence'):
        evidence  = args.evidence = evid_allchild(lmds_prev, vert_children)
    else:
        evidence = args.evidence 
        
    for i in range(I):
        lmds[i][:] = args.pseudocount
        pis[i][:] = args.pseudocount
    #breakpoint()
    for i in xrange(I):  # can be parallelized
        for t in xrange(T):
            #print i, T
            #print pis[i][0,t]
            #print pis_prev[i][0,t] 
            
            if i == 0:
                if t == 0:
                    # msg to vertical child
                    for id_vc, vc in enumerate(vert_children[i]):
                        pis[i][id_vc, t, :] += gamma * emit_probs_mat[0, t, :] * evidence[i,t] / lmds_prev[vc][0,t,:]
                    # msg to horizontal child
                    pis[i][-1, t,:] += gamma * emit_probs_mat[0, t, :] * evidence[i,t] / lmds_prev[i][1,t+1,:]
                    # no lambda meessage in this case

                else:  # different than t=0 case : now there is a parent, need to sum over; also there is lambda message
                    tmp1, tmp2 = sp.ix_(pis_prev[i][-1, t-1,:], emit_probs_mat[i,t,:] * evidence[i,t,:])
                    BEL = alpha * (tmp1 * tmp2)
                    # msg to (iterate over) vertical child
                    for id_vc, vc in enumerate(vert_children[i]):
                        pis[i][id_vc, t,:] += sp.dot(BEL, diag(1./lmds_prev[vc][0,t,:])).sum(axis=0)  
                    # msg to horizontal child
                    if t < T-1:
                        pis[i][-1, t,:] += sp.dot(BEL, diag(1./lmds_prev[i][1,t+1,:])).sum(axis=0)
                    else: 
                        pis[i][-1, t,:] += BEL.sum(axis=0)
                        
                    # msg to horizontal parent
                    lmds[i][1, t,:] += sp.dot(diag(1/pis_prev[i][-1, t-1,:]), BEL).sum(axis=1)
                    #breakpoint()

            else: # case (i>0)
                # msg to horizontal child (no vertical child, only 1 horizontal child, so no need to iterate over)
                vp = vert_parent[i]
                #evid_allchild = evidence(lmds_prev, i, t)
                if t==0:
                    pis[i][-1,t,:] = sp.dot(pis_prev[vp][i-1,t,:], beta) * emit_probs_mat[i,t,:] # the i-1 index is a hand-waiving way to do it, in principle should match the index of child species i in pis
                    # in general, should iterate over parents
                    lmds[i][0,t,:] = sp.dot(beta, emit_probs_mat[i,t,:]*evidence[i,t])
                    lmds[i][-1,t,:] = sp.ones(K) #  doesn't matter for there is no horizontal parent
                    
                    #tmp1, tmp2 = sp.ix_(pis_prev[vp][i-1,t,:], emit_probs_mat[i,t,:]*evidence[i,t,:])
                    #BEL = beta * (tmp1 * tmp2)
                    ## in general, should iterate over children
                    #pis[i][-1,t,:] += sp.dot(BEL, 1/lmds_prev[i][1,t+1,:]).sum(axis=0) 
                    ##pis[i][-1,t,:] += (BEL/lmds_prev[i][1,t+1,:].reshape(1,K)).sum(axis=0) 
                    ## in general, should iterate over parents
                    #lmds[i][0,t,:] += sp.dot(diag(1/pis_prev[vp][i-1, t,:]), BEL).sum(axis=1)  # The index i-1 is a hand-waiving way to do it, in principle should match the index of child species i in pis
                    ##lmds[i][0,t,:] += (BEL//pis_prev[vp][i-1, t,:].reshape(K,1)).sum(axis=1)
                    #lmds[i][1,t,:] += sp.ones(K) #  doesn't matter for there is no horizontal parent

                    
                else:
                    #tmp = sp.zeros(K)
                    #for k1 in range(K):
                    #    for k2 in range(K):
                    #        tmp += theta[k1, k2, :] * pis_prev[vp][i-1,t,k1] * pis_prev[i][-1, t-1, k2] * emit_probs_mat[i,t,:]
                    #pis[i][-1,t,:] = tmp
                    
                    tmp1, tmp2, tmp3 = sp.ix_(pis_prev[vp][i-1,t,:], pis_prev[i][-1, t-1, :], emit_probs_mat[i,t,:]*evidence[i,t,:])
                    BEL = theta* (tmp1* tmp2 * tmp3)
                    if t < T-1:
                        pis[i][-1,t,:] += (sp.dot(BEL, 1/lmds_prev[i][1,t+1,:]).sum(axis=0)).sum(axis=0)
                    else:
                        pis[i][-1, t,:] += (BEL.sum(axis=0)).sum(axis=0)

                    
                    #tmp_lmd1= sp.zeros(K)
                    #tmp_lmd2= sp.zeros(K)
                    #for k1 in range(K):
                    #    for k2 in range(K):
                    #        tmp_lmd1 += theta[:,k1, k2] * pis_prev[i][0, t-1, k2]* emit_probs_mat[i,t,k2] * evidence[i,t,k2]
                    #        tmp_lmd2 += theta[k1,:,k2] * pis_prev[vp][i-1, t, k2]* emit_probs_mat[i,t,k2] * evidence[i,t,k2]
                    #print tmp_lmd1
                    #print tmp_lmd2
                    
                    ##lmds[i][0,t,:] += tmp_lmd1
                    ##lmds[i][1,t,:] += tmp_lmd2
                    lmds[i][0,t,:] += ((BEL / pis_prev[vp][i-1, t, :].reshape(K,1,1)).sum(axis=1)).sum(axis=1)
                    lmds[i][1,t,:] += ((BEL / pis_prev[i][-1, t-1, :].reshape(1,K,1)).sum(axis=0)).sum(axis=1)     # t=T is not used
                    #print lmds[i][0,t,:]
                    #checked, same as above version (from line 353)
                    #breakpoint()
    normalize_msg(lmds, pis)
    args.evidence = evid_allchild(lmds, vert_children)