Sort by

recency

|

102 Discussions

|

  • + 0 comments
    #python
    sxy, sx, sy, sx2=0, 0, 0, 0
    for _ in range(5):
        x, y=list(map(int, input().split()))
        sx+=x
        sy+=y
        sxy+=x*y
        sx2+=x**2
    b=(5*sxy-sx*sy)/(5*sx2-sx**2)
    a=(sy-b*sx)/5
    print(round(a+b*80, 3))
    
  • + 0 comments

    C++

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    
    double dot_product(vector<double>x, vector<double> y)
    {
        double sum=0;
        for(int i=0; i<x.size(); ++i)
        {
            sum+=x[i]*y[i];
        }
        return sum;
    }
    
    double sum(vector<double> x, int p=1)
    {   double sum=0;
        for(int i=0; i<x.size(); i++)
            sum+=pow(x[i], p);
        return sum;
    }
    double mean(vector<double> x)
    {   double total=sum(x);
        return total/x.size();
    }
    
    double linear_regression(vector<double> x, vector<double>y, double t)
    {   double n=double(x.size());
        double sumX=sum(x,1);
        double sumX2=sum(x,2);
        double sumY=sum(y,1);
        double meanX=mean(x);
        double meanY=mean(y);
        
        double b=(n*dot_product(x, y)-sumX*sumY)/(n*sumX2-sumX*sumX);
        double a=meanY-b*meanX;
        
        return a+b*t;
        
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int a=5;
        vector<double> math(a);
        vector<double> stat(a);
        double m,s;    
        for(int i=0; i<a; ++i)
        {
            cin>>m>>s;
            math.push_back(m);
            stat.push_back(s);
        }
        double math_grade=80;
        double stat_grade=linear_regression(math,stat,math_grade);
        
        cout<<fixed<<setprecision(3);
        cout<<stat_grade<<endl;
        return 0;
    }
    
    }
    
  • + 0 comments

    Python simple and clean solution:

    x = [95, 85, 80, 70, 60]
    y = [85, 95, 70, 65, 70]
    
    sum_x = sum(x)
    sum_y = sum(y)
    n = len(x) # or len(y)
    
    b = (n * sum(x[i] * y[i] for i in range(n))) - (sum_x * sum_y)
    b /= n * sum(list(map(lambda i: i*i, x))) - sum_x ** 2
    # or b /= n * sum(i ** 2 for i in x) - sum_x ** 2
    
    mean_x = sum_x / n
    mean_y = sum_y / n
    
    a = mean_y - b * mean_x
    
    print(a + b * 80)
    
  • + 0 comments
    import statistics
    import math
    
    X = [95, 85, 80, 70, 60]
    Y = [85, 95, 70, 65, 70]
    
    mean_x = statistics.mean(X)
    mean_y = statistics.mean(Y)
    
    diff_x = 0
    diff_y = 0
    for i in range(len(X)):
        diff_x += pow(X[i] - mean_x, 2)
        diff_y += pow(Y[i] - mean_y, 2)
        
    stand_dev_x = math.sqrt(1 / len(X) * diff_x)
    stand_dev_y = math.sqrt(1 / len(Y) * diff_x)
    
    sum = 0
    for i in range(len(X)):
        sum += ((X[i] - mean_x) / stand_dev_x ) * ((Y[i] - mean_y) / stand_dev_y)
        
    r = 1 / len(X) * sum
    
    b = r * stand_dev_y / stand_dev_x
    a = mean_y - b * mean_x
    
    answer = a + b * 80
    
    print(round(answer, 3))
    
  • + 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))