def linearRegression_2(inputs, outputs): """ Computers the least squares estimator (LSE) B_hat that minimises the sum of the squared errors. Computes B_hat as B_hat = X^+ . y with X^+ the pseudoinverse of matrix X. http://en.wikipedia.org/wiki/Moore-Penrose_pseudoinverse In: inputs: Matrix of inputs (X) (nxp matrix) format: [[observation_1], ..., [observation_n]] outputs: Column vector (Matrix) of outputs y (nx1 matrix) format: [[y_1], ... , [y_n]] Out: B_hat: Column vector (Matrix) of fitted slopes (px1 matrix) format: [[b_0], ... , [b_{p-1}]] """ X = T.dmatrix('X') y = T.dcol('y') # http://deeplearning.net/software/theano/library/sandbox/linalg.html # MatrixPinv is the class. # pinv is the method based upon the MatrixPinv class. # B_hat = X^+ . y B_hat = T.dot(linOps.pinv(X),y) lse = function([X, y], B_hat) b = lse(inputs, outputs) return b
def test_pseudoinverse_correctness(): rng = numpy.random.RandomState(utt.fetch_seed()) d1 = rng.randint(4) + 2 d2 = rng.randint(4) + 2 r = rng.randn(d1, d2).astype(theano.config.floatX) x = tensor.matrix() xi = pinv(x) ri = function([x], xi)(r) assert ri.shape[0] == r.shape[1] assert ri.shape[1] == r.shape[0] assert ri.dtype == r.dtype # Note that pseudoinverse can be quite unprecise so I prefer to compare # the result with what numpy.linalg returns assert _allclose(ri, numpy.linalg.pinv(r))