I have a vector with 50 elements. These are values of a random variable. I have represented the data as Histogram[RandomVariable1Histogram]. However, I need to obtain a bigger sample from said histogram, about 1000 values. If I were working with a known distribution, lets say a Gamma, I could simply write

nsim = 1000;
newValues = 
 RandomVariate[GammaDistribution[\[Alpha]0, \[Beta]0], nsim]; 

However, since I am currently dealing with an unknown random variable, I don't know how to sample said histogram. My only idea is to use HistogramDistribution and somehow put the output inside of RandomVariate. However, I am not very happy with this since it requires the calculation of a probability density function. Is there a better way of obtaining a sample of a random variable of which only a histogram is available?


2 Answers 2


You may do this using "SmoothKernelDistribution".

Here is a simple example. We first create some data from a Normal Distribution:

d = RandomVariate[NormalDistribution[], 100];

Then we get a smooth distribution from this data:

dist= SmoothKernelDistribution[d];

We can now test the outcome by sampling from this distribution and see if it is approximately normal:

rv = RandomVariate[dist, 1000];

enter image description here

  • $\begingroup$ Thanks for your answer. I have read the documentation of SmoothKernelDistribution and attempted empiricPDF = SmoothKernelDistribution[RandomVariable1Histogram] and d = RandomVariate[empiricPDF, 1000]; it seems to work if I write Histogram[d] $\endgroup$ Commented Nov 25, 2021 at 11:25
  • $\begingroup$ The answer needs to be modified. What is dist above? $\endgroup$
    – Asim
    Commented Nov 25, 2021 at 15:31
  • $\begingroup$ This is exactly what one would do if the raw data is available. But I read that the OP only has the histogram (bins and bin counts). $\endgroup$
    – JimB
    Commented Nov 25, 2021 at 16:30
  • $\begingroup$ From a histogram one can always create some data with multiple entries. $\endgroup$ Commented Nov 25, 2021 at 16:40
  • $\begingroup$ @Asim. Thanks, it looks like I copied the wrong line. I corrected it. $\endgroup$ Commented Nov 25, 2021 at 16:41

This is precisely what RandomChoice does: choosing numbers according to weights. For example, if your histogram is

hist = {99, 217, 1026, 3};

then you can draw a random number from $\{1,2,3,4\}$ that is distributed similarly with

RandomChoice[hist -> Range[Length[hist]]]

or you can draw $10^6$ numbers simultaneously with

RandomChoice[hist -> Range[Length[hist]], 10^6]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.