spark MLlib 概念 1:相關系數( PPMCC or PCC or Pearson's r皮爾森相關系數) and Spearman's correlation(史匹曼等級相關系數)


皮爾森相關系數 定義: 協方差與標准差乘積的商。

Pearson's correlation coefficient when applied to a population is commonly represented by the Greek letter ρ (rho) and may be referred to as the population correlation coefficient or the population Pearson correlation coefficient. The formula for ρ is:

 \rho_{X,Y}={\mathrm{cov}(X,Y) \over \sigma_X \sigma_Y} ={E[(X-\mu_X)(Y-\mu_Y)] \over \sigma_X\sigma_Y}

where,  \mathrm{cov}  is the covariance \sigma_X  is the standard deviation of  X  \mu_X  is the mean of  X , and  E  is the expectation.

 適合計算機運行的公式:

Alternative formulae for the sample Pearson correlation coefficient are also available:


r_{xy}=\frac{\sum x_iy_i-n \bar{x} \bar{y}}{(n-1) s_x s_y}=\frac{n\sum x_iy_i-\sum x_i\sum y_i}
{\sqrt{n\sum x_i^2-(\sum x_i)^2}~\sqrt{n\sum y_i^2-(\sum y_i)^2}}.

The above formula suggests a convenient single-pass algorithm for calculating sample correlations, but, depending on the numbers involved, it can sometimes benumerically unstable.


數學意義:
  描述兩個變量X, Y的線性相關性,且 不隨變量的平移而改變。范圍 [-1,1],當X,Y協變時大於0,逆變時小於0. 線性無關時等於0.


Spearman's rank correlation coefficient

分析兩個變量的一致性程度。
定義:

For a sample of size n, the n raw scores X_i, Y_i are converted to ranks x_i, y_i, and ρ is computed from:

 \rho = {1- \frac {6 \sum d_i^2}{n(n^2 - 1)}}.

where d_i = x_i - y_i, is the difference between ranks


示例[編輯]

在此例中,我們要使用下表所給出的原始數據計算一個人的 智商和其每周花在 電視上的小時數的相關性。

智商X_i 每周花在電視上的小時數, Y_i
106 7
86 0
100 27
101 50
99 28
103 29
97 20
113 12
112 6
110 17

首先,我們必須根據以下步驟計算出 d^2_i,如下表所示。

  1. 排列第一列數據 (X_i)。 創建新列 x_i 並賦以等級值 1,2,3,...n
  2. 然后,排列第二列數據 (Y_i). 創建第四列 y_i 並相似地賦以等級值 1,2,3,...n
  3. 創建第五列 d_i 保存兩個等級列的差值 (x_i 和 y_i).
  4. 創建最后一列 d^2_i 保存 d_i 的平方.
智商, X_i 每周花在電視上的小時數, Y_i 等級 x_i 等級 y_i d_i d^2_i
86 0 1 1 0 0
97 20 2 6 −4 16
99 28 3 8 −5 25
100 27 4 7 −3 9
101 50 5 10 −5 25
103 29 6 9 −3 9
106 7 7 3 4 16
110 17 8 5 3 9
112 6 9 2 7 49
113 12 10 4 6 36

根據 d^2_i 計算 \sum d_i^2 = 194。 樣本容量n為 10。 將這些值帶入方程

 \rho = 1- {\frac {6\times194}{10(10^2 - 1)}}

得 ρ = −0.175757575...


 








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM