官术网_书友最值得收藏!

Comparing scaled data using the Pearson correlation coefficient

Another way to measure how closely two items relate to each other is by examining their individual trends. For example, two items that both show an upward trend are more closely related. Likewise, two items that both show a downward trend are also closely related. To simplify the algorithm, we will only consider linear trends. This calculation of correlation is called the Pearson correlation coefficient. The closer the coefficient is to zero, the less correlated the two data sets will be.

The Pearson correlation coefficient for a sample is calculated using the following formula:

How to do it...

Create a new file, which we will call Main.hs, and perform the following steps:

  1. Implement main to compute the correlation coefficient between two lists of numbers:
    main :: IO ()
    main = do
      let d1 = [3,3,3,4,4,4,5,5,5]
      let d2 = [1,1,2,2,3,4,4,5,5]
      let r = pearson d1 d2
      print r
  2. Define the function to compute the Pearson coefficient:
     pearson xs ys = (n * sumXY - sumX * sumY) / 
                     sqrt ( (n * sumX2 - sumX*sumX) * 
                            (n * sumY2 - sumY*sumY) )
    
      where n = fromIntegral (length xs)
            sumX = sum xs
            sumY = sum ys
            sumX2 = sum $ zipWith (*) xs xs
    
            sumY2 = sum $ zipWith (*) ys ys
            sumXY = sum $ zipWith (*) xs ys
  3. Run the code to print the coefficient.
    $ runhaskell Main.hs
    
    0.9128709291752768
    

How it works...

The Pearson correlation coefficient measures the degree of linear relationship between two variables. The magnitude of this coefficient describes how strongly the variables are related. If positive, the two variables change together. If negative, as one variable increases, the other decreases.

主站蜘蛛池模板: 突泉县| 辽宁省| 扎赉特旗| 兴国县| 临清市| 兴安盟| 新化县| 乐陵市| 太白县| 乌鲁木齐市| 淳安县| 象州县| 汉川市| 泰安市| 山阴县| 息烽县| 安丘市| 呼玛县| 乐亭县| 甘南县| 都昌县| 越西县| 伊通| 芜湖市| 武川县| 太康县| 江永县| 衡阳市| 郸城县| 龙门县| 竹溪县| 日照市| 绥化市| 永丰县| 洪泽县| 静乐县| 黄龙县| 灌阳县| 长兴县| 贞丰县| 合肥市|