Integrating finaquant with math.net numerics

Table-valued functions in finaquant .net libraries

Integrating finaquant with math.net numerics

Postby kubot » 13 Jan 2015, 21:42

Hello,

for a forecasting scenario we want to use statistical matrix functions of math.net numerics. Is it possible to use functions of such a library with finaquant's table functions like subtable transformer, or function router?

Martin
kubot
 
Posts: 2
Joined: 26 Jul 2014, 12:11

Re: Integrating finaquant with math.net numerics

Postby tunc » 18 Jan 2015, 14:27

Hello Martin

It is quite easy to integrate another numerical .net library like MatNet.Numerics with Finaquant.

See: Matrices and Vectors in MathNet

Following code examples in C# show how you can convert MathNet matrices and vectors into Finaquant matrices and vectors, and vice versa:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. using FinaquantCalcs;
  2. using MatNet = MathNet.Numerics.LinearAlgebra;
  3. using MathNet.Numerics.Statistics;
  4.  
  5. // create a dense matrix with 3 rows and 4 columns
  6. // filled with random numbers sampled from the standard distribution
  7. MatNet.Matrix<double> m = MatNet.Matrix<double>.Build.Random(3, 4);
  8. Console.WriteLine(m.ToString());
  9.  
  10. // create a dense zero-vector of length 10
  11. MatNet.Vector<double> v = MatNet.Vector<double>.Build.Random(5);
  12. Console.WriteLine(v.ToString());
  13.  
  14. // convert MatNet matrix to Finaquant matrix
  15. KeyMatrix km = new KeyMatrix(m.ToArray());
  16. Console.WriteLine(km.ToString());
  17.  
  18. // convert Finaquant matrix to MatNet matrix
  19. m = MatNet.Matrix<double>.Build.DenseOfArray(km.toArray);
  20. Console.WriteLine(m.ToString());
  21.  
  22. // convert MatNet vector to Finaquant vector
  23. KeyVector kv = new KeyVector(v.ToArray());
  24. Console.WriteLine(kv.ToString());
  25.  
  26. // convert Finaquant vector to MatNet vector
  27. v = MatNet.Vector<double>.Build.DenseOfArray(kv.toArray);
  28. Console.WriteLine(v.ToString());
GeSHi ©


Following code example shows how you can use matrix or vector functions of MathNet within Finaquant, like the descriptive statistics of MathNet in the example below:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Apply descriptive statistics on a Finaquant vector using MatNet methods
  2. kv = KeyVector.CreateVectorWithElements(1, 5, 3, 2, 5, 7, 18, 6, 8, 7);
  3. DescriptiveStatics(kv);
  4.  
  5. static void DescriptiveStatics(KeyVector fvec)
  6. {
  7.     // first, convert Finaquant vector to MatNet vector
  8.     var mvec = MatNet.Vector<double>.Build.DenseOfArray(fvec.toArray);
  9.  
  10.     // apply MatNet methods for descriptive statictics on vector
  11.     var statistics = new DescriptiveStatistics(mvec);
  12.  
  13.     double largestElement = statistics.Maximum;
  14.     double smallestElement = statistics.Minimum;
  15.  
  16.     double mean = statistics.Mean;
  17.     double variance = statistics.Variance;
  18.     double stdDev = statistics.StandardDeviation;
  19.  
  20.     double kurtosis = statistics.Kurtosis;
  21.     double skewness = statistics.Skewness;
  22.  
  23.     // print results
  24.     Console.WriteLine("Descriptive statistics for vector:");
  25.     Console.WriteLine("Largest Element: {0}", largestElement);
  26.     Console.WriteLine("Smallest Element: {0}", smallestElement);
  27.     Console.WriteLine("Mean: {0}", mean);
  28.     Console.WriteLine("Variance: {0}", variance);
  29.     Console.WriteLine("Std deviation: {0}", stdDev);
  30.     Console.WriteLine("Kurtosis: {0}", kurtosis);
  31.     Console.WriteLine("Skewness: {0}", skewness);
  32. }
GeSHi ©


Following example shows how you can write a matrix function using MathNet where the input and output parameters are Finaquant matrices:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // Matrix Multiplication using MathNet method
  2. KeyMatrix A = KeyMatrix.CreateRandomMatrix(nRows: 5, nCols: 10, nRandSeed: 100);
  3. KeyMatrix B = KeyMatrix.CreateRandomMatrix(nRows: 10, nCols: 6, nRandSeed: 100);
  4. KeyMatrix C = MatrixMult(A, B);
  5. Console.WriteLine("C = \n" + C.ToString());
  6.  
  7. // input and output parameters are Finaquant matrices
  8. static KeyMatrix MatrixMult(KeyMatrix km1, KeyMatrix km2)
  9. {
  10.     // convert Finaquant matrices to MathNet
  11.     var m1 = MatNet.Matrix<double>.Build.DenseOfArray(km1.toArray);
  12.     var m2 = MatNet.Matrix<double>.Build.DenseOfArray(km2.toArray);
  13.  
  14.     // Matrix Multiplication in MathNet
  15.     var r = m1.Multiply(m2);
  16.  
  17.     // return Finaquant matrix
  18.     return new KeyMatrix(r.ToArray());
  19. }
  20.  
GeSHi ©


As the code example shows above, you can implement matrix functions using MathNet. Because these matrix functions will have Finaquant matrices as input and output arguments they can be used in Finaquant's Subtable Transformers and Function Routers.

Note:
A Subtable Transformer applies the same table (or matrix) function on every subtable of an input table.

A Function Router applies selected table (or matrix) functions on selected subtables of an input table.

Tunc
tunc
 
Posts: 25
Joined: 22 Jul 2014, 19:29
Location: Switzerland


Return to Table Functions

Who is online

Users browsing this forum: No registered users and 2 guests

cron