Fitting the Nelson Siegel Curve

Fitting the Nelso Siege Curve essentially boils down to finding the 4 parameters (see Nelson Siegel) which minimizes the error (loss function) between the Nelson curve and the datapoints that are used to fit the curve.

This example utitiizes the Nelson Siegel API.

Error (Loss) Function

The error function is provided by the Nelson Siegel API looks like the following.
function error(data, level, slope, shape, decay,process = err=>err*err){ let crv = curve(level, slope, shape, decay); let total = 0; if(!Array.isArray(data))data = [data]; for(let item of data){ let t = item[0]; let value = item[1]; let value2 = crv(t); let err = process(value - value2); total += err; } return total; }

It sums the error from each point to the total. It utilizes a process function to process each individual error before doing the sum. The default processor is the square function (yielding a squared error)

Fitting the Curve

Given a defined error, a standard way to fit the function to the data is to use the gradient descent. The Nelson API provides a method named fit which runs the algorithm. algorithm.

Demonstration

Video Demo

Video Demo