Draw random numbers from pre-specified probability mass function in Matlab

Using the Statistics Toolbox The randsample function can do that directly: result = randsample(supp_epsilon, n, true, pr_mass_epsilon); Without using toolboxes Manual approach: Generate n samples of a uniform random variable in the interval (0,1). Compare each sample with the distribution function (cumulative sum of mass function). See in which interval of the distribution function each … Read more

Vary range of uniform_int_distribution

Distribution objects are lightweight. Simply construct a new distribution when you need a random number. I use this approach in a game engine, and, after benchmarking, it’s comparable to using good old rand(). Also, I’ve asked how to vary the range of distribution on GoingNative 2013 live stream, and Stephen T. Lavavej, a member of … Read more

How to plot a mean line on a kdeplot between 0 and the y value of the mean

Update for the latest versions of matplotlib (3.3.4) and seaborn (0.11.1): the kdeplot with shade=True now doesn’t create a line object anymore. To get the same outcome as before, setting shade=False will still create the line object. The curve can then be filled with ax.fill_between(). The code below is changed accordingly. (Use the revision history … Read more

What do all the distributions available in scipy.stats look like?

Visualizing all scipy.stats distributions Based on the list of scipy.stats distributions, plotted below are the histograms and PDFs of each continuous random variable. The code used to generate each distribution is at the bottom. Note: The shape constants were taken from the examples on the scipy.stats distribution documentation pages. alpha(a=3.57, loc=0.00, scale=1.00) anglit(loc=0.00, scale=1.00) arcsine(loc=0.00, … Read more

Generate random numbers following a normal distribution in C/C++

There are many methods to generate Gaussian-distributed numbers from a regular RNG. The Box-Muller transform is commonly used. It correctly produces values with a normal distribution. The math is easy. You generate two (uniform) random numbers, and by applying an formula to them, you get two normally distributed random numbers. Return one, and save the … Read more

Fitting empirical distribution to theoretical ones with Scipy (Python)?

Distribution Fitting with Sum of Square Error (SSE) This is an update and modification to Saullo’s answer, that uses the full list of the current scipy.stats distributions and returns the distribution with the least SSE between the distribution’s histogram and the data’s histogram. Example Fitting Using the El NiƱo dataset from statsmodels, the distributions are … Read more