1

那是我第一次来到 Stack,也是我接触 Python 的第一天。我正在处理 HAR-RV 模型,试图运行这个方程,但根本没有成功将我的操作存储在数组上

这是我要计算的内容:

r_[t,i] = Y_[t,i] - Y_[t,i-1]

https://cdn1.imggmi.com/uploads/2019/8/30/299a4ab026de7db33c4222b30f3ed70a-full.png

我在这里使用第一个关系,其中“r”表示回报,“Y”表示股票价格

t = 10 # daily intervals

i = 30 # number of days

s = 1

# (Here I've just created some fake numbers, intending to simulate some stock prices)

Y = abs(np.random.random((10,30)) + 1)  

# initializing my return array
return = np.array([])

# (I also tried to initialize it as a Matrix before..) : 
return = np.zeros((10,30))

# here is my for loop to store each daily return at its position on the "return" Array. I wanted an Array  but got just "() size"

for t in range(0,9):    
    for i in range(1,29):                                  
        return = np.array( Y.item((t,i)) - Y.item((t,i-1)) )

...所以,我期待这样的事情:

return = [第一个差异,第二个差异,第三个差异......]

我怎样才能做到这一点 ?

4

1 回答 1

0

first don't use return as a variable name in python as it is a python keyword (it signifies the result of a function). I have changed your variable return to ret_val.

You are wanting to make a change at each position in your array, so make the following change to your for loop:

for t in range(0,10):
    for i in range(1,30):
        ret_val[t][i] = Y[t][i] - Y[t][i-1]
print(ret_val)

This is saying to change the value at index ret_val[t][i] with the result of subtracting the values at that specific index in Y. You should see an array of the same shape when you print.

Also, the range function in python does not include the upper number. So when you say, for i in range(0,9) you are saying to include numbers 0-8. For your array, you'll want to do for i in range(0,10) to include all values in your array. Correspondingly, you'll want to do the same for i in range(1,30).

于 2019-08-30T19:00:47.617 回答