Saved

Press the star next to a tool's name to save it.

All my tools

Polynomial Regression Calculator

Use this polynomial regression calculator to fit a quadratic, cubic or quartic curve, or an exponential curve, to your data, with R², mean squared error and residuals.

Restored your last values ·

Paste one x and y pair per line, choose the degree of the polynomial, and optionally an x to predict. You get the fitted equation, R², mean squared error and residuals. Choose exponential for a curve y = a × b^x, or the error mode to compare actual and predicted values.

Everything is worked out in your browser. Nothing you type is sent anywhere.

–

–

Enter your pairs, choose a fit, then press Calculate.

A polynomial regression calculator fits a curve through your x and y data by least squares. Choose a degree of 2 for a quadratic, 3 for a cubic or 4 for a quartic, and it gives the equation, the coefficients, R², the mean squared error and the residuals. It also fits an exponential curve and computes the mean squared error of any actual and predicted values.

How to use the polynomial regression calculator

  1. Paste your data, one x and y pair per line.
  2. Choose a polynomial fit and its degree, or an exponential fit, or the error mode for actual and predicted values.
  3. Add an x to predict, and press Calculate.

The model

The Wikipedia article on polynomial regression writes the model as y = β0 + β1x + β2x² + … + βm x to the power m + ε.[1] For n pairs of data, each y is the polynomial of x plus an error. The page puts this in matrix form, y = Xβ + ε, where each row of the design matrix X is 1, x, x², up to x to the power m.[1]

Least squares

The page gives the estimator as β̂ = (XᵀX)⁻¹Xᵀy, the unique least-squares solution, which exists when the order m is smaller than the number of points n.[1] It adds that X is a Vandermonde matrix, so XᵀX can be inverted if all the x values are distinct.[1] It also gives the expanded normal equations: the entry in row j and column k of the coefficient matrix is the sum of x to the power j + k, and the right-hand side is the sum of y times x to the power j.[1] The calculator solves these equations, after centring and scaling x for numerical stability, and then converts the answer back to the original x. The conversion is our own working. It requires at least m + 1 different x values, which is why the calculator tells you when you have too few.

A worked example

The points (1, 2), (2, 5), (3, 10), (4, 17) and (5, 26) lie exactly on y = x² + 1. With a degree of 2 the calculator returns that equation, with b0 = 1, b1 = 0 and b2 = 1, an R² of 1 and a mean squared error of 0. Ask for a prediction at x = 6 and it gives 37, with a note that this is outside your data, an extrapolation. The pages we read give no numerical example, so this one is ours.

Residuals, MSE and R²

The article on errors and residuals defines the residual as the difference between the observed value and the estimated value, so in regression it is the distance of each point from the fitted curve.[3] The mean squared error is the average of the squared differences, MSE = (1 ÷ n) times the sum of (Y − Ŷ)², and its square root is the RMSE.[2] The page says that the unbiased estimate of the error variance divides the residual sum of squares by the degrees of freedom instead of n.[2] The calculator shows both. The coefficient of determination is R² = 1 − SSres ÷ SStot, where SSres is the sum of the squared residuals and SStot the sum of squared deviations from the mean of y.[4] The page says that the best case is R² = 1, that a baseline model that always predicts the mean has R² = 0, and that R² can be negative for a bad fit.[4]

Choosing the degree

The article on R² says that R² never decreases as regressors are added, so a high degree always looks as good or better, and it gives an adjusted R² that rises only when the improvement is more than chance would give.[4] The errors page notes that if the data show a trend in the residuals, the model is likely incorrect, and the true function may be a quadratic or higher-order polynomial.[3] Plot the residuals: they should look random. The pages we read give no rule for choosing the degree, so start low and add a degree only when the residuals show a pattern.

Cautions about coefficients

The polynomial regression page warns that the coefficients are hard to interpret because the powers of x are highly correlated: for x uniform on the interval from 0 to 1, x and x² have a correlation of about 0.97.[1] It advises considering the fitted function as a whole.[1] A high-degree polynomial also swings wildly outside the data, so predictions beyond the range are unreliable; the calculator warns when you predict outside your x values.

Exponential fit

For growth or decay, choose the exponential fit y = a × b to the power x. The calculator fits a straight line to ln y against x and converts back, so every y must be positive. This is our own working because the pages we read do not cover it. The result is not the same as a least squares fit on y itself, so the R² and the MSE are computed on the original y values from the fitted curve. Data of 0 to 1, 1 to 2, 2 to 4 and 3 to 8 give y = 1 × 2^x, a growth of 100 percent per unit and a doubling every unit.

Mean squared error of predictions

In the error mode, enter the actual value then the predicted value on each line. The calculator gives the residuals, the MSE, the RMSE and R² of the predictions. For the pairs (3, 2.5), (−0.5, 0), (2, 2) and (7, 8) the squared errors are 0.25, 0.25, 0 and 1, so the MSE is 1.5 ÷ 4 = 0.375. This is our own example.

Common mistakes

  • Using too high a degree. It chases noise and predicts badly.
  • Extrapolating. Predictions outside the data range are unreliable.
  • Reading the coefficients one by one. The powers of x are correlated.

What next

Use this often? Add Polynomial R to your Home screen.

Frequently asked questions

What is a polynomial regression calculator?

A tool that fits a polynomial curve of a chosen degree to x and y data by least squares and gives the equation, R², MSE and residuals.

How do I fit a quadratic or cubic curve?

Choose polynomial fit and a degree of 2 for a quadratic or 3 for a cubic, paste your x and y pairs and press Calculate.

What is the mean squared error?

The average of the squared residuals, MSE = SSE ÷ n. An unbiased estimate divides by the degrees of freedom instead.

What is R²?

R² = 1 − SSres ÷ SStot. A value of 1 is a perfect fit and 0 is no better than predicting the mean.

Can I use it for exponential growth?

Yes. The exponential fit finds y = a × b^x from ln y, and reports the growth per unit and the doubling time.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Math Tools