Message Boards Message Boards

Setup NetLink with SsfPack (a .dll) for use in State Space Time Series?

Posted 8 years ago

I have recently been doing work in the field of State Space Time Series. Mathematica does not have a lot of built in tools for working in this area. There is a well known package that is available, SsfPack, that should help bridge that gap. I am having trouble getting that package to work within Mathematica.

You can read about the package here and download here. Inside the zip is a starting guide and use guide.

The site states, "SsfPack is a suite of C routines for carrying out computations involving the statistical analysis of time series models in state space form." It then states, "SsfPack is primarily developed as a module for the object-oriented matrix programming language Ox. The library is written in C, which greatly improves execution speed compared to a direct Ox implementation."

This would make me think that it is a program written in C that should work with Library link.

In the zip file there is a couple headers (.h files) as well as a couple .dll files.

I have tried

Needs["NETLink`"]
kalfil=DefineDLLFunction["KalmanFil", filepath, "long", {"long"}]
During evaluation of In[4]:= LinkOpen::linke: Could not find MathLink executable.
During evaluation of In[4]:= InstallNET::fail: A link to the .NET runtime could not be established.
During evaluation of In[4]:= DefineDLLFunction::netlink: DefineDLLFunction cannot operate because InstallNET[] failed to launch the .NET runtime.
$Failed

I think having this package available to Mathematica users would really help. Maybe it only works with the Ox programming language? Thank you.

POSTED BY: Ray Troy
3 Replies

Ray,

I see from the Developers Manual that it is possible to call OX functions from C (or Java or several other languages). Since SSFPack is essentially an OX function written in C, you can follow that guideline to call the SSFPack routines from Mathematica. Java might be easier to integrate into Mathematica (especially if you do not know C). To answer your question about functions, you only need one function to interface Mathematica to the external dll.

Another viable option is to call OX routines from the command line. The manual discusses how you can do this. Since you know OX, maybe you should write some small OX programs and call them from Mathematica (with the Run related functions) and read back the results. This will not win the "coding excellence" award but should work as long as the calling overhead does not kill you on your application. In fact, you can use Mathematica's extensive templating functions to write the OX code for you on demand with your data in place and read back the results. This involves no coding outside of Mathematica and OX. for Template Documentation start here. Alternatively you can transfer data between OX and Mathematica by reading and writing files. (I am not familiar with OX or SSFPack so I really can't advise you as to which is better)

I think that given your situation, this might be your best approach. Are there many calls to the SSFPack routines or is it just a few?

If you want to talk this over, contact me through my user profile and we can set up a call.

I hope this helps.

POSTED BY: Neil Singer

Ray,

This should be doable. What you need to do is to make a C wrapper function that calls the SSFPack routines from C.

Start by writing a small C program that calls one SSFPack routine-- maybe you can even find sample code that is not going through OX on the web.

Next you will need to make the C program callable from Mathematica. There are two ways to do this. First you can make your C program standalone and call it with a Run, RunThrough, or RunProcess command (see documentation). Run the program at the command prompt -- burn in the data -- if it runs, then call it from Mathematica. you can pass information through the command line or through a file.

Next you can use WSTP See documentation here to make the connection a bit more seamless -- you can pass data back and forth in the right format. I linked to the Windows version because you seem to be on windows. but they have equivalent guides for other OS's. There is a .Net version as well although I really know nothing about .Net so I can't really tell you if things are easier under .net or in C -- Although the C is not bad if you use the guide that I linked to -- which is a bit hard to find.

Start with the WSTP examples in the guide -- they are very illustrative and they come on disk with your version of Mathematica so you can edit them and use them as a starting point.

However, I strongly suggest doing the command line version first so you make sure that you can call the .dll functions from C because if you can't do that you might as well save time and not bother with WSTP. Then it involves making wrapper functions for each SSFPack routine. I suspect that once you get one routine going the others will be straightforward.

Regards

POSTED BY: Neil Singer

Neil,

Thank you for your help!

I have never coded in C so this would be a very large challenge for me, nevertheless, I will attempt. I have also not seen any code that uses Ssfpack outside of Ox. So are there two C functions? One for calling the functions and one for connecting to Mathematica? Can they be the same? If I am writing a wrapper function in C why not make it work with LibraryLink? This is all pretty confusing!

If you have a link to sample code that would be very helpful.

I might even be interested in paying someone, depending on the price, to do bring this functionality to Mathematica. Can you recommend someone??

POSTED BY: Ray Troy
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract