IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39648


Ignore:
Timestamp:
Aug 5, 2016, 1:08:51 PM (10 years ago)
Author:
eugene
Message:

make the initial guess for IRLS stats use the median

Location:
trunk/Ohana/src/relphot
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/relphot/include/relphot.h

    r39643 r39648  
    1010
    1111# define ID_SECF_STACK_PRIMARY 0x00004000
     12
     13# define NBOOTSTRAP 100
    1214
    1315# ifndef MAX_INT
     
    121123  int    *msklist;            // mask modifications
    122124  int     Nlist;
     125
     126  double *values;
     127  double *wtvals;
     128  double *wtlist;
     129  double *ykeep;
     130  double *dykeep;
     131  double *wtkeep;
     132  double *ysample;
     133  double *dysample;
     134  double *wtsample;
     135  double *bvalue;
     136
    123137} StatDataSet;
    124138
  • trunk/Ohana/src/relphot/src/StatDataSetOps.c

    r39636 r39648  
    1414    ALLOCATE (dataset[i].measSeq,    int, Nmax);
    1515    ALLOCATE (dataset[i].msklist,    int, Nmax);
     16
     17    ALLOCATE (dataset[i].values,   double, Nmax);
     18    ALLOCATE (dataset[i].wtvals,   double, Nmax);
     19    ALLOCATE (dataset[i].wtlist,   double, Nmax);
     20    ALLOCATE (dataset[i].ykeep,    double, Nmax);
     21    ALLOCATE (dataset[i].dykeep,   double, Nmax);
     22    ALLOCATE (dataset[i].wtkeep,   double, Nmax);
     23    ALLOCATE (dataset[i].ysample,  double, Nmax);
     24    ALLOCATE (dataset[i].dysample, double, Nmax);
     25    ALLOCATE (dataset[i].wtsample, double, Nmax);
     26    ALLOCATE (dataset[i].bvalue,   double, NBOOTSTRAP);
    1627  } 
     28
    1729  return dataset;
    1830}
     
    2941    FREE (dataset[i].measSeq);
    3042    FREE (dataset[i].msklist);
     43
     44    FREE (dataset[i].values);
     45    FREE (dataset[i].wtvals);
     46    FREE (dataset[i].wtlist);
     47    FREE (dataset[i].ykeep);
     48    FREE (dataset[i].dykeep);
     49    FREE (dataset[i].wtkeep);
     50    FREE (dataset[i].ysample);
     51    FREE (dataset[i].dysample);
     52    FREE (dataset[i].wtsample);
     53    FREE (dataset[i].bvalue);
    3154  } 
    3255  FREE (dataset);
  • trunk/Ohana/src/relphot/src/liststats.c

    r39641 r39648  
    193193# define FLT_TOLERANCE 1e-6
    194194# define WEIGHT_THRESHOLD 0.3
    195 # define NBOOTSTRAP 100
    196195
    197196int fit_least_squares (double *fit, double *y, double *dy, double *wgt, double *wt, int Npts);
     
    202201int liststats_irls (StatDataSet *dataset, int Npoints, StatType *stats) {
    203202
    204   double value;
    205 
    206203  liststats_init (stats);
    207204
    208   // OLS
    209   if (!fit_least_squares (&value, dataset->flxlist, dataset->errlist, dataset->wgtlist, NULL, Npoints)) return FALSE;
     205  if (Npoints == 0) {
     206    double value = NAN;
     207    stats->mean = value;
     208    stats->min  = value;
     209    stats->max  = value;
     210    stats->Nmeas = Npoints;
     211    stats->chisq = NAN;
     212    stats->sigma = NAN;
     213    stats->error = NAN;
     214    return TRUE;
     215  }
     216
     217  if (Npoints == 1) {
     218    double value = dataset->flxlist[0];
     219    stats->mean = value;
     220    stats->min  = value;
     221    stats->max  = value;
     222    stats->Nmeas = Npoints;
     223    stats->chisq = NAN;
     224    stats->sigma = NAN;
     225    stats->error = dataset->errlist[0];
     226    return TRUE;
     227  }
     228
     229  int midpt = 0.5 * Npoints;
     230
     231  // make the initial guess based on the median (not weighted mean)
     232  // ALLOCATE_PTR (values, double, Npoints);
     233  for (int i = 0; i < Npoints; i++) {
     234    dataset->values[i] = dataset->flxlist[i];
     235  }
     236  dsort (dataset->values, Npoints);
     237  double value = (Npoints % 2) ? dataset->values[midpt] : 0.5*(dataset->values[midpt] + dataset->values[midpt-1]);
     238
     239  // OLS (replace by median above)
     240  // if (!fit_least_squares (&value, dataset->flxlist, dataset->errlist, dataset->wgtlist, NULL, Npoints)) return FALSE;
    210241 
    211242  // XXX add to dataset elements?
    212   ALLOCATE_PTR (wtvals, double, Npoints);
    213   ALLOCATE_PTR (wtlist, double, Npoints);
     243  // ALLOCATE_PTR (wtvals, double, Npoints);
     244  // ALLOCATE_PTR (wtlist, double, Npoints);
    214245
    215246  int converged = FALSE;
     
    218249    for (int i = 0; i < Npoints; i++) {
    219250      // we are only including the formal error, not the weight in the definition of wt[]
    220       wtvals[i] = weight_cauchy ((dataset->flxlist[i] - value) / dataset->errlist[i]);
     251      dataset->wtvals[i] = weight_cauchy ((dataset->flxlist[i] - value) / dataset->errlist[i]);
    221252    }
    222253   
    223254    double oldValue = value;
    224     if (!fit_least_squares (&value, dataset->flxlist, dataset->errlist, dataset->wgtlist, wtvals, Npoints)) {
     255    if (!fit_least_squares (&value, dataset->flxlist, dataset->errlist, dataset->wgtlist, dataset->wtvals, Npoints)) {
    225256      value = oldValue;
    226257      break;
     
    238269  // double Sum_W = 0.0;
    239270  for (int i = 0; i < Npoints; i++) {
    240     wtvals[i] = weight_cauchy ((dataset->flxlist[i] - value) / dataset->errlist[i]);
    241     wtlist[i] = wtvals[i];
    242     // Sum_W += wtvals[i];
    243   }
    244   dsort (wtlist, Npoints);
    245   int midpt = 0.5 * Npoints;
    246   double WtMedian = (Npoints % 2) ? wtlist[midpt] : 0.5*(wtlist[midpt] + wtlist[midpt-1]);
     271    dataset->wtvals[i] = weight_cauchy ((dataset->flxlist[i] - value) / dataset->errlist[i]);
     272    dataset->wtlist[i] = dataset->wtvals[i];
     273    // Sum_W += dataset->wtvals[i];
     274  }
     275  dsort (dataset->wtlist, Npoints);
     276  double WtMedian = (Npoints % 2) ? dataset->wtlist[midpt] : 0.5*(dataset->wtlist[midpt] + dataset->wtlist[midpt-1]);
    247277  // double WtThreshold = WEIGHT_THRESHOLD * Sum_W / (1.0 * Npoints);
    248278  double WtThreshold = WEIGHT_THRESHOLD * WtMedian;
     
    250280  // generate the unmasked subset
    251281  // XXX add these to the dataset elements?
    252   ALLOCATE_PTR ( ykeep, double, Npoints);
    253   ALLOCATE_PTR (dykeep, double, Npoints);
    254   ALLOCATE_PTR (wtkeep, double, Npoints);
     282  // ALLOCATE_PTR ( ykeep, double, Npoints);
     283  // ALLOCATE_PTR (dykeep, double, Npoints);
     284  // ALLOCATE_PTR (wtkeep, double, Npoints);
    255285
    256286  // save unmasked points
     
    261291  double dSig = 0.0;
    262292  for (int i = 0; i < Npoints; i++) {
    263     if ((wtvals[i] < WtThreshold) || !isfinite(dataset->flxlist[i])) {
     293    if ((dataset->wtvals[i] < WtThreshold) || !isfinite(dataset->flxlist[i])) {
    264294      dataset->msklist[i] = TRUE; // mark the masked points
    265295      continue;
    266296    }
    267      ykeep[Nkeep] = dataset->flxlist[i];
    268     dykeep[Nkeep] = dataset->errlist[i];
    269     wtkeep[Nkeep] = dataset->wgtlist[i]; // externally-supplied weight
     297     dataset->ykeep[Nkeep] = dataset->flxlist[i];
     298    dataset->dykeep[Nkeep] = dataset->errlist[i];
     299    dataset->wtkeep[Nkeep] = dataset->wgtlist[i]; // externally-supplied weight
    270300    Nkeep ++;
    271301   
     
    283313  // bootstrap resampling to generate the errorbars
    284314  // XXX add these to the dataset elements?
    285   ALLOCATE_PTR (ysample,  double, Nkeep);
    286   ALLOCATE_PTR (dysample, double, Nkeep);
    287   ALLOCATE_PTR (wtsample, double, Nkeep);
    288   ALLOCATE_PTR (bvalue,   double, NBOOTSTRAP); // vector to save the bootstrap values
     315  // ALLOCATE_PTR (ysample,  double, Nkeep);
     316  // ALLOCATE_PTR (dysample, double, Nkeep);
     317  // ALLOCATE_PTR (wtsample, double, Nkeep);
     318  // ALLOCATE_PTR (bvalue,   double, NBOOTSTRAP); // vector to save the bootstrap values
    289319
    290320  int Nboot = 0;
     
    295325      // I need to draw Npoints random entries from 'points' with replacement:
    296326      int N = Nkeep * drand48();
    297        ysample[i] =  ykeep[N];
    298       dysample[i] = dykeep[N];
    299       wtsample[i] = wtkeep[N];
    300     }
    301 
    302     if (!fit_least_squares (&value, ysample, dysample, wtsample, NULL, Nkeep)) continue;
    303 
    304     bvalue[Nboot] = value;
     327      dataset->ysample[i]  = dataset->ykeep[N];
     328      dataset->dysample[i] = dataset->dykeep[N];
     329      dataset->wtsample[i] = dataset->wtkeep[N];
     330    }
     331
     332    if (!fit_least_squares (&value, dataset->ysample, dataset->dysample, dataset->wtsample, NULL, Nkeep)) continue;
     333
     334    dataset->bvalue[Nboot] = value;
    305335    Nboot ++;
    306336  }
    307337
    308   dsort (bvalue, Nboot);
     338  dsort (dataset->bvalue, Nboot);
    309339 
    310   double Slo = VectorFractionInterpolate (bvalue, 0.158655, Nboot);
    311   double Shi = VectorFractionInterpolate (bvalue, 0.841345, Nboot);
     340  double Slo = VectorFractionInterpolate (dataset->bvalue, 0.158655, Nboot);
     341  double Shi = VectorFractionInterpolate (dataset->bvalue, 0.841345, Nboot);
    312342  stats->error = (Shi - Slo) / 2.0;
    313 
    314   free (bvalue);
    315   free ( ysample);
    316   free (dysample);
    317   free (wtsample);
    318   free ( ykeep);
    319   free (dykeep);
    320   free (wtkeep);
    321   free (wtvals);
    322   free (wtlist);
    323343
    324344  return TRUE;
  • trunk/Ohana/src/relphot/src/select_images.c

    r39643 r39648  
    4646  double DmaxSkyRegion = region[0].Dmax;
    4747
    48   double dD = CALIBRATE_STACKS_AND_WARPS ? 0.5 : 5.0;
     48  double dD = CALIBRATE_STACKS_AND_WARPS ? 0.0 : 5.0;
    4949  double dR = dD / cos(RAD_DEG*DminSkyRegion);
    5050  RminSkyRegion -= dR;
Note: See TracChangeset for help on using the changeset viewer.