def project(v, w): """return the projection of v onto w""" coefficient = dot(v, w) return scalar_multiply(coefficient, w)
def transform_vector(v, components): return [dot(v, w) for w in components]
def directional_variance_gradient_i(x_i, w): """the contribution of row x_i to the gradient of the direction-w variance""" projection_length = dot(x_i, direction(w)) return [2 * projection_length * x_ij for x_ij in x_i]
def directional_variance_i(x_i, w): """the variance of the row x_i in the direction w""" return dot(x_i, direction(w))**2
def ridge_penalty(beta, alpha): return alpha * dot(beta[1:], beta[1:])
def predict(x_i, beta): return dot(x_i, beta)
random.seed(0) # so that you get the same results as me bootstrap_betas = bootstrap_statistic(zip(x, daily_minutes_good), estimate_sample_beta, 100) bootstrap_standard_errors = [ standard_deviation([beta[i] for beta in bootstrap_betas]) for i in range(4)] print "bootstrap standard errors", bootstrap_standard_errors print print "p_value(30.6143, 1.174)", p_value(30.63, 1.174) print "p_value(0.972, 0.079)", p_value(0.972, 0.079) print "p_value(-1.868, 0.131)", p_value(-1.868, 0.131) print "p_value(0.911, 0.990)", p_value(0.911, 0.990) print print "regularization" random.seed(0) for alpha in [0.0, 0.01, 0.1, 1, 10]: beta = estimate_beta_ridge(x, daily_minutes_good, alpha=alpha) print "alpha", alpha print "beta", beta print "dot(beta[1:],beta[1:])", dot(beta[1:], beta[1:]) print "r-squared", multiple_r_squared(x, daily_minutes_good, beta) print