Changeset 39181
- Timestamp:
- Nov 20, 2015, 3:33:29 PM (11 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.astro/fitplx_irls.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.astro/fitplx_irls.c
r39168 r39181 15 15 double *dY; 16 16 17 double *IRLS_u; 17 double *Wx; 18 double *Wy; 19 18 20 int *index; 19 21 int Npts; … … 32 34 int Nfit; 33 35 int getChisq; 34 } PlxFit_IRLS; 36 } PlxFit; 37 38 39 int FitPMandPar (PlxFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts, int VERBOSE); 35 40 36 41 int VectorRobustStats_IRLS (Vector *vector, double *median, double *sigma); … … 45 50 int PlxBootstrapResample_IRLS (PlxFitData *src, PlxFitData *tgt); 46 51 47 int FitPMandPar_IRLS (PlxFit _IRLS *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, double *IRLS_u, int Npts, int max_iterations, int VERBOSE);52 int FitPMandPar_IRLS (PlxFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, double *Wx, double *Wy, int Npts, int max_iterations, double outlier_limit, int VERBOSE); 48 53 int sun_ecliptic_IRLS (double mjd, double *lambda, double *beta, double *epsilon, double *Radius); 49 54 int ParFactor_IRLS (double *pR, double *pD, double RA, double DEC, double Time); 50 55 51 int IRLS_converged (PlxFit _IRLS*fit);56 int IRLS_converged (PlxFit *fit); 52 57 int Plx_weighted_LS (double *T, double *pR, double *pD, double *X, double *WX, double *Y, double *WY, int Npts, 53 58 double **A, double **B, int VERBOSE); … … 114 119 remove_argument (N, &argc, argv); 115 120 } 121 122 Vector *outMask = NULL; 123 if ((N = get_argument (argc, argv, "-out-mask"))) { 124 remove_argument (N, &argc, argv); 125 if (!(outMask = SelectVector(argv[N], ANYVECTOR, TRUE))) return FALSE; 126 remove_argument (N, &argc, argv); 127 } 128 129 double outlier_limit = 0.1; 130 if ((N = get_argument (argc, argv, "-outlier-limit"))) { 131 remove_argument (N, &argc, argv); 132 outlier_limit = atof(argv[N]); 133 remove_argument (N, &argc, argv); 134 } 135 116 136 117 137 if (argc != 6) { … … 119 139 gprint (GP_ERR, " -outlier-tests Nsamples dPsigMax : run Nsample bootstrap-resamples to define the path deviations and reject based on dPsigMax\n"); 120 140 gprint (GP_ERR, " -dPsig vec : save path deviations in vec\n"); 121 gprint (GP_ERR, " -max_iterations : maximum number of IRLS iterations to run\n"); 141 gprint (GP_ERR, " -max-iterations : maximum number of IRLS iterations to run (default 10)\n"); 142 gprint (GP_ERR, " -outMask vect : save outlier mask in vec\n"); 143 gprint (GP_ERR, " -outlier-limit : fraction of average weight to reject on (default 0.1)\n"); 122 144 return (FALSE); 123 145 } … … 151 173 int Ntotal = tvec->Nelements; // XXX check other lengths 152 174 if (dPvec) ResetVector (dPvec, OPIHI_FLT, Ntotal); 153 175 if (outMask) ResetVector (outMask, OPIHI_INT, Ntotal); 176 154 177 double Rmean, Dmean, Tmean; 155 178 PlxSetMeanEpoch_IRLS (R, D, T, &Rmean, &Dmean, &Tmean, mask, Ntotal); … … 166 189 PlxSetEpochPosition_IRLS (&fitdata, R, D, dR, dD, T, mask, Ntotal, &coords, Tmean); 167 190 168 PlxFit _IRLS fit; memset (&fit, 0, sizeof(PlxFit_IRLS));191 PlxFit fit; memset (&fit, 0, sizeof(PlxFit)); 169 192 170 193 if (0) { … … 195 218 fitdata.Y, fitdata.dY, 196 219 fitdata.t, fitdata.pX, fitdata.pY, 197 fitdata. IRLS_u,198 fitdata.Npts, max_iterations, VERBOSE)) {220 fitdata.Wx, fitdata.Wy, 221 fitdata.Npts, max_iterations, outlier_limit, VERBOSE)) { 199 222 return FALSE; 200 223 } 201 224 // Set the output mask based on the input mask and the outlier limits. 225 if (outMask) { 226 double Sum_Wx = 0; 227 double Sum_Wy = 0; 228 int *outMask_V = outMask->elements.Int; 229 for (i = 0; i < fitdata.Npts; i++) { 230 Sum_Wx += fitdata.Wx[i]; 231 Sum_Wy += fitdata.Wy[i]; 232 } 233 for (i = 0; i < Ntotal; i++) { 234 outMask_V[i] = mask ? mask[i] : 0; 235 } 236 237 for (i = 0; i < fitdata.Npts; i++) { 238 if ((fitdata.Wx[i] > outlier_limit * Sum_Wx / (1.0 * fitdata.Npts))|| 239 (fitdata.Wy[i] > outlier_limit * Sum_Wy / (1.0 * fitdata.Npts))) { 240 int n = fitdata.index[i]; 241 outMask_V[n] = 1; 242 243 if (VERBOSE == 2) { 244 fprintf (stderr, "%f %f : %f %d : %f %f %f : %f %f %f %f\n", R[n], D[n], T[n], outMask_V[n], fitdata.t[i], fitdata.X[i], fitdata.Y[i], fitdata.Wx[i], fitdata.Wy[i], Sum_Wx, Sum_Wy); 245 } 246 } 247 } 248 } 249 250 202 251 if (Nresample){ 203 252 PlxFitData sample; 204 253 PlxFitDataAlloc_IRLS (&sample, fitdata.Npts); 205 254 206 PlxFit _IRLS*testfit = NULL;207 ALLOCATE (testfit, PlxFit _IRLS, Nresample);255 PlxFit *testfit = NULL; 256 ALLOCATE (testfit, PlxFit, Nresample); 208 257 209 258 int Ngood = 0; … … 215 264 // fit the sample 216 265 testfit[Ngood].getChisq = FALSE; 217 if (!FitPMandPar _IRLS(&testfit[Ngood],218 sample.X, sample.dX,219 sample.Y, sample.dY, sample.t,220 sample.pX, sample.pY,221 sample.IRLS_u, sample.Npts, 1, VERBOSE)) continue;266 if (!FitPMandPar (&testfit[Ngood], 267 sample.X, sample.dX, 268 sample.Y, sample.dY, sample.t, 269 sample.pX, sample.pY, 270 sample.Npts, VERBOSE)) continue; 222 271 Ngood ++; 223 272 } … … 292 341 set_variable ("Nfit", fit.Nfit); 293 342 343 344 294 345 return (TRUE); 295 346 } 296 347 297 348 /* do we want an init function which does the alloc and a clear function to free? */ 298 int FitPMandPar_IRLS (PlxFit _IRLS*fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD,299 double * IRLS_u,300 int Npts, int max_iterations, int VERBOSE) {349 int FitPMandPar_IRLS (PlxFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, 350 double *Wx, double *Wy, 351 int Npts, int max_iterations, double outlier_limit, int VERBOSE) { 301 352 302 353 int i,j; … … 310 361 311 362 double sigma_ols, sigma_hat; // Sigma estimates. 312 double *Wx, *Wy; // Weight vectors. Not errors.363 // double *Wx, *Wy; // Weight vectors. Not errors. 313 364 double *rx, *ry; // Deviation from model 314 //double *u; // Deviation magnitude365 double *u; // Deviation magnitude 315 366 int converged; 316 367 int iterations; … … 338 389 ALLOCATE(Beta, double, 5); 339 390 ALLOCATE(Beta_prev, double, 5); 340 ALLOCATE(Wx, double, Npts);341 ALLOCATE(Wy, double, Npts);391 // ALLOCATE(Wx, double, Npts); 392 // ALLOCATE(Wy, double, Npts); 342 393 ALLOCATE(rx, double, Npts); 343 394 ALLOCATE(ry, double, Npts); 395 ALLOCATE(u, double, Npts); 344 396 345 397 // Convert the measurement errors into the initial weights. … … 416 468 rx[i] = X[i] - (T[i] * B[1][0] + B[0][0] + B[4][0] * pR[i]); 417 469 ry[i] = Y[i] - (T[i] * B[3][0] + B[2][0] + B[4][0] * pD[i]); 418 IRLS_u[i] = sqrt(SQ(rx[i] / dX[i]) + SQ(ry[i] / dY[i]));470 u[i] = sqrt(SQ(rx[i] / dX[i]) + SQ(ry[i] / dY[i])); 419 471 } 420 472 421 473 // Calculate sigma_hat from distribution of residual magnitudes 422 sigma_hat = Plx_MAD( IRLS_u,Npts) / 0.6745;474 sigma_hat = Plx_MAD(u,Npts) / 0.6745; 423 475 424 476 // Check convergence … … 511 563 double wx,wy; 512 564 for (i = 0; i < Npts; i++) { 513 if ((Wx[i] > 0.1* Sum_Wx / (1.0 * Npts))||514 (Wy[i] > 0.1* Sum_Wy / (1.0 * Npts))) {565 if ((Wx[i] > outlier_limit * Sum_Wx / (1.0 * Npts))|| 566 (Wy[i] > outlier_limit * Sum_Wy / (1.0 * Npts))) { 515 567 516 568 Xf = fit[0].Ro + fit[0].uR*T[i] + fit[0].p*pR[i]; … … 584 636 ALLOCATE (data->pX, double, N); 585 637 ALLOCATE (data->pY, double, N); 586 ALLOCATE (data->IRLS_u, double, N); 638 ALLOCATE (data->Wx, double, N); 639 ALLOCATE (data->Wy, double, N); 587 640 ALLOCATE (data->index, int, N); 588 641 return TRUE; … … 597 650 FREE (data->pX); 598 651 FREE (data->pY); 599 FREE (data->IRLS_u); 652 FREE (data->Wx); 653 FREE (data->Wy); 600 654 FREE (data->index); 601 655 } … … 614 668 tgt->pX[i] = src->pX[N]; 615 669 tgt->pY[i] = src->pY[N]; 616 tgt->IRLS_u[i] = src->IRLS_u[N]; 670 tgt->Wx[i] = src->Wx[N]; 671 tgt->Wy[i] = src->Wy[N]; 617 672 } 618 673 return TRUE; … … 674 729 pYmax = MAX (pYmax, fitdata->pY[Nsubset]); 675 730 676 fitdata->IRLS_u[Nsubset] = 0.0; 731 fitdata->Wx[Nsubset] = 1.0; 732 fitdata->Wy[Nsubset] = 1.0; 677 733 fitdata->index[Nsubset] = i; 678 734 Nsubset++; … … 706 762 int i, n; 707 763 708 PlxFit _IRLStestfit;764 PlxFit testfit; 709 765 testfit.getChisq = FALSE; 710 766 … … 728 784 729 785 // fit the sample 730 if (!FitPMandPar _IRLS(&testfit,731 sample.X, sample.dX,732 sample.Y, sample.dY, sample.t,733 sample.pX, sample.pY, sample.IRLS_u, sample.Npts, 1, VERBOSE)) continue;786 if (!FitPMandPar (&testfit, 787 sample.X, sample.dX, 788 sample.Y, sample.dY, sample.t, 789 sample.pX, sample.pY, sample.Npts, VERBOSE)) continue; 734 790 735 791 // fprintf (stderr, "%f +/- %f | %f %f\n", testfit.p, testfit.dp, testfit.uR, testfit.uD);
Note:
See TracChangeset
for help on using the changeset viewer.
