# -*- coding: utf-8 -*-

from __future__ import division, print_function, absolute_import

import scipy.misc
import scipy.random

def compute_ts(ratings):
    """
    Compute the test statistic
    .. math:: \rho_s \equiv
        \frac{1}{N_s {R \choose 2}} \sum_{i=1}^{N_s} \sum_{r=1}^{R-1}
            \sum_{v=r+1}^R 1(L_{s,i,r} = L_{s,i,v})

    Parameters
    ----------
    ratings: array_like
             Input array of dimension [R, Ns]

    Returns
    -------
    rho_s: float
           concordance of the ratings, where perfect concordance is 1.0
    """
    R, Ns = ratings.shape
    tmp = ratings.sum(0)
    counts = scipy.misc.comb(tmp, 2) + scipy.misc.comb(R-tmp, 2)
    rho_s = counts.sum()/(Ns*scipy.misc.comb(R, 2))
    return rho_s

def permute_rows(ratings):
    """
    Permute elements of each row in the ratings matrix except the top row.
    Each row corresponds to the ratings given by a single rater; columns correspond to items rated.
    Parameters
    ----------
    ratings: array_like
             Input array of dimension [R, Ns]

    Returns
    -------
    True

    Action
    ------
    Permutes the elements of each row of <ratings> in place; leaves the top row unchanged
    """
    np.apply_along_axis(np.random.shuffle, axis=1, arr=ratings)
    return True
@@ -1286,12 +1286,1859 @@
  return True
+%0A%0Adef simulate_ts_dist(ratings, obs_ts = None, iter=10000, keep_dist = False):%0A    %22%22%22%0A    Simulates the permutation distribution of the irr test statistic for a matrix of%0A    ratings %3Cratings%3E%0A%0A    If obs_ts is not null, computes the reference value of the test statistic before%0A    the first permutation. Otherwise, uses the value obs_ts for comparison.%0A%0A    If %3Ckeep_dist%3E, return the distribution of values of the test statistic; otherwise,%0A    return only the number of permutations for which the value of the irr test statistic is%0A    at least as large as obs_ts.%0A%0A%0A    Parameters%0A    ----------%0A    ratings : array_like%0A              Input array of dimension %5BR, Ns%5D%0A%0A    obs_ts : float%0A             observed value of the test statistic for the original data%0A%0A    iter : integer%0A           number of random permutation of the elements of each row of ratings%0A%0A    keep_dist : bool%0A                flag for whether to store and return the array of values of the irr%0A                test statistic%0A%0A%0A    Returns%0A    -------%0A    out : %7Bobs_ts, geq, iter, dist%7D%0A    obs_ts : observed value of the test statistic, or the input value of obs_ts if given%0A    geq : number of iterations for which the test statistic was greater than or equal to%0A          obs_ts%0A    iter : iter%0A    dist : if %3Ckeep_dist%3E, the array of values of the irr test statistic from the iter%0A           iterations.  Otherwise, null.%0A    %22%22%22%0A    if obs_ts is None:%0A        obs_ts = compute_ts(ratings)%0A    r = ratings.copy()%0A    if keep_dist:%0A        dist = np.zeros(iter)%0A        dist = %5Bcompute_ts(permute_rows(r)) for i in range(iter)%5D%0A        geq = np.sum(dist %3E= obs_ts)%0A    else:%0A        dist = None%0A        geq = 0%0A        for i in range(iter):%0A            geq += compute_ts(permute_rows(r)) %3E= obs_ts%0A    return %7B%22obs_ts%22: obs_ts, %22geq%22: geq, %22iter%22: iter, %22dist%22: dist%7D

add simulate_ts_dist