“Every problem becomes childish when once it is explained to you”

Monday, August 21, 2017

Using Linear Regression for Stock Market Model

From the previous blog, we got an overview of machine learning & linear regression. Here we can try to address usecase of very basic stock forecasting.

Usecase: In stock market, some stocks tend to change linearly with the entire markets valuation.
That means a particular stock is sensitive to the Market changes. We need to predict the stocks price based on markets valuation.


Approach:
We can use simple linear regression to solve this since we feel that the relationship is linear.

Predicting Equation: y=mx+b 


Lets say the points in Exchange as X
&
lets try predicting the value of Y, a single stock present in the stock exchange X


Use http://www.r-fiddle.org/  for writing the R code.

Lets take the history data set in R first. 

x <- c(350, 320, 410, 370, 290,
330, 340, 350, 340, 370)
y <- c(75, 68, 82, 76, 47, 70,
76, 72, 69, 74)


Now simply apply the linear function lm() on this dataset to find the relationship between these 2 datasets.

relationship <- lm(y~x)

Just print the results

print(relationship)

Coefficients:
(Intercept)            x  
   -15.2784       0.2484  


This will print the coefficents 

Now we can use the predict function( equation of straight line) to predict the results.

inputX <- data.frame(x = 450)
resultY <-  predict(relationship, inputX)
print(resultY)

       1 
96.48034 

This result can be plotted in graph for better understanding.

plot(y~x,col="red")

abline(lm(relationship))


No comments:

Post a Comment