Beta

Polynomial regression

Description:

In statistics, linear regression is a technique of fitting a straight line to given data points so that average square error is minimal. By extension polynomial regression is fitting nth degree polynomial which minimizes square error.

In this kata you will try to write a polynomial regression solver which finds nth degree polynomial to fit n+1 data points. Since you will always have n+1 data points, your polynomial function will fit all data points perfectly - you will be looking for a unique least squares solution rather than doing optimisation.

Let's look at some simple examples:
If you're given two data points {x: -1, y: -1} and {x: 1, y: 1}, 1st degree polynomial that fits these point perfectly is f(x) = x.
If you're given three data points {x: -1, y: 1}, {x: 0, y: 0} and {x: 2, y: 4}, polynomial f(x) = x^2 would fit them perfectly.

So, your task is to write function solveRegression. It will accept an array of data points as such:
[{x: -1, y: -1}, {x: 0, y: 5}, ...]

Your method should return a string of following form: f(x) = 2x^2 - x + 1.5. Please note the following rules for result formatting:

  • All coefficients should be rounded to 5 decimal places
  • If a given coefficient is 0, the term should be ignored, i.e. f(x) = 0x + 5 is invalid (should be f(x) = 5
  • Negative coefficients should be formatted as follows: f(x) = - 5x - 1

P.S. Please suggest rank for this kata, I was not sure 4 kyu is suitable.

Algorithms
Mathematics
Algebra

Similar Kata:

Stats:

CreatedApr 24, 2015
PublishedApr 24, 2015
Warriors Trained118
Total Skips23
Total Code Submissions141
Total Times Completed15
JavaScript Completions15
Total Stars4
% of votes with a positive feedback rating80% of 5
Total "Very Satisfied" Votes3
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes0
Total Rank Assessments6
Average Assessed Rank
5 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • zvytas Avatar
Ad