2

这是仅使用 numpy 数组的随机游走问题的一个版本。为了找到位置被重新访问超过 500 步的时间,我们必须将位置的排序数组与其偏移量进行比较,记录它接近 0 的时间,然后增加偏移量。

到目前为止,这是我的代码,问题出在“while”循环中,我试图将位置重新访问为“zeroArray”中的元素的最终次数

当我运行它时,我得到一个索引错误,没有记录任何结果,并且即使循环停止的布尔表达式已经改变,计数器也已经迭代了很多次。

编辑:如何使用 numpy 数组查找重复位置:1)将最终位置数组排序为升序。2)比较具有增加偏移量的切片,只要您在该偏移量处找到 0.001m 内的位置,即您将位置与相邻位置(偏移量 1)进行比较。您可能会发现 18 个案例,其中邻居计算出在两个空间中您可能只会找到 2 个案例。在三个空格处,您会发现 0 停止。

import numpy as np
import random

MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]
NUM_STEPS = 500         # number of steps in a random walk []
NUM_WALKS = 10        # number of random walks in a run []
TOLERANCE = 0.001    # separation of points considered the same [m]
STEP_TO_RECORD_1 = 100 # first step to record and analyze []
STEP_TO_RECORD_2 = 500 # 2nd step to record and analyze []
random.seed(12345)

#......................................................................distance
def distance(posA, posB) :
    """Distance between two positions"""
    return np.abs(posA - posB)


#...............................................................initialPosition
def initialPosition() :
    """Initial position of walker at the start of a random walk"""
    return 0.0

def genPositions(nSteps, maxStep) :
    """Return the new position after a random step between -maxStep and
    maxStep, given the previous position"""
    genArray1 = (maxStep - (-maxStep))*(np.random.random(nSteps+1)) + (-maxStep)
    genArray1[0]=initialPosition()    
    return np.cumsum(genArray1)

oneStep = np.zeros(NUM_WALKS)
fiveStep = np.zeros(NUM_WALKS)
zeroStep = np.zeros(NUM_WALKS)
walkArray = np.zeros(NUM_WALKS)
counter = 1
hitcounter = 0
zerocounter = 0
keepchecking = bool(1)

for ii in range(NUM_WALKS):
    position = (genPositions(NUM_STEPS, MAX_STEP_SIZE))   
    oneStep[ii] = position[100]
    fiveStep[ii] = position[-1]
    zeroArray = np.sort(position)
    while keepchecking == bool(1):
        zerocounter = 0
        for jj in range(len(zeroArray)):
            hitcounter = 0
            if distance(zeroArray[jj+counter], zeroArray[jj]) <= TOLERANCE:
               hitcounter +=1
            zerocounter += hitcounter
            counter +=1
            if hitcounter == 0:
                keepchecking = bool(0)
    zeroStep[ii] = zerocounter

谢谢你的帮助,

4

1 回答 1

2

您可以将您的位置向量与自身进行直接比较:

deltas = np.abs(position[None, :] - position[:, None])

现在,是 step和 stepdeltas[i, j]位置之间的距离。你可以得到你的命中:ij

hits = deltas <= TOLERANCE

要获得成对的平仓头寸,您可以执行以下操作:

row, col = np.nonzero(hits)
idx = row < col # get rid of the diagonal and the upper triangular part
row = row[idx]
col = col[idx]

举个例子:

>>> position = genPositions(NUM_STEPS, MAX_STEP_SIZE)
>>> row, col = np.nonzero(np.abs(position[None, :] -
...                              position[:, None]) < TOLERANCE)
>>> idx = row < col
>>> row= row[idx]
>>> col = col[idx]
>>> row
array([ 35,  40, 112, 162, 165, 166, 180, 182, 200, 233, 234, 252, 253,
       320, 323, 325, 354, 355, 385, 432, 443, 451], dtype=int64)
>>> col
array([ 64,  78, 115, 240, 392, 246, 334, 430, 463, 366, 413, 401, 315,
       380, 365, 348, 438, 435, 401, 483, 473, 492], dtype=int64)

>>> for j in xrange(len(row)) :
...     print '{0}, {1} --> {2}, {3}'.format(row[j], col[j], position[row[j]],
...                                          position[col[j]]) 
... 
35, 64 --> 2.56179226445, 2.56275159205
40, 78 --> 2.97310455111, 2.97247314695
112, 115 --> 3.40413767436, 3.40420856824
...
432, 483 --> 10.2560778101, 10.2556475598
443, 473 --> 10.7463713139, 10.7460626764
451, 492 --> 12.3804383241, 12.3805940238
于 2013-03-21T00:27:36.487 回答