File Exchange

image thumbnail

Sampling from multivariate correlated binary and poisson random variables

version (71.7 KB) by Philipp Berens
These Matlab functions can be used to generate multivariate correlated binary variables, and correl


Updated 09 Jul 2008

No License

You can use the software in this package to efficiently sample from
(1) correlated multivariate binary random variables (multivariate Bernoulli)
(2) correlated multivariate Poisson random variables
(3) correlated random variables with arbitrary marginal statistics.
Applications include modeling and generating of artificial neural data.

The implementation includes sampling and parameter fitting for the Dichotomized Gaussian distribution. For some parameters this provides and efficient alternative to the maximum-entropy distribution, the Ising model.

Detailed information about the contents are contained in the readme-file at For an instruction on how to use the code, run the demo.m script.

The methods implemented here are described in two publications:

J. H. Macke, P. Berens, et al., Generating spike-trains with specified correlation-coefficients, Neural Computation, 2008 (accepted) (

Matthias Bethge and Philipp Berens, Near-Maximum Entropy Models for Binary Neural Representations of Natural Images, Advances in Neural Information Processing 2008 (

Cite As

Philipp Berens (2021). Sampling from multivariate correlated binary and poisson random variables (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Adam Sturge

I figured out that I needed to run 'mex lib/bivnor.c' as well.

Davoud Mougouei

The code only works on 32-bit version of the Matlab. In order to run it on a 64-bit version of Matlab I think, the DLL file needs to be recompiled for the 64 bit version. It might worth doing that as the 32-bit version of Matlab allocates less memory to your loaded vectors and this may cause problem if you wanna generate populations of really large sizes (say beyond 1e8).

Davoud Mougouei

Hi, I am getting the following error. I have complied the bivnor.c file. But i'm still getting the same error. I would be very thankful if someone can guide me on this.

Output argument "y" (and maybe others) not assigned during call to "bivnor".

Error in findLatentGaussian (line 41)
if (c(i,j) - bivnor(-g(i),-g(j),-1) + pn) < -1e-3 || ...

Error in sampleDichGauss01 (line 33)
[gamma rho] = findLatentGaussian(mu,Sigma,acc);

Error in demo (line 27)
[S,g,L] = sampleDichGauss01(mu,C,1e5); % generate samples from the DG model

Raghav Singal

Thank you for this code. It is very useful. However, as pointed out by Paolo Bocchini, I am getting the following error:

"Error using findLatentGaussian (line 43)
A joint Bernulli distribution with the given
covariance matrix does not exist!"

I read your reply dated 08 Feb 2010 but I do not see how changing lines 33 and 34 in sampleDichGauss01.m will fix this problem.

Can you please advise?

Kozlov Sacha

OK, I see, it's a problem of Win64 MatLab: C compiler is not included, so you have to download it first (see "mex -setup") and then compile your bivnor.c for this plateform ("mex bivnor.c").

Andrea Carbone

I really appreciate your work on spike train modeling. I am using the DG model to reproduce the statistics of a population of responses from a set ICA-like estimated receptive fields to visual stimuli from natural image statistics. the DG-generated population reproduce nicely the correlation patterns of the original dataset (in a very clever way, I'd say).

However, I found that the DG Covariance matrix generated by findLatentGaussian is often not definite positive or non-invertible (null determinant).

Is this an expected outcome of the algorithm implemented in findLatentGaussian?

Andrea Carbone

Philipp Berens

Thank you for your inquiry. We decided not to switch this feature on in the default version (although the readme said so). In the version uploaded today, you just need to change line 33 to line 34 in sampleDichGauss01.m to use this feature.

Generally, higham.m may be used to obtain the closest correlation matrix to a predescribed matrix which is not a valid correlation matrix. In the case of sampling from binary variables, you can use this function to obtain a valid latent correlation matrix in case the usual transformation does not result in one.

Paolo Bocchini

The code is exactly what I am looking for.
Unfortunately, I almost always get the error
"A joint Bernulli distribution with the given covariance matrix does not exist!"
Isn't the code supposed to find an approximate latent Gaussian in this case?
Thank you,
Paolo Bocchini

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!