• + 0 comments
    X = [95, 85, 80, 70, 60]
    Y = [85, 95, 70, 65, 70]
    
    import math
    
    class JStatistics():
        @staticmethod
        def vectorial_operations(X, Y, operator):
            if type(Y)==type(1):
                Y=[Y for _ in X]
            assert len(X)==len(Y)
            if operator=="-":
                return [X[i] - Y[i] for i in range(len(X))] 
            elif operator=="+":
                return [X[i] + Y[i] for i in range(len(X))] 
            elif operator=="*":
                return [X[i] * Y[i] for i in range(len(X))] 
            elif operator=="**":
                return [X[i] ** Y[i] for i in range(len(X))] 
            elif operator=="/":
                return [X[i] / Y[i] for i in range(len(X))] 
            
            raise Exception("Operation not managed yet")
        
        @staticmethod
        def mean(v):
            return sum(v)/len(v)
    
        @staticmethod
        def variance(arr):
            mi = JStatistics.mean(arr)
            arr_minus_mi_pow2 = [math.pow(x-mi,2) for x in arr]
            return JStatistics.mean(arr_minus_mi_pow2)
            
        @staticmethod
        def stdDev(arr):
            return math.sqrt(JStatistics.variance(arr))
            
        @staticmethod
        def covariance(X, Y):
            assert len(X)==len(Y)
            n = len(X)
            xBar, yBar = JStatistics.mean(X), JStatistics.mean(Y)
            cov = (1/n)*sum( (X[i]-xBar)*(Y[i]-yBar) for i in range(n) )
            return cov
            
        @staticmethod
        def coefficientPearson(X, Y):
            xstd = JStatistics.stdDev(X)
            ystd = JStatistics.stdDev(Y)
            return JStatistics.covariance(X, Y)/(xstd*ystd)
        
        @staticmethod
        def regressionLine(X, Y):
            m = JStatistics.coefficientPearson(X,Y)*(JStatistics.stdDev(Y)/JStatistics.stdDev(X))
            q = JStatistics.mean(Y) - m * JStatistics.mean(X)
            return m,q
        
    m,q = JStatistics.regressionLine(X,Y)
    y_hat = m*80+q
    print(round(y_hat,3))