迴歸模型不僅是資料分析和預測的強大工具,也是實現自動化監控和異常檢測的有效手段。在複雜系統管理中,結合適當的閾值設定和警示機制,可以大大提高問題發現的及時性和準確性,進而保障系統的穩定運行。本文介紹迴歸分析函數的基本文法和樣本。
背景資訊
公式:y = a1 * x1 + a2 * x2 + b + noise
參數 | 說明 |
| Log Service採集的一列資料。 |
| Log Service採集的一列資料。 |
| 一個隨機變數。 |
| 根據公式計算的結果。 |
根據使用者提供的x1
、x2
、y
和權重資料,找出公式中的a1
、a2
和b
(三個係數),然後計算出結果。
本文介紹迴歸分析函數樣本的日誌包含六個欄位索引。更多資訊,請參見建立索引。
日誌範例如下:
{"group_id":"A","observation_id":"S001","time_offset":"0","x1":"1","x2":"5","y":"23.91700530543459"} {"group_id":"A","observation_id":"S002","time_offset":"-1","x1":"2","x2":"2","y":"6.931858878794941"} {"group_id":"A","observation_id":"S003","time_offset":"-2","x1":"3","x2":"8","y":"16.17603801639615"} {"group_id":"A","observation_id":"S004","time_offset":"-3","x1":"4","x2":"6","y":"24.97127625789946"} {"group_id":"A","observation_id":"S005","time_offset":"-4","x1":"5","x2":"2","y":"11.933292736756384"} {"group_id":"A","observation_id":"S006","time_offset":"-5","x1":"6","x2":"8","y":"21.034262717019995"} {"group_id":"A","observation_id":"S007","time_offset":"-6","x1":"7","x2":"1","y":"25.966770392099868"} {"group_id":"A","observation_id":"S008","time_offset":"-7","x1":"8","x2":"7","y":"16.93019469603219"} {"group_id":"A","observation_id":"S009","time_offset":"-8","x1":"9","x2":"2","y":"19.967258015889847"} {"group_id":"A","observation_id":"S010","time_offset":"-9","x1":"10","x2":"3","y":"27.0277513207651"}
迴歸分析函數列表
函數名稱 | 文法 | 說明 | 傳回值類型 |
| 該函數為純量涵式,通過array_agg實現彙總功能,輸入為迴歸模型的樣本及可選的樣本權重,輸出為識別出的迴歸模型,模型結果採用JSON格式返回。 | varchar | |
linear_model_predict(varchar model_in_json, array(double) x_sample) | 通過識別出的模型及輸入變數樣本進行預測。 | double | |
recent_regression(double y, array(double) x_array, double cur_sample_time_period, double cur_batch_begin_period, double cur_batch_end_period, double time_unit, double damping_weight_per_time_unit) | 基於最新收集的資料,線上更新模型參數與狀態變數。模型採用樣本年齡作為權重調整依據,使樣本的重要性隨年齡增長呈指數衰減。 | varchar | |
merge_recent_regression(varchar model_1_json, varchar model_2_json) | 將前後兩個階段識別出的模型參數和狀態變數進行合并,其結果與將兩批資料合併後重新識別出的模型參數相同。 | varchar | |
recent_regression_predict(varchar model_json, array(double) x_sample) | 使用自適應迴歸模型進行預測。 | double |
帶樣本權重的迴歸模型
支援為迴歸模型指定樣本權重,支援與時間或目標變數相關的權重設定。通過隨樣本年齡減小的權重,模型更關注最新資料,適應系統變化;使用目標變數絕對值倒數作為權重,則使迴歸模型最小化相對誤差。
linear_model函數
該函數為純量涵式,通過array_agg函數實現彙總,輸入為迴歸模型的樣本及可選的樣本權重,輸出為JSON格式的迴歸模型。
varchar linear_model(array(array(double)) x_samples, array(double) y_samples)
或
varchar linear_model(array(array(double)) x_samples, array(double) y_samples, array(double) weights)
參數 | 說明 |
| 由多個輸入變數樣本組成的資料矩陣結構,其中每一行代表一次針對這些輸入變數的觀測。 |
| 由輸出變數的樣本構成的向量。 |
| 選擇性參數,如果未指定則賦予相同的權重。 |
使用樣本
查詢和分析語句
* | select group_id, linear_model( array_agg(array[x1, x2]), array_agg(y) ) as model from log group by group_id
返回結果
傳回值中
coefficients
欄位表示的是通過資料識別出來的線性迴歸的係數。預測時此函數作為linear_model_predict函數的入參。
group_id
model
A
{ "coefficients": [ 0.8350068912618618, -0.741283054726383, 19.17405856472653 ], "isBuilt": true, "isBuildSuccessful": true, "sampleCount": 10, "xCount": 2, "wSum": 10.0, "ySumSquare": 3930.0, "ySum": 188.0, "xXSumProducts": [ [ 385.0, 367.0 ], [ 367.0, 475.0 ] ], "xYSumProducts": [ 1104.0, 1239.0 ], "xSums": [ 55.0, 67.0 ], "xMeans": [ 5.5, 6.7 ], "xStdDevs": [ 2.8722813232690143, 1.6155494421403511 ], "xVariances": [ 8.25, 2.6099999999999994 ], "yMean": 18.8, "yStdDev": 6.289674077406551, "yVariance": 39.559999999999945, "xCorrelations": [ [ 1.0, -0.03232540919176149 ], [ -0.03232540919176149, 1.0 ] ], "xYCorrelations": [ 0.3874743195572169, -0.202730375711539 ], "regularized": true, "regularWeight": 1.0E-6 }
linear_model_predict函數
通過識別出的模型及輸入變數樣本進行預測。
double linear_model_predict(varchar model_in_json, array(double) x_sample)
參數 | 說明 |
| 用linear_model函數識別出來的模型結果。 |
| 新的輸入變數。 |
使用樣本
查詢和分析語句
* | with group_models as ( select group_id, linear_model( array_agg(array[x1, x2]), array_agg(y) ) as model from log group by group_id ) select d.group_id, d.y, linear_model_predict(m.model, array[x1, x2]) as predicted_y from group_models as m join log as d on m.group_id = d.group_id
返回結果
predicted_y
是由輸入變數計算出來的預測值。group_id
observation_id
y
predicted_y
A
S001
23.91700530543459
15.68867910570816
A
S002
6.931858878794941
15.352330987812993
...
...
...
線上自適應迴歸演算法
一種線上增量演算法,該演算法在接收新資料時,僅需使用新資料對模型進行增量式更新,相較於批量演算法處理大量資料的需求,具有高效計算和低成本儲存的優勢。此外,該演算法適用於持久性分析(Continuous Profiling),因每次處理後即可丟棄樣本資料,故展現出更高的實用性和便捷性。
線上自適應迴歸演算法中的自適應指的是線上演算法在增量計算統計特徵和模型的時候,能夠自動地對舊的歷史樣本對統計特徵的影響按照指數衰退,讓最近的樣本保持較高的權重,跟上系統內容的變化。
recent_regression函數
根據最近採集到的一批資料,線上更新模型參數和狀態變數。模型會根據樣本的年齡讓樣本的重要性進行指數衰退。
varchar recent_regression(double y, array(double) x_array, double sample_time, double cur_batch_begin_period, double cur_batch_end_period, double time_unit, double unit_damping_weight)
參數 | 說明 |
| 預測目標變數的列資料,因變數的樣本。 |
| 由自變數(輸入變數)組成的樣本數組。 |
| 該樣本行對應的資料時間點是什麼,時間需要轉換成為數字。 |
| 當前這一批用於訓練模型的資料的時間段的起始時刻。 |
| 當前這一批用於訓練模型的資料的時間段的終止時刻,資料的時間段是 |
| 單位時間間隔。時間尺度和 |
| 指數衰退基數。樣本權重隨時間變化的關係,即每隔一個特定的時間單位(time_unit),樣本的權重會減少一個固定值(unit_damping_weight)。 讓樣本權重按照一定的半衰期以指數衰減,例如,最新時刻的資料的權重為1,一天前的資料的權重降為1/2,兩天前的資料的權重降到1/4,三天前的權重降到1/8,以此類推。 當前變數使用公式計算: unit_damping_weight = 2 ^ -(樣本時間間隔/半衰期) |
使用樣本
查詢和分析語句
* | select group_id, recent_regression( y, array[x1, x2, 1.0], -- 輸出輸入變數樣本 time_offset, -- 樣本的時間點 -4, -- 當前批次資料樣本的起始時間 0, -- 當前批次資料樣本的終止時間 1, -- 單位時間間隔 0.999 -- 指數衰退基數 ) as reg_model from log where time_offset >= -4 and time_offset <= 0 group by group_id
返回結果
傳回值中
coefficients
欄位表示的是通過資料識別出來的線性迴歸的係數。預測時此函數作為recent_regression_predict函數的入參。
group_id
reg_model
A
{ "sampleCount": 5, "xCount": 3, "timeUnit": 1.0, "beginTimePeriod": -4.0, "endTimePeriod": 0.0, "unitDampingWeight": 0.999, "wSum": 4.990009995001, "ySumSquare": 1644.6974283836598, "ySum": 83.76770287757991, "xXSumSquares": [ [ 54.830206884025, 70.82220388003, 14.960044976005001 ], [ 70.82220388003, 173.70327985603598, 25.955043976006 ], [ 14.960044976005001, 25.955043976006, 4.990009995001 ] ], "xYSumProducts": [ 245.21187055562675, 402.5070758759011, 83.76770287757991 ], "xSums": [ 14.960044976005001, 25.955043976006, 4.990009995001 ], "xMeans": [ 2.997999000200801, 5.201401199999158, 1.0 ], "xStdDevs": [ 1.4142126422148122, 2.7848935986573244, 0.0 ], "xVariances": [ 1.9999973974002003, 7.755632355842543, 0.0 ], "yMean": 16.78708118049834, "yStdDev": 6.913170639821401, "yVariance": 47.79192829528864, "xCorrelations": [ [ 1.0, -0.35572473794248516, 0.0 ], [ -0.35572473794248516, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ], "xYCorrelations": [ -0.12142097167729436, -0.34560624507434407, 0.0 ], "coefficients": [ -1.3675797278475395, -1.104969989478544, 0.0, 26.634476066516903 ], "isBuilt": true, "isBuildSuccessful": true }
merge_recent_regression函數
將前後兩個階段識別出的模型參數和狀態變數進行合并,其結果與將兩批資料合併後重新識別出的模型參數相同。
varchar merge_recent_regression(varchar model_1_json, varchar model_2_json)
參數 | 說明 |
| recent_regression函數的傳回值。 |
| recent_regression函數的傳回值。 |
使用樣本
查詢和分析語句
* | with model1 as ( select group_id, recent_regression( y, array[x1, x2, 1.0], -- 輸出輸入變數樣本 time_offset, -- 樣本的時間點 -4, -- 當前批次資料樣本的起始時間 0, -- 當前批次資料樣本的終止時間 1, -- 單位時間間隔 0.999 -- 指數衰退基數 ) as reg_model from log where time_offset >= -4 and time_offset <= 0 group by group_id ), model2 as ( select group_id, recent_regression(y, array[x1, x2, 1.0], time_offset, -9, -5, 1, 0.999) as reg_model from log where time_offset >= -9 and time_offset <= -5 group by group_id ) select m1.group_id, merge_recent_regression(m1.reg_model, m2.reg_model) as reg_model from model1 as m1 join model2 as m2 on m1.group_id = m2.group_id
返回結果
傳回值中
coefficients
欄位表示的是通過資料識別出來的線性迴歸的係數。預測時此函數作為recent_regression_predict函數的入參。
group_id
reg_model
A
{ "sampleCount": 10, "xCount": 3, "timeUnit": 1.0, "beginTimePeriod": -9.0, "endTimePeriod": 0.0, "unitDampingWeight": 0.999, "wSum": 9.955119790251791, "ySumSquare": 4159.2626495224, "ySum": 193.9139516502596, "xXSumSquares": [ [ 382.3684973894312, 268.46629177582946, 54.67098815430803 ], [ 268.46629177582946, 358.44803436913094, 51.78255011892536 ], [ 54.67098815430803, 51.78255011892536, 9.955119790251791 ] ], "xYSumProducts": [ 1132.090921413269, 919.4071924317548, 193.9139516502596 ], "xSums": [ 54.67098815430803, 51.78255011892536, 9.955119790251791 ], "xMeans": [ 5.4917458861562585, 5.201599901352432, 1.0 ], "xStdDevs": [ 2.8722740635191735, 2.991614845817865, 0.0 ], "xVariances": [ 8.249958295964944, 8.949759385717847, 0.0 ], "yMean": 19.478816502051856, "yStdDev": 6.1949232381571, "yVariance": 38.37707392665885, "xCorrelations": [ [ 1.0, -0.1859947674356197, 0.0 ], [ -0.1859947674356197, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ], "xYCorrelations": [ 0.3791693893070564, -0.4837793996174176, 0.0 ], "coefficients": [ 0.6460732812209116, -0.8864195347835274, 0.0, 20.541545982438304 ], "isBuilt": true, "isBuildSuccessful": true }
recent_regression_predict函數
使用自適應迴歸模型進行預測。
double recent_regression_predict(varchar model_json, array(double) x_sample)
參數 | 說明 |
model_json | |
| 用於計算預測值的輸入的資料樣本。 |
使用樣本
查詢和分析語句
* | with model1 as ( select group_id, recent_regression( y, array[x1, x2, 1.0], -- 輸出輸入變數樣本 time_offset, -- 樣本的時間點 -4, -- 當前批次資料樣本的起始時間 0, -- 當前批次資料樣本的終止時間 1, -- 單位時間間隔 0.999 -- 指數衰退基數 ) as reg_model from log where time_offset >= -4 and time_offset <= 0 group by group_id ), model2 as ( select group_id, recent_regression(y, array[x1, x2, 1.0], time_offset, -9, -5, 1, 0.999) as reg_model from log where time_offset >= -9 and time_offset <= -5 group by group_id ), model as ( select m1.group_id, merge_recent_regression(m1.reg_model, m2.reg_model) as reg_model from model1 as m1 join model2 as m2 on m1.group_id = m2.group_id ), new_data as ( select 'A' as group_id, 1 as obs_id, 3.0 as x1, 5.0 as x2, 1.0 as x3 union all select 'A' as group_id, 2 as obs_id, 7.0 as x1, 8.0 as x2, 1.0 as x3 ) select m.group_id, n.obs_id, recent_regression_predict(m.reg_model, array[n.x1, n.x2, 1.0]) as predicted_value from model as m join new_data as n on m.group_id = n.group_id order by m.group_id, n.obs_id
返回結果
predicted_value
欄位是預測值。group_id
obs_id
predicted_value
A
1
17.489274877305804
A
2
22.3233353394362