Time for action – predicting price with a linear model
Keeping an open mind, let's assume that we can express a stock price as a linear combination of previous values, that is, a sum of those values multiplied by certain coefficients we need to determine. In linear algebra terms, this boils down to finding a least squares solution. This recipe goes as follows.
First, form a vector
bbx
containing N price values.bbx = c[-N:] bbx = b[::-1] print "bbx", x
The result is as follows:
bbx [ 351.99 346.67 352.47 355.76 355.36]
Second, pre-initialize the matrix
A
to be N x N and containing zeroes.A = np.zeros((N, N), float) print "Zeros N by N", A Zeros N by N [[ 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0.]]
Third, fill the matrix
A
with N preceding price values for each value inbbx
.for i in range(N): A[i, ] = c[-N - 1 - i: - 1 - i] print "A", A
Now,
A
looks like this:A [[ 360. 355.36 355.76 352.47 346.67] [ 359.56 360. ...