Gauss-Newton non-linear regression.

This class is useful for computing the coefficients for some non-linear equation using the Gauss-Newton algorithm.

Algorithm (see 2): b_n+1 = b_n + (J^T J)^-1 J r( b_n ) Where b is the current guess for the coefficients, n is the current guess J is the Jacobian, and r is the residual (error).

This is an abstract class and can not be used by itself. It requires the the function and function partial differentials to be define.

package GaussNewtonRegression
abstract

 Methods

Evaluate function at x.

getFunction(float $x, array $coefficients) : float

Return f( x ) for a given set of coefficients.

abstract

Parameters

$x

float

Real value given to equation.

$coefficients

array

Coefficients used in calculation.

Returns

floatValue result of equation.

Compute R-Squared.

getR_Squared(array $x, array $y, array $coefficients) : float

Compute Coefficient of determination (R squared) for a set of coefficients. This is useful for determining how good a fit the coefficients are. (See 5)

Parameters

$x

array

Array of x data points.

$y

array

Array of y data points that correspond to x.

$coefficients

array

Current coefficients to compare against.

Returns

floatReal value between 0 and 1.

Refine coefficients one around.

refineCoefficients(array $x, array $y, array $coefficients) : array

Run an iteration of the Gauss-Newton algorithm. Run this as many times as needed to refine coefficients to their best values.

Parameters

$x

array

Array of x data points.

$y

array

Array of y data points that correspond to x.

$coefficients

array

Initial or previous guess at coefficients.

Returns

arrayAn array that is the new guess at coefficients.

Number of coefficients.

getNumberOfCoefficients() : integer

abstract

Returns

integerThe number of coefficients for the equation being used.

Compute partial-differential of x.

partialDifferential(float $x, integer $coefficientIndex, array $coefficients) : float

Compute the partial-differential with respect to a given coefficient for a given value of x. p( x ) = d f( x ) / d c_n Example:

  If f( x ) = c_0 e^(c_1 x ), then:
    d f( x ) / d c_0 = e^( c_1 x )
    d f( x ) / d c_1 = c_0 x e^( c_1 x )
  Each function must be defined.

Parameters

$x

float

Value of x to supply to partial-differential function.

$coefficientIndex

integer

Which coefficient to be used in the partial-differential function.

$coefficients

array

Values of the coefficients to be used.

Returns

floatp( x ) for the supplied input.

Get a matrix of how much error is between the calculated value, and the true value.

getErrorMatrix(float $x, float $y, array $coefficients) : array

Parameters

$x

float

Array of x-coordinates.

$y

float

Array of known y-coordinates that correspond to $x.

$coefficients

array

Current coefficients to check for error.

Returns

arrayMatrix of error at each point.

Multiply two matrices (see 1).

multiply(array $matrix, array $multiplicand) : array

[ A ] = [ B ][ C ]

Parameters

$matrix

array
  • Matrix [ B ].

$multiplicand

array
  • Matrix [ C ].

Returns

arrayMatrix [ C ].

Solve a system of equations in matrix form.

solve(array $matrix, array $answers) 

Done by concatenating answer matrix to the left, doing Gaussian elimination (see 3), and returning the last column. [ M ][ C ] = [ A ] ==> [ C ] = [ M ]^1 [ A ]

output array Single column matrix [ C ].

Parameters

$matrix

array
  • Square matrix [ M ].

$answers

array
  • Single column matrix [ A ].

Return the transpose of a matrix.

transpose(array $matrix) : array

Parameters

$matrix

array

Any matrix.

Returns

arrayA matrix representing transpose.