线性回归是指利用最小二乘法计算出所选择的数据距离的一条直线。
最小二乘法的和
最小二乘法为比较哪一条直线与所选数据最接近提供了一个客观的衡量标准。
1. 绘出所给定的数据点。
2. 计算每点到给定的一条直线的距离。
3. 计算出该距离的平方数(为了便于去除负值)。
4. 计算出所有平方数的和。
5. 每一条可能的直线都重复步骤2、3、4
6. 根据第四步计算的平方和挑选出与所选数据最近的一条直线。
示例
下图表格展示了一条直线的最小二乘法计算步骤。
价格 = 20.50 + 0.11 * 天数n
选定数据与每一条直线的距离的平方和都要分别计算出来。并选取平方和最小的一条直线。
数学计算公式
运用最小二乘法手动的计算每条可能直线的距离平方和将会消耗很多时间。幸运的是,下面是一种较为简便的方法:
目标直线的计算公式为: y = a + bx
• y 表示价格
• x 是日期
• a 是常数
• b 是直线的斜率
目标直线的计算公式为
b = ( nΣxy - ΣxΣy ) / ( nΣx² - (Σx)² )
a = ( Σy - bΣx ) / n
其中,n是所有选定数据的数值。
Linear Regression
Linear regression fits a straight line to the selected data using a method called the Sum Of Least Squares.
Sum Of Least Squares
The Sum Of Least Squares method provides an objective measure for comparing a number of straight lines to find the one that best fits the selected data.
1. Plot each data point in a table
2. Calculate the distance between each data point and the proposed straight line
3. Square the distances (to remove negative values)
4. Calculate the sum of the squares
5. Repeat steps 2 to 4 for each possible line
6. Select the line with the lowest sum of squares (from step 4).
EXAMPLE
The table below demonstrates how the sum of squares is calculated for a line where
Price = 20.50 + 0.11 * day n
The sum of squares is calculated for each possible line and the line with the lowest sum is selected.
Mathematical Formula
Manually calculating the sum of squares for each possible line would be enormously time-consuming. Fortunately there is a quicker way.
The formula for a straight line is
y = a + bx
For our purposes:
• y is the price
• x is the date
• a is the constant (the value when x equals zero)
• b is the slope of the line
The formula for calculating the line of best fit is
b = ( nΣxy - ΣxΣy ) / ( nΣx² - (Σx)² )
a = ( Σy - bΣx ) / n
Where n is the number of data points selected.
本文翻译由兄弟财经提供
文章来源:http://www.incrediblecharts.com/indicators/linear_regression.php