Exemplo n.º 1
0
def testMeander(samples,
                k=5,
                permutations=200,
                seed=123,
                tests=100,
                sig=0.05,
                k_perm=None,
                corrCheck=False):
    np.random.seed(seed)

    ff = FisherCI()
    knn = KnnEstimator(k=k,
                       permutations=permutations,
                       sig=sig,
                       corrCheck=corrCheck,
                       k_perm=k_perm)
    #knn = KnnEstimator(k = k, permutations = permutations, sig = sig)
    #maxS = np.max(samples)

    XIIYf = 0
    XIIYZf = 0

    XIIYknn = 0
    XIIYZknn = 0

    for ii in range(tests):

        X, Y, Z = createMeanderData(samples)
        #X= scale(X)
        #Y = scale(Y)
        #Z = scale(Z)

        indepf, depf = ff.independent(X, Y)
        indepk, depk = knn.independent(X, Y)

        if (indepf == False):
            XIIYf += 1
        if (indepk == False):
            XIIYknn += 1

        indepf, depf = ff.independent(X, Y, Z)
        indepk, depk = knn.independent(X, Y, Z)

        if (indepf == True):
            XIIYZf += 1
        if (indepk == True):
            XIIYZknn += 1
    print("Sample size:", samples)
    print("        Reject X || Y      Accept X || Y | Z")
    print("Fisher    ", XIIYf / tests, "           ", XIIYZf / tests)
    print("kNN       ", XIIYknn / tests, "           ", XIIYZknn / tests)
Exemplo n.º 2
0
class MBAlgorithms:
    def __init__(self, X, algorithm, estimator=None):
        self.cache = dict()
        self.nTests = 0
        self.X = X

        if estimator is None:
            self.estimator = KnnEstimator()
        else:
            self.estimator = estimator
        self.algorithm = algorithm

    def clearCache(self):
        self.cache = dict()

    def resetTestCounter(self):
        self.nTests = 0

    def _dependence(self, var_inx, y, MB):
        yi = self.X[:, [y]]
        x = self.X[:, [var_inx]]

        if len(MB) == 0:
            z = None
        else:
            z = self.X[:, list(MB)]  #conditioning set

        inCache, key = self._isCached(var_inx, y, MB)

        if inCache:
            estMI = self.cache[key][1]
        else:
            estMI = self.estimator.dependence(x, yi, z)

        return estMI

    def _doIndepTest(self, var_inx, y, MB):

        if MB is None:
            z = None
        elif len(MB) == 0:
            z = None
        else:
            z = self.X[:, list(MB)]  #conditioning set

        assert np.isscalar(var_inx) and np.isscalar(y)

        yi = self.X[:, [y]]
        x = self.X[:, [var_inx]]

        inCache, key = self._isCached(var_inx, y, MB)

        if inCache:
            indep = self.cache[key][0]
        else:
            indep, estMI = self.estimator.independent(x, yi, z)
            #print("Testing", var_inx, y, "given", MB)
            #print("         ", indep, estMI)
            self._putInCache(key, (indep, estMI))
            self.nTests += 1

        return indep

    def _isCached(self, x, y, z):
        key = self._returnKey(x, y, z)

        if (key in self.cache):
            #print("cache used", x, y ,z)
            return (True, key)
        else:
            return (False, key)

    # key is tuple containing tuples (x,y) (sorted) and (z) (sorted)
    def _returnKey(self, x, y, z):

        xy = [x, y]
        xy.sort()

        zz = list(z)
        zz.sort()

        return (tuple(xy), tuple(zz))

    def _putInCache(self, key, value):
        self.cache[key] = value