#NEWLINE# def ridge_regression( X , Y , c1 = 0.0 , c2 = 0.0 , offset = None ) : #NEWLINE# #INDENT# '\n    Also known as Tikhonov regularization. This solves the minimization problem:\n\n    min_{beta} ||(beta X - Y)||^2 + c1||beta||^2 + c2||beta - offset||^2\n\n    One can find more information here: http://en.wikipedia.org/wiki/Tikhonov_regularization\n\n    Parameters:\n        X: a (n,d) numpy array\n        Y: a (n,) numpy array\n        c1: a scalar\n        c2: a scalar\n        offset: a (d,) numpy array.\n\n    Returns:\n        beta_hat: the solution to the minimization problem.\n        V = (X*X^T + (c1+c2)I)^{-1} X^T\n\n    ' #NEWLINE# ( n , d ) = X . shape #NEWLINE# X = X . astype ( float ) #NEWLINE# penalizer_matrix = ( ( c1 + c2 ) * np . eye ( d ) ) #NEWLINE# if ( offset is None ) : #NEWLINE# #INDENT# offset = np . zeros ( ( d , ) ) #NEWLINE# #UNINDENT# V_1 = inv ( ( np . dot ( X . T , X ) + penalizer_matrix ) ) #NEWLINE# V_2 = ( np . dot ( X . T , Y ) + ( c2 * offset ) ) #NEWLINE# beta = np . dot ( V_1 , V_2 ) #NEWLINE# return ( beta , np . dot ( V_1 , X . T ) )