IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 12338


Ignore:
Timestamp:
Mar 8, 2007, 1:23:00 PM (19 years ago)
Author:
rhl
Message:

Allow for loss of degrees of freedom in fitting when estimating variance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/rel-1_0/psLib/src/math/psMinimizePolyFit.c

    r10999 r12338  
    1010 *  @author EAM, IfA
    1111 *
    12  *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2007-01-09 22:38:53 $
     12 *  @version $Revision: 1.29.2.1 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2007-03-08 23:23:00 $
    1414 *
    1515 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    879879        double stdevValue = psStatsGetValue (stats, stdevOption);
    880880
     881        // correct for the number of degrees of freedom absorbed by the fit
     882        const int nterm = psPolynomial1DNterm(poly);
     883        if (Nkeep <= nterm) {   // we can't estimate the standard deviation
     884            psError(PS_ERR_UNKNOWN, true, "Number of data points %d is <= number of degrees of freedom %d",
     885                    Nkeep, nterm);
     886            psFree(resid);
     887            psFree(fit);
     888           
     889            return false;
     890        }
     891        stdevValue *= (Nkeep - 1)/(Nkeep - nterm - 1);
     892
    881893        psTrace("psLib.math", 5, "Mean is %f\n", meanValue);
    882894        psTrace("psLib.math", 5, "Stdev is %f\n", stdevValue);
     
    10451057    }
    10461058    psFree(xySums);
    1047 
     1059#if 0
     1060    /*
     1061     * psMatrixGJSolve doesn't detect singular matrices very well. I'll do an SVN decomp
     1062     * to test for this -- but this is quite expensive, and a better solver would be wise. RHL.
     1063     *
     1064     * Don't run this test for now as I made the Gauss-Jordan solver require that |pivot| > FLT_EPSILON
     1065     *
     1066     * We could use lambda/evectors to solve the system without proceeding to a Gauss-Jordan
     1067     * solver, but I'm too lazy for now
     1068     */
     1069    psVector *lambda = psVectorAlloc(A->numCols, A->type.type); // eigenvalues
     1070    psImage *evectors = psMatrixSVD(NULL, lambda, A); // eigenvectors
     1071
     1072    for (int i = 0; i < lambda->n; i++) {
     1073        const double l = psVectorGet(lambda, i);
     1074
     1075        if (l > -FLT_EPSILON && l < FLT_EPSILON) {
     1076            psError(PS_ERR_UNKNOWN, true, "Found almost zero eigenvalue %g.  Returning NULL.\n", l);
     1077            psFree(lambda); psFree(evectors);
     1078            psFree(A);
     1079            psFree(B);
     1080            return false;
     1081        }
     1082    }
     1083
     1084    psFree(lambda);
     1085    psFree(evectors);
     1086#endif
    10481087    if (!psMatrixGJSolve(A, B)) {
    10491088        psError(PS_ERR_UNKNOWN, false, "Could not solve linear equations.  Returning NULL.\n");
     
    11161155        result = VectorFitPolynomial2DOrd(poly, mask, maskValue, f64, fErr64, x64, y64);
    11171156        if (!result) {
    1118             psError(PS_ERR_UNKNOWN, true, "Could not fit polynomial.  Returning NULL.\n");
     1157            psError(PS_ERR_UNKNOWN, false, "Could not fit polynomial.  Returning NULL.\n");
    11191158        }
    11201159        break;
     
    12531292        if (!psVectorStats(stats, resid, NULL, mask, maskValue)) {
    12541293            psError(PS_ERR_UNKNOWN, false, "Could not compute statistics on the resid vector.  Returning NULL.\n");
    1255             psFree(resid)
    1256             psFree(fit)
     1294            psFree(resid);
     1295            psFree(fit);
    12571296            return false;
    12581297        }
     
    12601299        double meanValue = psStatsGetValue (stats, meanOption);
    12611300        double stdevValue = psStatsGetValue (stats, stdevOption);
     1301
     1302        // correct for the number of degrees of freedom absorbed by the fit
     1303        const int nterm = psPolynomial2DNterm(poly);
     1304        if (Nkeep <= nterm) {   // we can't estimate the standard deviation
     1305            psError(PS_ERR_UNKNOWN, true, "Number of data points %d is <= number of degrees of freedom %d",
     1306                    Nkeep, nterm);
     1307            psFree(resid);
     1308            psFree(fit);
     1309           
     1310            return false;
     1311        }
     1312        stdevValue *= (Nkeep - 1)/(Nkeep - nterm - 1);
    12621313
    12631314        psTrace("psLib.math", 5, "Mean is %f\n", meanValue);
Note: See TracChangeset for help on using the changeset viewer.