IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 12, 2021, 4:16:47 PM (6 years ago)
Author:
eugene
Message:

update fit1d_irls to include priors on the parameters

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.data/fit1d_irls.c

    r39640 r41470  
    1010double weight_cauchy (double x);
    1111double VectorFractionInterpolate (double *values, float fraction, int Npts);
     12
     13// using file-globals to carry values into 'fit_least_squares'
     14double *priorValue = NULL;
     15double *priorStdev = NULL;
    1216
    1317int fit1d_irls (int argc, char **argv) {
     
    7377    UseMean = FALSE;
    7478  }
     79  int UsePriors = FALSE;
     80  if ((N = get_argument (argc, argv, "-use-priors"))) {
     81    remove_argument (N, &argc, argv);
     82    UsePriors = TRUE;
     83  }
    7584
    7685  if ((argc != 4) || !dyvec) {
    7786    // require a y error value
    78     gprint (GP_ERR, "USAGE: fit1d x y order -dy wt [-quiet/-q] [-wt wtOut] [-yfit yfit] [-mask maskOut]\n");
     87    gprint (GP_ERR, "USAGE: fit1d_irls x y order [-dy wt] [-quiet/-q] [-wt wtOut] [-yfit yfit] [-mask maskOut] [-Nboot N] [-max-iterations N] [-weight-threshold f] [-use-median] [-use-priors]\n");
     88    gprint (GP_ERR, "  [-dy wt]        : optional error vector (use value of 1 by default)\n");
     89    gprint (GP_ERR, "  [-quiet/-q]     : reduce verbosity\n");
     90    gprint (GP_ERR, "  [-wt wtOut]     : save the weights in supplied vector\n");
     91    gprint (GP_ERR, "  [-yfit yfit]    : save the fitted values in supplied vector\n");
     92    gprint (GP_ERR, "  [-mask maskOut] : save the masked / unmasked state (weight below threshold\n");
     93    gprint (GP_ERR, "  [-Nboot N]      : number of bootstrap samples\n");
     94    gprint (GP_ERR, "  [-max-iterations N] : max number of iterations\n");
     95    gprint (GP_ERR, "  [-weight-threshold f] : limit for masked values\n");
     96    gprint (GP_ERR, "  [-use-median]   : use median (as opposed to mean) to calculate weight threshold\n");
     97    gprint (GP_ERR, "  [-use-priors]   : include priors for parameters (specified as prior value $P0v and stdev $dP0, $P1v, $dP1, etc).  Unspecified or NAN values are ignored.\n");
    7998    return (FALSE);
    8099  }
     
    100119  order = atof (argv[3]);
    101120  nterm = order + 1;
    102   mterm = 2*nterm;
     121  mterm = 2*order + 1;
    103122
    104123  ALLOCATE_PTR (yoff, opihi_flt, xvec[0].Nelements);
     
    119138  }
    120139
     140  if (UsePriors) {
     141    ALLOCATE (priorValue, double, nterm);
     142    ALLOCATE (priorStdev, double, nterm);
     143    for (i = 0; i < nterm; i++) {
     144      int isFound;
     145      double tmpValue;
     146      snprintf (name, 64, "P%dv", i);
     147      tmpValue = get_double_variable (name, &isFound);
     148      priorValue[i] = isFound ? tmpValue : NAN;
     149      snprintf (name, 64, "dP%d", i);
     150      tmpValue = get_double_variable (name, &isFound);
     151      priorStdev[i] = isFound ? tmpValue : NAN;
     152      if (!isFound) priorValue[i] = NAN; // need both Pnv and dPn
     153    }
     154  }
     155
    121156  // define initial weights as 1.0
    122157  for (i = 0; i < xvec[0].Nelements; i++) {
     
    137172    }
    138173
     174    // save this solution
     175    for (i = 0; i < nterm; i++) {
     176      b_old[i][0] = b[i][0];
     177    }
     178
    139179    if (!fit_least_squares (s, b, c, nterm, mterm, xvec[0].elements.Flt, yvec[0].elements.Flt, dyvec[0].elements.Flt, wt, NULL, xvec[0].Nelements)) {
    140       // restore
     180      // restore the saved solution
    141181      for (i = 0; i < nterm; i++) {
    142182        b[i][0] = b_old[i][0];
    143183      }
    144184      break;
    145     }
    146 
    147     // save this solution
    148     for (i = 0; i < nterm; i++) {
    149       b_old[i][0] = b[i][0];
    150185    }
    151186
     
    172207  return TRUE; */
    173208
     209  // save the solution. we re-use b[][] below in the bootstrap resampling
    174210  for (i = 0; i < nterm; i++) {
    175211    b_old[i][0] = b[i][0];
    176212  }
    177 
    178213
    179214  // calculate the weight thresholds to mask the bad points:
     
    307342  free (dysample);
    308343
     344  if (priorValue) {
     345    free (priorValue); priorValue = NULL;
     346    free (priorStdev); priorStdev = NULL;
     347  }
     348
    309349  return (TRUE);
    310350
     
    356396    for (j = 0; j < nterm; j++) {
    357397      c[i][j] = s[i + j];
     398      if (priorValue && (i == j)) {
     399        if (!isnan(priorValue[i])) {
     400          c[i][i] += 1.0 / SQ(priorStdev[i]);
     401          b[i][0] += priorValue[i] / SQ(priorStdev[i]);
     402        }
     403      }
    358404    }
    359405  }
Note: See TracChangeset for help on using the changeset viewer.