Write a super-efficient OLS regression function in Java

  • Tình trạng: Closed
  • Giải thưởng: $25
  • Các bài thi đã nhận: 1
  • Người chiến thắng: NikolayBokhan

Tóm tắt cuộc thi

Write an efficient Java function to perform Multiple Ordinary Least Squares regression. The function should be in the form:

double[] getLineParams(double[][] independentVars, double[] dependentVar);

The length of dependentVar and each of independetVars[] is the number of datapoints in the sample. The length of the return array is the number of independent variables (the length of independentVars), plus 1; it should describe the line that minimizes the sum of squared errors, with the nth element describing the linear contribution of the nth independent variable (which may be negative). The last element of the return array should contain the constant (offset) part of the line equation.

So, assuming there were three independent variables, the objective would be to minimize totalError in the following test code:

double totalError=0;
for (int i=0;i<n;i++) {
double error=dependentVar[i] -
returnArray[0] * independentVars[0][i] +
returnArray[1] * independentVars[1][i] +
returnArray[2] * independentVars[2][i] +
returnArray[4];
totalError+=error*error;
}

Solutions should be implemented as a single class. Any version of Java may be used. Solutions should use only core Java, Math functions, etc. In particular no linear algebra libraries may be used. The function/class is not permitted to spawn any threads.

The winning entry will be the function that gives the correct results as efficiently as possible. The efficiency will be measures in terms of total execution time over a very large sample of pseudo-random datasets, which will vary wildly in width (the number of independent variables) and in length (the number of datapoints).

Các kĩ năng yêu cầu

Phản hồi của người thuê

“Fast high quality solution delivered. Expert knowledge. Professional service. Great communication. Adaptable. First rate!”

Hình ảnh hồ sơ jwashtell, United Kingdom.

Bảng thông báo công khai

  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    To all participants: this is a contest for a *solution*, not for a proposal.

    If it *was* a contest for a proposal, the winner would not be a proposal which says "I can do it".

    Presently, @NikolayBokhan and @muneshparmar92 are the only people to link to an actual solution.

    Thanks.

    • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    @Nikolay. I might be wrong, but it seems to me that one can reduce convergeIteration() from three to two nested loops, substantially reducing the complexity of this solution. (?)

    • cách đây 11 năm
    1. NikolayBokhan
      NikolayBokhan
      • cách đây 11 năm

      https://drive.google.com/file/d/0B91AEhaVA7YecGxOODFtWnRfTTg/edit?usp=sharing

      • cách đây 11 năm
    2. jwashtell
      Chủ cuộc thi
      • cách đây 11 năm

      Excellent. That is much better :-)

      • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    Alternatively, you could submit a screen-grab of your code (I'm sure we're not using this site correctly :-/)

    • cách đây 11 năm
    1. NikolayBokhan
      NikolayBokhan
      • cách đây 11 năm

      That is my first trial to work on this site either. :-)

      • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    @brokenaiub, would you like to submit a solution? Just put a link to your solution in the text accompanying your submission, or in the comments below.

    • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    Thanks Nikolay - that looks excellent. This is the first time I have used Freelancer, but I think you need to make this a proper submission in order for me to be able to award you the prize.

    • cách đây 11 năm
  • NikolayBokhan
    NikolayBokhan
    • cách đây 11 năm

    The following link is the link to the class with algorithm. You can specify the expected accuracy with a parameter in the constructor.

    https://drive.google.com/file/d/0B91AEhaVA7YeTl9sbmpCVTZpY2c/edit?usp=sharing

    • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    Hi Nikalay. If you are going to adopt an iterative approach, then I would be much more interested in Least Absolute Deviations! Can you do that? At any rate, an iterative approach is OK: please use a parameter to specify the precision and I will conduct the evaluation based on a few different precisions, and I will also take any computational complexity benefits to your approach into account (i.e. its improved performance on the larger datasets) when deciding a winner. Ultimately I will have to apply some judgement, because these are unlike approaches and cannot be directly compared. Of course if you are the only person to submit, and you submit an iterative implementation, then you will win the contest regardless of performance! :-)

    • cách đây 11 năm
  • NikolayBokhan
    NikolayBokhan
    • cách đây 11 năm

    Accurate answer will be received only if the regression will be solved like human does that. For AI and machine learning iterative way is usually used, just because for big arrays of data (20+ independent variables) it is much more efficient. if you need to solve the regression in human-like manner - sorry i will not help you (at least for this budget). Otherwise i need accuracy to be specified. Best regards, Nikolay.

    • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    Hi Nikolay. 1) It seems that this shouldn't have a significant bearing on the efficiency, as the correct answer can be found analytically. The precision should therefore be restricted only by the limitations of the types (i.e. double) and mathematical operations involved, within reason. 2) No, the data has not been normalized.

    • cách đây 11 năm
  • NikolayBokhan
    NikolayBokhan
    • cách đây 11 năm

    Dear jwashtell, I have following questions: 1) We should minimize the error. What is allowable accuracy? (0,1 or maybe 0,00001 ?) It has a great influence on the speed of the algorithm. 2) Is the incomming data normalized to any range? Could it happen that for some column we have values between 0 and 0,00001 and for the other between -1000 and 1000? Best regards, Nikolay

    • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    In case it is not obvious from the fact that this is an open contest - your submissions should link to your actual code, not just a note saying that you have done it or can do it :-)

    • cách đây 11 năm
  • jwashtell
    Chủ cuộc thi
    • cách đây 11 năm

    Thanks muneshparmar92. Please note: "Solutions should use only core Java, Math functions, etc. In particular no linear algebra libraries may be used." I've revised the description to read "Solutions should be implemented as a single class."

    • cách đây 11 năm
  • mrhassancse
    mrhassancse
    • cách đây 11 năm

    i'm interested..............................

    • cách đây 11 năm
  • muneshparmar92
    muneshparmar92
    • cách đây 11 năm

    import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
    import org.apache.commons.math3.stat.regression.SimpleRegression;
    final OLSMultipleLinearRegression regression2 = new OLSMultipleLinearRegression();
    double[] y = {
    4.0,
    8,
    13,
    };
    double[][] x2 =
    {
    { 1.0, 1, 1 },
    { 1.0, 2, 4 },
    { 0.0, 3, 9 },
    };
    regression2.newSampleData(y, x2);
    regression2.setNoIntercept(true);
    regression2.newSampleData(y, x2);
    double[] beta = regression2.estimateRegressionParameters();
    for (double d : beta) {
    System.out.println("D: " + d);
    }

    • cách đây 11 năm
  • muneshparmar92
    muneshparmar92
    • cách đây 11 năm

    double totalError=0;
    for (int i=0;i

    • cách đây 11 năm

Xem thêm bình luận

Làm thế nào để bắt đầu với cuộc thi

  • Đăng cuộc thi của bạn

    Đăng cuộc thi của bạn Nhanh chóng và dễ dàng

  • Nhận được vô số bài dự thi

    Nhận được vô số Bài dự thi Từ khắp nơi trên thế giới

  • Trao giải cho bài thi xuất sắc nhất

    Trao giải cho bài thi xuất sắc nhất Download File - Đơn giản!

Đăng cuộc thi ngay hoặc tham gia với chúng tôi ngay hôm nay!