Calling calculation engine from matlab?

Table-valued functions in finaquant .net libraries

Calling calculation engine from matlab?

Postby selmar » 28 Nov 2013, 22:47

Hello,
Is it possible to call finaquant's calculation engine from matlab for simulation purposes?
I plan to use calculation engine for calculating security performance, and matlab for simulations to finally generate some graphs.
Thanks
Selena
selmar
 
Posts: 11
Joined: 19 Sep 2013, 19:26

Calling .NET methods from matlab

Postby finaquant » 05 Dec 2013, 22:27

Following links may help:

Is it possible to use .NET application in MatLab?
http://stackoverflow.com/questions/1448 ... -in-matlab

Work in MATLAB® with .NET libraries
http://www.mathworks.ch/ch/help/matlab/ ... atlab.html
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37

Calling .NET methods from matlab, passing parameters

Postby finaquant » 07 Dec 2013, 15:02

Here is a simple example demonstrating:
  • How to call methods written in C#/.NET from matlab
  • How to pass scalar and array parameters from matlab to C#/.NET methods
1) Write methods in C#/.NET
We will call the simple mathematical functions Transpose and Square below from matlab.

Transpose() requires a 2-dimensional array (matrix, of type double[,]), Square() requires a scalar number (of type double) as input parameter.
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Call_NETfromMatlab
  6. {
  7.     public class MathLib
  8.     {
  9.         // Transpose matrix
  10.         public static double[,] Transpose(double[,] M)
  11.         {
  12.             if (M == null || M.Length == 0)
  13.                 throw new Exception("Null-valued or empty input matrix M!");
  14.  
  15.             try
  16.             {
  17.                 // get row and column sizes
  18.                 int row_count = M.GetUpperBound(0) + 1;
  19.                 int col_count = M.GetUpperBound(1) + 1;
  20.  
  21.                 // create output matrix
  22.                 double[,] P = new double[col_count, row_count];
  23.  
  24.                 // fill values
  25.                 for (int i = 0; i < row_count; i++)
  26.                 {
  27.                     for (int j = 0; j < col_count; j++)
  28.                     {
  29.                         P[j,i] = M[i,j];
  30.                     }
  31.                 }
  32.                 return P;
  33.             }
  34.             catch(Exception ex)
  35.             {
  36.                 throw new Exception("MatrixFunc.Transpose: " + ex.Message);
  37.             }
  38.         }
  39.  
  40.         // Return square of a number
  41.         public static double Square(double x)
  42.         {
  43.             return x * x;
  44.         }
  45.     }
  46. }
GeSHi ©


2) Compile .NET code to obtain the assembly (dll file) Call_NETfromMatlab.dll
We will load this .NET assembly in matlab to access its method from matlab

3) Write and execute matlab script
Note how .NET assembly Call_NETfromMatlab.dll is loaded NET.addAssembly with to access its methods.

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. % create .NET array
  2. A = [1 2 3; 4 5 6]
  3.  
  4. % path to dll assembly (replace it with your own path)
  5. assemblypath = 'C:\Users\finaquant\Documents\...\Call_NETfromMatlab.dll';
  6.  
  7. % load .NET matrix function library
  8. NET.addAssembly(assemblypath);
  9.  
  10. % call methods
  11. MathLib = Call_NETfromMatlab.MathLib;
  12.  
  13. % square scalar number
  14. y = MathLib.Square(100)
  15.  
  16. % transpose matrix
  17. % http://www.mathworks.ch/ch/help/matlab/ref/net.convertarray.html
  18. P = MathLib.Transpose(NET.convertArray(A,'System.Double'));
  19.  
  20. % convert .NET array to matlab array
  21. % http://www.mathworks.com/matlabcentral/answers/46827
GeSHi ©

As the example above demonstrates, you can call .NET methods from matlab. It is also quite easy to pass parameters from matlab to .NET methods.

This means, it is possible to connect matlab with the Calculation Engine finaquant® calcs for simulations.
Image

You can first implement a complex cause-effect relationship with finaquant® calcs, and use matlab for running this Calculation Engine with varied inputs (value drivers), and presenting the results visually with graphs, tables etc.

This simulation capability makes all sort of analytical evaluations possible: Scenario analysis, estimations & predictions, multi-dimensional optimizations..
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37

Article: Simulation of Dealer Commissions

Postby finaquant » 22 Dec 2013, 22:37

New article at finaquant.com: Simulation of Dealer Commissions with Matlab
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37


Return to Table Functions

Who is online

Users browsing this forum: No registered users and 1 guest