Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1108345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:04:07+00:00 2026-05-17T02:04:07+00:00

I have to compute the correlation matrix on vectors contained in a csv file

  • 0

I have to compute the correlation matrix on vectors contained in a csv file of 5GB. Each row contains one observation for each random variable. To do this I wrote the following:

let getCorrMatrix data =   

    let getMatrixInfo nCol (count,crossProd:float array array,sumVector:float array,sqVector:float array) (newLine:float array)   = 

        for i in 0..(nCol-1) do
                sumVector.[i]<-sumVector.[i]+newLine.[i]
                sqVector.[i]<-sqVector.[i]+(newLine.[i]*newLine.[i])
                for j in (i+1)..(nCol-1)  do
                    crossProd.[i].[j-(i+1)]<-crossProd.[i].[j-(i+1)]+newLine.[i]*newLine.[j] 

        let newCount = count+1
        //(newCount,newMatrix,newSumVector,newSqVector)    
        (newCount,crossProd,sumVector,sqVector)         

    //Get number of columns
    let nCol = data|>Seq.head|>Seq.length

    //Initialize objects for the fold
    let matrixStart = Array.init nCol (fun i -> Array.create (nCol-i-1) 0.0)                    
    let sumVector = Array.init nCol (fun _ -> 0.0)
    let sqVector = Array.init nCol (fun _ -> 0.0)

    let init = (0,matrixStart,sumVector,sqVector)

    //Run the fold and obtain all the elements to build te correlation matrix
    let (count,crossProd,sum,sq) = 
        data
        |>PSeq.fold(getMatrixInfo nCol) init

    //Compute averages standard deviations, and finally correlations
    let averages = sum|>Array.map(fun s ->s/(float count))
    let std = Array.zip3 sum sq averages
              |> Array.map(fun (elemSum,elemSq,av)-> let temp = elemSq-2.0*av*elemSum+float(count)*av*av 
                                                     sqrt (temp/(float count-1.0)))

    //Map allteh elements to correlation                                         
    let rec getCorr i j =
        if i=j then
            1.0
        elif i<j then
            (crossProd.[i].[j-(i+1)]-averages.[i]*sum.[j]-averages.[j]*sum.[i]+(float count*averages.[i]*averages.[j]) )/((float count-1.0)*std.[i]*std.[j])
        else
            getCorr j i

    let corrMatrix =  Array2D.init nCol nCol (fun i j -> getCorr i j)

    corrMatrix 

I have tested it against R computation and it matches. Since I plan to use this again and again if you have some feedback (or spot a mistake) it would be greatly appreciated. (Note I post this because thought it might be useful to others too).

Thanks

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:04:08+00:00Added an answer on May 17, 2026 at 2:04 am

    The major problem is in the following code:

        //Update crossproduct
        let newMatrix = 
            [| for i in 0..(nCol-1) do
                 yield [| for j in (i+1)..(nCol-1)  -> crossProd.[i].[j-(i+1)]+newLine.[i]*newLine.[j] |]
                                       |]
    

    You create a new matrix for each row in your data. This is inefficient, you can use only one such matrix.

    There are some minor F# to note:

    1. Use sqrt as a shortcut for System.Math.Sqrt.

    2. Avoid using list comprehension to initialize simple arrays. E.g. your code

      let matrixStart = [| for i in 0..(nCol-1) do
                           yield [| for j in (i+1)..(nCol-1)  ->  0.0 |]
                                     |]
      

      could be be written using standard procedures:

      let matrixStart = Array.init nCol (fun i -> Array.create (nCol-i-1) 0.0)
      

      another example, for

      let corrMatrix = 
          [| for i in 0..(nCol-1) do
             yield [| for j in 0..(nCol-1)  -> getCorr i j |]
                                         |]
      

      instead of using float [][] you can use float [,] and write

      let corrMatrix = Array2D.init nCol nCol (fun i j -> getCorr i j)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two vectors of floats, x and y, and I want to compute
I have two very large matrices (60x25000) and I'd like to compute the correlation
I have to compute large sums of 3d vectors and a comparison of using
I have a function compute() that has parallelized matrix multiplication inside of it using
Possible Duplicate: Compute the minimum of a pair of vectors I have two vectors
I have to compute a value involving data from several tables. I was wondering
I have a huge data set and I have to compute for every point
Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
I have been trying to compute the total sum of all these ord s
I have a VBA code where I compute the values of many variables (For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.