2.1.11.19 ocmath_polynomial_fit


Description

Function to Perform polynomail fit on the given X and Y data points. The function will normailize X data first, and then call ocmath_multiple_linear_regression(), which is the main computational engine, and finally correct the variance-covariance matrix.

Syntax

int ocmath_polynomial_fit( int nSize, const double * pX, const double * pY, const double * pWeightIn, int nOrder, const LROptions * psLROptions, FitParameter * psFitParameter, int nSizeFitParams, RegStats * psRegStats = NULL, RegANOVA * psRegANOVA = NULL, double * pCov = NULL, double * pCorr = NULL, double * pH = NULL, int nSizeCovCorr = -1 )

Parameters

nSize
[Input] the number of observations, n. n>=2;
pX
[Input] vector containing data points of the independent variables, size of pX = nSize;
pY
[Input] vector containing data points of dependent variable, size of pY = nSize;
pWeightIn
[Input] vector containing weight of the data, all data should be bigger than zero, size of pY = nsize. Can be NULL if no weighting;
nOrder
[Input] the polynomial order, nOrder >= 1;
psLROptions
[Input] structuure for linear regression options;
psFitParameter
[Output] array of structs to receive information on each fitting parameter;
nSizeFitParams
[input] size (number of structs) of psFitParameter, must be at least (nOrder+1);
psRegStats
[output] Optional, pointer to struct containing Regression statistics;
psRegANOVA
[output] Optional, pointer to struct containing ANOVA statistics;
pCov
[output] Optional, coriviance matrix of estimate; The size should be (nOrder+1)*(nOrder+1).
pCorr
[output] Optional, correlation matrix of estimate, having the same size as pCov.
pH
[output] Optional output pointer to diagonal element of H, leverage Size of the pH should be nSize
nSizeCovCorr
[input] size of pCov and pCorr, will check this size if pCov or pCorr passed and nSizeCovCorr >= 0 and will return STATS_ERROR_ARRAY_TOO_SMALL if nSizeCovCorr < (nOrder+1)*(nOrder+1)

Return

Returns STATS_NO_ERROR or positive on successful exit and a negative STATS error code on failure.

LARGE_X_NORMALIZED(1): the independent data have large value and small range, so are normalized first.

Error Codes:

STATS_ERROR_ARRAY_TOO_SMALL(-99): nSizeCovCorr < (nOrder+1)*(nOrder+1)

STATS_INPUT_NULL_POINTER(-156): pX and pY must not be empty.

STATS_ERROR_TOO_FEW_DATA_PTS(-165): nSize must not be less than 2

STATS_ERROR_SETTING(-151): nOrder must not be less than 1

STATS_ERROR_SVD_FAIL(-179): The singular value decomposition is failed

STATS_ERROR_RANK_DEFICIENT(-180): The pX is not full rank.

STATS_ERROR_BAD_WEIGHT(-108): Some data of the weight are less than zero

Examples

EX1

void ocmath_polynomial_fit_ex1()
{
    int nOrder;    
    int nRet;    
    //parameters required by stats_polynomial_fit
    LROptions sLROptions;
    //LROptionsNANUM(sLROptions);
    RegStats sRegStats;    
    RegANOVA sRegANOVA;

    int nSize = 10;
    vector vX, vY;
    vX.SetSize(10);
    vY.SetSize(10);
    for(int i=0; i<nSize; i++)  // Y = X^2
    {
        vX[i] = i+1;
        vY[i] = (i+1)*(i+1);
    }        

    double vW[10];                        

    for(i=0; i<nSize; i++)
    {
        vW[i] = 1;
    }

    sLROptions.Confidence = 0.95;    

    nOrder = 2;        
    double pCov[9], pCorr[9] ;
    int nParam = (sLROptions.FixIntercept) ? nOrder : nOrder + 1;

    FitParameter sFitParameter[3];

    nRet = ocmath_polynomial_fit(nSize,vX,vY,vW,nOrder,&sLROptions,sFitParameter,nParam,&sRegStats,&sRegANOVA,pCov,pCorr);
    if (nRet == LARGE_X_NORMALIZED)
        printf("x data are normalized!\n");
    else if (nRet < STATS_NO_ERROR)
        printf("Error!\n");
}

Remark

See Also

Header to Include

origin.h

Reference