示例#1
0
文件: pc.py 项目: pliz/gunfolds
def dpc(data, pval=0.05):
    n = data.shape[0]
    # stack the data: first n rows is t-1 slice, the next n are slice t
    data = np.asarray(np.r_[data[:,:-1],data[:,1:]])

    def tetrad_cind_(y,x,condset=[], alpha=0.01, shift=0):
        y = data[n+int(y)-1,:]
        x = data[shift+int(x)-1,:]
        if condset:
            X  = data[condset,:]
            ry, rx = residuals_(y,x,X)
        else:
            ry, rx = [y,x]
        return independent_(ry, rx, alpha = alpha)

    def cind_(y,x, condset=[], pval=pval, shift=0):
        yd = data[n+int(y)-1,:].T
        X  = data[[shift+int(x)-1]+condset,:].T
        return independent(yd, X, pval=pval)

    def cindependent(y, x, counter, parents=[], pval=pval):
        for S in [j for j in iter.combinations(parents,counter)]:
            if cind_(y, x, condset=list(S), pval=pval): return True
            #if tetrad_cind_(x, y, condset=list(S), alpha=pval): return True                
        return False

    def bindependent(y, x, parents=[], pval=pval):
        return cind_(y, x, condset=parents, pval=pval, shift=n)
        #return tetrad_cind_(y, x, condset=parents, alpha=pval, shift=n)

    def prune(elist, mask, g):
        for e in mask:
            g[e[0]][e[1]].remove((0,1))
            elist.remove(e)
        gk.clean_leaf_nodes(g)

    g  = gk.superclique(n)
    gtr= bfu.gtranspose(g)

    el = gk.edgelist(g)
    for counter in range(n):
        to_remove = []
        for e in el:
            ppp = [int(k)-1 for k in gtr[e[1]] if k != e[0]]
            if counter <= len(ppp):
                if cindependent(e[1], e[0], counter, parents=ppp, pval=pval):
                    to_remove.append(e)
                    gtr[e[1]].pop(e[0],None)
        prune(el, to_remove, g)

    bel = [map(lambda k: str(k+1), x) for x in iter.combinations(range(n),2)]
    for e in bel:
        ppp = list(set(gtr[e[0]].keys()) | set(gtr[e[1]].keys()))
        ppp = map(lambda x: int(x)-1, ppp)
        if bindependent(e[0], e[1], parents=ppp, pval=pval):
            g[e[0]][e[1]].remove((2,0))
            g[e[1]][e[0]].remove((2,0))
    gk.clean_leaf_nodes(g)

    return g
示例#2
0
def reverse(H, verbose=True, capsize=1000):
    n = len(H)
    s = set()

    g      = gk.superclique(n)
    sloops = set(allsloops(n))

    ds = {bfu.g2num(g): sloops}

    if verbose: print '%3s'%'i'+'%10s'%' graphs'
    i=0
    while ds:
        ds, ss = del_loop(ds, H, iter=i,
                          verbose=verbose,
                          capsize=capsize)
        s = s | ss
        i += 1
        if capsize <= len(s): break

    return s
示例#3
0
文件: pc.py 项目: sergeyplis/gunfolds
def dpc(data, pval=0.05):
    n = data.shape[0]
    # stack the data: first n rows is t-1 slice, the next n are slice t
    data = np.asarray(np.r_[data[:, :-1], data[:, 1:]])

    def tetrad_cind_(y, x, condset=[], alpha=0.01, shift=0):
        y = data[n + int(y) - 1, :]
        x = data[shift + int(x) - 1, :]
        if condset:
            X = data[condset, :]
            ry, rx = residuals_(y, x, X)
        else:
            ry, rx = [y, x]
        return independent_(ry, rx, alpha=alpha)

    def cind_(y, x, condset=[], pval=pval, shift=0):
        yd = data[n + int(y) - 1, :].T
        X = data[[shift + int(x) - 1] + condset, :].T
        return independent(yd, X, pval=pval)

    def cindependent(y, x, counter, parents=[], pval=pval):
        for S in [j for j in iter.combinations(parents, counter)]:
            if cind_(y, x, condset=list(S), pval=pval): return True
            #if tetrad_cind_(x, y, condset=list(S), alpha=pval): return True
        return False

    def bindependent(y, x, parents=[], pval=pval):
        return cind_(y, x, condset=parents, pval=pval, shift=n)
        #return tetrad_cind_(y, x, condset=parents, alpha=pval, shift=n)

    def prune(elist, mask, g):
        for e in mask:
            g[e[0]][e[1]].remove((0, 1))
            elist.remove(e)
        gk.clean_leaf_nodes(g)

    g = gk.superclique(n)
    gtr = bfu.gtranspose(g)

    el = gk.edgelist(g)
    for counter in range(n):
        to_remove = []
        for e in el:
            ppp = [int(k) - 1 for k in gtr[e[1]] if k != e[0]]
            if counter <= len(ppp):
                if cindependent(e[1], e[0], counter, parents=ppp, pval=pval):
                    to_remove.append(e)
                    gtr[e[1]].pop(e[0], None)
        prune(el, to_remove, g)

    bel = [
        map(lambda k: str(k + 1), x) for x in iter.combinations(range(n), 2)
    ]
    for e in bel:
        ppp = list(set(gtr[e[0]].keys()) | set(gtr[e[1]].keys()))
        ppp = map(lambda x: int(x) - 1, ppp)
        if bindependent(e[0], e[1], parents=ppp, pval=pval):
            g[e[0]][e[1]].remove((2, 0))
            g[e[1]][e[0]].remove((2, 0))
    gk.clean_leaf_nodes(g)

    return g