'''Univariate linear regression

Input:
    x, y... column vectors

Output:
    X... matrix
    beta... estimated regression coefficients
    yhat... predicted values
    ybar... mean value
    Syy, SSE, SSR... total variability
    R2... coefficient of determination

Typical call of program:
    
    from python18_1 import fct
    from python18_examples import buildings
    x, y = buildings()
    res = fct(x,y)
    
Check python18_examples for more examples
'''

import numpy as np

def fct(x,y):
    assert x.shape[1] == 1  # x has to be a column vector
    assert y.shape[1] == 1  # y has to be a column vector
    
    X = np.ones((x.shape[0],2))
    X[:,1] = x[:].ravel()   # Matrix
    
    beta = np.linalg.solve(X.T.dot(X), X.T.dot(y))
    yhat = X.dot(beta)
    SSE = np.sum((y -yhat)**2)
    
    ybar = np.mean(y)
    Syy = np.sum((y - ybar)**2)
    
    SSR = Syy - SSE
    R2 = SSR/Syy
    
    print('X = ', X)
    print('beta = ', beta)
    print('yhat = ', yhat)
    print('ybar = ', ybar)
    print('Syy = ', Syy)
    print('SSE = ', SSE)
    print('SSR = ', SSR)
    print('R2 = ', R2)

    return X, beta, yhat, ybar, Syy, SSE, SSR, R2