Skip to content

PolySlope

Description

PolySlope calculates the slope of a polynomial fit to the data at a specified value of X. It is a useful helper function for error propagation in inverse regression.

Syntax

PolySlope(Y, X, x_u, [p=1], [S]) where

  • Y and X are the dependent and independent variables (both vectors)
  • x_u is the value at which to evaluate the slope
  • p is the polynomial order, defaulting to p = 1 (best fit line)
  • S is a vector of standard deviations of the Y values. If provided then weighted least-squares is used to calculate the fit.

The function returns the slope of the polynomial fit at x=xux = x_u; if S is specified then the slope of the weighted LS fit is returned.

Source Code

Helpers used: _PolyFit

PolySlope = LAMBDA(
    Y, X,       // input one-dimensional data
    x_u,        // value at which to evaluate the slope
    [p],        // regression order (default is 1)
    [S],       // optional std devs of Y for weighted LS
    LET(
        p, IF(ISOMITTED(p),1,p),
        // calculate polynomial regression coeffs b_1 ... b_p
        fit, IF(ISOMITTED(S), _PolyFit(Y, X, p, 1), _PolyFit(Y, X, p, S)),
        b, DROP(TAKE(fit, 1), , 1),
        // now calculate the slope at x = x_u using the power rule
        IF(
            ISTEXT(INDEX(fit, 1, 1)),
            INDEX(fit, 1, 1),
            IF(
                x_u = 0,            // causes an error
                INDEX(b, 1, 1),     // initial slope is always b_1
                SUM(SEQUENCE(, p) * b * x_u ^ SEQUENCE(, p, 0))
            )
        )
    )
);