All problems
Trend Slope OLS
Problem
Fit a linear trend x[t] = intercept + slope * t by ordinary least squares with t = 0, ..., n-1 and return the slope: sum((t - mean_t) * (x[t] - mean_x)) / sum((t - mean_t)^2).
Return 0.0 if the series has fewer than two points.
Hint
Precompute mean_t = (n - 1) / 2 to avoid building an index list.
Test cases
case 1
in: [[1,2,3,4]]
expected: 1
case 2
in: [[4,3,2,1]]
expected: -1
case 3
in: [[5,5,5]]
expected: 0
case 4
in: [[1,3,5]]
expected: 2
Starter codePython
def trend_slope(series):
# Your code here
pass4 test cases · run your code against them in the editor