对一个序列应用两段线性回归,返回多个列。Applies two segments linear regression on a series, returning multiple columns.

采用包含动态数值数组的表达式作为输入,并应用两段线性回归,以便识别和量化序列中的趋势变化。Takes an expression containing dynamic numerical array as input and applies two segments linear regression in order to identify and quantify a trend change in a series. 该函数针对序列索引进行迭代。The function iterates on the series indexes. 在每次迭代中,该函数将序列拆分为两部分,为每个部分单独拟合一条线(使用 series_fit_line()),并计算总相关系数平方。In each iteration, the function splits the series to two parts, fits a separate line (using series_fit_line()) to each part, and calculates the total r-square. 最好的拆分方式是最大限度提高相关系数平方值;函数将返回其参数:The best split is the one that maximized r-square; the function returns its parameters:

参数Parameter 描述Description
rsquare 相关系数平方是衡量拟合质量的标准。R-square is standard measure of the fit quality. 它是 [0-1] 范围内的数字,其中 1 表示拟合质量可能最佳,0 表示数据是无序的且无法与任何线拟合。It's a number in the range [0-1], where 1 - is the best possible fit, and 0 means the data is unordered and do not fit any line.
split_idx 两段的拆分点的索引(从零开始)。The index of breaking point to two segments (zero-based).
variance 输入数据的方差。Variance of the input data.
rvariance 剩余方差,即输入数据值和近似数据值之间的方差(按两个线段)。Residual variance, which is the variance between the input data values the approximated ones (by the two line segments).
line_fit 包含一系列最佳拟合线值的数值数组。Numerical array holding a series of values of the best fitted line. 序列长度等于输入数组的长度。The series length is equal to the length of the input array. 它主要用于绘制图表。It's mainly used for charting.
right_rsquare 拆分点右侧线的相关系数平方,请参阅 series_fit_line()R-square of the line on the right side of the split, see series_fit_line().
right_slope 右近似线(形式为 y=ax+b)的斜率。Slope of the right approximated line (of the form y=ax+b).
right_interception 左近似线的截距(即 y=ax+b 中的 b)。Interception of the approximated left line (b from y=ax+b).
right_variance 拆分点右侧输入数据的方差。Variance of the input data on the right side of the split.
right_rvariance 拆分点右侧输入数据的剩余方差。Residual variance of the input data on the right side of the split.
left_rsquare 拆分点左侧线的相关系数平方,请参阅 series_fit_line()R-square of the line on the left side of the split, see series_fit_line().
left_slope 左近似线(形式为 y=ax+b)的斜率。Slope of the left approximated line (of the form y=ax+b).
left_interception 左近似线(形式为 y=ax+b)的截距。Interception of the approximated left line (of the form y=ax+b).
left_variance 拆分点左侧输入数据的方差。Variance of the input data on the left side of the split.
left_rvariance 拆分点左侧输入数据的剩余方差。Residual variance of the input data on the left side of the split.


此函数返回多个列,因此不能用作另一个函数的参数。This function returns multiple columns an so cannot be used as an argument for another function.


project series_fit_2lines(x)project series_fit_2lines(x)

  • 将会返回上述所有列,其名称为:series_fit_2lines_x_rsquare、series_fit_2lines_x_split_idx 等。Will return all mentioned above columns with the following names: series_fit_2lines_x_rsquare, series_fit_2lines_x_split_idx etc.

project (rs, si, v)=series_fit_2lines(x)project (rs, si, v)=series_fit_2lines(x)

  • 将会返回以下列:rs(相关系数平方)、si(拆分索引)、v(方差),其余列的名称将会类似于 series_fit_2lines_x_rvariance、series_fit_2lines_x_line_fit 等。Will return the following columns: rs (r-square), si (split index), v (variance) and the rest will look like series_fit_2lines_x_rvariance, series_fit_2lines_x_line_fit and etc.

extend (rs, si, v)=series_fit_2lines(x)extend (rs, si, v)=series_fit_2lines(x)

  • 将仅返回:rs(相关系数平方)、si(拆分索引)和 v(方差)。Will return only: rs (r-square), si (split index) and v (variance).


  • x:数值的动态数组。x: Dynamic array of numeric values.


使用此函数最便捷的方法是将其应用于 make-series 运算符的结果。The most convenient way of using this function is applying it to the results of make-series operator.


print id=' ', x=range(bin(now(), 1h)-11h, bin(now(), 1h), 1h), y=dynamic([1,2.2, 2.5, 4.7, 5.0, 12, 10.3, 10.3, 9, 8.3, 6.2])
| extend (Slope,Interception,RSquare,Variance,RVariance,LineFit)=series_fit_line(y), (RSquare2, SplitIdx, Variance2,RVariance2,LineFit2)=series_fit_2lines(y)
| project id, x, y, LineFit, LineFit2
| render timechart

序列拟合 2 条线