線性回歸是指利用最小二乘法計算出所選擇的數據距離的一條直線。
最小二乘法的和
最小二乘法為比較哪一條直線與所選數據最接近提供了一個客觀的衡量標準。
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