Please consider registering
guest

Log In

Lost password?
Advanced Search:

— Forum Scope —



— Match —



— Forum Options —




Wildcard usage:
*  matches any number of characters    %  matches exactly one character

Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
Topic RSS
How can a function router call matrix functions written in R?
February 10, 2013
4:14 pm
tuncalik
Moderator
Forum Posts: 37
Member Since:
February 20, 2012
Offline

Following question was directed in another thread; I will try to answer it here.

How can a function router call matrix functions written in R?

For information about function routers see related article:

http://finaquant.com/function-…..outer/2802

 

In summary, with a functioun router (a high-level table function) you can apply selected table (or matrix) functions on selected subtables of an input table.

Matrix functions can also be implemented with another numerical software like R, matlab or ILNumerics (R and ILNumerics are available for free) for matrix and vector computations, and embedded into a usual .net method.

I will explain below how - for R only, but this may also clarify the general approach for integrating table and matrix functions with a subtable transformer or function router. We will also publish an example scenario in an upcoming article related with statistical estimation with R .

 

A matrix router function like MatrixTable.MatrixFunctionRouterA can call an array of matrix functions in the following delegate format (see example 4 in related article and demo function Matrix_Function_Router() in downloadabe Visual Studio project FinaquantProtosStarter):

public delegate void TransformSubMatrixFunc_OP(NumMatrix MnumIn, KeyMatrix MkeyIn,            out NumMatrix MnumOut, out KeyMatrix MkeyOut, params Object[] OtherParameters);

An example below for simple matrix function that conforms to this delegate function format:

public static void MatrixMultiplication(NumMatrix MnumIn, KeyMatrix MkeyIn,out NumMatrix MnumOut, out KeyMatrix MkeyOut, params Object[] OtherParameters){// PARAMETER CHECKS HERE
// …

 

// get input matrices

KeyMatrix M1 = MkeyIn;

KeyMatrix M2 = (KeyMatrix) OtherParameters[0];

 

// return output matrix

MkeyOut = M1 * M2;

}

 

The general integration approach is just envelopping (or wrapping) a matrix function written in an another numerical library. With this integration approach the problem boils down to following question:

How can we call a user-defined (custom) function in R from within c# (or .net in general)?

Following steps show how this can be done based on the open-source codeplex library R.NET, so that a user-defined R function is called for the same matrix multiplication above.

 

1) Open an existing or new project in MS Visual Studio referencing to Finaquant Protos, .net library with table functions

2) Install R.NET (NuGet) http://rdotnet.codeplex.com

Installation is quiteeasy:

Menu: Visual Studio (2012) > Library Package Manager > Package Manager Console type "Install-Package R.NET"

3) Initialize a function in R and call it from C#
See http://rdotnet.codeplex.com/do…..umentation for data types in R.

using RDotNet;

public static void MatrixMultiplication(NumMatrix MnumIn, KeyMatrix MkeyIn,out NumMatrix MnumOut, out KeyMatrix MkeyOut, params Object[] OtherParameters){

 

// PARAMETER CHECKS HERE

// …

 

// get input matrices

KeyMatrix M1 = MkeyIn;

KeyMatrix M2 = (KeyMatrix) OtherParameters[0];

 

// initialize R connection

// Set the folder in which R.dll locates

var envPath = Environment.GetEnvironmentVariable("PATH");

// check the version and path on your computer

var rBinPath = @"C:Program FilesRR-2.14.1binx64";

Environment.SetEnvironmentVariable("PATH", envPath + System.IO.Path.PathSeparator + rBinPath);

// initialite REngine object

REngine engine = REngine.CreateInstance("RDotNet");

 

// convert KeyMatrix of finaquant to NumericMatrix of R

NumericMatrix M1R = engine.CreateNumericVector(M1.toArray);

NumericMatrix M2R = engine.CreateNumericVector(M2.toArray);

 

// embed your R function here

// See: http://rdotnet.codeplex.com/wikipage?title=Examples&referringTitle=Home

Function matrix_mult = engine.Evaluate(@"matrix_mult <- function(a,b){

c = a %*% b;

return(c);

}").AsFunction();

 

// call your R function from c#

NumericMatrix c = engine.Evaluate(@"c <- matrix_mult(a,b)").AsNumericMatrix();

 

// convert NumericMatrix of R back to KeyMatrix of finaquant

double[,] arr = new double[c.RowCount, c.ColumnCount];

c.CopyTo(arr, c.RowCount, c.ColumnCount);

KeyMatrix M3 = KeyMatrix.ArrayToMatrix(arr);

 

// return output matrix M3 = M1 x M2

MkeyOut = M3;

 

// …

}

February 13, 2013
10:44 pm
selmar
Member
Forum Posts: 5
Member Since:
January 17, 2013
Offline

Super! I will try this.

Serena

February 20, 2013
9:03 pm
tuncalik
Moderator
Forum Posts: 37
Member Since:
February 20, 2012
Offline

See also related article for a complete demonstration:

How can we apply estimations functions in R on selected parts (subtables) of an input table

http://finaquant.com/table-dat…..-in-r/3082

Tunc

Forum Timezone: UTC 1

Most Users Ever Online: 10

Currently Online:
1 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

selmar: 5

vargas: 4

shakirshabbir: 4

jason663: 4

merinos: 4

swapnil: 2

kmudrikuva: 2

wincoop: 2

nevil: 2

noddy: 2

Member Stats:

Guest Posters: 0

Members: 1091

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 5

Topics: 41

Posts: 87

Newest Members: OutunAllnutt, JulietaJae, JorjaUYXJtqic, kevian, MarcyConover

Moderators: tuncalik (37), vbamaster (9)

Administrators: admin (10)