Exemple #1
0
    def testConsistencyOfBases(self):
        m = 6
        sa = iisignature.rotinv2dprepare(m, "a")
        sk = iisignature.rotinv2dprepare(m, "k")
        ca = iisignature.rotinv2dcoeffs(sa)[-1]
        ck = iisignature.rotinv2dcoeffs(sk)[-1]

        #every row of ck should be in the span of the rows of ca
        #i.e.  every column of ck.T should be in the span of the columns of
        #ca.T
        #i.e.  there's a matrix b s.t.  ca.T b = ck.T
        residuals = lstsq(ca.T, ck.T)[1]
        self.assertLess(numpy.max(numpy.abs(residuals)), 0.000001)

        sq = iisignature.rotinv2dprepare(m, "q")
        cq = iisignature.rotinv2dcoeffs(sq)[-1]
        ss = iisignature.rotinv2dprepare(m, "s")
        cs = iisignature.rotinv2dcoeffs(ss)[-1]
        # every row of cs and cq should be in the span of the rows of ca
        residuals2 = lstsq(ca.T, cs.T)[1]
        self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001)
        residuals2 = lstsq(ca.T, cq.T)[1]
        self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001)

        self.assertEqual(cq.shape, cs.shape)

        #check that the invariants are linearly independent (not for k)
        for c, name in ((cs, "s"), (ca, "a"), (cq, "q")):
            self.assertEqual(numpy.linalg.matrix_rank(c), c.shape[0], name)

        #check that rows with nonzeros in evil columns are all before
        #rows with nonzeros in odious columns
        #print ((numpy.abs(ca)>0.00000001).astype("int8"))
        for c, name in ((cs, "s"), (ck, "k"), (ca, "a"), (cq, "q")):
            evilRows = []
            odiousRows = []
            for i in range(c.shape[0]):
                evil = 0
                odious = 0
                for j in range(c.shape[1]):
                    if numpy.abs(c[i, j]) > 0.00001:
                        if bin(j).count("1") % 2:
                            odious = odious + 1
                        else:
                            evil = evil + 1
                if evil > 0:
                    evilRows.append(i)
                if odious > 0:
                    odiousRows.append(i)
            #print (evilRows, odiousRows)
            self.assertLess(numpy.max(evil), numpy.min(odious),
                            "bad order of rows in " + name)
Exemple #2
0
    def testConsistencyOfBases(self):
        m = 6
        sa = iisignature.rotinv2dprepare(m,"a")
        sk = iisignature.rotinv2dprepare(m,"k")
        ca = iisignature.rotinv2dcoeffs(sa)[-1]
        ck = iisignature.rotinv2dcoeffs(sk)[-1]
        
        #every row of ck should be in the span of the rows of ca
        #i.e.  every column of ck.T should be in the span of the columns of
        #ca.T
        #i.e.  there's a matrix b s.t.  ca.T b = ck.T
        residuals = lstsq(ca.T,ck.T)[1]
        self.assertLess(numpy.max(numpy.abs(residuals)),0.000001)

        sq = iisignature.rotinv2dprepare(m, "q")
        cq = iisignature.rotinv2dcoeffs(sq)[-1]
        ss = iisignature.rotinv2dprepare(m, "s")
        cs = iisignature.rotinv2dcoeffs(ss)[-1]
        # every row of cs and cq should be in the span of the rows of ca
        residuals2 = lstsq(ca.T, cs.T)[1]
        self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001)
        residuals2 = lstsq(ca.T, cq.T)[1]
        self.assertLess(numpy.max(numpy.abs(residuals2)), 0.000001)

        self.assertEqual(cq.shape, cs.shape)

        #check that the invariants are linearly independent (not for k)
        for c, name in ((cs, "s"), (ca, "a"), (cq, "q")):
            self.assertEqual(numpy.linalg.matrix_rank(c),c.shape[0],name)

        #check that rows with nonzeros in evil columns are all before
        #rows with nonzeros in odious columns
        #print ((numpy.abs(ca)>0.00000001).astype("int8"))
        for c, name in ((cs, "s"), (ck, "k"), (ca, "a"), (cq, "q")):
            evilRows = []
            odiousRows = []
            for i in range(c.shape[0]):
                evil = 0
                odious = 0
                for j in range(c.shape[1]):
                    if numpy.abs(c[i, j]) > 0.00001:
                        if bin(j).count("1") % 2:
                            odious = odious + 1
                        else:
                            evil = evil + 1
                if evil > 0:
                    evilRows.append(i)
                if odious > 0:
                    odiousRows.append(i)
            #print (evilRows, odiousRows)
            self.assertLess(numpy.max(evil),numpy.min(odious),"bad order of rows in " + name)
