# Creating a variance covariance matrix in fsharp

**URL:** <https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243>\
**Category:** General\
**Tags:** beginners\
**Created:** [March 28, 2020, 7:17am UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243 "2020-03-28T07:17:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![quant254](https://avatars.discourse-cdn.com/v4/letter/q/c6cbf5/32.png) [@quant254](https://forums.fsharp.org/u/quant254)\
**Post date:** [March 28, 2020, 7:17am UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/1 "2020-03-28T07:17:02Z")

</div>

i’m new to fsharp using it for quantitative finance,trying to write a function that creates variance co-variance matrix  
any assistance will be appreciated

---

<div class="post-metadata">

**Author:** ![FoggyFinder](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/foggyfinder/32/136_2.png) [@FoggyFinder](https://forums.fsharp.org/u/FoggyFinder)\
**Post date:** [March 29, 2020, 12:30pm UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/2 "2020-03-29T12:30:16Z")

</div>

Your question isn’t very specific.

If the main goal is to learn how to implement the algorithm then the best option is write something by yourself and come back here with concrete question.

For example, if you decided to use array2D type for matrix representation you might ask how to calculate sum of an each columns.

But for now it isn’t very clear what part is puzzled you.

If the main goal is to find some implementation then simple search gives you various options. For example, there is a Q on SO:

[Find covariance of Math Net matrix](https://stackoverflow.com/questions/32256998/find-covariance-of-math-net-matrix)

I will translate the [answer](https://stackoverflow.com/a/46880340/4651351) from [tearvisus](https://stackoverflow.com/users/1892138/tearvisus) for you:

```auto
open MathNet.Numerics.LinearAlgebra

let covarianceMatrix matrix = 
    let columnAverages = 
        Matrix.sumCols matrix / float matrix.RowCount
    let centered = 
        matrix
        |> Matrix.mapCols
            (fun i vec -> vec - columnAverages.[i])
    let normalizationFactor = 
        if matrix.RowCount = 1 then 1 
        else matrix.RowCount - 1
    centered.TransposeThisAndMultiply centered / float normalizationFactor

```

or use `Accord` [Measures. Covariance](http://accord-framework.net/docs/html/M_Accord_Statistics_Measures_Covariance_5.htm) as it was suggested in another answer.

---

<div class="post-metadata">

**Author:** ![quant254](https://avatars.discourse-cdn.com/v4/letter/q/c6cbf5/32.png) [@quant254](https://forums.fsharp.org/u/quant254)\
**Post date:** [March 30, 2020, 10:50am UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/3 "2020-03-30T10:50:07Z")

</div>

so my main aim was to create it without any library using the csharp examples i have something like this

````auto
  let cor xs ys nmethod = 
            match std xs nmethod, std ys nmethod with
    | 0., _ | _, 0. -> 0.
    | xstd, ystd -> cov xs ys nmethod / xstd / ystd

    let covariancematrix (items: matrix) nmethod f =
           let n = assetcovariance.NumCols
                     let x = seq {
                       for i in 0..n - 1 do
                           for j in i..n - 1 do
                               yield f (items.Column i) (items.Column j) nmethod
                   } |> Seq.toArray
           Matrix.init items.NumCols items.NumCols (fun i j -> 
               let i', j' = if i <= j then i, j else j, i
               x.[i'*n + j' - (i'+1)*i'/2])  

so my trouble is in ``` let covariancematrix (items: matrix<decimal>)``` the type matrix which how i can assign it as type matrix in my data fsharp doesnt accept that on my end.
````

---

<div class="post-metadata">

**Author:** ![FoggyFinder](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/foggyfinder/32/136_2.png) [@FoggyFinder](https://forums.fsharp.org/u/FoggyFinder)\
**Post date:** [March 30, 2020, 1:39pm UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/4 "2020-03-30T13:39:41Z")

</div>

Can you share a link to the C# sample?  
And provide more code so anyone can run a sample without guessing what are `assetcovariance` , `cov` etc.

---

<div class="post-metadata">

**Author:** ![quant254](https://avatars.discourse-cdn.com/v4/letter/q/c6cbf5/32.png) [@quant254](https://forums.fsharp.org/u/quant254)\
**Post date:** [March 31, 2020, 8:47am UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/5 "2020-03-31T08:47:08Z")

</div>

hey thank you for your assistance managed to get something here  
you can check it out

> <https://stackoverflow.com/questions/47003597/f-write-value-in-multidimensional-array>

---

<div class="post-metadata">

**Author:** ![quant254](https://avatars.discourse-cdn.com/v4/letter/q/c6cbf5/32.png) [@quant254](https://forums.fsharp.org/u/quant254)\
**Post date:** [April 15, 2020, 8:31am UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/6 "2020-04-15T08:31:06Z")

</div>

so i am exploring the [math.net](http://math.net) library i am failling to understand how it works  
for example i want to move from lists to matix using the mathnumerics it suggests functions like Densematrix.init which is not working for me.  
e.g  
i have two list as my initial data

```auto
let list1 = [[0.10M;0.2M;0.35M;0.14M];[0.5M;0.60M;0.76M;0.18M];[0.19M;0.10M;0.51M;0.32M;];[0.13M;0.64M;0.95M;0.26M]]
let list2 = [0.1M;0.2M;0.3M;0.4M]
i want to use math.net to transform the lists to matrices and multiply them

```

---

<div class="post-metadata">

**Author:** ![FoggyFinder](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/foggyfinder/32/136_2.png) [@FoggyFinder](https://forums.fsharp.org/u/FoggyFinder)\
**Post date:** [April 15, 2020, 9:24am UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/7 "2020-04-15T09:24:01Z")

</div>

Take a look to a documentation. There is a section about applying arithmetic operators:

[Arithmetics](https://numerics.mathdotnet.com/Matrix.html#Arithmetics)

For your sample I don’t think Math.Numerics has support for decimal type yet so better to go with float for now:

```auto
let list1 = 
    [[ 0.10; 0.2 ; 0.35; 0.14]
      [0.5 ; 0.60; 0.76; 0.18]
      [0.19; 0.10; 0.51; 0.32]
      [0.13; 0.64; 0.95; 0.26] ]

let list2 = [0.1; 0.2; 0.3; 0.4]

open MathNet.Numerics.LinearAlgebra

let m = list1 |> matrix
let v = list2 |> vector

let r = m * v
r |> printfn "%A"

```

Make sure you’re using [MathNet.Numerics.FSharp](https://www.nuget.org/packages/MathNet.Numerics.FSharp/) package.

---

<div class="post-metadata">

**Author:** ![quant254](https://avatars.discourse-cdn.com/v4/letter/q/c6cbf5/32.png) [@quant254](https://forums.fsharp.org/u/quant254)\
**Post date:** [April 15, 2020, 6:02pm UTC](https://forums.fsharp.org/t/creating-a-variance-covariance-matrix-in-fsharp/1243/8 "2020-04-15T18:02:53Z")

</div>

thank you i have been using a different package and not for MathNet.Numerics.Fsharp i really appreciate it.
