Topic RSS
4:14 pm
February 20, 2012
OfflineFollowing 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):
An example below for simple matrix function that conforms to this delegate function format:
// …
// 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.
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;
// …
}
9:03 pm
February 20, 2012
OfflineSee 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
Most Users Ever Online: 10
Currently Online:
1 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
selmar: 5
merinos: 4
shakirshabbir: 4
vargas: 4
nevil: 2
noddy: 2
kmudrikuva: 2
swapnil: 2
wincoop: 2
tinymex: 1
Member Stats:
Guest Posters: 0
Members: 199
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 5
Topics: 37
Posts: 82
Newest Members: niedziladarek, alikwiat85, wojciechsz90, anch95ag, spgodman
Moderators: tuncalik (36), vbamaster (9)
Administrators: admin (10)

Log In
Register
Home

