美文网首页
相关性分析`correlation`

相关性分析`correlation`

作者: minus_1 | 来源:发表于2018-12-14 00:42 被阅读0次

    作者@ weanl
    创建于 2018-12-13T14:20:00

    1. Pearson correlation coefficient & p-value

    1.1 理论分析:

      Pearson Correlation Coefficient, PCC 被称作线性相关系数,可以衡量两个服从正态分布的随机变量XY 的线性相关性。其实就是统计学中的相关系数
    给出协方差的计算公式:

    cov(X,Y) = \mathbb E((X-\mu_1)(Y-\mu_2)) = \mathbb E(XY) - \mu_1\mu_2 \tag{1-1}
    其中 \mu_1 = \mathbb E(X), \mu_2 = \mathbb E(Y)称为分布的均值。实际上 XY 相互独立(P(X, Y)=P(X)P(Y)),则 \mathbb E(XY) = \mathbb E(X) \mathbb E(Y) ,所以 cov(X,Y)=0 ,反之并不成立。

    给出 PCC 的计算公式:
    cor_{pearson}(X,Y) = \frac {cov(X,Y)} {\sqrt{\mathbb D(X) \mathbb D(Y) }} \tag{1-2}
    其中 \mathbb D(X) = \mathbb E[(X-\mu_1)^2]=\mathbb E[X^2]-\mu_1^2, \mathbb D(Y) = \mathbb E[(Y-\mu_2)^2]=\mathbb E[Y^2]-\mu_2^2 称为分布的方差。

    在随机变量观测为X \approx \{ x_1, x_2,...,x_n\}, Y \approx \{ y_1, y_2, ..., y_n\}时,PCC 的估计计算公式:

    \hat {cor_{pearson}(X,Y)} = \frac {\sum_{i=1}^{n} (x_i - \overline{x}) (y_i - \overline{y})} {\sqrt{\sum_{i=1}^{n} (x_i - \overline{x})^2 \sum_{i=1}^{n} (y_i - \overline{y})^2}} \tag{1-3}

    (式1-3 可以做其他的变形,另外这里分布方差的估计选的是有偏估计) (根据Cauchy–Schwarz inequality可知值域为[-1,1]) (如果数据进行了中心化处理,即\overline{x}=0, \overline{y}=0,PCC与余弦相似度"等价")

      1.2 应用总结:

      1.3 附加:
    { scipy 源码:scipy.stats.pearsonr }

    def pearsonr(x, y):
       # x and y should have same length.
        x = np.asarray(x)
        y = np.asarray(y)
        n = len(x)
        mx = x.mean()
        my = y.mean()
        xm, ym = x-mx, y-my
        r_num = np.add.reduce(xm * ym)
        r_den = np.sqrt(ss(xm) * ss(ym))
        r = r_num / r_den
    
        # Presumably, if abs(r) > 1, then it is only some small artifact of floating
        # point arithmetic.
        r = max(min(r, 1.0), -1.0)
        df = n-2
        if abs(r) == 1.0:
            prob = 0.0
        else:
            t_squared = r*r * (df / ((1.0 - r) * (1.0 + r)))
            prob = betai(0.5*df, 0.5, df / (df + t_squared))
        return r, prob
    

    (好好看一下源码 ^ _ ^ )

    2. Spearman's rank correlation coefficient & p-value

      scipy.stats.spearmanr

    ch2 序列相关性分析

    -1. 参考材料


    且听下回分解

    相关文章

      网友评论

          本文标题:相关性分析`correlation`

          本文链接:https://www.haomeiwen.com/subject/hedihqtx.html