![]() |
Toolbox snapshot
The Reactive C++ Toolbox
|
Useful utility for insertion sort.
A numerically stable algorithm is given below. It also computes the mean. This algorithm is due to Knuth, who cites Welford.
sum_sq = 0 mean = x[1] for i in 2 to n: sweep = (i - 1.0) / i delta = x[i] - mean sum_sq += delta * delta * sweep mean += delta / i end
The following algorithm (in pseudocode) will compute the covariate version with good numerical stability.
sum_x2 = 0 sum_y2 = 0 sum_xy = 0 mean_x = x[1] mean_y = y[1] for i in 2 to n: sweep = (i - 1.0) / i delta_x = x[i] - mean_x delta_y = y[i] - mean_y sum_x2 += delta_x * delta_x * sweep sum_y2 += delta_y * delta_y * sweep sum_xy += delta_x * delta_y * sweep mean_x += delta_x / i mean_y += delta_y / i end