piecewiseLinFit#
- swordfish.function.piecewiseLinFit()#
Fit a continuous piecewise linear function for a specified number of line segments. Use differential evolution to find the optimal location of breakpoints for a given number of line segments by minimizing the sum of the square error. Note: Due to the randomness of the differential evolution, the results of this function may vary slightly each time.
The fitted model can be used as an input for function pwlfPredict.
- Parameters:
X (Constant) – A numeric vector indicating the data point locations of x. Null value is not allowed.
Y (Constant) – A numeric vector indicating the data point locations of y. Null value is not allowed.
numSegments (Constant) – A positive integer indicating the desired number of line segments.
XC (Constant, optional) – A numeric vector indicating the x locations of the data points that the piecewise linear function will be forced to go through. It only takes effect when method=’de’.
YC (Constant, optional) – A numeric vector indicating the y locations of the data points that the piecewise linear function will be forced to go through. It only takes effect when method=’de’.
bounds (Constant, optional) – A numeric matrix of shape (numSegments-1, 2), indicating the bounds for each breakpoint location within the optimization.
lapackDriver (Constant, optional) – A string indicating which LAPACK driver is used to solve the least-squares problem. It can be ‘gelsd’ (default), ‘gelsy’ and ‘gelss’.
degree (Constant, optional) – A non-negative integer indicating the degree of polynomial to use. The default is 1 for linear models. Use 0 for constant models.
weights (Constant, optional) – A numeric vector indicating the weights used in least-squares algorithms. The individual weights are typically the reciprocal of the standard deviation for each data point, where weights[i] corresponds to one over the standard deviation of the ith data point. Null value is not allowed.
method (Constant, optional) –
A string indicating the model used. It can be:
’nm’ (default): Nelder-Mead simplex algorithm.
’bfgs’: BFGS algorithm.
’lbfgs’: LBFGS algorithm.
’slsqp’: Sequential Least Squares Programming algorithm.
’de’: Differential Evolution algorithm.
maxIter (Constant, optional) – An integral scalar or vector indicating the maximum number of iterations for the optimization algorithm during the fitting process.
initialGuess (Constant, optional) – A numeric vector indicating the initial guess for the parameters that optimize the function. Its length is numSegments-1.
seed (Constant, optional) – An integer indicating the random number seed used in the differential evolution algorithm to ensure the reproducibility of results. It only takes effect when method=’de’ or initialGuess is null. If not specified, a non-deterministic random number generator is used.
- Returns:
A dictionary with the following keys:
breaks: A floating-point vector indicating the breakpoint locations.
beta: A floating-point vector indicating the beta parameter for the linear fit.
xData: A floating-point vector indicating the input data point locations of x.
yData: A floating-point vector indicating the input data point locations of y.
XC: A floating-point vector indicating the x locations of the data points that the piecewise linear function will be forced to go through.
YC: A floating-point vector indicating the y locations of the data points that the piecewise linear function will be forced to go through.
weights: A floating-point vector indicating the weights used in least-squares algorithms.
degree: A non-negative integer indicating the degree of polynomial.
lapackDriver: A string indicating the LAPACK driver used to solve the least-squares problem.
numParameters: An integer indicating the number of parameters.
predict: The function used for prediction. The method is called by model.predict(X, [beta], [breaks]). See pwlfPredict.
modelName: A string “Piecewise Linear Regression” indicating the model name.
- Return type: