def cindex(prediction, survtime, survevent):
    """Function to compute the concordance index for a risk prediction,
    i.e., the probability that, for a pair of randomly chosen
    comparable samples, the sample with the higher risk prediction
    will experience an event before the other sample or belongs to a
    higher binary class.

    :param prediction: a vector of risk predictions.
    :param survtime: a vector of event times.
    :param survevent: a vector of event occurence indicators (True and False).

    :: 

        >>> from dreamtools.core.cindex import cindex
        >>> print(cindex([0, 1, 3,4], [0, 4, 3, 1], [True]*4))
        0.5
        >>> print(cindex([0, 1, 3,4], [0, 1, 3, 4], [True]*4))
        0.0

    """
    count = 0
    score = 0
    N = len(survtime)
    for i in range(0, N - 1):
        for j in range(i + 1, N):
            if survevent[i] == True and survevent[j] == True or survevent[i
                ] == True and survtime[j] >= survtime[i] or survevent[j
                ] == True and survtime[i] >= survtime[j]:
                count = count + 2
                if survtime[i] < survtime[j]:
                    if prediction[i] > prediction[j]:
                        score = score + 2
                    if prediction[i] == prediction[j]:
                        score = score + 1
                if survtime[i] == survtime[j]:
                    if prediction[i] == prediction[j]:
                        score = score + 2
                if survtime[i] > survtime[j]:
                    if prediction[i] < prediction[j]:
                        score = score + 2
                    if prediction[i] == prediction[j]:
                        score = score + 1
    return score / float(count)
