简体   繁体   English

Python:使用指数截止图绘制幂律函数

[英]Python: Plotting a power law function with exponential cutoff

I have a graph between 2 functions f and g. 我有两个函数f和g之间的图。 I know it follows a power law function with exponential cutoff. 我知道它遵循幂律函数并具有指数截止值。

f(x) = x**(-alpha)*e**(-lambda*x)

How do I find the value of exponent alpha? 如何找到指数alpha的值?

If you have sufficiently close x points (for example one every 0.1), you can try the following: 如果您有足够接近的x点(例如,每0.1个点),则可以尝试以下操作:

ln(f(x)) = -alpha ln(x) - lambda x
ln(f(x))' = - alpha / x - lambda

So depending on where you have your points: If you have a lot of points near 0, you can try: 因此,请根据您的位置:如果您有很多接近0的点,则可以尝试:

h(x) = x ln(f(x))' = -alpha - lambda x

So the limit of the function h when x goes to 0 is -alpha If you have large values of x, the function x -> ln(f(x))' tends toward lambda when x goes to infinity, so you can guess lambda and use pwdyson's expression. 因此,当x变为0时,函数h的极限为-alpha。如果x的值较大,则当x变为无穷大时,函数x-> ln(f(x))'趋向于lambda,因此您可以猜测为lambda并使用pwdyson的表达式。

If you don't have close x points, the numerical derivative will be very noisy, so I would try to guess lambda as the limit of -ln(f(x)/x for large x's... 如果您没有接近的x点,则数值导数会非常嘈杂,因此我会尝试将lambda视为-ln(f(x)/x对于大x的极限-ln(f(x)/x

If you don't have large values, but a large number of x's, you can try a minimization of 如果您没有大的值,但是有大量的x,则可以尝试将

sum_x_i (ln(y_i) + alpha ln(x_i) + lambda x_i) ^2

on both alpha and lambda (I guess It would be more precise than the initial expression)... It is a simple least square regression ( numpy.linalg.lstsq will do the job). 在alpha和lambda上(我想它会比初始表达式更精确)...这是一个简单的最小二乘回归( numpy.linalg.lstsq将完成此工作)。 So you have plenty of methods, the one to chose really depends on you inputs. 因此,您有很多方法,选择的方法实际上取决于您的输入。

The usual and general way of doing what you want is to perform a non-linear regression (even though, as noted in another response, it is possible to linearize the problem). 做您想要的事情的通常的通用方法是执行非线性回归(即使正如另一个答复中所述,也可以使问题线性化)。 Python can do this quite easily with the help of the SciPy package, which is used by many scientists. 在许多科学家使用的SciPy软件包的帮助下,Python可以很容易地做到这一点。

The routine you are looking for is its least-square optimization routine (scipy.optimize.leastsq). 您正在寻找的例程是最小二乘优化例程 (scipy.optimize.leastsq)。 Once you wrap your head around the way this general optimization procedure works (see the example), you will probably find many other opportunities to use it. 一旦掌握了这种通用优化过程的工作方式(请参见示例),您可能会发现许多使用它的机会。 Basically, you calculate the list of differences between your measurements and their ideal value f(x) , and you ask SciPy to find the parameters that make these differences as small as possible, so that your data fits the model as well as possible. 基本上,您可以计算出测量值与其理想值f(x)之间的差异的列表,并要求SciPy查找使这些差异尽可能小的参数,以使数据尽可能适合模型。 This then gives you the parameter you are looking for. 然后,这会为您提供所需的参数。

It sounds like you might be trying to fit a power-law to a distribution with an exponential cutoff at the low end due to incompleteness - but I may be reading too far into your problem. 听起来您可能由于不完整性而试图将幂定律拟合到具有指数截止值的低端分布,但是我可能对您的问题了解得太深了。

If that is the problem you're dealing with, this website (and accompanying publication) addresses the issue: http://tuvalu.santafe.edu/~aaronc/powerlaws/ . 如果这是您要解决的问题,则此网站(及其随附的出版物)解决了该问题: http : //tuvalu.santafe.edu/~aaronc/powerlaws/ I wrote the python implementation of the power-law fitter on that page; 我在那页上写了幂律钳工的python实现; it is linked from there. 它从那里链接。

If you know that the points follow this law exactly, then invert the equation and put in an x and its corresponding f(x) value: 如果您知道这些点完全遵循此定律,则将等式求逆,并输入x及其对应的f(x)值:

import math
alpha = -(lambda*x + math.log(f(x)))/math.log(x)

But the if the points do not exactly fit the equation you will need to do some sort of regression to determine alpha. 但是,如果这些点与方程式不完全吻合,则需要进行某种回归以确定alpha。

EDIT: Ok, so they don't fit exactly. 编辑:好的,所以它们不完全适合。 This is getting beyond a Python question, but there may be something in numpy that can handle it. 这已经超出了Python问题的范围,但是numpy中可能有一些可以解决的问题。 Here is a numpy linear regression recipe but your equation can't be rearranged into a linear form, so you'll have to look into non-linear regression. 这是一个numpy的线性回归方法,但是您的方程无法重新排列为线性形式,因此您必须研究非线性回归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM