def doKruskalWallisTest(verbose, groups, combivehlist, assignments, label, hValues):
    if verbose:
        print '\nbegin the Kruskal-Wallis test!'
        print 'methods:', label
        print 'number of samples:', len(combivehlist)
    adjlabel = label + '_' + "adjusted"
    if groups >= 100.:
        groups = 100.
    lowvalue = chiSquareTable[int(groups)-1][2]
    highvalue = chiSquareTable[int(groups)-1][4]
   
    H = H_Value(label, lowvalue, highvalue)
    adjH = H_Value(adjlabel, lowvalue, highvalue)
    hValues.append(H)
    hValues.append(adjH)
    
    for index in [("traveltime"),("speed"),("travellength"),("waittime")]:
        for veh in combivehlist:
            veh.rank = 0.
        for method in assignments.itervalues():
            method.sumrank = 0.
            
        samecountlist = []
        current = 0
        lastrank = 0
        subtotal = 0.
        adjusted = 0.
        
        combivehlist.sort(key=operator.attrgetter(index))
        totalsamples = len(combivehlist)

        for i in range (0,len(combivehlist)):
            samecount = 0
            if i <= current:
                if index == "traveltime":
                    value = combivehlist[current].traveltime
                elif index == "speed":
                    value = combivehlist[current].speed
                elif index == "travellength":
                    value = combivehlist[current].travellength
                elif index == "waittime":
                    value = combivehlist[current].waittime
            else:
                print 'error!'

            for j in range (current,len(combivehlist)):
                if index == "traveltime":
                    if combivehlist[j].traveltime == value:
                        samecount += 1
                    else:
                        break
                elif index == "speed":
                    if combivehlist[j].speed == value:
                        samecount += 1
                    else:
                        break
                elif index == "travellength":
                    if combivehlist[j].travellength == value:
                        samecount += 1
                    else:
                        break
                elif index == "waittime":
                    if combivehlist[j].waittime == value:
                        samecount += 1
                    else:
                        break                    

            if samecount == 1.:
                lastrank += 1.
                combivehlist[current].rank = lastrank
            else:
                sumrank = 0.
                for j in range (0, samecount):
                    lastrank += 1.
                    sumrank += lastrank
                rank = sumrank/samecount
                for j in range (0, samecount):
                    combivehlist[current+j].rank = rank
                
                elem = (value, samecount)
                samecountlist.append(elem)
            
            current = current + samecount
            
            if current > (len(combivehlist) - 1):
                break
                print 'current:', current
                            
        for veh in combivehlist:
            for method in assignments.itervalues():
                if veh.method == method.label:
                    method.sumrank += veh.rank

        for method in assignments.itervalues():
            subtotal += (method.sumrank**2.) / method.totalVeh
            
        for elem in samecountlist:
            adjusted += (float(elem[1]**3) - float(elem[1]))
        
        c = 1. - (adjusted /float(totalsamples**3 - totalsamples))            

        if index == "traveltime":
            H.traveltime = 12./(totalsamples*(totalsamples+1)) * subtotal - 3.*(totalsamples+1)
            if c > 0.:
                adjH.traveltime = H.traveltime / c
        elif index == "speed":
            H.travelspeed = 12./(totalsamples*(totalsamples+1)) * subtotal - 3.*(totalsamples+1)
            if c > 0.:
                adjH.travelspeed = H.travelspeed / c
        elif index == "travellength":
            H.travellength = 12./(totalsamples*(totalsamples+1)) * subtotal - 3.*(totalsamples+1)
            if c > 0.:
                adjH.travellength = H.travellength / c
        elif index == "waittime":    
            H.waittime = 12./(totalsamples*(totalsamples+1)) * subtotal - 3.*(totalsamples+1)
            if c > 0.:
                adjH.waittime = H.waittime / c
Esempio n. 2
0
def doKruskalWallisTest(verbose, groups, combivehlist, assignments, label,
                        hValues):
    if verbose:
        print '\nbegin the Kruskal-Wallis test!'
        print 'methods:', label
        print 'number of samples:', len(combivehlist)
    adjlabel = label + '_' + "adjusted"
    if groups >= 100.:
        groups = 100.
    lowvalue = chiSquareTable[int(groups) - 1][2]
    highvalue = chiSquareTable[int(groups) - 1][4]

    H = H_Value(label, lowvalue, highvalue)
    adjH = H_Value(adjlabel, lowvalue, highvalue)
    hValues.append(H)
    hValues.append(adjH)

    for index in [("traveltime"), ("speed"), ("travellength"), ("waittime")]:
        for veh in combivehlist:
            veh.rank = 0.
        for method in assignments.itervalues():
            method.sumrank = 0.

        samecountlist = []
        current = 0
        lastrank = 0
        subtotal = 0.
        adjusted = 0.

        combivehlist.sort(key=operator.attrgetter(index))
        totalsamples = len(combivehlist)

        for i in range(0, len(combivehlist)):
            samecount = 0
            if i <= current:
                if index == "traveltime":
                    value = combivehlist[current].traveltime
                elif index == "speed":
                    value = combivehlist[current].speed
                elif index == "travellength":
                    value = combivehlist[current].travellength
                elif index == "waittime":
                    value = combivehlist[current].waittime
            else:
                print 'error!'

            for j in range(current, len(combivehlist)):
                if index == "traveltime":
                    if combivehlist[j].traveltime == value:
                        samecount += 1
                    else:
                        break
                elif index == "speed":
                    if combivehlist[j].speed == value:
                        samecount += 1
                    else:
                        break
                elif index == "travellength":
                    if combivehlist[j].travellength == value:
                        samecount += 1
                    else:
                        break
                elif index == "waittime":
                    if combivehlist[j].waittime == value:
                        samecount += 1
                    else:
                        break

            if samecount == 1.:
                lastrank += 1.
                combivehlist[current].rank = lastrank
            else:
                sumrank = 0.
                for j in range(0, samecount):
                    lastrank += 1.
                    sumrank += lastrank
                rank = sumrank / samecount
                for j in range(0, samecount):
                    combivehlist[current + j].rank = rank

                elem = (value, samecount)
                samecountlist.append(elem)

            current = current + samecount

            if current > (len(combivehlist) - 1):
                break
                print 'current:', current

        for veh in combivehlist:
            for method in assignments.itervalues():
                if veh.method == method.label:
                    method.sumrank += veh.rank

        for method in assignments.itervalues():
            subtotal += (method.sumrank**2.) / method.totalVeh

        for elem in samecountlist:
            adjusted += (float(elem[1]**3) - float(elem[1]))

        c = 1. - (adjusted / float(totalsamples**3 - totalsamples))

        if index == "traveltime":
            H.traveltime = 12. / (totalsamples *
                                  (totalsamples + 1)) * subtotal - 3. * (
                                      totalsamples + 1)
            if c > 0.:
                adjH.traveltime = H.traveltime / c
        elif index == "speed":
            H.travelspeed = 12. / (totalsamples *
                                   (totalsamples + 1)) * subtotal - 3. * (
                                       totalsamples + 1)
            if c > 0.:
                adjH.travelspeed = H.travelspeed / c
        elif index == "travellength":
            H.travellength = 12. / (totalsamples *
                                    (totalsamples + 1)) * subtotal - 3. * (
                                        totalsamples + 1)
            if c > 0.:
                adjH.travellength = H.travellength / c
        elif index == "waittime":
            H.waittime = 12. / (totalsamples *
                                (totalsamples + 1)) * subtotal - 3. * (
                                    totalsamples + 1)
            if c > 0.:
                adjH.waittime = H.waittime / c