def solve_simple_regression(x,y): """ solve simple regression coefficients returns w_0 (slope) and w_1 (intercept) >>> solve_simple_regression(range(9),[19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]) (0.7166666666666667, 19.18888888888889) """ n = float(len(x)) slope = (dot(y,x) - ((sum(y)*sum(x))/n))/(dot(x,x)-((sum(x)*sum(x))/n)) intercept = sum(y)/n - slope*sum(x)/n return slope, intercept
def solve_simple_regression(x, y): """ solve simple regression coefficients returns w_0 (slope) and w_1 (intercept) >>> solve_simple_regression(range(9),[19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]) (0.7166666666666667, 19.18888888888889) """ n = float(len(x)) slope = (dot(y, x) - ((sum(y) * sum(x)) / n)) / (dot(x, x) - ((sum(x) * sum(x)) / n)) intercept = sum(y) / n - slope * sum(x) / n return slope, intercept
def residual_sum_of_squares(input_feature, output, slope, intercept): """ simple regression model based on input coefficients input: input_feature (x vector), output (y vector), slope(w_0), intercept (w_1) output: rss """ predictions = get_regression_predictions(input_feature, slope, intercept) residuals = output - predictions rss = dot(residuals, residuals) return (rss)
def residual_sum_of_squares(input_feature, output, slope, intercept): """ simple regression model based on input coefficients input: input_feature (x vector), output (y vector), slope(w_0), intercept (w_1) output: rss """ predictions = get_regression_predictions(input_feature, slope, intercept) residuals = output - predictions rss = dot(residuals, residuals) return(rss)
def covariance(x,y): """ measure dispersion in another way >>> covariance([1,2,3,4,5,6],[1,2,3,4,5,7]) 4.0 """ return dot(from_mean(x), from_mean(y)) / (len(x) - 1)