IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 36290


Ignore:
Timestamp:
Nov 20, 2013, 3:21:02 PM (13 years ago)
Author:
watersc1
Message:

This should resolve the issues with the fitted statistics. Briefly,
the prior change to enforce 5-sigma clipping thresholds altered the
effect of the choice of binsize. Because of this, we we making the
histogram for the fitting data with binsizes far too small to be
appropriate. This led to very noisy histograms, and poor centering
for the fit, and so very bad fits, with very bad statistics.

I've changed the binsize calculation to attempt to keep the 2-sigma
bin filled with ~50 data samples. This seems to produce a usable
histogram, and the fitted statistics match better. There is a slight
reduction in the mean bias from the random data test, and some
improvement in the associated sigma values. This appears to be the
same in the ppSim test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psStats.c

    r36238 r36290  
    10491049       
    10501050#if (CZW)
    1051         printf("CZW: bad sigma?: %f %f  %f %f  %f %f  %f %f %f  %f\n",
    1052                binH2F32,binL2F32,binHiF32,binLoF32,binH4F32,binL4F32,
    1053                sigma1,sigma2,sigma4,sigma);
     1051        //      printf("CZW: bad sigma?: %f %f  %f %f  %f %f  %f %f %f  %f\n",
     1052        //             binH2F32,binL2F32,binHiF32,binLoF32,binH4F32,binL4F32,
     1053        //             sigma1,sigma2,sigma4,sigma);
    10541054       
    1055         printf("CZW (%d): median %f sigma %f delta: %f \n\t %f %f %f %f %f %f %f \n\t %f %f %f %f %f %f %f\n",
     1055        printf("CZW Robust (%d): median %f sigma %f delta: %f \n\t %f %f %f %f %f %f %f \n\t %f %f %f %f %f %f %f\n",
    10561056               iterate,
    1057            stats->robustMedian,stats->robustStdev,
    1058            fabs(cumulative->bounds->data.F32[binMedian] - cumulative->bounds->data.F32[binMedian + 1]),
    1059 
    1060            cumulative->bounds->data.F32[binMedian-3],cumulative->bounds->data.F32[binMedian-2],
    1061            cumulative->bounds->data.F32[binMedian-1],
    1062            cumulative->bounds->data.F32[binMedian],
    1063            cumulative->bounds->data.F32[binMedian+1],
    1064            cumulative->bounds->data.F32[binMedian+2],cumulative->bounds->data.F32[binMedian+3],
    1065 
    1066            cumulative->nums->data.F32[binMedian-3],cumulative->nums->data.F32[binMedian-2],
    1067            cumulative->nums->data.F32[binMedian-1],
    1068            cumulative->nums->data.F32[binMedian],
    1069            cumulative->nums->data.F32[binMedian+1],
    1070            cumulative->nums->data.F32[binMedian+2],cumulative->nums->data.F32[binMedian+3]);
     1057               stats->robustMedian,stats->robustStdev,
     1058               fabs(cumulative->bounds->data.F32[binMedian] - cumulative->bounds->data.F32[binMedian + 1]),
     1059               
     1060               cumulative->bounds->data.F32[binMedian-3],cumulative->bounds->data.F32[binMedian-2],
     1061               cumulative->bounds->data.F32[binMedian-1],
     1062               cumulative->bounds->data.F32[binMedian],
     1063               cumulative->bounds->data.F32[binMedian+1],
     1064               cumulative->bounds->data.F32[binMedian+2],cumulative->bounds->data.F32[binMedian+3],
     1065               
     1066               cumulative->nums->data.F32[binMedian-3],cumulative->nums->data.F32[binMedian-2],
     1067               cumulative->nums->data.F32[binMedian-1],
     1068               cumulative->nums->data.F32[binMedian],
     1069               cumulative->nums->data.F32[binMedian+1],
     1070               cumulative->nums->data.F32[binMedian+2],cumulative->nums->data.F32[binMedian+3]);
    10711071        //      PS_VECTOR_PRINT_F32(histogram->bounds);
    10721072        //      PS_VECTOR_PRINT_F32(histogram->nums);
     
    12621262            // set roughly so that the lowest bins have about 2 cnts
    12631263            // Nsmallest ~ N50 / (4*dN))
    1264             psF32 dN = PS_MAX (1, PS_MIN (4, stats->robustN50 / 8));
    1265            binSize = guessStdev / dN;
     1264          //            psF32 dN = PS_MAX (1, PS_MIN (4, stats->robustN50 / 8));
     1265
     1266          // CZW 2013-11-20: We know that the histogram is going to be basically Gaussian.
     1267          // Furthermore, we only use the inner +/- 2 sigma parts.  Therefore, define the
     1268          // binsize such that the bin at 2 sigma contains ~50 points (S/N ~ 7).  robustN50
     1269          // contains half the total points, so 2 * robustN50 / 50 is the fraction of all
     1270          // points in the 2 sigma bin.  Dance the erf() relations around, and it looks like
     1271          // there's a factor of about 1/20 to include.  Keep the PS_MAX to ensure we never bin
     1272          // wider than 1 sigma when the number of points is small.
     1273          psF32 dN = PS_MAX(1, (stats->robustN50 / 500.0));
     1274          binSize = guessStdev / dN;
    12661275        }
    12671276
     
    12911300        // We no longer want to reset the binSize here, as it can cause odd things.  Better to select
    12921301        // a number of bins, and then set the min/max values to put those bins sanely around the mean.
    1293         long numBins = PS_MAX (50, PS_MIN (10000, (max - min) / binSize));
     1302        //        long numBins = PS_MAX (50, PS_MIN (10000, (max - min) / binSize));
    12941303        //        binSize = (max - min) / (float) numBins;
    12951304        psTrace(TRACE, 6, "The new min/max values are (%f, %f).\n", min, max);
    12961305        psTrace(TRACE, 6, "The new bin size is %f.\n", binSize);
    1297         psTrace(TRACE, 6, "The numBins is %ld\n", numBins);
     1306        //        psTrace(TRACE, 6, "The numBins is %ld\n", numBins);
    12981307
    12991308
     
    13051314          max = guessMean + FITTED_CLIPPING_NUM * guessStdev;
    13061315        }
    1307        
     1316        long numBins = PS_MAX (50, PS_MIN (10000, (max - min) / binSize));
     1317        if (CZW) { printf("I've clipped: %f %f => %f %f ; %f %ld\n",guessMean,guessStdev,min,max,binSize,stats->robustN50); }
    13081318        psHistogram *histogram = psHistogramAlloc(min, max, numBins); // A new histogram (without outliers)
    13091319        if (!psVectorHistogram(histogram, myVector, errors, mask, maskVal)) {
     
    13571367            }
    13581368            psTrace (TRACE, 6, "(%f = %.0f) ", histogram->bounds->data.F32[i], histogram->nums->data.F32[i]);
     1369            if (CZW) { printf("CENTERED_HISTOGRAM: %f %f\n",
     1370                              PS_BIN_MIDPOINT(histogram,i),
     1371                              histogram->nums->data.F32[i]); }
    13591372        }
    13601373        psTrace (TRACE, 6, "\n");
    13611374
     1375        if (CZW) { printf("Bin selection done: %ld %f %f %ld %f %f %ld %f %f\n",
     1376                          binMin,PS_BIN_MIDPOINT(histogram,binMin),histogram->nums->data.F32[binMin],
     1377                          binMax,PS_BIN_MIDPOINT(histogram,binMax),histogram->nums->data.F32[binMax],
     1378                          binPeak,PS_BIN_MIDPOINT(histogram,binPeak),histogram->nums->data.F32[binPeak]);
     1379        }
     1380       
    13621381        // assume a reasonably well-defined gaussian-like population; run from peak out until val < 0.25*peak
    13631382        psTrace(TRACE, 6, "The clipped numBins is %ld\n", binMax - binMin);
     
    14111430            psPolynomial1D *poly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2);
    14121431            bool status = psVectorFitPolynomial1D (poly, NULL, 0, y, NULL, x);
     1432#if (CZW && 1)
     1433            printf("CZW: LowfitPoly: %f %f %f\n",poly->coeff[0],poly->coeff[1],poly->coeff[2]);
     1434            for (long i = 0; i < x->n; i++) {
     1435              printf("CZW: Lowfit: %d %ld %f %f %f\n",
     1436                     status,i,x->data.F32[i],y->data.F32[i],
     1437                     poly->coeff[0] + poly->coeff[1] * x->data.F32[i] +
     1438                     poly->coeff[2] * pow(x->data.F32[i],2));
     1439            }
     1440#endif
    14131441            psFree(x);
    14141442            psFree(y);
     
    14171445                psErrorClear();
    14181446                COUNT_WARNING(10, 100, "Failed to fit a gaussian to the robust histogram.\n");
    1419 
    14201447                psFree(poly);
    14211448                psFree(histogram);
     
    14261453            if (poly->coeff[2] >= 0.0) {
    14271454                COUNT_WARNING(10, 100, "Failed parabolic fit: %f + %f x + %f x^2\n", poly->coeff[0], poly->coeff[1], poly->coeff[2]);
     1455
    14281456                psFree(poly);
    14291457                psFree(histogram);
     
    14781506                }
    14791507            }
     1508
    14801509            psTrace(TRACE, 6, "Lower bound for symmetric range: %f (%ld)\n",
    14811510                    PS_BIN_MIDPOINT(histogram, binS), binS);
     
    14991528            psPolynomial1D *poly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2);
    15001529            bool status = psVectorFitPolynomial1D (poly, NULL, 0, y, NULL, x);
    1501 #if (CZW && 0)
     1530#if (CZW && 1)
     1531            printf("CZW: FullfitPoly: %f %f %f\n",poly->coeff[0],poly->coeff[1],poly->coeff[2]);
    15021532            for (long i = 0; i < x->n; i++) {
    1503               printf("CZW: Dcheck: %ld %f %f %f\n",
    1504                      i,x->data.F32[i],y->data.F32[i],
     1533              printf("CZW: Fullfit: %d %ld %f %f %f\n",
     1534                     status,i,x->data.F32[i],y->data.F32[i],
    15051535                     poly->coeff[0] + poly->coeff[1] * x->data.F32[i] +
    15061536                     poly->coeff[2] * pow(x->data.F32[i],2));
Note: See TracChangeset for help on using the changeset viewer.