Take the 2-minute tour ×

Does anyone know of a reliable, accurate library to compute the multivariate normal (MVN) CDF in Java? I'm looking for something like MATLAB's mvncdf function. I need to be able to do it for dimensions of up to 10 or more. Most statistics/math libraries don't have this functionality. Being able to compute the log probability is a plus.

From this post, there doesn't seem to be a free implementation mentioned for some other languages. While a straight-java implementation would rock, I would accept implementations in other languages that don't require licenses (not MATLAB or IMSL, for example) and can be called easily from Java with minimal overhead.

(This question is a derivative of a post on StackExchange math where I am trying to compute a probability of an ordering of normal random variables...if you're interested in trying to solve the problem directly using other mathematical methods, please do check it out.)

share|improve this question

2 Answers 2

Adding to the OP's solution (e.g.: that the best option was fortran code, and nothing else identified came close), one way to get to a pure java library is with the the f2j compiler (fortran to java) http://icl.cs.utk.edu/f2j

I've found the code it generates it be quite workable (e.g. such as this minpack library: http://www1.fpl.fs.fed.us/optimization/LmderTest_f77.html ). The only annoyance I recall was that arrays start from '1' vs '0', but that can be dealt with easily (if you care) with a trivial wrapper function.

@Andrew: if you do convert it, I'd be interested!

share|improve this answer
up vote 2 down vote accepted

After doing some additional research, it seems that the following is the most reasonable way to go.

The multivariate normal CDF is not trivial to compute (especially for large dimensions) and there have been several academic papers written on the subject. Professor Alan Genz has a bunch of Fortran-77 subroutines that compute various multivariate densities and CDFs, available on his page here: http://www.math.wsu.edu/faculty/genz/software/software.html

As you can see from some of that code, it's not exactly a cakewalk to re-implement in another language, and that's probably why it hasn't been done unless someone has paid for it. A lot of mathematical/numerical programming is done in Fortran at the research level, so that's where most of the best code is.

As such, for optimal results, it would probably be best to call the (native-compiled) Fortran subroutine directly using JNI or JNA. JNA seems to be the easiest to implement, following instructions such as these: http://www.javaforge.com/wiki/66061. Using that, and some other references, I've implemented the Java-JNA-Fortran link to be able to call the MVNEXP (expected value) and MVNDST (cdf) subroutines. You can check out the code here:

Also to point out: there does exist native Java code for some bivariate distributions and other things you won't find in commons math; it's adapted from the source above: http://www.iro.umontreal.ca/~simardr/ssj/indexe.html . This is a very good math library that I hadn't found until now.

share|improve this answer
If you can do what you need in R, you might be able to use rforge.net/JRI to invoke R from Java. –  GreyBeardedGeek Jan 6 '13 at 22:36
The R implementations of the above are just all calling the original Fortran code. I'm aware of several ways to call R from Java; it's kind of slow and probably not the way to go unless you only need to use it once or twice. –  Andrew Mao Jan 6 '13 at 22:37
if you want a pure java solution, try the f2j (fortran to java) compiler: icl.cs.utk.edu/f2j ; outside of a few nit annoyances you can paperwrap (like the arrays start from '1' vs '0'), I've found the code it generates it be quite workable (e.g. such as this minpack library: www1.fpl.fs.fed.us/optimization/LmderTest_f77.html ) ; if you do convert it, I'd be interested –  Matt S. Jul 7 at 7:54
@MattS. looks very promising if they've been able to convert most of BLAS and LAPACK. I'll definitely take a look if I end up pursuing this further. You should consider posting your comment as an answer; I'd upvote it and I think it would be good for anyone else looking for similar info. –  Andrew Mao Jul 7 at 14:44

Your Answer


