I am trying to predict the return of BN4.SI ( a singapore stock ) and part of Strait Times 30 component index using principal component Analysis. I have written my code in python.
My Question is i have got factors loading how can i predict the next day return of BN4.SI based on this PCA Factors.
Please help me.
The steps i have taken are
- Get the matrix of standardised returns of all stocks. Standardised log returns are [(x - mean)/std for x in array]
- Generate covariance matrix of the return matrix and that should give me a square matrix ( m X m)
- I am using numpy linear algebra eig function to calculate eigen values and eigen vectors. Since my returns are standardised then sum of eigen values should be equalled to number of components.
- Then sort the eigen values in decending order to get first 3 eigen values which explains almost 80% of the variance. This is shown in scree plot in the picture added below.
- Next step is to get top 3 PCA vector which is acheived by getting the dot product of eigen vector and the original return matrix data. pca1 = ti.np.dot(newDF,eVector1.reshape(-1,1)).reshape(1,-1) pca2 = ti.np.dot(newDF,eVector2.reshape(-1,1)).reshape(1,-1) pca3 = ti.np.dot(newDF,eVector3.reshape(-1,1)).reshape(1,-1)
- To perform linear regression with BN4.SI standarised return data. I need to get the matrix of the transpose of PCA vectors. np.column_stack([pca1.T,pca2.T,pca3.T])
I am using sklearn to do linear regression.
from sklearn import linear_model model = linear_model.LinearRegression() model.fit(ti.np.column_stack([pca1.T,pca2.T,pca3.T]),newDF["BN4.SI"]) model.score(ti.np.column_stack([pca1.T,pca2.T,pca3.T]),newDF["BN4.SI"]) print(model.coef_) print(model.intercept_)
[ 0.2802088 0.37944899 0.13462393] -1.15582251414e-18