tags:

views:

121

answers:

1

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

+2  A: 

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)
    
Yin Zhu
@Yin Zhu Thanks code runs much faster ! (Just discovered your website, very cool !)
jlezard
and `fun i j -> getCorr i j` can just be `getCorr`. :-)
Jon Harrop