NLFitSession::GetChiSqr

Description

Calculate the current reduced Chi-square value.

Syntax

void        GetChiSqr(vector& vChiSqr = NULL, bool bGenerateOutputs = false)

Parameters

vChiSqr
[output] If not NULL, it receives Chi-Sqr value.
bGenerateOutputs
[input] If true, will generate(update) outputs that can be retrieved by GetFitResultsParams, else will not recalculate the outputs and the outputs retrieved by GetFitResultsParams will only be junk.

Return

There is no return value for this function.

Examples

EX1

This example shows how to get reduced chi-sqr and output results on multiple datasets.

  1. Prior to running the following example, the nlsf_utils.c file need to be loaded and compiled. This can be done from script with the command run.LoadOC(Originlab\nlsf_utils.c) or just add this file to your workspace.
  2. New a worksheet and import \Samples\Curve Fitting\Gaussian.dat.
  3. Copy and compile the following codes, and run "NLFitSession_GetChiSqr_ex1" in Command window.
#include <..\originlab\NLFitSession.h>

void NLFitSession_GetChiSqr_ex1()
{
        Worksheet wks = Project.ActiveLayer();
        if ( !wks || wks.GetNumCols() < 3 )
                return;
        
        NLFitSession nlfSession;
        
        //set fit function
        if ( !nlfSession.SetFunction("Gauss") )
        {
                out_str("Fail to set function!");
                return;
                
        }
        
        const int nDatasets = 2; //two datasets
        //prepare input dataset 1
        XYRange drInput;
        drInput.Add(wks, 0, "X");
        drInput.Add(wks, 1, "Y");
        
        vector vX, vY;
        drInput.GetData(vY, vX);
        
        //set data 1
        if ( !nlfSession.SetData(vY, vX, NULL, 0, nDatasets ) )
        {
                out_str("Fail to set data 1!");
                return;
        }
        
        //prepare input dataset 2
        drInput.Reset();
        drInput.Add(wks, 0, "X");
        drInput.Add(wks, 2, "Y");    
        drInput.GetData(vY, vX);
        
        //set data 2
        if ( !nlfSession.SetData(vY, vX, NULL, 1, nDatasets ) )
        {
                out_str("Fail to set data 2!");
                return;
        }
        
        
        //set parameters
        vector vParams(8); //gauss function need y0, xc, w, A each dataset, and there are two datasets.
        vParams[0] = 4;
        vParams[1] = 25;
        vParams[2] = 20;
        vParams[3] = 255;
        
        vParams[4] = 3;
        vParams[5] = 20;
        vParams[6] = 15;
        vParams[7] = 25;
        
        nlfSession.SetParamFix(4, true); //y0 of dataset 2 as fixed.
        
        nlfSession.SetEnableLinearConstraints(true, NULL);
        
        if ( 0 != nlfSession.SetParamValues(vParams) )
        {
                out_str("Fail to set parameter values!");
                return;
        }

        //generate and show results.
        vector  vChiSqr;
        bool       bGenerateOutputs = true;
        nlfSession.GetChiSqr(vChiSqr, bGenerateOutputs);
        
        if ( vChiSqr.GetSize() != nDatasets )
        {
                out_str("Fail to get Chi-Sqr of all datasets!");
                return;
        }
        
        for ( int iData = 0; iData < nDatasets; iData++ )
        {
                if ( vChiSqr.GetSize() > iData )
                        printf("Reduced Chi-Sqr of dataset %d is : %lf\n", iData + 1, vChiSqr[iData]);
                
                if( bGenerateOutputs ) //the output results is updated and reliable
                {
                        FitParameter arrFitParams[4]; //y0, xc, w, A
                        RegStats regStats;
                        NLSFFitInfo fitInfo;
                        if ( nlfSession.GetFitResultsStats(&regStats, &fitInfo, false, iData) && nlfSession.GetFitResultsParams(arrFitParams, &regStats, iData) > 0 )
                        {
                                printf("\nFit parameters : \n");
                                vector<string> vParaNames = {"y0", "xc", "w", "A"};
                                for ( int iPara = 0; iPara < vParaNames.GetSize(); iPara++ )
                                {
                                        _show_fitparams(arrFitParams[iPara], vParaNames[iPara]);
                                }
                                
                                printf("\nRegular statistics : \n");
                                _show_regstats(regStats);
                        }
                }
        }
        return;
}

static     void _show_fitparams(FitParameter& fitParams, LPCSTR lpcszParamName)
{
        printf("Parameter %s value : %lf\n", lpcszParamName, fitParams.Value);
        printf("Parameter %s fix : %s\n", lpcszParamName, fitParams.Fix ? "True" : "False");
        printf("Parameter %s error : %lf\n", lpcszParamName, fitParams.Error);
        return;
        
}

static void _show_regstats(RegStats& regStats)
{
        printf("Number of points : %d\n", (int)regStats.N);
        printf("Degrees of freedom : %lf\n", regStats.DOF);
        printf("ReducedChisq(Chi^2/DOF) : %lf\n", regStats.ReducedChiSq);
        printf("Regression sum of squares : %lf\n", regStats.SSR);
        printf("Correlation : %lf\n", regStats.Correlation);
        printf("R Value : %lf\n", regStats.Rvalue);
        printf("Coefficient of determination : %lf\n", regStats.RSqCOD);
        printf("Adjusted residual sum of squares : %lf\n", regStats.AdjRSq);
        printf("Root_MSE(SD) : %lf\n", regStats.RMSESD);
        printf("Norm of Residuals : %lf\n", regStats.NormResiduals);
        return;
}

Remark

See Also

GetFitResultsParams

header to Include

Originlab\NLFitSession.h