IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 3, 2013, 2:37:22 PM (13 years ago)
Author:
eugene
Message:

deprecate KiiOpen,KiiClose (now KapaOpen,etc); major rework of psEllipse translations : use common functions pmModelAxesToParams and pmModelParamsToAxes ; use new convergence method in pmPCM_MinimizeChisq; add convergence crerition options to psMinimization; threaded versions of pmPSFtryFitEXT and pmPSFtryFitPSF

Location:
trunk/psModules
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules

  • trunk/psModules/src/objects/pmPCM_MinimizeChisq.c

    r34403 r35768  
    8181        psAbort ("programming error: no unmasked parameters to be fit\n");
    8282    }
     83    psAssert (pcm->nPar == Beta->n, "did we set the masked parameters correctly??");
    8384
    8485    // 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);
    8788    psVector *Params = psVectorAlloc(params->n, PS_TYPE_F32);
    8889
     
    9091    psF32 lambda = 0.001;
    9192    psF32 dLinear = 0.0;
     93    psF32 nu = 2.0;
    9294
    9395# if (USE_FFT && PRE_CONVOLVE)
     
    120122    bool done = (min->iter >= min->maxIter);
    121123    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        }
    124131
    125132        // set a new guess for Alpha, Beta, Params
     
    140147            p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)");
    141148        }
     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);
    142172
    143173        // calculate Chisq for new guess, update Alpha & Beta
     
    164194        }
    165195
    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
    167204        if (rho >= -1e-6) {
    168             min->lastDelta = (min->value - Chisq) / (source->pixels->numCols*source->pixels->numRows - params->n);
    169205            min->value = Chisq;
    170206            alpha  = psImageCopy(alpha, Alpha, PS_TYPE_F32);
    171207            beta   = psVectorCopy(beta, Beta, PS_TYPE_F32);
    172208            params = psVectorCopy(params, Params, PS_TYPE_F32);
    173             lambda *= 0.25;
    174209
    175210            // save the new convolved model image
    176211            psFree (source->modelFlux);
    177212            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        }
    181243        min->iter++;
    182244
     245        // ending conditions:
     246        // 1) hard limit : too many iterations
    183247        done = (min->iter >= min->maxIter);
    184248       
    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
    186259        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) {
    188266            done |= (min->lastDelta < min->minTol);
     267        } else {
     268            done |= (rParSigma < min->minTol*pcm->nPar);
    189269        }
    190270    }
     
    220300
    221301    // 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        }
    225312    }
    226313    psTrace(FACILITY, 6, "---- end (false) ----\n");
Note: See TracChangeset for help on using the changeset viewer.