Data and Model Fitting
Mathwrist C++ API Series
Abstract
This document details the C++ programming interface of data and model fitting features in Mathwrist’s Numerical Programming Library (NPL). Please refer to our “Data and Model Fitting” white paper for technical overview.
1 Linear Least Square
Assume a dimensional linear model parameterized by coefficient vector . Given number of observations , we provides two least square based methods to recover parameters in “linear_ls.h” header file.
1.1 Generalized Ridge Regression
Function linear_fit::grr() under namesapce linear_fit solves a generalized ridge regression problem as the following,
(1) |
Parameter Description:
-
1.
X is the matrix in equation (1). The -th row of holds the -th observation of n-dimensional independent variables.
-
2.
y is the vector in equation (1). The -th element of is the observed linear model value.
-
3.
Omega is a regularization matrix in equation (1). It needs be at least positive definite.
- 4.
-
5.
beta will holds the n-d linear model parameters on funciton return.
1.2 Constrained Least Square
Function linear_fit::lsq under namespace linear_fit minimizes the sum of square residuals (SSR) subject to general linear constraints and simple bounds.
s.t. | (2) | ||||
(3) | |||||
(4) |
Parameter Description:
-
1.
X is the matrix in equation (2). The -th row of holds the -th observation of n-dimensional independent variables.
-
2.
y is the vector in equation (2). The -th element of is the observed linear model value.
-
3.
c is the linear constraint specification that includes general linear constraints in equation (LABEL:eqn:linear-lsq-general-constraints) and simple bounds in equation (LABEL:eqn:linear-lsq-simple-bound). Please refer to our Linear Programming (LP) API for details of linear constraint specification.
-
4.
beta will holds the n-d linear model parameters on funciton return.
2 Nonlinear Least Square
Let be a -dimensional nonlinear function with model parameter . Nonlinear least square fit estimates parameter given number of observations , by minimizing the norm of residual vector,
(5) |
, where the -th element .
In header file “non_linear_ls.h”, we provide three least square based methods, all derived from the NonlinearLS base class, to calibrate model parameters . Each method is represented by a solver class designed with the following inheritance hierarchy,
2.1 Termination Control
The exact termination behavior depends on the actual fitting methodology implemented in a derived least square solver class. Please refer to our “Data and Model Fitting” white paper for the technical details of termination conditions. In general, a nonlinear least square solver terminates at a major iteration whenever any of the following conditions is satisfied,
-
1.
the residual satisfies , where is the residual tolerance.
-
2.
the max number of iterations is reached.
-
3.
both the stationary and convergence conditions are satisfied with , where is the convergence tolerance.
The max number of iterations and convergence tolerance are control functions defined in IterativeMethod base class. Please refer to our “1-d and n-d Functions” API documentation for the details of IterativeMethod.
The residual tolerance control functions are public member functions of the NonlinearLS base class.
Description:
-
1.
Set the tolerance to be stored as a class data member of NonlinearLS.
-
2.
Return the tolerance that is used to test termination condition.
2.2 Max Step Change
Description:
Due to the nonlinear nature of function , we want to restrict the max amount of step change at each iteration of the least square solver, details as the following:
-
•
In modified Gauss-Newton method, determines the max step change in a line search method.
-
•
In Levenberg-Marquardt method, determines the max trust region radius.
-
•
In regularized and constrained method, determines the max step change in a line search method.
The following two functions are public member functions of the NonlinearLS base class.
-
1.
Set the max step change passed from parameter magnitude.
-
2.
Return the max step change used by the solver.
2.3 Solver Operator
Description:
This is the public interface function declared in NonlinearLS base class and implemented by concrete nonlinera least square derived classes.
-
1.
Parameter residuals is the residual function .
-
2.
Parameter beta holds the initial guess of model parameter on call and holds the calibrated parameters on return.
2.4 Modified Gauss-Newton
Description:
These functions are public function of the derived class NLS_GaussNewton that uses a modified Gauss-Newton method to solve the nonlinear least square problem.
-
1.
Constructor, create a solver object.
-
2.
Set the bump size that is used to compute the second order Hessian term by finite (center) difference. Details in white paper.
-
3.
Return the bump size to compute by finite difference.
2.5 Levenberg-Marquardt
Description:
These functions are public function of the derived class NLS_Levenberg that uses the Levenberg-Marquardt method to solve the nonlinear least square problem.
-
1.
Constructor, create a solver object. Parameter max_radius determines the max trust region radius. Parameter max_lambda_iters determines the max number of iterations in root finding in trust region method. Details in white paper.
-
2.
Set the max number of iterations allowed to root find in trust region method.
-
3.
Return the max number of iterations allowed to root find in trust region method.
2.6 Regularization and Constraints
Description:
These functions are public function of the derived class NLS_General that adds a regularization term, and optionally additional constraints, to solve the nonlinear least square problem. The regularization matrix needs be at least positive semi-definite.
(6) |
(7) | |||
(8) |
-
1.
Constructor, create a solver object. Parameter _O, if supplied, is the regularization matrix in equation (6). If a null pointer is supplied, is taken as an identity matrix.
-
2.
Constructor, create a solver object. Parameter c is the linear constraint specification in equation (LABEL:eqn:nlsq-general-constraint). Please refer to our LP API for constraint specification details. Parameter _O is a pointer to the regularization matrix as the first constructor.
-
3.
Store the regularization penalty in equation (6) as a data member of the solver. If is supplied, the algorithm will use the Generalized Cross Validation (GCV) method to compute an optimal penalty factor .
3 Curve Fitting
We represent a 1-d curve function as the linear combination of a basis vector and coefficient vector .
Mathwrist NPL offers two choices of basis functions . Class SmoothSplineCurve uses B-spline as basis. Class SmoothChebyshevCurve uses Chebyshev polynomial of the first kind as basis. They are both derived from SmoothCurveFitting, which in turn inherits from the top level LinearModelFitting base class.
The relevant C++ header files are:
-
1.
linear_model_fitting.h
-
2.
smooth_curve_fitting.h
-
3.
smooth_spline_curve.h
-
4.
smooth_chebyshev_curve.h
3.1 Smooth Curve Fitting
This section documents the public interface of the SmoothCurveFitting base class. Derived class SmoothSplineCurve and SmoothChebyshevCurve share the same API as the base class.
3.1.1 Curve Domain
Description:
The curve is defined over a finite domain . x_lo() and x_hi() are implemented by concreted derived classes to return the boundary and respectively.
3.1.2 Fitted Curve
Description:
This function is implemented by concrete derived classes to retrieve the underlying curve object after the fitting.
3.1.3 Roughness Measure
Description:
The above enumeration lists available regularization choices of curve roughness measure . Users can call the property setting function set_reg_form() to choose the roughness measure to be used in curve fitting.
-
1.
INT_SQUARE_D0:
-
2.
INT_SQUARE_D1:
-
3.
INT_SQUARE_D2:
-
4.
INT_SQUARE_D3:
-
5.
SUM_SQUARE_DIFF_1: over predefined curve intervals .
-
6.
SUM_SQUARE_DIFF_2: over predefined curve intervals .
-
7.
L2_PARAM: .
3.1.4 Roughness Leverage
It is possible to partition into sub intervals and apply different roughness weights in different sub interval.
The sub interval weights play the role as micro leverage factors and can be set through a piecewise constant function .
Description:
-
1.
Set the leverage factors through . Parameter leverage is a reference to .
-
2.
Return if the current curve fitting object has micro leverage factors.
-
3.
Clear all micro leverage factors stored in the current fitting object.
3.1.5 Shape Constraints
Users can guide the curve fitting by restricting , where is the -th derivative of the curve function wrt . Effectively, it applies a numerical bound to curve value , slope and curvature at a given . The constraint specification is defined as a nested struct SmoothCurveFitting::Constraint.
Description:
-
1.
Enumeration Type indicates the constraint type , or .
-
2.
Data member Bound value is the numeric bound of the constraint.
-
3.
Data member double x is the location of the constrained point .
-
4.
Data member Type type specifies the constraint type.
-
5.
The first constructor creates an empty constraint object.
-
6.
The second constructor initializes the constraint object with numerical bound parmater b, location parameter xx and type paramter t.
-
7.
The comparison operator for internal use.
Users can add or remove constraints by calling the following member functions of class SmoothCurveFitting.
Description:
-
1.
Add a constraint at lower boundary . Parameter bv is the numberic bound of this constraint. Parameter bc indicates the constraint type.
-
2.
Add a constraint at upper boundary . Parameter bv is the numberic bound of this constraint. Parameter bc indicates the constraint type.
-
3.
Add a constraint at an interior point. Parameter c is the constraint object.
-
4.
Remove all constraints stored in the curve fitting object.
3.2 Data Fitting Operators
Given -element vectors and , where are the observed values of and function , we construct a basis matrix where its -th element is . Effectively is the vector of fitting errors (residuals). Based on the user’s choice of curve roughness measure, we construct a regularization (roughness) matrix .
Two data fitting methodologies are provided to recover parameter from the curve data. They are implemented by two public operators in the LinearModelFitting base class. Derived class SmoothSplineCurve and SmoothChebyshevCurve share the same fitting operators.
3.2.1 Regularized Least Squre
Description:
This functions recovers by solving the following regularized linear least square problem.
(9) |
, possibly subject to the shape constraints,
(10) |
Parameters:
-
1.
Parameter Xy is a matrix. Its first and second columns hold the observation vector and .
-
2.
Parameter lambda is the regularization penalty in equation (9). If is supplied, we compute an optimal penalty factor using the Generalized Cross Validation (GCV) method.
3.2.2 Error as Constraints
Description:
This functions recovers by minimizing the curve roughness subject to fitting errors constraints,
(11) |
and possibly additional curve shape constraints in equation (10).
Parameters:
-
1.
Parameter Xy is a matrix. Its first and second columns hold the observation vector and .
-
2.
Parameter eps is a vector of tolerance as fitting error constraints in equation (11).
-
3.
Parameter max_iter is the max number of iterations to solve (11).
-
4.
If parameter elastic_penalty is provided, the fitting error constraints are treated as elastic constraints.
3.3 Model Fitting Operators
In the context of model fitting, we design a mathematical model to be a smooth curve. The dependent variable is connected to independent variable by a known nonlinear mapping function through the model, i.e. .
Given model parameter , let be the vector of residual functions where the -th element is the residual error for observation . The LinearModelFitting base class provides two model fitting operators. Derived class SmoothSplineCurve and SmoothChebyshevCurve share the same fitting operators.
3.3.1 Regularized Least Squre
Description:
This fitting operator calibrates model parameter by solving the following regularized nonlinear least square problem.
(12) |
and possibly additional curve shape constraints in equation (10).
Parameters:
-
1.
residuals is the vector of residual functions .
-
2.
max_iter is the max number of iterations allowed to solve problem 12).
-
3.
exception_on_max_iter controls whether an exception should be thrown if the max number of iterations are reached.
-
4.
residual_tolerance is the residual tolerance for termination control. Details in white paper.
-
5.
converge_tolerance is the convergency tolerance for termination control. Details in white paper.
-
6.
max_step_move is the max step change at each iteration. Details in white paper.
-
7.
lambda is the penalty factor in equation (12). If is supplied, GCV method is used.
3.3.2 Error as Constraints
Description:
This fitting operator calibrates model parameter by minimizing the curve roughness subject to residual error constraints,
(13) |
and possibly additional curve shape constraints in equation (10).
Parameters:
-
1.
residuals is the vector of residual functions .
-
2.
eps is the vector of residual tolerance in equation (13).
-
3.
max_iter is the max number of iterations allowed to solve problem 13).
-
4.
exception_on_max_iter controls whether an exception should be thrown if the max number of iterations are reached.
-
5.
converge_tolerance is the convergency tolerance for termination control. Details in white paper.
-
6.
max_step_move is the max step change at each iteration. Details in white paper.
3.4 Model Parameter Access
The following functions are public interface functions declared in base class LinearModelFitting and implemented in concrete curve subclasses.
Description:
-
1.
Return the size of model parameter .
-
2.
Explicitly set the values of model parameter passed from a point parameter _beta.
-
3.
Explicitly set the values of model parameter passed from a matrix object reference parameter beta.
3.5 Smooth Spline Curve
Two constructors are provided to create a smooth curve using B-spline as the basis functions .
Parameters:
-
1.
The buffer parameter knots in the first constructor specifies the knot points of B-spline basis. is the lower boundary point . is the upper boundary point .
-
2.
The pointer parameter knots and size parameter n_knots in the second constructor together specify the knot points of B-spline basis. is the lower boundary point . is the upper boundary point .
-
3.
degree specifies the polynomial degree of B-spline basis.
-
4.
n_constraints specifies the max number of curve shape constraints to be added for fitting.
-
5.
reg specifies the choice of curve roughness measure.
3.6 Smooth Chebyshev Curve
The following constructor is provided to create a smooth curve using Chebyshev polynomial of the first kind as the basis functions .
Parameters:
-
1.
n_degree is the highest degree of Chebyshev polynomial basis to be used.
-
2.
n_constraints specifies the max number of curve shape constraints to be added for fitting.
-
3.
a is the lower boundary of curve domain .
-
4.
b is the upper boundary of curve domain .
-
5.
reg specifies the choice of curve roughness measure.
User can directly access the fitted Chebyshev curve object from the member function of SmoothChebyshevCurve class.
4 Surface Fitting
We represent a 2-d surface function as the tensor product of two basis vectors and ,
, where is the coefficient matrix of the tensor product. Both and are of the same type of basis functions.
Mathwrist NPL offers three concrete surfaces types based on the choices of basis functions. Class SmoothSplineSurface uses B-spline as basis. Class SmoothChebyshevSurface uses Chebyshev polynomial of the first kind as basis. A special case of B-spline basis is when and are piecewise linear basis. This special case is separately treated in class SmoothBilinearSurface. All these concrete surfaces are derived from SmoothSurfaceFitting class that in turn inherits from top level LinearModelFitting class.
The relevant C++ header files are:
-
1.
linear_model_fitting.h
-
2.
smooth_surface_fitting.h
-
3.
smooth_spline_surface.h
-
4.
smooth_chebyshev_surface.h
-
5.
smooth_bilinear_surface.h
4.1 Smooth Surface Fitting
This section documents the public interface of the SmoothSurfaceFitting base class, shared by SmoothSplineSurface, SmoothChebyshevSurface and SmoothBilinearSurface derived classes.
4.1.1 Surface Domain
Description:
The surface is defined over a rectangular domain . x_lo() and x_hi() return the boundary and respectively. y_lo() and y_hi() return the boundary and respectively. These virtual functions are implemented by concrete sub-classes.
4.1.2 Fitted Surface
Description:
This function is implemented by concrete sub-classes to retrieve the underlying surface object after fitting.
4.1.3 Roughness Measure
Description:
The above enumeration lists available regularization choices of surface roughness measure . Users can call the property setting function set_reg_form() to choose the roughness measure to be used in surface fitting.
-
1.
DIRICHLET_ENERGY:
.
-
2.
THIN_PLATE_ENERGY:
-
3.
FROBENIUS_NORM: is the Frobenius norm of .
4.1.4 Roughness Leverage
It is possible to partition the whole surface domain into sub areas and apply different roughness weights in different sub areas.
The sub area weights play the role as micro leverage factors and can be set through a 2-d piecewise constant function .
Description:
-
1.
Set the leverage factors through . Parameter leverage is a reference to .
-
2.
Return if the current surface fitting object has micro leverage factors.
-
3.
Clear all micro leverage factors stored in the current fitting object.
4.1.5 Shape Constraints
Users can guide the surface fitting by imposing constraints
, where is the operation to take -th order partial derivative wrt and then -th order partial derivative wrt . The supported values of and effective constraint types are the following:
-
1.
: function value is in range ;
-
2.
: partial delta is in range ;
-
3.
: partial delta is in range ;
-
4.
: gamma is in range ;
-
5.
: gamma is in range ;
-
6.
: cross gamma is in range ;
The constraint specification is defined as a nested struct SmoothSurfaceFitting::Constraint.
Description:
-
1.
Enumeration Type indicates the constraint type, effectively the operation.
-
2.
Data member Bound value is the numerical bound of the constraint.
-
3.
Data member double x is the coordinate of the constrained point.
-
4.
Data member double y is the coordinate of the constrained point.
-
5.
Data member Type type specifies the constraint type.
-
6.
The first constructor creates an empty constraint object.
-
7.
The second constructor initializes the constraint object with numerical bound parameter b, coordinate parameter xv, coordinate parameter yv and type parameter t.
-
8.
The comparison operator for internal use.
4.2 Data Fitting Operators
Given number of data points , , two data fitting methodologies are provided to recover parameter . They are implemented by two public operators in the LinearModelFitting base class.
4.2.1 Regularized Least Squre
Description:
This functions recovers by solving the following regularized linear least square problem.
(14) |
, possibly subject to the shape constraints,
(15) |
Parameters:
-
1.
Parameter Xy is a matrix. The -th row of the matrix holds the observation data point .
-
2.
Parameter lambda is the regularization penalty in equation (14). If is supplied, we compute an optimal penalty factor using the Generalized Cross Validation (GCV) method.
4.2.2 Error as Constraints
Description:
This functions recovers by minimizing the surface roughness subject to fitting errors constraints,
(16) | |||||
(17) |
and possibly additional surface shape constraints in equation (15).
Parameters:
-
1.
Parameter Xy is a matrix. The -th row of the matrix holds the observation data point .
-
2.
Parameter eps is a vector of tolerance as fitting error constraints in equation (LABEL:eqn:surface-optimization).
-
3.
Parameter max_iter is the max number of iterations to solve (LABEL:eqn:surface-optimization).
-
4.
If parameter elastic_penalty is provided, the fitting error constraints are treated as elastic constraints.
4.3 Model Fitting Operators
In the context of model fitting, we design a mathematical model to be a smooth surface. The dependent variable is connected to independent variables by a known nonlinear mapping function through the model, i.e. .
Given number of data points , , let be the vector of residual functions where the -th element is the residual error for observation . The LinearModelFitting base class provides two model fitting operators.
4.3.1 Regularized Least Squre
Description:
This fitting operator calibrates model parameter by solving the following regularized nonlinear least square problem.
(18) |
and possibly additional surface shape constraints in equation (15).
Parameters:
-
1.
residuals is the vector of residual functions .
-
2.
max_iter is the max number of iterations allowed to solve problem 18).
-
3.
exception_on_max_iter controls whether an exception should be thrown if the max number of iterations are reached.
-
4.
residual_tolerance is the residual tolerance for termination control. Details in white paper.
-
5.
converge_tolerance is the convergency tolerance for termination control. Details in white paper.
-
6.
max_step_move is the max step change at each iteration. Details in white paper.
-
7.
lambda is the penalty factor in equation (18). If is supplied, GCV method is used.
4.3.2 Error as Constraints
Description:
This fitting operator calibrates model parameter by minimizing the surface roughness subject to residual error constraints,
(19) |
and possibly additional surface shape constraints in equation (15).
Parameters:
-
1.
residuals is the vector of residual functions .
-
2.
eps is the vector of residual tolerance in equation (19).
-
3.
max_iter is the max number of iterations allowed to solve problem 19).
-
4.
exception_on_max_iter controls whether an exception should be thrown if the max number of iterations are reached.
-
5.
converge_tolerance is the convergency tolerance for termination control. Details in white paper.
-
6.
max_step_move is the max step change and at each iteration. Details in white paper.
4.4 Model Parameter Access
The following functions are public interface functions declared in base class LinearModelFitting and implemented in concrete smooth surface sub-classes.
Description:
-
1.
Return the number of model parameter .
-
2.
Explicitly set the values of model parameter passed from a point parameter _beta.
-
3.
Explicitly set the values of model parameter passed from a matrix object reference parameter beta.
4.5 Smooth Spline Surface
Two constructors are provided to create a smooth surface using B-spline as the basis functions for and .
Parameters:
-
1.
The buffer reference parametes knots_x and knots_y in the first constructor specifies the knot points of B-spline basis and .
-
2.
In the second constructor, the pointer parameter knots_x and size parameter n_knots_x together specify the B-spline basis knot points for . The pointer parameter knots_y and size parameter n_knots_y together specify the B-spline basis knot points for .
-
3.
degree_x specifies the polynomial degree of B-spline basis .
-
4.
degree_y specifies the polynomial degree of B-spline basis .
-
5.
n_constraints specifies the max number of surface shape constraints to be added for fitting.
-
6.
reg specifies the choice of surface roughness measure.
Users can directly access the fitted B-spline surface object from the member function of SmoothSplineSurface class.
4.6 Smooth Chebyshev Surface
The following constructors are provided to create a smooth surface using Chebyshev polynomial of the first kind as the basis functions for and .
Parameters:
-
1.
a is the lower boundary of surface coordinate domain .
-
2.
b is the upper boundary of surface coordinate domain .
-
3.
c is the lower boundary of surface coordinate domain .
-
4.
d is the upper boundary of surface coordinate domain .
-
5.
degree_x specifies the highest degree of Chebyshev polynomial basis .
-
6.
degree_y specifies the highest degree of Chebyshev polynomial basis .
-
7.
n_constraints specifies the max number of surface shape constraints to be added for fitting.
-
8.
reg specifies the choice of surface roughness measure.
Users can directly access the fitted Chebyshev surface object from the member function of SmoothChebyshevSurface class.
4.7 Smooth Bilinear Surface
Two constructors are provided to create a smooth surface for the special case when and are linear B-spline basis.
Parameters:
-
1.
The buffer reference parametes knots_x and knots_y in the first constructor specifies the knot points of basis and .
-
2.
In the second constructor, the pointer parameter knots_x and size parameter n_knots_x together specify the knot points for . The pointer parameter knots_y and size parameter n_knots_y together specify the knot points for .
-
3.
n_constraints specifies the max number of surface shape constraints to be added for fitting.
-
4.
reg specifies the choice of surface roughness measure. Thin-plate energy is not supported for bilinear surface.
Users can directly access the fitted bilinear surface object from the member function of SmoothBilinearSurface class.