# Linear regression

Linear regression is a method of organizing data.  Sometimes it is appropriate to show data as points on a graph, then try to draw a straight line through the data.  Linear regression is an algorithm for drawing such a line.  Linear regression typically uses the least squares method to determine which line best fits the data.  R-Squared is a measure of how well the data points match the resulting line.

Many trading strategies assume that the way a stock moves during a specific time of day can be used to predict the way a stock will move later in the day.  How would you verify or automate such a strategy?  Start by recording historical values.  Each day, record the size and direction of the change in the first period, and the direction and size of the second change, later in the day.  One point on a graph will represent each day’s data.  If the original idea was correct, these points should look like a line.  If this is the case, a trader can look at the size of a move in the morning, and guess what the second move that day will look like.

Linear regression provides a deterministic way to this.  First, linear regression will provide an R-Squared value for the historical data.  If this value is too small, the data is not linear, so the original assumptions must change.  If R-Squared is large enough, then the linear regression will provide the best prediction of the second move each day based on the first move.

Imagine that the six points on the graph below represent the historical data that we collected above.  A common way to look at this is to say that the trend is obvious from five of the points, and the sixth point must be a mistake.  This type of reasoning leads to traders who are very successful right up until the day they loose it all.  The least squares method provides a more appropriate way to view the data, because it incorporates all of the points.  In this case it is clear that the strategy is risky, and requires more work. Our alerts server does not endorse any strategy this specific.  Instead, it looks for and reports any unusual trading activity.  One way it does this is by using linear regression to make various predictions, then reporting when a stock diverges too far from these predictions.