No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Sampling from multivariate correlated binary and poisson random variables

4.0 | 3 ratings Rate this file 10 Downloads (last 30 days) File Size: 71.7 KB File ID: #20591 Version: 1.0

Sampling from multivariate correlated binary and poisson random variables


Philipp Berens (view profile)


07 Jul 2008 (Updated )

These Matlab functions can be used to generate multivariate correlated binary variables, and correl

| Watch this File

File Information

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 (

Required Products Optimization Toolbox
Statistics and Machine Learning Toolbox
MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.

No tags are associated with this file.

Please login to add a comment or rating.
Comments and Ratings (8)
11 Jan 2017 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).

Comment only
05 Jan 2017 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

Comment only
04 Aug 2016 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?

21 Jul 2012 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").

Comment only
07 Apr 2010 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?

Comment only
07 Apr 2010 Andrea Carbone

08 Feb 2010 Philipp Berens

Philipp Berens (view profile)

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.

Comment only
21 Dec 2009 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

Contact us