PEATDB/Ekin/Fitting.py
def findBestModel(ek, models, fitters=None, checkfunc=None,
                    conv=None, grad=None, alpha=0.05, silent=False):
    """Finds the best fit model using f-testing.
       We do an f-test here between each successive model
       we iterate from simplest to last comparing each model to the more complex ones
       when it finds a better model, this is the best one and is compared with later ones
       in turn.
    """
    import copy, operator
    p=None
    modelfits = {}
    modelstocompare = []

    #sort models by number of parameters first
    sortm=[]
    for m in models:
        X = getFitter(model=m)
        n = len(X.variables)
        sortm.append((m,n))
    sortm = sorted(sortm, key=operator.itemgetter(1))
    models = map(operator.itemgetter(0), sortm)

    #fit with all models first
    print 'Estimating best model..'

    for m in models[:]:
        tempfit, X = doFit(ek, model=m, silent=True, noiter=100, conv=conv, grad=grad)

        if tempfit != None:
            modelfits[m] = copy.deepcopy(tempfit)
            modelstocompare.append(m)

    if len(modelstocompare)<=1:
        return None, None

    best=modelstocompare[0]
    numdps = ek.length()
    modelstocompare.remove(best)
    for n in modelstocompare:
        if silent == False: print best,'vs.',n
        error1 = float(modelfits[best]['error'])
        error2 = float(modelfits[n]['error'])
        if silent == False: print 'error1',error1, 'error2',error2

        numparams1 = len(getFitter(model=best).getVariables())
        numparams2 = len(getFitter(model=n).getVariables())
        if silent == False: print 'numparams1 ',numparams1;print 'numparams2 ',numparams2
        result, p = Utils.doFtest(modelfits[best],modelfits[n],numparams1,numparams2,numdps,alpha=alpha)

        if checkfunc != None and result == 1:
            #use some other checking function, define externally
            result = checkfunc(ek.x, modelfits[n])
        if result == 1:
            if silent == False: print 'Better'
            best=n
        else:
            if silent == False: print 'Worse'
            pass
        if result == 0:
            break
    print 'Best model is', modelfits[best]['model'] , 'with error', modelfits[best]['error']
    print '--------------------------------------------------------- '
    fitdata = modelfits[best]
    return fitdata, p
Finds the best fit model using f-testing.
       We do an f-test here between each successive model
       we iterate from simplest to last comparing each model to the more complex ones
       when it finds a better model, this is the best one and is compared with later ones
       in turn.
    
Finds the best fit model using f-testing.
We do an f-test here between each successive model
we iterate from simplest to last comparing each model to the more complex ones
when it finds a better model, this is the best one and is compared with later ones
in turn.
Finds the best fit model using f - testing . We do an f - test here between each successive model we iterate from simplest to last comparing each model to the more complex ones when it finds a better model this is the best one and is compared with later ones in turn .
def findBestModel(ek, models, fitters=None, checkfunc=None,
                    conv=None, grad=None, alpha=0.05, silent=False):
    import copy, operator
    p=None
    modelfits = {}
    modelstocompare = []
    sortm=[]
    for m in models:
        X = getFitter(model=m)
        n = len(X.variables)
        sortm.append((m,n))
    sortm = sorted(sortm, key=operator.itemgetter(1))
    models = map(operator.itemgetter(0), sortm)
    print 'Estimating best model..'
    for m in models[:]:
        tempfit, X = doFit(ek, model=m, silent=True, noiter=100, conv=conv, grad=grad)
        if tempfit != None:
            modelfits[m] = copy.deepcopy(tempfit)
            modelstocompare.append(m)
    if len(modelstocompare)<=1:
        return None, None
    best=modelstocompare[0]
    numdps = ek.length()
    modelstocompare.remove(best)
    for n in modelstocompare:
        if silent == False: print best,'vs.',n
        error1 = float(modelfits[best]['error'])
        error2 = float(modelfits[n]['error'])
        if silent == False: print 'error1',error1, 'error2',error2
        numparams1 = len(getFitter(model=best).getVariables())
        numparams2 = len(getFitter(model=n).getVariables())
        if silent == False: print 'numparams1 ',numparams1;print 'numparams2 ',numparams2
        result, p = Utils.doFtest(modelfits[best],modelfits[n],numparams1,numparams2,numdps,alpha=alpha)
        if checkfunc != None and result == 1:
            result = checkfunc(ek.x, modelfits[n])
        if result == 1:
            if silent == False: print 'Better'
            best=n
        else:
            if silent == False: print 'Worse'
            pass
        if result == 0:
            break
    print 'Best model is', modelfits[best]['model'] , 'with error', modelfits[best]['error']
    print '--------------------------------------------------------- '
    fitdata = modelfits[best]
    return fitdata, p
def findBestModel ( ek , models , fitters = None , checkfunc = None , conv = None , grad = None , alpha = 0.05 , silent = False ) : import copy , operator p = None modelfits = { } modelstocompare = [ ] sortm = [ ] for m in models : X = getFitter ( model = m ) n = len ( X . variables ) sortm . append ( ( m , n ) ) sortm = sorted ( sortm , key = operator . itemgetter ( 1 ) ) models = map ( operator . itemgetter ( 0 ) , sortm ) print 'Estimating best model..' for m in models [ : ] : tempfit , X = doFit ( ek , model = m , silent = True , noiter = 100 , conv = conv , grad = grad ) if tempfit != None : modelfits [ m ] = copy . deepcopy ( tempfit ) modelstocompare . append ( m ) if len ( modelstocompare ) <= 1 : return None , None best = modelstocompare [ 0 ] numdps = ek . length ( ) modelstocompare . remove ( best ) for n in modelstocompare : if silent == False : print best , 'vs.' , n error1 = float ( modelfits [ best ] [ 'error' ] ) error2 = float ( modelfits [ n ] [ 'error' ] ) if silent == False : print 'error1' , error1 , 'error2' , error2 numparams1 = len ( getFitter ( model = best ) . getVariables ( ) ) numparams2 = len ( getFitter ( model = n ) . getVariables ( ) ) if silent == False : print 'numparams1 ' , numparams1 ; print 'numparams2 ' , numparams2 result , p = Utils . doFtest ( modelfits [ best ] , modelfits [ n ] , numparams1 , numparams2 , numdps , alpha = alpha ) if checkfunc != None and result == 1 : result = checkfunc ( ek . x , modelfits [ n ] ) if result == 1 : if silent == False : print 'Better' best = n else : if silent == False : print 'Worse' pass if result == 0 : break print 'Best model is' , modelfits [ best ] [ 'model' ] , 'with error' , modelfits [ best ] [ 'error' ] print '--------------------------------------------------------- ' fitdata = modelfits [ best ] return fitdata , p
Finds the best fit model using f-testing.
Finds the best fit model using f - testing .
"""Finds the best fit model using f-testing.
       We do an f-test here between each successive model
       we iterate from simplest to last comparing each model to the more complex ones
       when it finds a better model, this is the best one and is compared with later ones
       in turn.
    """ #sort models by number of parameters first #fit with all models first #use some other checking function, define externally
ek
models
fitters
checkfunc
conv
grad
alpha
silent