def testFitDiscretePowerLaw2(self):
        try:
            import networkx
        except ImportError:
            logging.debug("Networkx not found, can't run test")
            return

        nxGraph = networkx.barabasi_albert_graph(1000, 2)
        graph = SparseGraph.fromNetworkXGraph(nxGraph)
        degreeSeq = graph.outDegreeSequence()

        output = Util.fitDiscretePowerLaw(degreeSeq)
    def testFitDiscretePowerLaw(self):
        #Test with small x
        x = numpy.array([5])
        ks, alpha2, xmin = Util.fitDiscretePowerLaw(x)

        self.assertEquals(ks, -1)
        self.assertEquals(alpha2, -1)

        x = numpy.array([5, 2])
        ks, alpha2, xmin = Util.fitDiscretePowerLaw(x)

        #Test with a large vector x 
        alpha = 2.5
        exponent = (1/(alpha-1))
        numPoints = 15000
        x = 10*numpy.random.rand(numPoints)**-exponent
        x = numpy.array(numpy.round(x), numpy.int)
        x = x[x<=500]
        x = x[x>=1]

        xmins = numpy.arange(1, 15)

        ks, alpha2, xmin = Util.fitDiscretePowerLaw(x, xmins)
        self.assertAlmostEqual(alpha, alpha2, places=1)
Exemple #3
0
    def fitPowerLaw(self):
        """
        Fits the out-degree probabilities of this graph using the power law
        p_d ~ d^-gamma. The value of xmin is the point to start taking examples.

        :returns alpha: The power law exponent.
        :returns ks: A fit of the power law curve to the data using KS.
        :returns xmin: The minimum value of x.
        """
        if self.getNumEdges() == 0:
            return 0,0,0

        logging.warn("Use with caution: fitPowerLaw may not be stable")
        degreeSeq = self.outDegreeSequence()
        degreeMax = numpy.max(degreeSeq)
        xmins = numpy.arange(1, numpy.minimum(20, degreeMax))
        ks, alpha, xmin = Util.fitDiscretePowerLaw(degreeSeq, xmins)

        return alpha, ks, xmin