Sort by

recency

|

103 Discussions

|

  • + 0 comments

    Python 3 solution with comments, taking the formulas from the tutorial:

    n = 5
    X = [95, 85, 80, 70, 60]
    Y = [85, 95, 70, 65, 70]
    
    mean_X = sum(X)/len(X)
    mean_Y = sum(Y)/len(Y)
    
    
    # determine the equation parameters for y = a + b*x 
    
    b = ((n * sum(x*y for x, y in zip(X,Y))) - (sum(X) * sum(Y))) / (n * sum(x**2 for x in X) - (sum(X)**2))
    
    a = mean_Y - (b * mean_X)
    
    # calculate the predicted y for x = 80
    
    y_predicted = a + (b * 80)
    
    # should come out to # 78.288
    print(round(y_predicted, 3))
    
  • + 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))