Changeset 35768 for trunk/psModules/src/objects/pmPCM_MinimizeChisq.c
- Timestamp:
- Jul 3, 2013, 2:37:22 PM (13 years ago)
- Location:
- trunk/psModules
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
src/objects/pmPCM_MinimizeChisq.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules
- Property svn:mergeinfo changed
/branches/eam_branches/ipp-20130509/psModules (added) merged: 35594,35613,35628,35638-35639,35643-35648,35653,35657,35662,35750
- Property svn:mergeinfo changed
-
trunk/psModules/src/objects/pmPCM_MinimizeChisq.c
r34403 r35768 81 81 psAbort ("programming error: no unmasked parameters to be fit\n"); 82 82 } 83 psAssert (pcm->nPar == Beta->n, "did we set the masked parameters correctly??"); 83 84 84 85 // allocate internal arrays (current vs Guess) 85 psImage *alpha = psImageAlloc( Alpha->numCols, Alpha->numRows, PS_TYPE_F32);86 psVector *beta = psVectorAlloc( Beta->n, PS_TYPE_F32);86 psImage *alpha = psImageAlloc(pcm->nPar, pcm->nPar, PS_TYPE_F32); 87 psVector *beta = psVectorAlloc(pcm->nPar, PS_TYPE_F32); 87 88 psVector *Params = psVectorAlloc(params->n, PS_TYPE_F32); 88 89 … … 90 91 psF32 lambda = 0.001; 91 92 psF32 dLinear = 0.0; 93 psF32 nu = 2.0; 92 94 93 95 # if (USE_FFT && PRE_CONVOLVE) … … 120 122 bool done = (min->iter >= min->maxIter); 121 123 while (!done) { 122 psTrace("psModules.objects", 5, "Iteration number %d. (max iterations is %d).\n", min->iter, min->maxIter); 123 psTrace("psModules.objects", 5, "Last delta is %f. stop if < %f, accept if < %f\n", min->lastDelta, min->minTol, min->maxTol); 124 psTrace(FACILITY, 5, "Iteration number %d. (max iterations is %d).\n", min->iter, min->maxIter); 125 126 if (min->chisqConvergence) { 127 psTrace(FACILITY, 5, "Last delta is %f. stop if < %f, accept if < %f\n", min->lastDelta, min->minTol, min->maxTol); 128 } else { 129 psTrace(FACILITY, 5, "Last delta is %f. stop if < %f, accept if < %f\n", min->rParSigma, min->minTol*pcm->nPar, min->maxTol*pcm->nPar); 130 } 124 131 125 132 // set a new guess for Alpha, Beta, Params … … 140 147 p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)"); 141 148 } 149 150 // calculate the parameter change (rParDelta) and error radius (rParSigma) 151 // rParDelta : radius of parameter change; 152 // rParSigma : radius of parameter error 153 154 // note that (before SetABX) Alpha[i][i] is the covariance matrix and 155 // Beta is the actual parameter change for this pass 156 157 // note that Alpha & Beta only represent unmasked parameters, while params and Params have all 158 159 // dParSigma = Alpha[i][i] : error (squared) on parameter i 160 // dParDelta = Params->data.F32[i] - params->data.F32[i] : change on parameter i 161 float rParSigma = 0.0; 162 for (int j = 0, J = 0; j < Params->n; j++) { 163 if (paramMask && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[j])) { 164 continue; 165 } 166 rParSigma += PS_SQR(Params->data.F32[j] - params->data.F32[j]) / Alpha->data.F32[J][J]; 167 J++; 168 } 169 rParSigma = sqrt(rParSigma); 170 psTrace(FACILITY, 5, "rParSigma: %f, Niter: %d\n", rParSigma, min->iter); 171 // fprintf (stderr, "rParSigma: %f, Niter: %d\n", rParSigma, min->iter); 142 172 143 173 // calculate Chisq for new guess, update Alpha & Beta … … 164 194 } 165 195 166 /* if (Chisq < min->value) { */ 196 // change in chisq/nDOF since last minimum 197 min->lastDelta = (min->value - Chisq) / pcm->nDOF; 198 199 // rho is positive if the new chisq is smaller; allow for some insignificant change (slight negative rho) 200 201 // XXX the old version of lambda changes: 202 // XXX : Madsen gives suggestion for better use of rho 203 // rho is positive if the new chisq is smaller 167 204 if (rho >= -1e-6) { 168 min->lastDelta = (min->value - Chisq) / (source->pixels->numCols*source->pixels->numRows - params->n);169 205 min->value = Chisq; 170 206 alpha = psImageCopy(alpha, Alpha, PS_TYPE_F32); 171 207 beta = psVectorCopy(beta, Beta, PS_TYPE_F32); 172 208 params = psVectorCopy(params, Params, PS_TYPE_F32); 173 lambda *= 0.25;174 209 175 210 // save the new convolved model image 176 211 psFree (source->modelFlux); 177 212 source->modelFlux = pmPCMdataSaveImage(pcm); 178 } else { 179 lambda *= 10.0; 180 } 213 } 214 switch (min->gainFactorMode) { 215 case 0: 216 if (rho >= -1e-6) { 217 lambda *= 0.25; 218 } else { 219 lambda *= 10.0; 220 } 221 break; 222 223 case 1: 224 // adjust the gain ratio (lambda) based on rho 225 if (rho < 0.25) { 226 lambda *= 2.0; 227 } 228 if (rho > 0.75) { 229 lambda *= 0.333; 230 } 231 break; 232 233 case 2: 234 if (rho > 0.0) { 235 lambda *= PS_MAX(0.33, (1.0 - pow(2.0*rho - 1.0, 3.0))); 236 nu = 2.0; 237 } else { 238 lambda *= nu; 239 nu *= 2.0; 240 } 241 break; 242 } 181 243 min->iter++; 182 244 245 // ending conditions: 246 // 1) hard limit : too many iterations 183 247 done = (min->iter >= min->maxIter); 184 248 185 // check for convergence: 249 // 2) require deltaChi > 1e-6 (ie, chisq is decreasing, but accept an insignificant change) 250 if (min->lastDelta < -1e-6) { 251 continue; 252 } 253 254 // save this value in case we stop iterating 255 min->rParSigma = rParSigma; 256 257 // 2) require chisqDOF < maxChisqDOF (if maxChisqDOF is not NAN) 258 // keep iterating regardless of rParSigma in this case 186 259 float chisqDOF = Chisq / pcm->nDOF; 187 if (!isfinite(min->maxChisqDOF) || ((chisqDOF < min->maxChisqDOF) && isfinite(min->lastDelta))) { 260 if (isfinite(min->maxChisqDOF) && (chisqDOF > min->maxChisqDOF)) { 261 continue; 262 } 263 264 // delta-chisq or rParSigma ? 265 if (min->chisqConvergence) { 188 266 done |= (min->lastDelta < min->minTol); 267 } else { 268 done |= (rParSigma < min->minTol*pcm->nPar); 189 269 } 190 270 } … … 220 300 221 301 // if the last improvement was at least as good as maxTol, accept the fit: 222 if (min->lastDelta <= min->maxTol) { 223 psTrace(FACILITY, 6, "---- end (true) ----\n"); 224 return(true); 302 if (min->chisqConvergence) { 303 if (min->lastDelta <= min->maxTol) { 304 psTrace(FACILITY, 6, "---- end (true) ----\n"); 305 return(true); 306 } 307 } else { 308 if (min->rParSigma <= min->maxTol*pcm->nPar) { 309 psTrace(FACILITY, 6, "---- end (true) ----\n"); 310 return(true); 311 } 225 312 } 226 313 psTrace(FACILITY, 6, "---- end (false) ----\n");
Note:
See TracChangeset
for help on using the changeset viewer.
