Solving Power Law Distribution in Python

此生再无相见时 提交于 2021-02-07 10:27:05

问题


I have data that closely resembles a power law distribution. Using Python, I want to approximate the data by solving two equations in the form:

y is the y axis data. In Python it would be data[i]. x would be i + 1. It follows that we get two equations with two unknown variables at the first data index and at a "random" 2nd one somewhere else in the data:


The problem comes down to solving just

due to mathematical simplification. I don't know how to solve an equation like this using libraries like numpy.linalg.solve. How do I find the value of a using Python?


回答1:


Alright, I got it.

import math

def get_power_law_variables(data):
    c = data[0]
    middle_index = len(data) / 2
    division = float(data[middle_index]) / c
    logarithm_base = middle_index + 1
    a = math.log(division, logarithm_base)
    return c, a

# Example usage
data = range(50, 150)
c, a = get_power_law_variables(data)
print c, a


来源:https://stackoverflow.com/questions/36892637/solving-power-law-distribution-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!