python - 如何使用 python 的 scipy.stats.levy 限制步长?

标签 python random scipy

我正在尝试使用 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()

这是一个示例输出图,您可以清楚地看到发生了很少但非常大的步骤:

2D Lévy flight (walk)

所以,我的问题是限制步长的正确方法是什么? (使用 scale 选项并没有真正的帮助,因为它只会缩小一切)

最佳答案

这实际上是它应有的行为方式。 expected value of a Levy distribution is ∞ , 极其“肥尾”。如果那是您正在使用的分布,那么您将获得相当数量的非常大的值。如果你截断它们,它就不是 Levy 分布。所以不,在使用 Levy 分布时没有适当的方法来限制步长。如果您获得的结果与观察数据或您的直觉不匹配,您的替代方法是对步长使用不同的分布。

关于python - 如何使用 python 的 scipy.stats.levy 限制步长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002508/

相关文章:

python - 从一副纸牌中生成 52 张随机纸牌而不重复

python - 从列表中随机选择子例程

python - 我想从txt文件中提取x和y值

Python:如何比较多个作为参数的字符串的长度?

python - 使用 "Face Recognition"(lib) 来识别 10 万张现有面孔中的新面孔?

c++ - 带随 secret 钥生成的双重 AES 加密

python - 数据功能在其域的一小部分上的多重集成 - 准确性和效率

使用 Scikit-learn 进行拟合时出现 Python MemoryError

python - reset_index() 到 pandas groupby() 之后的原始列索引?

python - 如何根据条件拆分列表?