def plfit_lsq ( x , y ) : n = len ( x ) btop = n * ( log ( x ) * log ( y ) ) . sum ( ) - ( log ( x ) ) . sum ( ) * ( log ( y ) ) . sum ( ) bbottom = n * ( log ( x ) ** 2 ) . sum ( ) - ( log ( x ) . sum ( ) ) ** 2 b = btop / bbottom a = ( log ( y ) . sum ( ) - b * log ( x ) . sum ( ) ) / n A = exp ( a ) return A , b
log fit unbalanced python
Returns A and B in y = Ax^B http : // mathworld . wolfram . com / LeastSquaresFittingPowerLaw . html
def plfit_lsq(x,y):
    """
    Returns A and B in y=Ax^B
    http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
    """
    n = len(x)
    btop = n * (log(x)*log(y)).sum() - (log(x)).sum()*(log(y)).sum()
    bbottom = n*(log(x)**2).sum() - (log(x).sum())**2
    b = btop / bbottom
    a = ( log(y).sum() - b * log(x).sum() ) / n

    A = exp(a)
    return A,b