(self, alpha=0.85, maxIterations=100, convergenceThres=1.0e-6, projection='scaled', includeSubPaths=True)
return first_order_PR
Calculates the PageRank of higher-order nodes based on a 
        power iteration. If the order of the higher-order network is larger than one,
        the PageRank calculated based on the higher-order
        topology will automatically be projected back to first-order 
        nodes.

        @param projection: Indicates how the projection from k-th-order nodes (v1, v2, ... , v{k-1})
            shall be performed. For the method 'all', the pagerank value of the higher-order node 
            will be added to *all* first-order nodes on the path corresponding to the higher-order node. For 
            the method 'last', the PR value of the higher-order node will only be assigned to *last* 
            first-order node v{k-1}. For the method 'scaled' (default), the PageRank of higher-order 
            nodes will be assigned proportionally to first-order nodes, i.e. each of the three nodes in the 
            third-order node (a,b,c) will receive one third of the PageRank of (a,b,c).
        @param includeSubpaths: whether or not to use subpath statistics in the PageRank calculation
Calculates the PageRank of higher-order nodes based on a 
        power iteration. If the order of the higher-order network is larger than one,
        the PageRank calculated based on the higher-order
        topology will automatically be projected back to first-order 
        nodes.
Calculates the PageRank of higher - order nodes based on a power iteration . If the order of the higher - order network is larger than one the PageRank calculated based on the higher - order topology will automatically be projected back to first - order nodes .
def PageRank(self, alpha=0.85, maxIterations=100, convergenceThres=1.0e-6, projection='scaled', includeSubPaths=True):
        """
        Calculates the PageRank of higher-order nodes based on a 
        power iteration. If the order of the higher-order network is larger than one,
        the PageRank calculated based on the higher-order
        topology will automatically be projected back to first-order 
        nodes.

        @param projection: Indicates how the projection from k-th-order nodes (v1, v2, ... , v{k-1})
            shall be performed. For the method 'all', the pagerank value of the higher-order node 
            will be added to *all* first-order nodes on the path corresponding to the higher-order node. For 
            the method 'last', the PR value of the higher-order node will only be assigned to *last* 
            first-order node v{k-1}. For the method 'scaled' (default), the PageRank of higher-order 
            nodes will be assigned proportionally to first-order nodes, i.e. each of the three nodes in the 
            third-order node (a,b,c) will receive one third of the PageRank of (a,b,c).
        @param includeSubpaths: whether or not to use subpath statistics in the PageRank calculation
        """

        assert projection == 'all' or projection == 'last' or projection == 'first' or projection == 'scaled', 'Invalid projection method'

        Log.add('Calculating PageRank in ' + str(self.order) + '-th order network...', Severity.INFO)

        higher_order_PR = _co.defaultdict( lambda: 0 )

        n = float(len(self.nodes))

        assert n>0, "Number of nodes is zero"

        # entries A[s,t] give directed link s -> t
        A = self.getAdjacencyMatrix(includeSubPaths=includeSubPaths, weighted=False, transposed=False)

        # sum of outgoing node degrees
        row_sums = _sp.array(A.sum(axis=1)).flatten()
        
        row_sums[row_sums != 0] = 1.0 / row_sums[row_sums != 0]
        d = _sp.where(row_sums == 0)[0]

        Q = _sparse.spdiags(row_sums.T, 0, *A.shape, format='csr')
        A = Q * A

        p = _sp.array([1.0 / n] * int(n))
        pr = p
       
        # Power iteration
        for i in range(maxIterations):
            last = pr
            pr = alpha * (pr * A + sum(pr[d]) * p) + (1 - alpha) * p

            if _sp.absolute(pr - last).sum() < n * convergenceThres:
                higher_order_PR = dict(zip(self.nodes, map(float, pr)))
                break

        if self.order == 1:
            return higher_order_PR

        # project PageRank of higher-order nodes to first-order network
        first_order_PR = _co.defaultdict( lambda: 0.0 )

        # sum PageRank values based on higher-order nodes 
        # and normalize the result
        for v in self.nodes:
            # turns node a-b-c in path tuple (a,b,c)
            p = self.HigherOrderNodeToPath(v)
            if projection == 'all':
                # assign PR of higher-order node to all first-order nodes
                for x in p:                    
                    first_order_PR[x] += higher_order_PR[v]
            elif projection == 'scaled':
                for x in p:
                    # each node on e.g. a 4-th-order path a-b-c-d receives one fourth of the 
                    # PageRank value, to ensure that the resulting first-order PageRank sums 
                    # to one
                    first_order_PR[x] += higher_order_PR[v] / float( len(p) )
            elif projection == 'last':
                # assign PR of higher-order node to last first-order node
                first_order_PR[p[-1]] += higher_order_PR[v]
            elif projection == 'first':
                # assign PR of higher-order node to last first-order node
                first_order_PR[p[0]] += higher_order_PR[v]

        # for projection method 'scaled', the values sum to one anyway
        if projection != 'scaled':
            for v in first_order_PR:
                first_order_PR[v] /= sum(first_order_PR.values())

        # assign centrality zero to nodes not occurring in higher-order PR
        nodes = self.paths.getNodes()
        for v in nodes:
            first_order_PR[v] += 0

        Log.add('finished.', Severity.INFO)

        return first_order_PR
