File Exchange

image thumbnail

vmrand(fMu, fKappa, varargin)

version (4.76 KB) by Dylan Muir
Draw random numbers from the Von Mises distribution


Updated 16 Aug 2017

View License

This function uses an envelope-rejection method based on a wrapped Cauchy distribution to draw random variates from an arbitrary Von Mises distribution, first proposed in [1].
'fMu' and 'fKappa' are the mean and variance parameter of the Von Mises distribution over [-pi, pi). 'tVMVariates' will be a tensor containing random variates drawn from the defined distribution. If 'fMu' and 'fKappa' are non-scalar, then they must be the same size. In this case 'tVMVariates' will be the same size. If 'fMu' and 'fKappa' are scalar, then the number of variates returned can be specified as extra arguments.

vmrand(linspace(-pi, pi, 20), 2); % - Return variates for several distributions, with changing 'fMu'
vmrand(0, 2, [100 1]); % - Return 100 variates (size [100 1]) for the VM distribution with 'fMu' = 0 and 'fKappa' = 2
If only single dimension 'M' is provided, then the return argument 'tfVMVariates' will be M x M.

This implementation is vectorised, and requires O(7.5*N) space.

[1] D. J. Best and N. I. Fisher, 1979. "Efficient Simulation of the von Mises Distribution", Journal of the Royal Statistical Society. Series C (Applied Statistics), Vol. 28, No. 2, pp. 152-157.

Cite As

Dylan Muir (2020). vmrand(fMu, fKappa, varargin) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)

I'm going to use Von Mises distribution to calculate angle of arrivals to the user handset in a three dimensional wireless channel model, is there any modified Von mises function that also takes angle of departures (elevation and azimuth angles) as the imputs to calculate angle of arrivals?


Hi Dylan, Is it possible to use the code to generate random variate from a Fisher distribution? Thanks.

Dylan Muir

Hi Damilola Ife-Abidakun, in that case you can simply divide the random variates returned by vmrand by 2*pi.

Hi Dylan. What if i want to limit the possible generated samples between -1/2 and 1/2 instead of -pi to pi

Dylan Muir

Dear Geoffrey and Giorgio,
I have made an attempt to fix the bug, although I struggled to reproduce it. Please download the updated version and check.
Thanks, Dylan.

I am having the same experience as Giorgio. I can run vmrand(0,1) multiple times and most often it is fine but occasionally (~10% of times) I get the same error.

Dylan Muir

Hi Giorgio, What do you mean by "from time to time"? When I run the line of code you quoted I do not receive an error.
Best, Dylan.

Hi, Dylan. I also have (from time to time) the problem pointed out by Mr. M.:

>> vmrand(0.5, 1)
Error using .*
Matrix dimensions must agree.

Error in vmrand (line 163)
tfVMVariates(tbDraw & tbAccept) = sign(rand(1, nNumToAccept) - 0.5) .* acos(tfF(tbDraw & tbAccept));


Dylan Muir

Dear Mr M.,
thanks for your feedback. I have updated the code and fixed the bug. Please let me know if you still receive the error.

Best regards,

Mr M.

Sometimes I've got the following error:

Error using .*
Matrix dimensions must agree.

Error in vmrand (line 148)
tfVMVariates(tbDraw & tbAccept) = sign(rand(nNumToAccept, 1) - 0.5)
.* acos(tfF(tbDraw & tbAccept));

Mr M.


Updated description

Updated description

Updated description

Updated description

Updated description

Updated usage

Fixed a bug when no variates were accepted

Fixed bugs, where providing non-scalar parameters would sometimes cause an error message.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux