PEATDB/Ekin/Utils.py
def doFtest(model1, model2, numparams1, numparams2, numdps, alpha=0.05):
    """Performs an F test between 2 fitted models.
       model1 represents the simpler null hypothesis model
       the null hypothesis will be that the simpler model fits better
       ss1 : sum of squared error 1
       numdps: number of datapoints """

    def fdist(F,df_n,df_d):

        a = df_n/2.
        b = df_d/2.
        x = df_n*F/float(df_n*F+df_d)
        p = 1 - betai(a,b,x,)
        return p

    ss1 = float(model1['error'])
    ss2 = float(model2['error'])
    if ss1==0 or ss2==0:
        return 0,0
    if ss2>=ss1:
        print 'error on new model is worse - no need for f-test..'
        return 0, 0

    df1 = numdps - numparams1
    df2 = numdps - numparams2
    if df2 <= 0:
        return 0, 0
    #if same degrees of freedom then just compare square diffs and return
    if df1 == df2:
        if ss2>=ss1:
            return 0, 0
        else:
            return 1, 0
    
    #get F value
    F = ((ss1-ss2) / (df1-df2)) / (ss2/df2)    
    #find P value   
    P = fdist(F, df1-df2, df2)
    print 'ss1', 'ss2', 'numparams1', 'numparams2', 'df1', 'df2'
    print ss1, ss2, numparams1, numparams2, df1, df2
    print 'F-ratio:', F, 'P:', P
    #if P value is less than alpha than the null hypothesis is rejected,
    #model2 actually does fit the data better not just due to the extra param
    #we return 1
    if P <= alpha:
        return 1, P
    else:
        return 0, P
Performs an F test between 2 fitted models.
       model1 represents the simpler null hypothesis model
       the null hypothesis will be that the simpler model fits better
       ss1 : sum of squared error 1
       numdps: number of datapoints 
Performs an F test between 2 fitted models.
model1 represents the simpler null hypothesis model
the null hypothesis will be that the simpler model fits better
ss1 : sum of squared error 1
numdps: number of datapoints
Performs an F test between 2 fitted models . model1 represents the simpler null hypothesis model the null hypothesis will be that the simpler model fits better ss1 : sum of squared error 1 numdps : number of datapoints
def doFtest(model1, model2, numparams1, numparams2, numdps, alpha=0.05):
    def fdist(F,df_n,df_d):
        a = df_n/2.
        b = df_d/2.
        x = df_n*F/float(df_n*F+df_d)
        p = 1 - betai(a,b,x,)
        return p
    ss1 = float(model1['error'])
    ss2 = float(model2['error'])
    if ss1==0 or ss2==0:
        return 0,0
    if ss2>=ss1:
        print 'error on new model is worse - no need for f-test..'
        return 0, 0
    df1 = numdps - numparams1
    df2 = numdps - numparams2
    if df2 <= 0:
        return 0, 0
    if df1 == df2:
        if ss2>=ss1:
            return 0, 0
        else:
            return 1, 0
    F = ((ss1-ss2) / (df1-df2)) / (ss2/df2)    
    P = fdist(F, df1-df2, df2)
    print 'ss1', 'ss2', 'numparams1', 'numparams2', 'df1', 'df2'
    print ss1, ss2, numparams1, numparams2, df1, df2
    print 'F-ratio:', F, 'P:', P
    if P <= alpha:
        return 1, P
    else:
        return 0, P
def doFtest ( model1 , model2 , numparams1 , numparams2 , numdps , alpha = 0.05 ) : def fdist ( F , df_n , df_d ) : a = df_n / 2. b = df_d / 2. x = df_n * F / float ( df_n * F + df_d ) p = 1 - betai ( a , b , x , ) return p ss1 = float ( model1 [ 'error' ] ) ss2 = float ( model2 [ 'error' ] ) if ss1 == 0 or ss2 == 0 : return 0 , 0 if ss2 >= ss1 : print 'error on new model is worse - no need for f-test..' return 0 , 0 df1 = numdps - numparams1 df2 = numdps - numparams2 if df2 <= 0 : return 0 , 0 if df1 == df2 : if ss2 >= ss1 : return 0 , 0 else : return 1 , 0 F = ( ( ss1 - ss2 ) / ( df1 - df2 ) ) / ( ss2 / df2 ) P = fdist ( F , df1 - df2 , df2 ) print 'ss1' , 'ss2' , 'numparams1' , 'numparams2' , 'df1' , 'df2' print ss1 , ss2 , numparams1 , numparams2 , df1 , df2 print 'F-ratio:' , F , 'P:' , P if P <= alpha : return 1 , P else : return 0 , P
Performs an F test between 2 fitted models.
Performs an F test between 2 fitted models .
"""Performs an F test between 2 fitted models.
       model1 represents the simpler null hypothesis model
       the null hypothesis will be that the simpler model fits better
       ss1 : sum of squared error 1
       numdps: number of datapoints """ #if same degrees of freedom then just compare square diffs and return #get F value #find P value    #if P value is less than alpha than the null hypothesis is rejected, #model2 actually does fit the data better not just due to the extra param #we return 1
model1
model2
numparams1
numparams2
numdps
alpha