Exemple #3
0
    def dotest(self,type):
        m = 8
        nPaths = 95
        nAngles = 348
        numpy.random.seed(775)
        s = iisignature.rotinv2dprepare(m,type)
        coeffs = iisignature.rotinv2dcoeffs(s)
        angles = numpy.random.uniform(0,math.pi * 2,size=nAngles + 1)
        angles[0] = 0
        rotationMatrices = [numpy.array([[math.cos(i),math.sin(i)],[-math.sin(i),math.cos(i)]]) for i in angles]
        paths = [numpy.random.uniform(-1,1,size=(32,2)) for i in range(nPaths)]
        samePathRotInvs = [iisignature.rotinv2d(numpy.dot(paths[0],mtx),s) for mtx in rotationMatrices]

        #check the length matches
        (length,) = samePathRotInvs[0].shape
        self.assertEqual(length,sum(i.shape[0] for i in coeffs))
        self.assertEqual(length,iisignature.rotinv2dlength(s))
        if type == "a":
            self.assertEqual(length,sumCentralBinomialCoefficient(m // 2))

        self.assertLess(length,nAngles)#sanity check on the test itself

        #check that the invariants are invariant
        if 0:
            print("\n",numpy.column_stack(samePathRotInvs[0:7]))
        for i in range(nAngles):
            if 0 and diff(samePathRotInvs[0],samePathRotInvs[1 + i]) > 0.01:
                print(i)
                print(samePathRotInvs[0] - samePathRotInvs[1 + i])
                print(diff(samePathRotInvs[0],samePathRotInvs[1 + i]))
            self.assertLess(diff(samePathRotInvs[0],samePathRotInvs[1 + i]),0.01)

        #check that the invariants match the coefficients
        if 1:
            sigLevel=iisignature.sig(paths[0],m)[iisignature.siglength(2,m-1):]
            lowerRotinvs = 0 if 2==m else iisignature.rotinv2dlength(iisignature.rotinv2dprepare(m-2,type))
            #print("\n",numpy.dot(coeffs[-1],sigLevel),"\n",samePathRotInvs[0][lowerRotinvs:])
            #print(numpy.dot(coeffs[-1],sigLevel)-samePathRotInvs[0][lowerRotinvs:])
            self.assertTrue(numpy.allclose(numpy.dot(coeffs[-1],sigLevel),samePathRotInvs[0][lowerRotinvs:],atol=0.000001))

        #check that we are not missing invariants
        if type == "a":
            #print("\nrotinvlength=",length,"
            #siglength=",iisignature.siglength(2,m))
            sigOffsets = []
            for path in paths:
                samePathSigs = [iisignature.sig(numpy.dot(path,mtx),m) for mtx in rotationMatrices[1:70]]
                samePathSigsOffsets = [i - samePathSigs[0] for i in samePathSigs[1:]]
                sigOffsets.extend(samePathSigsOffsets)
            #print(numpy.linalg.svd(numpy.row_stack(sigOffsets))[1])
            def split(a, dim, level):
                start = 0
                out = []
                for m in range(1,level + 1):
                    levelLength = dim ** m
                    out.append(a[:,start:(start + levelLength)])
                    start = start + levelLength
                assert(start == a.shape[1])
                return out
            allOffsets = numpy.row_stack(sigOffsets)
            #print (allOffsets.shape)
            splits = split(allOffsets,2,m)
            #print()
            rank_tolerance = 0.01 # this is hackish
            #print
                                             #([numpy.linalg.matrix_rank(i.astype("float64"),rank_tolerance)
                                                                              #for
                                                                              #i in splits])
                                                                              #print ([i.shape for i in splits])
                                                                              #print(numpy.linalg.svd(splits[-1])[1])

            #sanity check on the test
            self.assertLess(splits[-1].shape[1],splits[0].shape[0])
            totalUnspannedDimensions = sum(i.shape[1] - numpy.linalg.matrix_rank(i,rank_tolerance) for i in splits)
            self.assertEqual(totalUnspannedDimensions,length)

        if 0: #This doesn't work - the rank of the whole thing is less than
        #sigLength-totalUnspannedDimensions, which suggests that there are
        #inter-level dependencies,
        #even though the shuffle product dependencies aren't linear. 
        #I don't know why this is.
            sigLength = iisignature.siglength(2,m)
            numNonInvariant = numpy.linalg.matrix_rank(numpy.row_stack(sigOffsets))

            predictedNumberInvariant = sigLength - numNonInvariant
            print(sigLength,length,numNonInvariant)
            self.assertLess(sigLength,nAngles)
            self.assertEqual(predictedNumberInvariant,length)
Exemple #4
0
    def dotest(self,type):
        m = 8
        nPaths = 95
        nAngles = 348
        numpy.random.seed(775)
        s = iisignature.rotinv2dprepare(m,type)
        coeffs = iisignature.rotinv2dcoeffs(s)
        angles = numpy.random.uniform(0,math.pi * 2,size=nAngles + 1)
        angles[0] = 0
        rotationMatrices = [numpy.array([[math.cos(i),math.sin(i)],[-math.sin(i),math.cos(i)]]) for i in angles]
        paths = [numpy.random.uniform(-1,1,size=(32,2)) for i in range(nPaths)]
        samePathRotInvs = [iisignature.rotinv2d(numpy.dot(paths[0],mtx),s) for mtx in rotationMatrices]

        #check the length matches
        (length,) = samePathRotInvs[0].shape
        self.assertEqual(length,sum(i.shape[0] for i in coeffs))
        self.assertEqual(length,iisignature.rotinv2dlength(s))
        if type == "a":
            self.assertEqual(length,sumCentralBinomialCoefficient(m // 2))

        self.assertLess(length,nAngles)#sanity check on the test itself

        #check that the invariants are invariant
        if 0:
            print("\n",numpy.column_stack(samePathRotInvs[0:7]))
        for i in range(nAngles):
            if 0 and diff(samePathRotInvs[0],samePathRotInvs[1 + i]) > 0.01:
                print(i)
                print(samePathRotInvs[0] - samePathRotInvs[1 + i])
                print(diff(samePathRotInvs[0],samePathRotInvs[1 + i]))
            self.assertLess(diff(samePathRotInvs[0],samePathRotInvs[1 + i]),0.01)

        #check that the invariants match the coefficients
        if 1:
            sigLevel=iisignature.sig(paths[0],m)[iisignature.siglength(2,m-1):]
            lowerRotinvs = 0 if 2==m else iisignature.rotinv2dlength(iisignature.rotinv2dprepare(m-2,type))
            #print("\n",numpy.dot(coeffs[-1],sigLevel),"\n",samePathRotInvs[0][lowerRotinvs:])
            #print(numpy.dot(coeffs[-1],sigLevel)-samePathRotInvs[0][lowerRotinvs:])
            self.assertTrue(numpy.allclose(numpy.dot(coeffs[-1],sigLevel),samePathRotInvs[0][lowerRotinvs:],atol=0.000001))

        #check that we are not missing invariants
        if type == "a":
            #print("\nrotinvlength=",length,"
            #siglength=",iisignature.siglength(2,m))
            sigOffsets = []
            for path in paths:
                samePathSigs = [iisignature.sig(numpy.dot(path,mtx),m) for mtx in rotationMatrices[1:70]]
                samePathSigsOffsets = [i - samePathSigs[0] for i in samePathSigs[1:]]
                sigOffsets.extend(samePathSigsOffsets)
            #print(numpy.linalg.svd(numpy.row_stack(sigOffsets))[1])
            def split(a, dim, level):
                start = 0
                out = []
                for m in range(1,level + 1):
                    levelLength = dim ** m
                    out.append(a[:,start:(start + levelLength)])
                    start = start + levelLength
                assert(start == a.shape[1])
                return out
            allOffsets = numpy.row_stack(sigOffsets)
            #print (allOffsets.shape)
            splits = split(allOffsets,2,m)
            #print()
            rank_tolerance = 0.01 # this is hackish
            #print
                                             #([numpy.linalg.matrix_rank(i.astype("float64"),rank_tolerance)
                                                                              #for
                                                                              #i in splits])
                                                                              #print ([i.shape for i in splits])
                                                                              #print(numpy.linalg.svd(splits[-1])[1])

            #sanity check on the test
            self.assertLess(splits[-1].shape[1],splits[0].shape[0])
            totalUnspannedDimensions = sum(i.shape[1] - numpy.linalg.matrix_rank(i,rank_tolerance) for i in splits)
            self.assertEqual(totalUnspannedDimensions,length)

        if 0: #This doesn't work - the rank of the whole thing is less than
        #sigLength-totalUnspannedDimensions, which suggests that there are
        #inter-level dependencies,
        #even though the shuffle product dependencies aren't linear. 
        #I don't know why this is.
            sigLength = iisignature.siglength(2,m)
            numNonInvariant = numpy.linalg.matrix_rank(numpy.row_stack(sigOffsets))

            predictedNumberInvariant = sigLength - numNonInvariant
            print(sigLength,length,numNonInvariant)
            self.assertLess(sigLength,nAngles)
            self.assertEqual(predictedNumberInvariant,length)