def PageRank ( self , alpha = 0.85 , maxIterations = 100 , convergenceThres = 1.0e-6 , projection = 'scaled' , includeSubPaths = True ) : assert projection == 'all' or projection == 'last' or projection == 'first' or projection == 'scaled' , 'Invalid projection method' Log . add ( 'Calculating PageRank in ' + str ( self . order ) + '-th order network...' , Severity . INFO ) higher_order_PR = _co . defaultdict ( lambda : 0 ) n = float ( len ( self . nodes ) ) assert n > 0 , "Number of nodes is zero" # entries A[s,t] give directed link s -> t A = self . getAdjacencyMatrix ( includeSubPaths = includeSubPaths , weighted = False , transposed = False ) # sum of outgoing node degrees row_sums = _sp . array ( A . sum ( axis = 1 ) ) . flatten ( ) row_sums [ row_sums != 0 ] = 1.0 / row_sums [ row_sums != 0 ] d = _sp . where ( row_sums == 0 ) [ 0 ] Q = _sparse . spdiags ( row_sums . T , 0 , * A . shape , format = 'csr' ) A = Q * A p = _sp . array ( [ 1.0 / n ] * int ( n ) ) pr = p # Power iteration for i in range ( maxIterations ) : last = pr pr = alpha * ( pr * A + sum ( pr [ d ] ) * p ) + ( 1 - alpha ) * p if _sp . absolute ( pr - last ) . sum ( ) < n * convergenceThres : higher_order_PR = dict ( zip ( self . nodes , map ( float , pr ) ) ) break if self . order == 1 : return higher_order_PR # project PageRank of higher-order nodes to first-order network first_order_PR = _co . defaultdict ( lambda : 0.0 ) # sum PageRank values based on higher-order nodes  # and normalize the result for v in self . nodes : # turns node a-b-c in path tuple (a,b,c) p = self . HigherOrderNodeToPath ( v ) if projection == 'all' : # assign PR of higher-order node to all first-order nodes for x in p : first_order_PR [ x ] += higher_order_PR [ v ] elif projection == 'scaled' : for x in p : # each node on e.g. a 4-th-order path a-b-c-d receives one fourth of the  # PageRank value, to ensure that the resulting first-order PageRank sums  # to one first_order_PR [ x ] += higher_order_PR [ v ] / float ( len ( p ) ) elif projection == 'last' : # assign PR of higher-order node to last first-order node first_order_PR [ p [ - 1 ] ] += higher_order_PR [ v ] elif projection == 'first' : # assign PR of higher-order node to last first-order node first_order_PR [ p [ 0 ] ] += higher_order_PR [ v ] # for projection method 'scaled', the values sum to one anyway if projection != 'scaled' : for v in first_order_PR : first_order_PR [ v ] /= sum ( first_order_PR . values ( ) ) # assign centrality zero to nodes not occurring in higher-order PR nodes = self . paths . getNodes ( ) for v in nodes : first_order_PR [ v ] += 0 Log . add ( 'finished.' , Severity . INFO ) return first_order_PR