Changeset 42089 for trunk/psLib/src/math/psMinimizeLMM.c
- Timestamp:
- Feb 28, 2022, 2:43:25 PM (4 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psMinimizeLMM.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psMinimizeLMM.c
r36082 r42089 220 220 221 221 // XXX should we give up if chisq is nan? 222 psF32 chisq = psMinLM_SetABX(alpha, Beta, params, paramMask, x, y, dy, func); 222 // do not apply reweighting 223 psF32 chisq = psMinLM_SetABX(alpha, Beta, params, paramMask, x, y, dy, false, func); 223 224 if (isnan(chisq)) { 224 225 psTrace ("psLib.math", 5, "psMinLM_SetABX() returned a NAN chisq.\n"); … … 282 283 return(0.5*dLinear); 283 284 } 285 286 // NOTE : reweight is true, the implementation below calculates the alpha,beta terms 287 // including a modified weight, but the chi-square value is calculated usig standard 288 // weights. 284 289 285 290 // alpha, beta, params are already allocated … … 292 297 const psVector *y, 293 298 const psVector *dy, 299 bool reweight, // if true, we calculate the reweighting for each point based on distance from model 294 300 psMinimizeLMChi2Func func) 295 301 { … … 306 312 } 307 313 308 psF32 chisq;309 psF32 delta;310 psF32 weight;311 psF32 ymodel;312 314 psVector *deriv = psVectorAlloc(params->n, PS_TYPE_F32); 313 315 … … 315 317 psImageInit (alpha, 0.0); 316 318 psVectorInit (beta, 0.0); 317 chisq = 0.0;319 psF32 chisq = 0.0; 318 320 319 321 // calculate chisq, alpha, beta. alpha & beta only represent unmasked parameters; skip 320 322 // masked ones 321 323 for (psS32 i = 0; i < y->n; i++) { 322 ymodel = func(deriv, params, (psVector *) x->data[i]); 323 324 delta = ymodel - y->data.F32[i]; 325 chisq += PS_SQR(delta) * dy->data.F32[i]; 324 psF32 ymodel = func(deriv, params, (psVector *) x->data[i]); 325 psF32 delta = ymodel - y->data.F32[i]; 326 psF32 dChi2 = PS_SQR(delta) * dy->data.F32[i]; 327 psF32 wtmod = reweight ? 1.0 / (1.0 + 5.69*dChi2) : 1.0; // see fit1d_irls.c:weight_cauchy 328 chisq += dChi2; 329 330 // if we are doing an IRLS-style fit, we downweight points based on the dChisq 331 // value above: this means setting an additional weight term 326 332 327 333 // XXX remove this later: … … 337 343 if (paramMask && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[j])) continue; 338 344 339 weight = deriv->data.F32[j] * dy->data.F32[i];345 psF32 weight = deriv->data.F32[j] * dy->data.F32[i] * wtmod; 340 346 341 347 for (int k = 0, K = 0; k <= j; k++) { … … 477 483 478 484 // calculate initial alpha and beta, set chisq (min->value) 479 min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func); 485 // do not apply IRLS reweighting yet, even if requested 486 min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, false, func); 480 487 if (isnan(min->value)) { 481 488 min->iter = min->maxIter; … … 529 536 530 537 // calculate Chisq for new guess, update Alpha & Beta 531 Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, func);538 Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, min->useReweighting, func); 532 539 if (isnan(Chisq)) { 533 540 min->iter ++; … … 716 723 717 724 // calculate initial alpha and beta, set chisq (min->value) 718 min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func); 725 // do not apply IRLS reweighting yet, even if requested 726 min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, false, func); 719 727 if (isnan(min->value)) { 720 728 min->iter = min->maxIter; … … 788 796 789 797 // calculate Chisq for new guess, update Alpha & Beta 790 Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, func); 798 // if requested, modify the weights IRLS-style 799 Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, min->useReweighting, func); 791 800 if (isnan(Chisq)) { 792 801 min->iter ++; … … 985 994 min->gainFactorMode = 0; 986 995 min->isInteractive = false; 996 min->useReweighting = false; 987 997 988 998 return(min);
Note:
See TracChangeset
for help on using the changeset viewer.
