PIXELBANKv8.2.1
Menu

Polynomial Regression Error

Implement a polynomial regression error calculator to evaluate the goodness of fit for a given polynomial model. The task involves computing the mean squared error between observed data points and predicted values based on the polynomial coefficients.

The concept of polynomial regression is a form of least squares optimization, where a polynomial function of degree nn is fitted to a set of data points (xi,yi)(x_i, y_i). The polynomial function can be represented as p(x)=a0+a1x+a2x2++anxnp(x) = a_0 + a_1x + a_2x^2 + \cdots + a_nx^n, where aia_i are the coefficients of the polynomial. The goal is to find the best fit polynomial that minimizes the difference between observed and predicted values.

To compute the error, follow these steps:

  1. Evaluate the polynomial at each data point xix_i using the given coefficients.
  2. Compute the squared difference between the observed value yiy_i and the predicted value p(xi)p(x_i).
  3. Average the squared differences over all data points.
MSE=1ni(yip(xi))2MSE = \frac{1}{n}\sum_i (y_i - p(x_i))^2

This technique is widely used in data analysis and machine learning for model evaluation and optimization.

Example:

Input:
poly_mse([0, 1], [(0,0), (1,1), (2,2)])
Output:
0.0
Reasoning:
  • The coefficients [0, 1] define the polynomial p(x)=0+1x=xp(x) = 0 + 1x = x.
  • Evaluate p(x)p(x) at each xix_i:
    • x=0p(0)=0x=0 \Rightarrow p(0)=0
    • x=1p(1)=1x=1 \Rightarrow p(1)=1
    • x=2p(2)=2x=2 \Rightarrow p(2)=2
  • Compare to the given points (xi,yi)(x_i, y_i): predicted p(xi)p(x_i) equals actual yiy_i for all three points, so each error (yip(xi))(y_i - p(x_i)) is 00 and each squared error is 00.
  • Compute MSE: MSE=13(02+02+02)=0.0\text{MSE} = \frac{1}{3}(0^2 + 0^2 + 0^2) = 0.0.

Constraints:

  • Return MSE rounded to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.