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
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
I will translate the answer from tearvisus for you:
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 as it was suggested in another answer.
so my main aim was to create it without any library using the csharp examples i have something like this
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.
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.
hey thank you for your assistance managed to get something here
you can check it out
so i am exploring the 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
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
Take a look to a documentation. There is a section about applying arithmetic operators:
For your sample I don’t think Math.Numerics has support for decimal type yet so better to go with float for now:
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 package.
thank you i have been using a different package and not for MathNet.Numerics.Fsharp i really appreciate it.