Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

    ...