Python scipy.stats.spearmanr() Examples

The following are code examples for showing how to use scipy.stats.spearmanr(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: explirefit   Author: codogogo   File: appleveleval.py    Apache License 2.0 6 votes vote down vote up
def evaluate_reps_simlex(embs, simlex_entries, lang = 'en', lang_prefix_first = None, lang_prefix_second = None, lower = False):	
	golds = []
	sims = []
	for entry in simlex_entries:
		w1 = lang_prefix_first + "__" + entry[0].strip() if lang_prefix_first else entry[0].strip()
		w2 = lang_prefix_first + "__" + entry[1].strip() if lang_prefix_first else entry[1].strip()
		if lower:
			w1 = w1.lower()
			w2 = w2.lower()	
		vec1 = embs.get_vector(lang, w1)
		vec2 = embs.get_vector(lang, w2)
		if vec1 is not None and vec2 is not None:
			golds.append(float(entry[2]))
			sims.append(simple_stats.cosine(vec1, vec2))
		if vec1 is None:
			print("Not found: " + w1)
		if vec2 is None:
			print("Not found: " + w2)
	print("Correlation evaluating on " + str(len(golds)) + " pairs")
	spearman = stats.spearmanr(sims, golds)
	pearson = stats.pearsonr(sims, golds)
	return spearman[0], pearson[0] 
Example 2
Project: procgen   Author: juancroldan   File: white.py    Apache License 2.0 6 votes vote down vote up
def white4D_functional():
	print("Testing correlation for 4D white noise")
	N = 20
	x1 = randrange(-1000, 1000, 1)
	y1 = randrange(-1000, 1000, 1)
	z1 = randrange(-1000, 1000, 1)
	w1 = randrange(-1000, 1000, 1)
	x2 = x1 + randrange(-1000, 1000, 1)
	y2 = y1 + randrange(-1000, 1000, 1)
	z2 = z1 + randrange(-1000, 1000, 1)
	w2 = w1 + randrange(-1000, 1000, 1)
	values1 = [[[[combined(white, x / N, y / N) for x in range(x1, x1 + N)] for y in range(y1, y1 + N)] for z in range(z1, z1 + N)] for w in range(w1, w1 + N)]
	values2 = [[[[combined(white, x / N, y / N) for x in range(x2, x2 + N)] for y in range(y2, y2 + N)] for z in range(z2, z2 + N)] for w in range(w2, w2 + N)]
	rho = spearmanr(values1, values2, axis=None)
	assert abs(rho[0]) < 0.5
	print("rho = %s" % rho[0])
	print("\tNot signifying correlation found") 
Example 3
Project: phoneticSimilarity   Author: ronggong   File: eval_embedding.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def correlationDistanceMat():
    """calculate the corr coef between two distance mats"""
    path_eval_0 = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phoneme_embedding_frame_level'
    path_eval_1 = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phone_embedding_classifier'
    path_eval_2 = '/Users/gong/Documents/pycharmProjects/phoneticSimilarity/eval/phoneme_embedding_siamese_triplet'

    dist_mat_0 = np.load(os.path.join(path_eval_0, 'dist_mat_0.npy'))
    dist_mat_1 = np.load(os.path.join(path_eval_1, 'dist_mat_0.npy'))
    dist_mat_2 = np.load(os.path.join(path_eval_2, 'dist_mat_0.npy'))

    sample_num = dist_mat_0.shape[0]
    iu1 = np.triu_indices(sample_num, 1)  # trim the upper mat

    r_01, _ = spearmanr(dist_mat_0[iu1], dist_mat_1[iu1])
    r_02, _ = spearmanr(dist_mat_0[iu1], dist_mat_2[iu1])
    r_12, _ = spearmanr(dist_mat_1[iu1], dist_mat_2[iu1])

    print('correlation frame, classifier', r_01)
    print('correlation frame, siamese', r_02)
    print('correlation classifier, siamese', r_12)
    # return r 
Example 4
Project: scanorama   Author: brianhie   File: time_align.py    MIT License 6 votes vote down vote up
def time_dist(datasets_dimred, time):
    time_dist = euclidean_distances(time, time)

    time_dists, scores = [], []
    for i in range(time_dist.shape[0]):
        for j in range(time_dist.shape[1]):
            if i >= j:
                continue
            score = np.mean(euclidean_distances(
                datasets_dimred[i], datasets_dimred[j]
            ))
            time_dists.append(time_dist[i, j])
            scores.append(score)

    print('Spearman rho = {}'.format(spearmanr(time_dists, scores)))
    print('Pearson rho = {}'.format(pearsonr(time_dists, scores))) 
Example 5
Project: link-prediction_with_deep-learning   Author: cambridgeltl   File: evalrank.py    MIT License 6 votes vote down vote up
def evaluate(wv, reference):
    """Evaluate wv against reference, return (rho, count) where rwo is
    Spearman's rho and count is the number of reference word pairs
    that could be evaluated against.
    """
    gold, predicted = [], []
    for words, sim in sorted(reference, key=lambda ws: ws[1]):
        try:
            v1, v2 = wv[words[0]], wv[words[1]]
        except KeyError:
            continue
        gold.append((words, sim))
        predicted.append((words, cosine(v1, v2)))
    simlist = lambda ws: [s for w,s in ws]
    rho, p = spearmanr(simlist(gold), simlist(predicted))
    return (rho, len(gold)) 
Example 6
Project: LaserTOF   Author: kyleuckert   File: test_mstats_basic.py    MIT License 6 votes vote down vote up
def test_spearmanr(self):
        # Tests some computations of Spearman's rho
        (x, y) = ([5.05,6.75,3.21,2.66],[1.65,2.64,2.64,6.95])
        assert_almost_equal(mstats.spearmanr(x,y)[0], -0.6324555)
        (x, y) = ([5.05,6.75,3.21,2.66,np.nan],[1.65,2.64,2.64,6.95,np.nan])
        (x, y) = (ma.fix_invalid(x), ma.fix_invalid(y))
        assert_almost_equal(mstats.spearmanr(x,y)[0], -0.6324555)

        x = [2.0, 47.4, 42.0, 10.8, 60.1, 1.7, 64.0, 63.1,
              1.0, 1.4, 7.9, 0.3, 3.9, 0.3, 6.7]
        y = [22.6, 8.3, 44.4, 11.9, 24.6, 0.6, 5.7, 41.6,
              0.0, 0.6, 6.7, 3.8, 1.0, 1.2, 1.4]
        assert_almost_equal(mstats.spearmanr(x,y)[0], 0.6887299)
        x = [2.0, 47.4, 42.0, 10.8, 60.1, 1.7, 64.0, 63.1,
              1.0, 1.4, 7.9, 0.3, 3.9, 0.3, 6.7, np.nan]
        y = [22.6, 8.3, 44.4, 11.9, 24.6, 0.6, 5.7, 41.6,
              0.0, 0.6, 6.7, 3.8, 1.0, 1.2, 1.4, np.nan]
        (x, y) = (ma.fix_invalid(x), ma.fix_invalid(y))
        assert_almost_equal(mstats.spearmanr(x,y)[0], 0.6887299)

        # test for namedtuple attributes
        res = mstats.spearmanr(x, y)
        attributes = ('correlation', 'pvalue')
        check_named_results(res, attributes, ma=True) 
Example 7
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 6 votes vote down vote up
def test_tie1(self):
        # Data
        x = [1.0, 2.0, 3.0, 4.0]
        y = [1.0, 2.0, 2.0, 3.0]
        # Ranks of the data, with tie-handling.
        xr = [1.0, 2.0, 3.0, 4.0]
        yr = [1.0, 2.5, 2.5, 4.0]
        # Result of spearmanr should be the same as applying
        # pearsonr to the ranks.
        sr = stats.spearmanr(x, y)
        pr = stats.pearsonr(xr, yr)
        assert_almost_equal(sr, pr)


#    W.II.E.  Tabulate X against X, using BIG as a case weight.  The values
#    should appear on the diagonal and the total should be 899999955.
#    If the table cannot hold these values, forget about working with
#    census data.  You can also tabulate HUGE against TINY.  There is no
#    reason a tabulation program should not be able to distinguish
#    different values regardless of their magnitude.

# I need to figure out how to do this one. 
Example 8
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: nanops.py    MIT License 6 votes vote down vote up
def get_corr_func(method):
    if method in ["kendall", "spearman"]:
        from scipy.stats import kendalltau, spearmanr
    elif callable(method):
        return method

    def _pearson(a, b):
        return np.corrcoef(a, b)[0, 1]

    def _kendall(a, b):
        rs = kendalltau(a, b)
        if isinstance(rs, tuple):
            return rs[0]
        return rs

    def _spearman(a, b):
        return spearmanr(a, b)[0]

    _cor_methods = {"pearson": _pearson, "kendall": _kendall, "spearman": _spearman}
    return _cor_methods[method] 
Example 9
Project: recruit   Author: Frank-qlu   File: nanops.py    Apache License 2.0 6 votes vote down vote up
def get_corr_func(method):
    if method in ['kendall', 'spearman']:
        from scipy.stats import kendalltau, spearmanr
    elif callable(method):
        return method

    def _pearson(a, b):
        return np.corrcoef(a, b)[0, 1]

    def _kendall(a, b):
        rs = kendalltau(a, b)
        if isinstance(rs, tuple):
            return rs[0]
        return rs

    def _spearman(a, b):
        return spearmanr(a, b)[0]

    _cor_methods = {
        'pearson': _pearson,
        'kendall': _kendall,
        'spearman': _spearman
    }
    return _cor_methods[method] 
Example 10
Project: kor2vec   Author: dongjun-Lee   File: similarity_test.py    MIT License 6 votes vote down vote up
def word_sim_test(filename, pos_vectors):
    delim = ','
    actual_sim_list, pred_sim_list = [], []
    missed = 0

    with open(filename, 'r') as pairs:
        for pair in pairs:
            w1, w2, actual_sim = pair.strip().split(delim)

            try:
                w1_vec = create_word_vector(w1, pos_vectors)
                w2_vec = create_word_vector(w2, pos_vectors)
                pred = float(np.inner(w1_vec, w2_vec))
                actual_sim_list.append(float(actual_sim))
                pred_sim_list.append(pred)

            except KeyError:
                missed += 1

    spearman, _ = st.spearmanr(actual_sim_list, pred_sim_list)
    pearson, _ = st.pearsonr(actual_sim_list, pred_sim_list)

    return spearman, pearson, missed 
Example 11
Project: basenji   Author: calico   File: accuracy.py    Apache License 2.0 6 votes vote down vote up
def spearmanr(self):
    """ Compute target SpearmanR vector. """

    scor = np.zeros(self.num_targets)

    for ti in range(self.num_targets):
      if self.targets_na is not None:
        preds_ti = self.preds[~self.targets_na, ti]
        targets_ti = self.targets[~self.targets_na, ti]
      else:
        preds_ti = self.preds[:, :, ti].flatten()
        targets_ti = self.targets[:, :, ti].flatten()

      sc, _ = stats.spearmanr(targets_ti, preds_ti)
      scor[ti] = sc

    return scor


################################################################################
# __main__
################################################################################ 
Example 12
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_mstats_basic.py    GNU General Public License v3.0 6 votes vote down vote up
def test_spearmanr(self):
        # Tests some computations of Spearman's rho
        (x, y) = ([5.05,6.75,3.21,2.66],[1.65,2.64,2.64,6.95])
        assert_almost_equal(mstats.spearmanr(x,y)[0], -0.6324555)
        (x, y) = ([5.05,6.75,3.21,2.66,np.nan],[1.65,2.64,2.64,6.95,np.nan])
        (x, y) = (ma.fix_invalid(x), ma.fix_invalid(y))
        assert_almost_equal(mstats.spearmanr(x,y)[0], -0.6324555)

        x = [2.0, 47.4, 42.0, 10.8, 60.1, 1.7, 64.0, 63.1,
              1.0, 1.4, 7.9, 0.3, 3.9, 0.3, 6.7]
        y = [22.6, 8.3, 44.4, 11.9, 24.6, 0.6, 5.7, 41.6,
              0.0, 0.6, 6.7, 3.8, 1.0, 1.2, 1.4]
        assert_almost_equal(mstats.spearmanr(x,y)[0], 0.6887299)
        x = [2.0, 47.4, 42.0, 10.8, 60.1, 1.7, 64.0, 63.1,
              1.0, 1.4, 7.9, 0.3, 3.9, 0.3, 6.7, np.nan]
        y = [22.6, 8.3, 44.4, 11.9, 24.6, 0.6, 5.7, 41.6,
              0.0, 0.6, 6.7, 3.8, 1.0, 1.2, 1.4, np.nan]
        (x, y) = (ma.fix_invalid(x), ma.fix_invalid(y))
        assert_almost_equal(mstats.spearmanr(x,y)[0], 0.6887299) 
Example 13
Project: rankeval   Author: hpclab   File: spearman_rho.py    Mozilla Public License 2.0 6 votes vote down vote up
def eval_per_query(self, y, y_pred):
        """
        This methods computes Spearman Rho at per query level (on the instances
        belonging to a specific query).

        Parameters
        ----------
        y: numpy array
            Represents the labels of instances corresponding to one query in the
            dataset (ground truth).
        y_pred: numpy array.
            Represents the predicted document scores obtained during the model
            scoring phase for that query.

        Returns
        -------
        rho: float
            The Spearman Rho per query.
        """
        spearman_rho = stats.spearmanr(y, y_pred)
        return spearman_rho.correlation 
Example 14
Project: bertwsi   Author: asafamr   File: semeval_utils.py    Apache License 2.0 6 votes vote down vote up
def get_n_senses_corr(gold_key, new_key):
    senses_lemma_gold = defaultdict(set)
    senses_lemma_sys = defaultdict(set)
    for dic, key in [(senses_lemma_gold, gold_key), (senses_lemma_sys, new_key)]:
        with open(key, 'r') as fin:
            for line in fin:
                split = line.split()
                lemma = split[0]
                senses = [x.split('/')[0] for x in split[2:]]
                dic[lemma].update(senses)
    oredered_lemmas = list(senses_lemma_gold.keys())
    corr = {}
    for ending, name in [('', 'all'), ('v', 'VERB'), ('n', 'NOUN'), ('j', 'ADJ')]:
        g = [len(senses_lemma_gold[x]) for x in oredered_lemmas if x.endswith(ending)]
        n = [len(senses_lemma_sys[x]) for x in oredered_lemmas if x.endswith(ending)]
        c = spearmanr(g, n)
        corr[name] = c.correlation, c.pvalue
    return corr 
Example 15
Project: procgen   Author: juancroldan   File: white.py    Apache License 2.0 5 votes vote down vote up
def white2D_functional():
	print("Testing correlation for 2D white noise")
	N = 100
	x1 = randrange(-1000, 1000, 1)
	y1 = randrange(-1000, 1000, 1)
	x2 = x1 + randrange(-1000, 1000, 1)
	y2 = y1 + randrange(-1000, 1000, 1)
	values1 = [[combined(white, x / N, y / N) for x in range(x1, x1 + N)] for y in range(y1, y1 + N)]
	values2 = [[combined(white, x / N, y / N) for x in range(x2, x2 + N)] for y in range(y2, y2 + N)]
	rho = spearmanr(values1, values2, axis=None)
	assert abs(rho[0]) < 0.5
	print("rho = %s" % rho[0])
	print("\tNot signifying correlation found") 
Example 16
Project: procgen   Author: juancroldan   File: white.py    Apache License 2.0 5 votes vote down vote up
def white3D_functional():
	print("Testing correlation for 3D white noise")
	N = 100
	x1 = randrange(-1000, 1000, 1)
	y1 = randrange(-1000, 1000, 1)
	z1 = randrange(-1000, 1000, 1)
	x2 = x1 + randrange(-1000, 1000, 1)
	y2 = y1 + randrange(-1000, 1000, 1)
	z2 = z1 + randrange(-1000, 1000, 1)
	values1 = [[[combined(white, x / N, y / N) for x in range(x1, x1 + N)] for y in range(y1, y1 + N)] for z in range(z1, z1 + N)]
	values2 = [[[combined(white, x / N, y / N) for x in range(x2, x2 + N)] for y in range(y2, y2 + N)] for z in range(z2, z2 + N)]
	rho = spearmanr(values1, values2, axis=None)
	assert abs(rho[0]) < 0.5
	print("rho = %s" % rho[0])
	print("\tNot signifying correlation found") 
Example 17
Project: LorentzianDistanceRetrieval   Author: MarcTLaw   File: lorentzian_embed_with_normalized_rank.py    MIT License 5 votes vote down vote up
def ranking(types, model, distfn, data, order_rank = None):
    lt = th.from_numpy(model.embedding())
    embedding = Variable(lt, volatile=True)
    ranks = []
    ap_scores = []
    norms = []
    ordered_ranks = []
    for s, s_types in types.items():
        if order_rank is not None:
            lts = lt[s]            
            ltsnorm = th.sum(lts * lts, dim=-1)
            
            norms.append(float(ltsnorm[0]))
            ordered_ranks.append(order_rank[data.objects[s]])
            
        s_e = Variable(lt[s].expand_as(embedding), volatile=True)
        _dists = model.dist()(s_e, embedding).data.cpu().numpy().flatten()
        _dists[s] = 1e+12
        _labels = np.zeros(embedding.size(0))
        _dists_masked = _dists.copy()
        _ranks = []
        for o in s_types:
            _dists_masked[o] = np.Inf
            _labels[o] = 1
        ap_scores.append(average_precision_score(_labels, -_dists))
        for o in s_types:
            d = _dists_masked.copy()
            d[o] = _dists[o]
            r = np.argsort(d)
            _ranks.append(np.where(r == o)[0][0] + 1)
        ranks += _ranks
    rho = None
    if order_rank is not None:
        rho, pval = spearmanr(ordered_ranks,norms)

    return np.mean(ranks), np.mean(ap_scores), rho 
Example 18
Project: botbuilder-python   Author: microsoft   File: bert_util.py    MIT License 5 votes vote down vote up
def pearson_and_spearman(preds, labels):
        pearson_corr = pearsonr(preds, labels)[0]
        spearman_corr = spearmanr(preds, labels)[0]
        return {
            "pearson": pearson_corr,
            "spearmanr": spearman_corr,
            "corr": (pearson_corr + spearman_corr) / 2,
        } 
Example 19
Project: phoneticSimilarity   Author: ronggong   File: eval.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def humanMachineCorrCoef(rating_human, rating_machine):

    score_human, score_machine = dict2score(rating_human, rating_machine)

    # r, p = pearsonr(score_human, score_SR_oracle)
    # print('pearson cof', r)
    r, p = spearmanr(score_human, score_machine)
    print('spearman cof', r)
    return r 
Example 20
Project: phoneticSimilarity   Author: ronggong   File: regression_train_predict.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def statisticsTrainPredict(features_head_train,
                           features_belly_train,
                           features_head_test,
                           features_belly_test,
                           mode):
    X_val, y_val = organizeFeaturesStat(features_head=features_head_train,
                                        features_belly=features_belly_train)

    reg = linear_model.LinearRegression()
    reg.fit(X_val, y_val)

    ransac = linear_model.RANSACRegressor()
    ransac.fit(X_val, y_val)

    svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
    svr_rbf.fit(X_val, y_val)

    X_test, y_test = organizeFeaturesStat(features_head=features_head_test,
                                          features_belly=features_belly_test)

    y_pred_lin = reg.predict(X_test)
    y_pred_ran = ransac.predict(X_test)
    y_pred_svr = svr_rbf.predict(X_test)

    r, p = spearmanr(y_test, y_pred_lin)
    print(r)

    y_pred_lin = reg.predict(X_val)
    r, p = spearmanr(y_val, y_pred_lin)
    print(r) 
Example 21
Project: scanorama   Author: brianhie   File: time_align.py    MIT License 5 votes vote down vote up
def time_align_correlate(alignments, time):
    time_dist = euclidean_distances(time, time)

    assert(time_dist.shape == alignments.shape)

    time_dists, scores = [], []
    for i in range(time_dist.shape[0]):
        for j in range(time_dist.shape[1]):
            if i >= j:
                continue
            time_dists.append(time_dist[i, j])
            scores.append(alignments[i, j])

    print('Spearman rho = {}'.format(spearmanr(time_dists, scores)))
    print('Pearson rho = {}'.format(pearsonr(time_dists, scores))) 
Example 22
Project: metk   Author: PatWalters   File: metk_report.py    MIT License 5 votes vote down vote up
def metk_report(df_kcal):
    """
    Generate a report
    :param df_kcal: input dataframe, activity should be in kcal/mol
    :param outfile: output file for the report
    :return: the report as a list of strings
    """
    N = df_kcal.shape[0]
    pred = df_kcal['Pred']
    expr = df_kcal['Exp']
    rms_val = rmse(pred, expr)
    mae_val = mean_absolute_error(pred, expr)
    pearson_r, pearson_p = pearsonr(pred, expr)
    pearson_vals = [x ** 2 for x in [pearson_r] + list(pearson_confidence(pearson_r, N))]
    spearman_r, spearman_p = spearmanr(pred, expr)
    kendall_t, kendall_p = kendalltau(pred, expr)
    max_correlation = max_possible_correlation([log10(kcal_to_ki(x, "M")) for x in df_kcal['Exp']])
    report = []
    report.append("N = %d" % N)
    report.append("RMSE = %.2f kcal/mol" % rms_val)
    report.append("MAE  = %.2f kcal/mol" % mae_val)
    report.append("Max possible correlation = %.2f" % max_correlation)
    report.append("Pearson R^2 = %0.2f  95%%CI = %.2f %.2f" % tuple(pearson_vals))
    report.append("Spearman rho = %0.2f" % spearman_r)
    report.append("Kendall tau = %0.2f" % kendall_t)
    return report 
Example 23
Project: mmvec   Author: biocore   File: test_method.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_fit(self):
        np.random.seed(1)
        tf.reset_default_graph()
        tf.set_random_seed(0)
        latent_dim = 2
        res_ranks, res_biplot = paired_omics(
            self.microbes, self.metabolites,
            epochs=1000, latent_dim=latent_dim,
            min_feature_count=1, learning_rate=0.1
        )
        res_ranks = clr_inv(res_ranks.T)
        s_r, s_p = spearmanr(np.ravel(res_ranks), np.ravel(self.exp_ranks))

        self.assertGreater(s_r, 0.5)
        self.assertLess(s_p, 1e-2)

        # make sure the biplot is of the correct dimensions
        npt.assert_allclose(
            res_biplot.samples.shape,
            np.array([self.microbes.shape[0], latent_dim]))
        npt.assert_allclose(
            res_biplot.features.shape,
            np.array([self.metabolites.shape[0], latent_dim]))

        # make sure that the biplot has the correct ordering
        self.assertGreater(res_biplot.proportion_explained[0],
                           res_biplot.proportion_explained[1])
        self.assertGreater(res_biplot.eigvals[0],
                           res_biplot.eigvals[1]) 
Example 24
Project: mmvec   Author: biocore   File: test_multimodal.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_fit(self):
        np.random.seed(1)
        tf.reset_default_graph()
        n, d1 = self.trainX.shape
        n, d2 = self.trainY.shape
        with tf.Graph().as_default(), tf.Session() as session:
            set_random_seed(0)
            model = MMvec(beta_1=0.8, beta_2=0.9, latent_dim=2)
            model(session,
                  coo_matrix(self.trainX.values), self.trainY.values,
                  coo_matrix(self.testX.values), self.testY.values)
            model.fit(epoch=1000)

            U_ = np.hstack(
                (np.ones((self.U.shape[0], 1)), self.Ubias, self.U))
            V_ = np.vstack(
                (self.Vbias, np.ones((1, self.V.shape[1])), self.V))

            u_r, u_p = spearmanr(pdist(model.U), pdist(self.U))
            v_r, v_p = spearmanr(pdist(model.V.T), pdist(self.V.T))

            res = softmax(model.ranks())
            exp = softmax(np.hstack((np.zeros((d1, 1)), U_ @ V_)))
            s_r, s_p = spearmanr(np.ravel(res), np.ravel(exp))

            self.assertGreater(u_r, 0.5)
            self.assertGreater(v_r, 0.5)
            self.assertGreater(s_r, 0.5)
            self.assertLess(u_p, 5e-2)
            self.assertLess(v_p, 5e-2)
            self.assertLess(s_p, 5e-2)

            # sanity check cross validation
            self.assertLess(model.cv.eval(), 500) 
Example 25
Project: LaserTOF   Author: kyleuckert   File: test_mstats_basic.py    MIT License 5 votes vote down vote up
def test_spearmanr(self):
        for n in self.get_n():
            x, y, xm, ym = self.generate_xy_sample(n)
            r, p = stats.spearmanr(x, y)
            rm, pm = stats.mstats.spearmanr(xm, ym)
            assert_almost_equal(r, rm, 14)
            assert_almost_equal(p, pm, 14) 
Example 26
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_scalar(self):
        y = stats.spearmanr(4., 2.)
        assert_(np.isnan(y).all()) 
Example 27
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_uneven_lengths(self):
        assert_raises(ValueError, stats.spearmanr, [1, 2, 1], [8, 9])
        assert_raises(ValueError, stats.spearmanr, [1, 2, 1], 8) 
Example 28
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_nan_policy(self):
        x = np.arange(10.)
        x[9] = np.nan
        assert_array_equal(stats.spearmanr(x, x), (np.nan, np.nan))
        assert_array_equal(stats.spearmanr(x, x, nan_policy='omit'),
                           (1.0, 0.0))
        assert_raises(ValueError, stats.spearmanr, x, x, nan_policy='raise')
        assert_raises(ValueError, stats.spearmanr, x, x, nan_policy='foobar') 
Example 29
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sXBIG(self):
        y = stats.spearmanr(X,BIG)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 30
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sXLITTLE(self):
        y = stats.spearmanr(X,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 31
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sXHUGE(self):
        y = stats.spearmanr(X,HUGE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 32
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sXTINY(self):
        y = stats.spearmanr(X,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 33
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sXROUND(self):
        y = stats.spearmanr(X,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 34
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sBIGLITTLE(self):
        y = stats.spearmanr(BIG,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 35
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sBIGHUGE(self):
        y = stats.spearmanr(BIG,HUGE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 36
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sBIGTINY(self):
        y = stats.spearmanr(BIG,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 37
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sBIGROUND(self):
        y = stats.spearmanr(BIG,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 38
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sLITTLELITTLE(self):
        y = stats.spearmanr(LITTLE,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 39
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sLITTLETINY(self):
        y = stats.spearmanr(LITTLE,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 40
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sLITTLEROUND(self):
        y = stats.spearmanr(LITTLE,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 41
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sHUGEHUGE(self):
        y = stats.spearmanr(HUGE,HUGE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 42
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sHUGETINY(self):
        y = stats.spearmanr(HUGE,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 43
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sHUGEROUND(self):
        y = stats.spearmanr(HUGE,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 44
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sTINYROUND(self):
        y = stats.spearmanr(TINY,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 45
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_sROUNDROUND(self):
        y = stats.spearmanr(ROUND,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 46
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 5 votes vote down vote up
def test_spearmanr_result_attributes(self):
        res = stats.spearmanr(X, X)
        attributes = ('correlation', 'pvalue')
        check_named_results(res, attributes) 
Example 47
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_nanops.py    MIT License 5 votes vote down vote up
def test_nancorr_spearman(self):
        from scipy.stats import spearmanr

        targ0 = spearmanr(self.arr_float_2d, self.arr_float1_2d)[0]
        targ1 = spearmanr(self.arr_float_2d.flat, self.arr_float1_2d.flat)[0]
        self.check_nancorr_nancov_2d(nanops.nancorr, targ0, targ1, method="spearman")
        targ0 = spearmanr(self.arr_float_1d, self.arr_float1_1d)[0]
        targ1 = spearmanr(self.arr_float_1d.flat, self.arr_float1_1d.flat)[0]
        self.check_nancorr_nancov_1d(nanops.nancorr, targ0, targ1, method="spearman") 
Example 48
Project: recruit   Author: Frank-qlu   File: test_nanops.py    Apache License 2.0 5 votes vote down vote up
def test_nancorr_spearman(self):
        from scipy.stats import spearmanr
        targ0 = spearmanr(self.arr_float_2d, self.arr_float1_2d)[0]
        targ1 = spearmanr(self.arr_float_2d.flat, self.arr_float1_2d.flat)[0]
        self.check_nancorr_nancov_2d(nanops.nancorr, targ0, targ1,
                                     method='spearman')
        targ0 = spearmanr(self.arr_float_1d, self.arr_float1_1d)[0]
        targ1 = spearmanr(self.arr_float_1d.flat, self.arr_float1_1d.flat)[0]
        self.check_nancorr_nancov_1d(nanops.nancorr, targ0, targ1,
                                     method='spearman') 
Example 49
Project: recruit   Author: Frank-qlu   File: test_analytics.py    Apache License 2.0 5 votes vote down vote up
def test_corr_rank(self):
        import scipy
        import scipy.stats as stats

        # kendall and spearman
        A = tm.makeTimeSeries()
        B = tm.makeTimeSeries()
        A[-5:] = A[:5]
        result = A.corr(B, method='kendall')
        expected = stats.kendalltau(A, B)[0]
        tm.assert_almost_equal(result, expected)

        result = A.corr(B, method='spearman')
        expected = stats.spearmanr(A, B)[0]
        tm.assert_almost_equal(result, expected)

        # these methods got rewritten in 0.8
        if LooseVersion(scipy.__version__) < LooseVersion('0.9'):
            pytest.skip("skipping corr rank because of scipy version "
                        "{0}".format(scipy.__version__))

        # results from R
        A = Series(
            [-0.89926396, 0.94209606, -1.03289164, -0.95445587, 0.76910310, -
             0.06430576, -2.09704447, 0.40660407, -0.89926396, 0.94209606])
        B = Series(
            [-1.01270225, -0.62210117, -1.56895827, 0.59592943, -0.01680292,
             1.17258718, -1.06009347, -0.10222060, -0.89076239, 0.89372375])
        kexp = 0.4319297
        sexp = 0.5853767
        tm.assert_almost_equal(A.corr(B, method='kendall'), kexp)
        tm.assert_almost_equal(A.corr(B, method='spearman'), sexp) 
Example 50
Project: jr-tools   Author: kingjr   File: test_stats.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_corr_functions():
    from scipy.stats import spearmanr
    _test_corr(np.corrcoef, repeated_corr, 1)
    _test_corr(spearmanr, repeated_spearman, 0) 
Example 51
Project: CNN-FRIQA   Author: Bobholamovic   File: utils.py    MIT License 5 votes vote down vote up
def _compute(self, x1, x2):
        return stats.spearmanr(x1, x2)[0] 
Example 52
Project: Fractalis   Author: LCSB-BioCore   File: main.py    Apache License 2.0 5 votes vote down vote up
def compute_stats(df: pd.DataFrame, method: str) -> dict:
        """Compute correlation statistics for the given data and the given
        correlation method.
        :param df: The DataFrame containing our data.
        :param method: The method to use.
        :return: Several relevant statistics
        """
        df = df.drop_duplicates('id')
        df = df.dropna()
        if df.shape[0] < 2:
            return {
                'coef': float('nan'),
                'p_value': float('nan'),
                'slope': float('nan'),
                'intercept': float('nan')
            }
        if method == 'pearson':
            corr_coef, p_value = stats.pearsonr(df['value_x'], df['value_y'])
        elif method == 'spearman':
            corr_coef, p_value = stats.spearmanr(df['value_x'], df['value_y'])
        elif method == 'kendall':
            corr_coef, p_value = stats.kendalltau(df['value_x'], df['value_y'])
        else:
            raise ValueError("Unknown correlation method.")
        slope, intercept, *_ = np.polyfit(df['value_x'], df['value_y'], deg=1)
        return {
            'coef': corr_coef,
            'p_value': p_value,
            'slope': slope,
            'intercept': intercept,
        } 
Example 53
Project: basenji   Author: calico   File: basenji_motifs.py    Apache License 2.0 5 votes vote down vote up
def plot_target_corr(filter_outs, seq_targets, filter_names, target_names, out_pdf, seq_op='mean'):
  num_seqs = filter_outs.shape[0]
  num_targets = len(target_names)

  if seq_op == 'mean':
    filter_outs_seq = filter_outs.mean(axis=2)
  else:
    filter_outs_seq = filter_outs.max(axis=2)

  # std is sequence by filter.
  filter_seqs_std = filter_outs_seq.std(axis=0)
  filter_outs_seq = filter_outs_seq[:, filter_seqs_std > 0]
  filter_names_live = filter_names[filter_seqs_std > 0]

  filter_target_cors = np.zeros((len(filter_names_live), num_targets))
  for fi in range(len(filter_names_live)):
    for ti in range(num_targets):
      cor, p = spearmanr(filter_outs_seq[:, fi], seq_targets[:num_seqs, ti])
      filter_target_cors[fi, ti] = cor

  cor_df = pd.DataFrame(
      filter_target_cors, index=filter_names_live, columns=target_names)

  sns.set(font_scale=0.3)
  plt.figure()
  sns.clustermap(cor_df, cmap='BrBG', center=0, figsize=(8, 10))
  plt.savefig(out_pdf)
  plt.close()


################################################################################
# plot_filter_seq_heat
#
# Plot a clustered heatmap of filter activations in
#
# Input
#  param_matrix: np.array of the filter's parameter matrix
#  out_pdf:
################################################################################ 
Example 54
Project: FARM   Author: deepset-ai   File: metrics.py    Apache License 2.0 5 votes vote down vote up
def pearson_and_spearman(preds, labels):
    pearson_corr = pearsonr(preds, labels)[0]
    spearman_corr = spearmanr(preds, labels)[0]
    return {
        "pearson": pearson_corr,
        "spearman": spearman_corr,
        "corr": (pearson_corr + spearman_corr) / 2,
    } 
Example 55
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_mstats_basic.py    GNU General Public License v3.0 5 votes vote down vote up
def test_spearmanr(self):
        for n in self.get_n():
            x, y, xm, ym = self.generate_xy_sample(n)
            r, p = stats.spearmanr(x, y)
            rm, pm = stats.mstats.spearmanr(xm, ym)
            assert_almost_equal(r, rm, 14)
            assert_almost_equal(p, pm, 14) 
Example 56
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sXX(self):
        y = stats.spearmanr(X,X)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 57
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sXBIG(self):
        y = stats.spearmanr(X,BIG)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 58
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sXLITTLE(self):
        y = stats.spearmanr(X,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 59
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sXTINY(self):
        y = stats.spearmanr(X,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 60
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sXROUND(self):
        y = stats.spearmanr(X,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 61
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sBIGBIG(self):
        y = stats.spearmanr(BIG,BIG)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 62
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sBIGLITTLE(self):
        y = stats.spearmanr(BIG,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 63
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sBIGHUGE(self):
        y = stats.spearmanr(BIG,HUGE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 64
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sBIGROUND(self):
        y = stats.spearmanr(BIG,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 65
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sLITTLELITTLE(self):
        y = stats.spearmanr(LITTLE,LITTLE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 66
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sLITTLEHUGE(self):
        y = stats.spearmanr(LITTLE,HUGE)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 67
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sLITTLETINY(self):
        y = stats.spearmanr(LITTLE,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 68
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sLITTLEROUND(self):
        y = stats.spearmanr(LITTLE,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 69
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sHUGETINY(self):
        y = stats.spearmanr(HUGE,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 70
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sHUGEROUND(self):
        y = stats.spearmanr(HUGE,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 71
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sTINYTINY(self):
        y = stats.spearmanr(TINY,TINY)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 72
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sTINYROUND(self):
        y = stats.spearmanr(TINY,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 73
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sROUNDROUND(self):
        y = stats.spearmanr(ROUND,ROUND)
        r = y[0]
        assert_approx_equal(r,1.0) 
Example 74
Project: fiction   Author: tedunderwood   File: main_experiment.py    MIT License 4 votes vote down vote up
def get_divergence(sampleA, sampleB):
    '''
    This function applies model a to b, and vice versa, and returns
    a couple of measures of divergence: notably lost accuracy and
    z-tranformed spearman correlation.
    '''

    # We start by constructing the paths to the sampleA
    # standard model criteria (.pkl) and
    # model output (.csv) on the examples
    # originally used to train it.

    # We're going to try applying the sampleA standard
    # criteria to another model's output, and vice-
    # versa.

    model1 = '../modeloutput/' + sampleA + '.pkl'
    meta1 = '../modeloutput/' + sampleA + '.csv'

    # Now we construct paths to the test model
    # criteria (.pkl) and output (.csv).

    model2 = '../modeloutput/' + sampleB + '.pkl'
    meta2 = '../modeloutput/' + sampleB + '.csv'

    model1on2 = versatiletrainer2.apply_pickled_model(model1, '../data/', '.tsv', meta2)
    model2on1 = versatiletrainer2.apply_pickled_model(model2, '../data/', '.tsv', meta1)

    spearman1on2 = np.arctanh(stats.spearmanr(model1on2.probability, model1on2.alien_model)[0])
    spearman2on1 = np.arctanh(stats.spearmanr(model2on1.probability, model2on1.alien_model)[0])
    spearman = (spearman1on2 + spearman2on1) / 2

    loss1on2 = accuracy_loss(model1on2)
    loss2on1 = accuracy_loss(model2on1)
    loss = (loss1on2 + loss2on1) / 2

    alienacc2 = accuracy(model1on2, 'alien_model')
    alienacc1 = accuracy(model2on1, 'alien_model')

    acc2 = accuracy(model1on2, 'probability')
    acc1 = accuracy(model2on1, 'probability')

    meandate2 = np.mean(model1on2.std_date)
    meandate1 = np.mean(model2on1.std_date)

    return spearman, loss, spearman1on2, spearman2on1, loss1on2, loss2on1, acc1, acc2, alienacc1, alienacc2, meandate1, meandate2 
Example 75
Project: fiction   Author: tedunderwood   File: new_experiment.py    MIT License 4 votes vote down vote up
def get_divergence(sampleA, sampleB):
    '''
    This function applies model a to b, and vice versa, and returns
    a couple of measures of divergence: notably lost accuracy and
    z-tranformed spearman correlation.
    '''

    # We start by constructing the paths to the sampleA
    # standard model criteria (.pkl) and
    # model output (.csv) on the examples
    # originally used to train it.

    # We're going to try applying the sampleA standard
    # criteria to another model's output, and vice-
    # versa.

    model1 = '../modeloutput/' + sampleA + '.pkl'
    meta1 = '../modeloutput/' + sampleA + '.csv'

    # Now we construct paths to the test model
    # criteria (.pkl) and output (.csv).

    model2 = '../modeloutput/' + sampleB + '.pkl'
    meta2 = '../modeloutput/' + sampleB + '.csv'

    model1on2 = versatiletrainer2.apply_pickled_model(model1, '../newdata/', '.fic.tsv', meta2)
    model2on1 = versatiletrainer2.apply_pickled_model(model2, '../newdata/', '.fic.tsv', meta1)

    spearman1on2 = np.arctanh(stats.spearmanr(model1on2.probability, model1on2.alien_model)[0])
    spearman2on1 = np.arctanh(stats.spearmanr(model2on1.probability, model2on1.alien_model)[0])
    spearman = (spearman1on2 + spearman2on1) / 2

    loss1on2 = accuracy_loss(model1on2)
    loss2on1 = accuracy_loss(model2on1)
    loss = (loss1on2 + loss2on1) / 2

    alienacc2 = accuracy(model1on2, 'alien_model')
    alienacc1 = accuracy(model2on1, 'alien_model')

    acc2 = accuracy(model1on2, 'probability')
    acc1 = accuracy(model2on1, 'probability')

    meandate2 = np.mean(model1on2.std_date)
    meandate1 = np.mean(model2on1.std_date)

    return spearman, loss, spearman1on2, spearman2on1, loss1on2, loss2on1, acc1, acc2, alienacc1, alienacc2, meandate1, meandate2

## MAIN 
Example 76
Project: fiction   Author: tedunderwood   File: methodological_experiment.py    MIT License 4 votes vote down vote up
def get_divergences(gold, testname, itera, size, pct):
    '''
    This function gets several possible measures of divergence
    between two models.
    '''

    # We start by constructing the paths to the gold
    # standard model criteria (.pkl) and
    # model output (.csv) on the examples
    # originally used to train it.

    # We're going to try applying the gold standard
    # criteria to another model's output, and vice-
    # versa.

    model1 = '../measuredivergence/modeloutput/' + gold + '.pkl'
    meta1 = '../measuredivergence/modeloutput/' + gold + '.csv'

    # Now we construct paths to the test model
    # criteria (.pkl) and output (.csv).

    testpath = '../measuredivergence/modeloutput/' + testname
    model2 = testpath + '.pkl'
    meta2 = testpath + '.csv'

    model1on2 = versatiletrainer2.apply_pickled_model(model1, '../data/', '.tsv', meta2)
    model2on1 = versatiletrainer2.apply_pickled_model(model2, '../data/', '.tsv', meta1)

    pearson1on2 = stats.pearsonr(model1on2.probability, model1on2.alien_model)[0]
    pearson2on1 = stats.pearsonr(model2on1.probability, model2on1.alien_model)[0]
    pearson = averagecorr(pearson1on2, pearson2on1)

    spearman1on2 = stats.spearmanr(model1on2.probability, model1on2.alien_model)[0]
    spearman2on1 = stats.spearmanr(model2on1.probability, model2on1.alien_model)[0]
    spearman = averagecorr(spearman1on2, spearman2on1)

    loss1on2 = accuracy_loss(model1on2)
    loss2on1 = accuracy_loss(model2on1)
    loss = (loss1on2 + loss2on1) / 2

    kl1on2 = kldivergence(model1on2.probability, model1on2.alien_model)
    kl2on1 = kldivergence(model2on1.probability, model2on1.alien_model)
    kl = (kl1on2 + kl2on1) / 2

    return pearson, spearman, loss, kl, spearman1on2, spearman2on1, loss1on2, loss2on1 
Example 77
Project: fiction   Author: tedunderwood   File: methodological_experiment.py    MIT License 4 votes vote down vote up
def get_divergence(sampleA, sampleB, twodatafolder = '../data/', onedatafolder = '../data/'):
    '''
    This function applies model a to b, and vice versa, and returns
    a couple of measures of divergence: notably lost accuracy and
    z-tranformed spearman correlation.
    '''

    # We start by constructing the paths to the sampleA
    # standard model criteria (.pkl) and
    # model output (.csv) on the examples
    # originally used to train it.

    # We're going to try applying the sampleA standard
    # criteria to another model's output, and vice-
    # versa.

    model1 = '../measuredivergence/newmodeloutput/' + sampleA + '.pkl'
    meta1 = '../measuredivergence/newmodeloutput/' + sampleA + '.csv'

    # Now we construct paths to the test model
    # criteria (.pkl) and output (.csv).

    model2 = '../measuredivergence/newmodeloutput/' + sampleB + '.pkl'
    meta2 = '../measuredivergence/newmodeloutput/' + sampleB + '.csv'

    model1on2 = versatiletrainer2.apply_pickled_model(model1, twodatafolder, '.tsv', meta2)
    model2on1 = versatiletrainer2.apply_pickled_model(model2, onedatafolder, '.tsv', meta1)

    spearman1on2 = np.arctanh(stats.spearmanr(model1on2.probability, model1on2.alien_model)[0])
    spearman2on1 = np.arctanh(stats.spearmanr(model2on1.probability, model2on1.alien_model)[0])
    spearman = (spearman1on2 + spearman2on1) / 2

    loss1on2 = accuracy_loss(model1on2)
    loss2on1 = accuracy_loss(model2on1)
    loss = (loss1on2 + loss2on1) / 2

    alienacc2 = accuracy(model1on2, 'alien_model')
    alienacc1 = accuracy(model2on1, 'alien_model')

    acc2 = accuracy(model1on2, 'probability')
    acc1 = accuracy(model2on1, 'probability')

    meandate2 = np.mean(model1on2.std_date)
    meandate1 = np.mean(model2on1.std_date)

    return spearman, loss, spearman1on2, spearman2on1, loss1on2, loss2on1, acc1, acc2, alienacc1, alienacc2, meandate1, meandate2 
Example 78
Project: LaserTOF   Author: kyleuckert   File: test_stats.py    MIT License 4 votes vote down vote up
def test_spearmanr():
    # Cross-check with R:
    # cor.test(c(1,2,3,4,5),c(5,6,7,8,7),method="spearmanr")
    x1 = [1, 2, 3, 4, 5]
    x2 = [5, 6, 7, 8, 7]
    expected = (0.82078268166812329, 0.088587005313543798)
    res = stats.spearmanr(x1, x2)
    assert_approx_equal(res[0], expected[0])
    assert_approx_equal(res[1], expected[1])

    attributes = ('correlation', 'pvalue')
    res = stats.spearmanr(x1, x2)
    check_named_results(res, attributes)

    # with only ties in one or both inputs
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", RuntimeWarning)
        assert_equal(stats.spearmanr([2,2,2], [2,2,2]), (np.nan, np.nan))
        assert_equal(stats.spearmanr([2,0,2], [2,2,2]), (np.nan, np.nan))
        assert_equal(stats.spearmanr([2,2,2], [2,0,2]), (np.nan, np.nan))

    # empty arrays provided as input
    assert_equal(stats.spearmanr([], []), (np.nan, np.nan))

    np.random.seed(7546)
    x = np.array([np.random.normal(loc=1, scale=1, size=500),
                np.random.normal(loc=1, scale=1, size=500)])
    corr = [[1.0, 0.3],
            [0.3, 1.0]]
    x = np.dot(np.linalg.cholesky(corr), x)
    expected = (0.28659685838743354, 6.579862219051161e-11)
    res = stats.spearmanr(x[0], x[1])
    assert_approx_equal(res[0], expected[0])
    assert_approx_equal(res[1], expected[1])

    assert_approx_equal(stats.spearmanr([1,1,2], [1,1,2])[0], 1.0)

    # test nan_policy
    x = np.arange(10.)
    x[9] = np.nan
    assert_array_equal(stats.spearmanr(x, x), (np.nan, np.nan))
    assert_allclose(stats.spearmanr(x, x, nan_policy='omit'),
                    (1.0, 0))
    assert_raises(ValueError, stats.spearmanr, x, x, nan_policy='raise')
    assert_raises(ValueError, stats.spearmanr, x, x, nan_policy='foobar')

    # test unequal length inputs
    x = np.arange(10.)
    y = np.arange(20.)
    assert_raises(ValueError, stats.spearmanr, x, y)

    #test paired value
    x1 = [1, 2, 3, 4]
    x2 = [8, 7, 6, np.nan]
    res1 = stats.spearmanr(x1, x2, nan_policy='omit')
    res2 = stats.spearmanr(x1[:3], x2[:3], nan_policy='omit')
    assert_equal(res1, res2) 
Example 79
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_analytics.py    MIT License 4 votes vote down vote up
def test_corr_rank(self):
        import scipy.stats as stats

        # kendall and spearman
        A = tm.makeTimeSeries()
        B = tm.makeTimeSeries()
        A[-5:] = A[:5]
        result = A.corr(B, method="kendall")
        expected = stats.kendalltau(A, B)[0]
        tm.assert_almost_equal(result, expected)

        result = A.corr(B, method="spearman")
        expected = stats.spearmanr(A, B)[0]
        tm.assert_almost_equal(result, expected)

        # results from R
        A = Series(
            [
                -0.89926396,
                0.94209606,
                -1.03289164,
                -0.95445587,
                0.76910310,
                -0.06430576,
                -2.09704447,
                0.40660407,
                -0.89926396,
                0.94209606,
            ]
        )
        B = Series(
            [
                -1.01270225,
                -0.62210117,
                -1.56895827,
                0.59592943,
                -0.01680292,
                1.17258718,
                -1.06009347,
                -0.10222060,
                -0.89076239,
                0.89372375,
            ]
        )
        kexp = 0.4319297
        sexp = 0.5853767
        tm.assert_almost_equal(A.corr(B, method="kendall"), kexp)
        tm.assert_almost_equal(A.corr(B, method="spearman"), sexp) 
Example 80
Project: basenji   Author: calico   File: basenji_test_reps.py    Apache License 2.0 4 votes vote down vote up
def regplot(vals1,
            vals2,
            out_pdf,
            poly_order=1,
            alpha=0.5,
            x_label=None,
            y_label=None):
  plt.figure(figsize=(6, 6))

  # g = sns.jointplot(vals1, vals2, alpha=0.5, color='black', stat_func=spearmanr)
  gold = sns.color_palette('husl', 8)[1]
  ax = sns.regplot(
      vals1,
      vals2,
      color='black',
      order=poly_order,
      scatter_kws={'color': 'black',
                   's': 4,
                   'alpha': alpha},
      line_kws={'color': gold})

  vmin, vmax = basenji.plots.scatter_lims(vals1, vals2)
  # ax.plot([vmin,vmax], [vmin,vmax], linestyle='--', color='black')

  ax.set_xlim(vmin, vmax)
  if x_label is not None:
    ax.set_xlabel(x_label)
  ax.set_ylim(vmin, vmax)
  if y_label is not None:
    ax.set_ylabel(y_label)

  # corr, _ = spearmanr(vals1, vals2)
  corr, _ = pearsonr(vals1, vals2)
  lim_eps = (vmax - vmin) * .02
  ax.text(
      vmin + lim_eps,
      vmax - 3 * lim_eps,
      'PearsonR: %.3f' % corr,
      horizontalalignment='left',
      fontsize=12)

  ax.grid(True, linestyle=':')

  # plt.tight_layout(w_pad=0, h_pad=0)

  plt.savefig(out_pdf)
  plt.close()


################################################################################
# __main__
################################################################################