【问题标题】:Comparing sample mean vs random assortments in Python比较 Python 中的样本均值与随机分类
【发布时间】:2016-02-24 03:11:05
【问题描述】:

给定df

            A         B         C
Date            
2010-01-17  -0.9304   3.7477    0.0000
2010-01-24  -3.6348   1.5733   -3.6348
2010-01-31  -1.8950   0.4957   -1.8950
2010-02-07  -0.6990  -0.1480   -0.6990
2010-02-14   1.4635  -3.4206    1.4635

对于每个日期,我想将 df['C'] 的平均值与从 df['A'] 的 1 个元素或从 df['B'] 中选择的 10.000 个随机序列进行比较,以查看平均排名(如果是最高的,则为 1,如果高于 9500 个随机数,则为 0.95,等等)。

我写了一个旧公式,但我不能再把它放在一起,也许这有帮助

def mean_diff(d):
    result = {}
    for k, (l, t) in d.iteritems():
        m = np.mean(t)
        len_ = len(t)
        result[k] = np.mean([m > np.mean(npr.choice(l, len_, True))
                            for _ in range(10000)])
    return result

谢谢

** 10000 因为原始数据的行数远远超过 5 行。

更新:

好吧,为了解决这个问题,我必须开始解决一个较小的问题。看到这个question

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    嗯,有一个捷径:

    由于我们在 A、B 两列中的元素数量相同。我们可以将它们放在一个列表中,并从该列表中取出 10000 个随机样本,并将它们与 C 的平均值进行比较

    sample = df['C'].values
    a = df['A'].values
    b = df['B'].values
    population = np.concatenate((a,b), axis=0)
    
    def mean_diff(s, p):
        m = np.mean(s)
        len_ = len(s)
        result = np.mean([m > np.mean(npr.choice(p, len_, True))
                                for _ in range(10000)])
        return result
    
    mean_diff(sample, population)
    

    【讨论】:

      猜你喜欢
      • 2022-12-17
      • 1970-01-01
      • 2015-06-05
      • 2016-11-04
      • 2018-10-04
      • 1970-01-01
      • 2020-06-04
      • 2012-01-31
      • 2015-05-28
      相关资源
      最近更新 更多