IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39238


Ignore:
Timestamp:
Dec 8, 2015, 1:56:09 PM (11 years ago)
Author:
eugene
Message:

adding IRLS to relastro

Location:
trunk/Ohana/src/relastro
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/relastro/Makefile

    r38986 r39238  
    2929$(SRC)/FitMosaic.$(ARCH).o           \
    3030$(SRC)/FitSimple.$(ARCH).o           \
    31 $(SRC)/FitAstromOps.$(ARCH).o           \
     31$(SRC)/FitAstromOps.$(ARCH).o        \
    3232$(SRC)/FitPM.$(ARCH).o               \
     33$(SRC)/FitPM_IRLS.$(ARCH).o          \
    3334$(SRC)/FitPMandPar.$(ARCH).o         \
    3435$(SRC)/FitPosPMfixed.$(ARCH).o       \
     
    103104$(SRC)/ConfigInit.$(ARCH).o          \
    104105$(SRC)/FitSimple.$(ARCH).o           \
    105 $(SRC)/FitAstromOps.$(ARCH).o           \
     106$(SRC)/FitAstromOps.$(ARCH).o        \
    106107$(SRC)/FitPM.$(ARCH).o               \
     108$(SRC)/FitPM_IRLS.$(ARCH).o          \
    107109$(SRC)/FitPMandPar.$(ARCH).o         \
    108110$(SRC)/FitPosPMfixed.$(ARCH).o       \
  • trunk/Ohana/src/relastro/include/relastro.h

    r39225 r39238  
    134134  double **A;
    135135  double **B;
     136  double **Cov;
     137  double *Beta;
     138  double *Beta_prev;
    136139  int Nterms;
    137140} FitAstromData;
    138141
     142// XXX do we need doubles for all of these?  I actually only have of order 100 of these
     143// allocated at a time, so size is not an issue.
    139144typedef struct {
    140145  double X, dX;
     
    143148  double D, dD;
    144149  double T, dT;
     150  double Wx, Wy;
     151  double rx, ry;
     152  double u;
    145153  double pR;
    146154  double pD;
     
    148156  double C_red;
    149157  int measure;
     158  int mask;
    150159} FitAstromPoint;
    151160
     
    739748int FitAstromResultSetPM (FitAstromResult *fit, int Nfit, Average *average);
    740749void AstromErrorSetLoop (int Nloop, int isImageMode);
     750
     751int FitPM_IRLS (FitAstromResult *fit, FitAstromData *data, FitAstromPoint *points, int Npoints, int VERBOSE);
     752
     753double MedianAbsDeviation(FitAstromPoint *points, int Npoints);
     754
     755int weighted_LS_PM (FitAstromResult *fit, FitAstromData *data, FitAstromPoint *points, int Npoints, int VERBOSE);
     756
     757double weight_cauchy (double x);
     758double dpsi_cauchy (double x);
  • trunk/Ohana/src/relastro/src/FitAstromOps.c

    r38986 r39238  
    9393
    9494  /* do I need to do this as 2 2x2 matrix equations? */
    95   fit->A = array_init (Nterms, Nterms);
    96   fit->B = array_init (Nterms, 1);
     95  fit->B   = array_init (Nterms, 1);
     96  fit->A   = array_init (Nterms, Nterms);
     97  fit->Cov = array_init (Nterms, Nterms);
     98
     99  ALLOCATE (fit->Beta, double, Nterms);
     100  ALLOCATE (fit->Beta_prev, double, Nterms);
    97101  fit->Nterms = Nterms;
    98102
     
    106110  array_free (fit->A, fit->Nterms);
    107111  array_free (fit->B, fit->Nterms);
     112  array_free (fit->Cov, fit->Nterms);
     113
     114  free (fit->Beta);
     115  free (fit->Beta_prev);
     116
    108117  free (fit);
    109118  return;
     
    126135  object->C_red  = 0.0;
    127136  object->measure= -1;
     137
     138  object->Wx     = 1.0;
     139  object->Wy     = 1.0;
     140
     141  object->rx     = 0.0;
     142  object->ry     = 0.0;
     143  object->u      = 0.0;
     144
     145  object->mask   = 0;
    128146  return;
    129147}
  • trunk/Ohana/src/relastro/src/FitPM_IRLS.c

    r39237 r39238  
    11# include "relastro.h"
    22
     3// These should probably be tunable:
     4# define MAX_ITERATIONS 10
     5# define FIT_TOLERANCE 1e-4
     6# define WEIGHT_THRESHOLD 0.3
     7
    38/* do we want an init function which does the alloc and a clear function to free? */
    4 int FitPMonly_IRLS (FitAstromResult *fit, FitAstromData *data, FitAstromPoint *points, int Npoints, int VERBOSE) {
     9int FitPM_IRLS (FitAstromResult *fit, FitAstromData *data, FitAstromPoint *points, int Npoints, int VERBOSE) {
    510
    611  int i,j;
    712
    8   static double **A, **B;
    9 
    10   double chisq, Xf, Yf;
    11 
    12   double **Cov;
    13   double *Beta, *Beta_prev;
    14  
    15   double sigma_ols, sigma_hat;
    16   double *Wx, *Wy;
    17   double *rx, *ry;
    18   //  double *ux, *uy;
    19   double *u;
    2013  int dof = 2 * Npoints - 4;
    2114  int p   = 4;
    2215  int n   = 2 * Npoints;
    23   double tolerance;
    24   int converged;
    25   int iterations;
    26  
    27   /* do I need to do this as 2 2x2 matrix equations? */
    28   if (A == NULL) {
    29     ALLOCATE (A, double *, 4);
    30     ALLOCATE (B, double *, 4);
    31     for (i = 0; i < 4; i++) {
    32       ALLOCATE (A[i], double, 4);
    33       ALLOCATE (B[i], double, 1);
    34       memset (A[i], 0, 4*sizeof(double));
    35       memset (B[i], 0, 1*sizeof(double));
    36     }
    37   }
    38 
    39   // things we need
    40   ALLOCATE (Cov, double *, 4);
    41   for (i = 0; i < 4; i++) {
    42     ALLOCATE ( Cov[i], double, 4);
    43   }
    44 
    45   ALLOCATE(Beta, double, 4);
    46   ALLOCATE(Beta_prev, double, 4);
    47   ALLOCATE(Wx, double, Npoints);
    48   ALLOCATE(Wy, double, Npoints);
    49   ALLOCATE(rx,  double, Npoints);
    50   ALLOCATE(ry,  double, Npoints);
    51   ALLOCATE(u,  double, Npoints);
     16 
     17  // data->A,B,Cov,Beta,Beta_prev are allocated outside by FitAstromDataInit()
     18  // points->Wx,Wy,rx,ry,u are elements of FitAstromPoint
    5219 
    5320  // Convert the measurement errors into initial weights.
    5421  for (i = 0; i < Npoints; i++) {
    55     Wx[i] = 1 / dX[i];
    56     Wy[i] = 1 / dY[i];
     22    points[i].Wx = 1 / points[i].dX;
     23    points[i].Wy = 1 / points[i].dY;
    5724  }
    5825 
    5926  // Solve OLS equation 
    60   if (!weighted_LS_PM(T,X,Wx,Y,Wy,Npoints,
    61                    A,B,VERBOSE)) {
    62     // Handle fail case
     27  if (!weighted_LS_PM(fit, data, points, Npoints, VERBOSE)) {
     28    myAbort ("handle failures, please!");
    6329    return(FALSE);
    6430  }
    6531
    6632  // Calculate r vector of residuals and least squares sigma
    67   sigma_ols = 0.0;
    68   for (i = 0; i < Npoints; i++) {
    69     rx[i] = X[i] - (T[i] * B[1][0] + B[0][0]);
    70     ry[i] = Y[i] - (T[i] * B[3][0] + B[2][0]);
    71     //    u[i] = r[i] /
    72     sigma_ols += SQ(rx[i]) + SQ(ry[i]);
    73 
     33  double sigma_ols = 0.0;
     34  for (i = 0; i < Npoints; i++) {
     35    points[i].rx = points[i].X - (points[i].T * fit->uR + fit->Ro);
     36    points[i].ry = points[i].Y - (points[i].T * fit->uD + fit->Do);
     37    sigma_ols += SQ(points[i].rx) + SQ(points[i].ry);
    7438  }
    7539  sigma_ols = sqrt(sigma_ols / dof);
    7640
    77   // Save OLS covariance;
     41  // Save OLS covariance and Beta (solution vector, which is actually also saved in fit)
    7842  for (i = 0; i < 4; i++) {
    7943    for (j = 0; j < 4; j++) {
    80       Cov[i][j] = A[i][j];
    81     }
    82   }
    83 
    84   // Save Beta
    85   for (i = 0; i < 4; i++) {
    86     Beta[i] = B[i][0];
     44      data->Cov[i][j] = data->A[i][j];
     45    }
     46    data->Beta[i] = data->B[i][0];
    8747  }
    8848
    8949  // Iterately reweight and solve
    90   converged = FALSE;
    91   iterations = 0;
    92   do {
     50  double sigma_hat = 0.0; // save for the error model
     51  int converged = FALSE;
     52  int iterations = 0;
     53
     54  // modify the weight based on the distance from the previous fit.  try up to MAX_ITERATIONS.
     55  // at the end "fit", has the last fit parameters
     56  for (iterations = 0; !converged && (iterations < MAX_ITERATIONS); iterations ++) {
    9357    // Save Beta.
    9458    for (i = 0; i < 4; i ++) {
    95       Beta_prev[i] = Beta[i];
    96     }
    97 
    98     // Assign W
     59      data->Beta_prev[i] = data->Beta[i];
     60    }
     61
     62    // Assign weights based on the deviation
    9963    for (i = 0; i < Npoints; i++) {
    100       Wx[i] = weight_cauchy(rx[i] / dX[i]);
    101       Wy[i] = weight_cauchy(ry[i] / dY[i]);
     64      points[i].Wx = weight_cauchy(points[i].rx / points[i].dX);
     65      points[i].Wy = weight_cauchy(points[i].ry / points[i].dY);
    10266    }   
    10367
    104     // Solve
    105     if (!weighted_LS_PM(T,X,Wx,Y,Wy,Npoints,
    106                      A,B,VERBOSE)) {
    107       // Handle fail case
     68    // Solve with the new weights
     69    if (!weighted_LS_PM(fit, data, points, Npoints, VERBOSE)) {
     70      myAbort ("handle failures, please!");
    10871      return(FALSE);
    10972    }
    11073
     74    // store the new Beta.
    11175    for (i = 0; i < 4; i++) {
    112       Beta[i] = B[i][0];
    113     }
    114 
    115     // r
    116     sigma_hat = 0.0;
     76      data->Beta[i] = data->B[i][0];
     77    }
     78
     79    // calculate the residuals:
    11780    for (i = 0; i < Npoints; i++) {
    118       rx[i] = X[i] - (T[i] * B[1][0] + B[0][0]);
    119       ry[i] = Y[i] - (T[i] * B[3][0] + B[2][0]);
    120       u[i] = sqrt(SQ(rx[i] / dX[i]) + SQ(ry[i] / dY[i]));
    121     }
    122     sigma_hat = MedianAbsDeviation(u,Npoints) / 0.6745;
     81      points[i].rx = points[i].X - (points[i].T * fit->uR + fit->Ro);
     82      points[i].ry = points[i].Y - (points[i].T * fit->uD + fit->Do);
     83      points[i].u = sqrt(SQ(points[i].rx / points[i].dX) + SQ(points[i].ry / points[i].dY));
     84    }
     85    sigma_hat = MedianAbsDeviation(points, Npoints) / 0.6745;
    12386   
    12487    // Check convergence
    12588    converged = TRUE;
    126     tolerance = 1e-4;  // This should probably be tunable.
    12789    for (i = 0; i < 4; i++) {
    128       if (fabs(Beta[i] - Beta_prev[i]) > tolerance * abs(Beta[i])) {
     90      if (fabs(data->Beta[i] - data->Beta_prev[i]) > FIT_TOLERANCE * fabs(data->Beta[i])) {
    12991        converged = FALSE;
    13092      }
    13193    }
    132 
    133     iterations++;
    134     if (iterations >= 10) {
    135       converged = TRUE;
    136       // Throw a warning or something here.
    137     }
    138    
    139   } while (!converged);
    140 
    141   double ax, ay;
    142   double bx, by;
    143   double lambda;
    144   double sigma_robust_x, sigma_robust_y;
    145   double sigma_final_x,  sigma_final_y;
    146   double Sum_Wx, Sum_Wy;
    147  
    148   Sum_Wx = 0.0;
    149   Sum_Wy = 0.0;
    150   ax = 0.0; ay = 0.0;
    151   bx = 0.0; by = 0.0;
    152   lambda = 0.0;
    153   for (i = 0; i < Npoints; i++) {
    154     Wx[i] = weight_cauchy(rx[i] / dX[i]);
    155     Wy[i] = weight_cauchy(ry[i] / dY[i]);
    156    
    157     ax += dpsi_cauchy(rx[i] / dX[i]);
    158     ay += dpsi_cauchy(ry[i] / dY[i]);
    159 
    160     bx += SQ(Wx[i]);
    161     by += SQ(Wy[i]);
    162 
    163     Sum_Wx += Wx[i];
    164     Sum_Wy += Wy[i];
    165   }
    166   ax /= 1.0 * Npoints;  // mean(psi_dot(r))
    167   ay /= 1.0 * Npoints;
    168   bx /= 1.0 * (Npoints - p); // mean(psi^2(r)) * (N / (N-p))
    169   by /= 1.0 * (Npoints - p);
    170  
    171   sigma_robust_x = lambda * sqrt(bx) * sigma_hat * 2.385 / ax;
    172   sigma_robust_y = lambda * sqrt(by) * sigma_hat * 2.385 / ay;
    173 
    174   // This is actually sigma^2, as that's the factor in the covariance (dumouchel 4.1)
    175   sigma_final_x  = MAX(SQ(sigma_robust_x), (n * SQ(sigma_robust_x) + SQ(p * sigma_ols)) / (n + SQ(p)));
    176   sigma_final_y  = MAX(SQ(sigma_robust_y), (n * SQ(sigma_robust_y) + SQ(p * sigma_ols)) / (n + SQ(p)));
    177 
    178   for (i = 0; i < 4; i++) {
    179     for (j = 0; j < 4; j++) {
    180       // This uses the original OLS covariance.
    181       if ((i < 2)&&(j < 2)) { // Upper portion
    182         Cov[i][j] *= sigma_final_x;
    183       }
    184       else if ((i > 1)&&(j > 1)) { // Lower portion
    185         Cov[i][j] *= sigma_final_y;
    186       }
    187       else { // Cross term
    188         Cov[i][j] *= sqrt(sigma_final_x * sigma_final_y);
    189       }
    190     }
    191   }
    192 
    193   // Finish.
    194   fit[0].Ro = Beta[0];
    195   fit[0].uR = Beta[1];
    196   fit[0].Do = Beta[2];
    197   fit[0].uD = Beta[3];
    198  
    199   fit[0].dRo = sqrt(Cov[0][0]);
    200   fit[0].duR = sqrt(Cov[1][1]);
    201   fit[0].dDo = sqrt(Cov[2][2]);
    202   fit[0].duD = sqrt(Cov[3][3]);
    203 
    204   // Sort out the final weight threshold.
    205 
    206   // add up the chi square for the fit
    207   chisq = 0.0;
     94  }
     95  if (!converged) {
     96    myAbort ("raise a warning on non-convergence");
     97  }
     98
     99  // this section calculates the formal error on the weighted fit using the covariance values
     100  double Sum_Wx = 0.0;
     101  double Sum_Wy = 0.0;
     102  if (1) {
     103    double ax = 0.0, ay = 0.0;
     104    double bx = 0.0, by = 0.0;
     105    double lambda = 0.0;
     106    for (i = 0; i < Npoints; i++) {
     107      points[i].Wx = weight_cauchy(points[i].rx / points[i].dX);
     108      points[i].Wy = weight_cauchy(points[i].ry / points[i].dY);
     109   
     110      ax += dpsi_cauchy(points[i].rx / points[i].dX);
     111      ay += dpsi_cauchy(points[i].ry / points[i].dY);
     112
     113      bx += SQ(points[i].Wx);
     114      by += SQ(points[i].Wy);
     115
     116      Sum_Wx += points[i].Wx;
     117      Sum_Wy += points[i].Wy;
     118    }
     119    ax /= 1.0 * Npoints;  // mean(psi_dot(r))
     120    ay /= 1.0 * Npoints;
     121    bx /= 1.0 * (Npoints - p); // mean(psi^2(r)) * (N / (N-p))
     122    by /= 1.0 * (Npoints - p);
     123 
     124    double sigma_robust_x = lambda * sqrt(bx) * sigma_hat * 2.385 / ax;
     125    double sigma_robust_y = lambda * sqrt(by) * sigma_hat * 2.385 / ay;
     126
     127    // This is actually sigma^2, as that's the factor in the covariance (dumouchel 4.1)
     128    double sigma_final_x  = MAX(SQ(sigma_robust_x), (n * SQ(sigma_robust_x) + SQ(p * sigma_ols)) / (n + SQ(p)));
     129    double sigma_final_y  = MAX(SQ(sigma_robust_y), (n * SQ(sigma_robust_y) + SQ(p * sigma_ols)) / (n + SQ(p)));
     130
     131    fit[0].dRo = sqrt(data->Cov[0][0]);
     132    fit[0].duR = sqrt(data->Cov[1][1]);
     133    fit[0].dDo = sqrt(data->Cov[2][2]);
     134    fit[0].duD = sqrt(data->Cov[3][3]);
     135
     136    fit[9].dRo *= sigma_final_x;
     137    fit[9].duR *= sigma_final_x;
     138
     139    fit[9].dDo *= sigma_final_y;
     140    fit[9].duD *= sigma_final_y;
     141  }
     142
     143  // set a mask (which can be used by the bootstrap resampling analysis)
     144  double WxThreshold = WEIGHT_THRESHOLD * Sum_Wx / (1.0 * Npoints);
     145  double WyThreshold = WEIGHT_THRESHOLD * Sum_Wy / (1.0 * Npoints);
     146
     147  for (i = 0; i < Npoints; i++) {
     148    // keep if either is above threshold?
     149    // drop if either is below threshold?
     150    // points are marked as keep by default
     151    if ((points[i].Wx < WxThreshold) || (points[i].Wy < WyThreshold)) {
     152      points[i].mask = 1;
     153    }
     154  }
     155
     156  // add up the chi square for the fit, only counting the unmasked points
     157  double chisq = 0.0;
    208158  fit[0].Nfit = 0;
    209159  for (i = 0; i < Npoints; i++) {
    210     if ((Wx[i] > 0.1 * Sum_Wx / (1.0 * Npoints))||
    211         (Wy[i] > 0.1 * Sum_Wy / (1.0 * Npoints))) {
    212       Xf = fit[0].Ro + fit[0].uR*T[i];
    213       Yf = fit[0].Do + fit[0].uD*T[i];
    214       chisq += SQ(X[i] - Xf) / SQ(dX[i]);
    215       chisq += SQ(Y[i] - Yf) / SQ(dY[i]);
    216       fit[0].Nfit += 1;
    217     }
    218     // if (VERBOSE) fprintf (stderr, "chisq contrib : %f %f : %f %f : %f %f : %f %f : %f\n", Xf, Yf, X[i] - Xf, Y[i] - Yf, dX[i], dY[i], (X[i] - Xf) / dX[i], (Y[i] - Yf) / dY[i], chisq);
    219   }
    220   //  fit[0].Nfit = Npoints;
    221 
    222   // the reduced chisq is divided by (Ndof = 2*Npoints - 4)
    223   fit[0].chisq = chisq / (2.0*Npoints - 4.0);
     160    if (points[i].mask) continue;
     161
     162    double Xf = fit[0].Ro + fit[0].uR*points[i].T;
     163    double Yf = fit[0].Do + fit[0].uD*points[i].T;
     164    chisq += SQ(points[i].X - Xf) / SQ(points[i].dX);
     165    chisq += SQ(points[i].Y - Yf) / SQ(points[i].dY);
     166    fit[0].Nfit ++;
     167  }
     168
     169  // the reduced chisq is divided by (Ndof = 2*Nfit - 4)
     170  fit[0].chisq = chisq / (2.0*fit[0].Nfit - 4.0);
    224171  return (TRUE);
    225172}
    226173
    227 int weighted_LS_PM (FitAstromResult *fit, FitAstromData *data, FitAstromPoint *points, int Npoints, double **A, double **B, int VERBOSE) {
    228 
    229   int i,j;
     174int weighted_LS_PM (FitAstromResult *fit, FitAstromData *data, FitAstromPoint *points, int Npoints, int VERBOSE) {
     175
     176  int i;
    230177  double Wx, Wy, Tx, Ty, Tx2, Ty2, Xs, Ys, XT, YT;
    231178  Wx = Wy = Tx = Ty = Tx2 = Ty2 = Xs = Ys = XT = YT = 0.0;
    232   for (i = 0; i < Npoints; i++) {
    233     Wx += WX[i];
    234     Wy += WY[i];
    235 
    236     Tx += T[i]*WX[i];
    237     Ty += T[i]*WY[i];
    238    
    239     Tx2 += SQ(T[i])*WX[i];
    240     Ty2 += SQ(T[i])*WY[i];
    241    
    242     Xs += X[i]*WX[i];
    243     Ys += Y[i]*WY[i];
    244 
    245     XT += X[i]*T[i]*WX[i];
    246     YT += Y[i]*T[i]*WY[i];
     179
     180  for (i = 0; i < Npoints; i++) {
     181    Wx += points[i].Wx;
     182    Wy += points[i].Wy;
     183
     184    double TWx = points[i].T*points[i].Wx;
     185    double TWy = points[i].T*points[i].Wy;
     186
     187    Tx += TWx;
     188    Ty += TWy;
     189   
     190    Tx2 += points[i].T*TWx;
     191    Ty2 += points[i].T*TWy;
     192   
     193    Xs += points[i].X*points[i].Wx;
     194    Ys += points[i].Y*points[i].Wy;
     195
     196    XT += points[i].X*TWx;
     197    YT += points[i].Y*TWy;
    247198  }
    248199
    249200  // X^T W X
    250   A[0][0] = Wx;
    251   A[0][1] = Tx;
    252 
    253   A[1][0] = Tx;
    254   A[1][1] = Tx2;
    255 
    256   A[2][2] = Wy;
    257   A[2][3] = Ty;
    258 
    259   A[3][2] = Ty;
    260   A[3][3] = Ty2;
     201  data->A[0][0] = Wx;
     202  data->A[0][1] = Tx;
     203
     204  data->A[1][0] = Tx;
     205  data->A[1][1] = Tx2;
     206  data->A[2][2] = Wy;
     207  data->A[2][3] = Ty;
     208  data->A[3][2] = Ty;
     209  data->A[3][3] = Ty2;
    261210
    262211  // X^T W Y
    263   B[0][0] = Xs;
    264   B[1][0] = XT;
    265   B[2][0] = Ys;
    266   B[3][0] = YT;
    267 
    268   if (!dgaussjordan ((double **)A, (double **)B, 4, 1)) {
     212  data->B[0][0] = Xs;
     213  data->B[1][0] = XT;
     214  data->B[2][0] = Ys;
     215  data->B[3][0] = YT;
     216
     217  if (!dgaussjordan (data->A, data->B, 4, 1)) {
     218# if (DEBUG)
    269219    if (VERBOSE) fprintf (stderr, "error in fit\n");
    270     if (VERBOSE == 2) {
    271       for (i = 0; i < 4; i++) {
    272         for (j = 0; j < 4; j++) {
    273           fprintf (stderr, "%e ", A[i][j]);
    274         }
    275         fprintf (stderr, " : %e\n", A[i][0]);
     220    int j;
     221    for (i = 0; i < 4; i++) {
     222      for (j = 0; j < 4; j++) {
     223        fprintf (stderr, "%e ", data->A[i][j]);
    276224      }
    277     }
     225      fprintf (stderr, " : %e\n", data->B[i][0]);
     226    }
     227# endif
    278228    return FALSE;
    279229  }
    280230
    281   // A => (X^T W X)^{-1}
    282   // B => beta
    283  
     231  fit->Ro = data->B[0][0];
     232  fit->uR = data->B[1][0];
     233  fit->Do = data->B[2][0];
     234  fit->uD = data->B[3][0];
     235  fit->p  = 0.0;
     236
    284237  return TRUE;
    285238}
     
    299252// median absolute deviation
    300253// MAD = median(abs(x - median(x)))
    301 double MedianAbsDeviation(double *in, int N) {
     254double MedianAbsDeviation(FitAstromPoint *points, int Npoints) {
     255
    302256  double *x;
    303257  double median = 0.0;
    304258  int i;
    305259 
    306   ALLOCATE(x,double,N);
    307   for (i = 0; i < N; i++) {
    308     x[i] = in[i];
    309   }
    310 
    311   dsort(x,N);
    312 
    313   if (N % 2) {
    314     median = 0.5*(x[(int)(0.5*N)] + x[(int)(0.5*N) - 1]);
     260  ALLOCATE(x, double, Npoints);
     261  for (i = 0; i < Npoints; i++) {
     262    x[i] = points[i].u;
     263  }
     264  dsort(x, Npoints);
     265
     266  if (Npoints % 2) {
     267    median = 0.5*(x[(int)(0.5*Npoints)] + x[(int)(0.5*Npoints) - 1]);
    315268  } else {
    316     median = x[(int)(0.5*N)];
    317   }
    318 
    319   for (i = 0; i < N; i++ ) {
     269    median = x[(int)(0.5*Npoints)];
     270  }
     271
     272  for (i = 0; i < Npoints; i++ ) {
    320273    x[i] = fabs(x[i] - median);
    321274  }
    322 
    323   dsort(x,N);
    324 
    325   if (N % 2) {
    326     median = 0.5*(x[(int)(0.5*N)] + x[(int)(0.5*N) - 1]);
     275  dsort(x, Npoints);
     276
     277  if (Npoints % 2) {
     278    median = 0.5*(x[(int)(0.5*Npoints)] + x[(int)(0.5*Npoints) - 1]);
    327279  } else {
    328     median = x[(int)(0.5*N)];
    329   }
    330 
    331   return(median);
    332 }
     280    median = x[(int)(0.5*Npoints)];
     281  }
     282
     283  return median;
     284}
  • trunk/Ohana/src/relastro/src/UpdateObjects.c

    r39237 r39238  
    153153    if (fitStats->NfitAlloc == 1) {
    154154      // if N_BOOTSTRAP_SAMPLES = 1, no bootstrap resampling:
    155       FitPM (&fitPM, fitStats->fitdataPM, fitStats->points, fitStats->Npoints);
    156       // FitPM_IRLS (&fitPM, fitStats->fitdataPM, fitStats->points, fitStats->Npoints);
     155      if (1) {
     156        FitPM (&fitPM, fitStats->fitdataPM, fitStats->points, fitStats->Npoints);
     157      } else {
     158        FitPM_IRLS (&fitPM, fitStats->fitdataPM, fitStats->points, fitStats->Npoints, VERBOSE);
     159      }
    157160    } else {
    158161      fitStats->Nfit = 0;
    159162      for (k = 0; k < fitStats->NfitAlloc; k++) {
    160163        BootstrapResample (fitStats->sample, fitStats->points, fitStats->Npoints);
    161         // if (!FitPM_IRLS (&fitStats->fit[k], fitStats->fitdataPM, fitStats->sample, fitStats->Npoints)) continue;
     164        // if (!FitPM_IRLS (&fitStats->fit[k], fitStats->fitdataPM, fitStats->sample, fitStats->Npoints, VERBOSE)) continue;
    162165        if (!FitPM (&fitStats->fit[k], fitStats->fitdataPM, fitStats->sample, fitStats->Npoints)) continue;
    163166        fitStats->Nfit ++;
     
    207210        BootstrapResample (fitStats->sample, fitStats->points, fitStats->Npoints);
    208211        // FitPMandPar_IRLS (&fitStats->fit[k], fitStats->fitdataPar, fitStats->sample, fitStats->Npoints);
    209         FitPMandPar_IRLS (&fitStats->fit[k], fitStats->fitdataPar, fitStats->sample, fitStats->Npoints);
     212        FitPMandPar (&fitStats->fit[k], fitStats->fitdataPar, fitStats->sample, fitStats->Npoints);
    210213        fitStats->Nfit ++;
    211214      }
Note: See TracChangeset for help on using the changeset viewer.