 # SQL Server polynomial curve-fitting

POLYFIT_q

Updated: 30 April 2011

Use POLYFIT_q for calculating the coefficients of a polynomial p(x) of degree n that fits the x- and y-values supplied to the function. The result is a two-column table having n+1 rows, containing the polynomial coefficients in descending powers: POLYFIT_q only calculates the coefficients of the polynomial. To evaluate the polynomial for a value of x, use the POLYVAL function.
Syntax
SELECT * FROM [wct].[POLYFIT_q](
<@XY_RangeQuery, nvarchar(max),>
,<@n, int,>)
Arguments
@XY_RangeQuery
the SELECT statement, as text, used to determine the x- and y-values to be evaluated. The SELECT statement specifies the column names from the table or view or can be used to enter the x- and y-values directly. Data returned from the @XY_RangeQuery select must be of the type float or of a type that implicitly converts to float.
@n
an integer specifying the degree of the polynomial.
Return Types
TABLE (
[coe_num] [int] NULL,
[coe_val] [float] NULL
Remarks
·         The x- and y-values are passed to the function as pairs
·         If x is NULL or y is NULL, the pair is not used in the calculation.
·         @n must be less than or equal to the number of rows returned by @XY_RangeQuery
·         Use the POLYFIT function for simpler queries.
·         Use the POLYVAL function to evaluate the polynomial for an x-value.
Examples
In this example, we will use the westclintech SERIESFLOAT function to generate a series of x-values equally spaced in the interval [0, 2.5] and then evaluate the error function, ERF, at those points.
SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
from wct.SeriesFloat(0,2.5,0.1,NULL,NULL)
Since the function will execute the @XY_RangeQuery, all we need to do is but single quotes around the previous statement and pass it into POLYFIT_q. We will specify an approximating polynomial of 6 degrees.
SET NOCOUNT ON

SELECT *
FROM wct.POLYFIT_q(
'SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
from wct.SeriesFloat(0,2.5,0.1,NULL,NULL)'      --@XY_RangeQuery
,6                                              --@n
)
This produces the following result.
This means that there are 7 coefficients. We could run the following SQL to see the structure of the approximating polynomial.
SET NOCOUNT ON

SELECT *
,ROW_NUMBER() OVER (ORDER BY coe_num DESC) - 1 as pow
FROM wct.POLYFIT_q(
'SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
from wct.SeriesFloat(0,2.5,0.1,NULL,NULL)'      --@XY_RangeQuery
,6                                              --@n
)
This produces the following result.
Thus, the approximating polynomial would be:
0.00841937x6 - 0.0982996x5 + 0.421736x4 - 0.743463x3 + 0.147104x2 + 1.10645x + 0.000441174
We can inspect the fit of the approximating polynomial by creating a table of the x- and y-values and evaluating the approximating polynomial for each x and comparing it to the actual y-values. While we could do this using the output of the POLYFIT_q table-valued function, it is much simpler to just use the POLYVAL function.
SET NOCOUNT ON

SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
INTO #erf
FROM wct.SeriesFloat(0,2.5,0.1,NULL,NULL)

SELECT a.x
,ROUND(a.y, 8) as y
,ROUND(wct.POLYVAL(b.x, b.y, 6, a.x), 8) as f
,ROUND(wct.POLYVAL(b.x, b.y, 6, a.x) - a. y, 8) as [f - y]
GROUP BY a.x
,a.y
ORDER BY

DROP TABLE #erf
This produces the following result.
It looks like the approximating polynomial is accurate to about 3 or 4 decimal places. In fact if we graphed the results, they would look like this: We can see that in this range, [0, 2.5] the fit between the y-values and the f-values is quite good.
However, what if we extend the range of the interval from 2.5 to 6? We will simply change SERIESFLOAT to stop at 6.0 rather than 2.5 and then graph the output.
SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
INTO #erf
FROM wct.SeriesFloat(0,6.0,0.1,NULL,NULL)

SELECT a.x
,a.y
,wct.POLYVAL(b.x, b.y, 6, a.x) as f
,wct.POLYVAL(b.x, b.y, 6, a.x) - a. y as [f - y]
FROM #erf a, #erf b
GROUP BY a.x
,a.y
ORDER BY
This produces the following graph. It’s important to note that coefficients in the interval [0, 6] are different than the coefficients in the interval [0, 2.5] because there were more x- and y-values passed to the function. What if we wanted to use the coefficients from the [0, 2.5] interval to predict the values in the [0, 6] interval?
SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
INTO #erf
FROM wct.SeriesFloat(0,2.5,0.1,NULL,NULL)

SELECT SeriesValue as x
,wct.ERF(SeriesValue) as y
,wct.POLYVAL(b.x, b.y, 6, a.SeriesValue) as f
,wct.POLYVAL(b.x, b.y, 6, a.SeriesValue) - wct.ERF(SeriesValue) as [f - y]
FROM wct.SeriesFloat(0,6.0,0.1,NULL,NULL) a, #erf b
GROUP BY a.SeriesValue
,wct.ERF(SeriesValue)
ORDER BY

DROP TABLE #erf

This produces the following graph. As you can see, the polynomial approximation quickly diverges from the function in the region above 2.5.
Let’s look at a different example. In this example we will look at some interest rate values over a time-horizon and calculate the 3rd degree polynomial approximation. The x-values represent the number of years and the y-values are the decimal values of the interest rate (.01 = 1%).
SET NOCOUNT ON

SELECT *
FROM wct.POLYFIT_q(
'SELECT *
from (VALUES
(1,0.0028),
(2,0.0056),
(3,0.0085),
(5,0.0164),
(7,0.0235),
(10,0.0299),
(20,0.0382),
(30,0.0406)
) n(x,y)'
,3)
This produces the following result.

While we would always recommend using the POLYVAL function to calculate the polynomial approximation for any x-value, in this example we will use the results of the POLYFIT_q TVF to calculate the rates for each year from 1 through 30 out to 4 decimal places.
SET NOCOUNT ON

SELECT coe_num
,coe_val
,ROW_NUMBER() OVER (order by coe_num DESC) - 1 as pow
into #coe
FROM wct.POLYFIT_q(
'SELECT *
from (VALUES
(1,0.0028),
(2,0.0056),
(3,0.0085),
(5,0.0164),
(7,0.0235),
(10,0.0299),
(20,0.0382),
(30,0.0406)
) n(x,y)'
,3)

SELECT SeriesValue as y
,ROUND(SUM(POWER(SeriesValue,pow) * coe_val),4) as r
FROM wct.SeriesInt(1,30,NULL,NULL,NULL), #coe
GROUP BY SeriesValue
ORDER BY 1

DROP TABLE #coe
This produces the following result.

### Support  Copyright 2008-2023 Westclintech LLC         Privacy Policy        Terms of Service