我正在尝试使用 scipy.stats.levy
模拟 Levy 行走.它工作正常,除了有几个步骤非常大,我想知道是否有适当的方法来限制步长。
这是我使用的代码:
import numpy as np
from scipy.stats import uniform
from scipy.stats import levy
import matplotlib.pyplot as plt
def levy_walk( n ):
# uniformly distributed angles
angle = uniform.rvs( size=(n,), loc=.0, scale=2.*np.pi )
# levy distributed step length
r = levy.rvs( size=n )
# x and y coordinates (position added to previous coordinate --> cum. sum)
x = np.cumsum( r * np.cos(angle) )
y = np.cumsum( r * np.sin(angle) )
return np.array( (x, y, r, angle) )
# number of steps to simulate
n = 500
# get levy walk (strictly speaking, it seems to be a flight)
foo = levy_walk( n )
# initialize figure
fig = plt.figure( figsize=(14,6) )
# plot 2D random walk with Levy stepsize
ax1 = fig.add_subplot( 1,2,1 )
ax1.plot( foo[0,:], foo[1,:] )
ax1.set_xlabel( 'x' )
ax1.set_ylabel( 'y' )
ax1.set_title( '2D Levy flight' )
# plot histogram
ax2 = fig.add_subplot( 1,2,2 )
num_bins = n/10
ax2.hist( foo[2,:], bins=n/10 )
ax2.set_yscale( 'log' )
ax2.set_xlabel( 'stepsize' )
ax2.set_title( 'histogram' )
plt.show()
这是一个示例输出图,您可以清楚地看到发生了很少但非常大的步骤:
所以,我的问题是限制步长的正确方法是什么? (使用 scale
选项并没有真正的帮助,因为它只会缩小一切)
最佳答案
这实际上是它应有的行为方式。 expected value of a Levy distribution is ∞ , 极其“肥尾”。如果那是您正在使用的分布,那么您将获得相当数量的非常大的值。如果你截断它们,它就不是 Levy 分布。所以不,在使用 Levy 分布时没有适当的方法来限制步长。如果您获得的结果与观察数据或您的直觉不匹配,您的替代方法是对步长使用不同的分布。
关于python - 如何使用 python 的 scipy.stats.levy 限制步长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002508/