Least-squares slope over sample index. Resistant to the single-outlier reading that a first-vs-last delta would treat as the whole trend.
(values)
| 66 | |
| 67 | |
| 68 | def linear_slope(values): |
| 69 | """Least-squares slope over sample index. Resistant to the single-outlier |
| 70 | reading that a first-vs-last delta would treat as the whole trend.""" |
| 71 | n = len(values) |
| 72 | if n < 2: |
| 73 | return 0.0 |
| 74 | mean_x = (n - 1) / 2.0 |
| 75 | mean_y = sum(values) / n |
| 76 | num = sum((i - mean_x) * (v - mean_y) for i, v in enumerate(values)) |
| 77 | den = sum((i - mean_x) ** 2 for i in range(n)) |
| 78 | return num / den if den else 0.0 |
| 79 | |
| 80 | |
| 81 | def parse_census(path): |