IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 28, 2022, 2:43:25 PM (4 years ago)
Author:
eugene
Message:

plug a leak in psFitsTableNew.c; fix implementation of FITS_SCALE_ASINH_MANUAL; add option to reweight the errors in LMM; compile-time option to activate a hard-coded random seed (only needed for psLib testing)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psMinimizeLMM.c

    r36082 r42089  
    220220
    221221    // 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);
    223224    if (isnan(chisq)) {
    224225        psTrace ("psLib.math", 5, "psMinLM_SetABX() returned a NAN chisq.\n");
     
    282283    return(0.5*dLinear);
    283284}
     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. 
    284289
    285290// alpha, beta, params are already allocated
     
    292297    const psVector *y,
    293298    const psVector *dy,
     299    bool  reweight, // if true, we calculate the reweighting for each point based on distance from model
    294300    psMinimizeLMChi2Func func)
    295301{
     
    306312    }
    307313
    308     psF32 chisq;
    309     psF32 delta;
    310     psF32 weight;
    311     psF32 ymodel;
    312314    psVector *deriv = psVectorAlloc(params->n, PS_TYPE_F32);
    313315
     
    315317    psImageInit (alpha, 0.0);
    316318    psVectorInit (beta, 0.0);
    317     chisq = 0.0;
     319    psF32 chisq = 0.0;
    318320
    319321    // calculate chisq, alpha, beta. alpha & beta only represent unmasked parameters; skip
    320322    // masked ones
    321323    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
    326332
    327333        // XXX remove this later:
     
    337343            if (paramMask && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[j])) continue;
    338344
    339             weight = deriv->data.F32[j] * dy->data.F32[i];
     345            psF32 weight = deriv->data.F32[j] * dy->data.F32[i] * wtmod;
    340346
    341347            for (int k = 0, K = 0; k <= j; k++) {
     
    477483
    478484    // 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);
    480487    if (isnan(min->value)) {
    481488        min->iter = min->maxIter;
     
    529536
    530537        // 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);
    532539        if (isnan(Chisq)) {
    533540            min->iter ++;
     
    716723
    717724    // 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);
    719727    if (isnan(min->value)) {
    720728        min->iter = min->maxIter;
     
    788796
    789797        // 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);
    791800        if (isnan(Chisq)) {
    792801            min->iter ++;
     
    985994    min->gainFactorMode = 0;
    986995    min->isInteractive = false;
     996    min->useReweighting = false;
    987997   
    988998    return(min);
Note: See TracChangeset for help on using the changeset viewer.