HistogramDistribution takes a list of data points, creates a histogram from them, and returns this histogram in a form that can be used as a distribution.

I already have the binned data, and I do not have the original data points. How can I create a distribution object equivalent to a histogram distribution using this binning data?

Let's assume we have the binned data in the same format that HistogramList returns.

Example data: bins = N@HistogramList@RandomVariate[NormalDistribution[], 1000].


1 Answer 1


Update: We can use WeightedData to represent binned data and work with it in the usual way:

values = MovingAverage[First[bins], 2];
weights = Last[bins];

binsize = bins[[1,2]] - bins[[1,1]] (* assuming a constant bin size!! *)

distr = HistogramDistribution[WeightedData[values, weights], {binsize}]

Original answer:

HistogramDistribution creates a DataDistribution object. The format of this object is not documented, but we can attempt guessing at the object structure and construct a DataDistribution directly.

Warning: The usual caveats about spelunking and using undocumented functionality apply. There's no guarantee that this will work in every situation or in future versions. The following seems to work in version 9.

DataDistribution objects have the following format:

DataDistribution[type, data, dimension, numberOfPoints]
  • type is a string describing the type of the data distribution. For histogram distributions it is "Histogram"

  • data must have the format {pdf, binSpec} for histogram distributions (it is different for other distributions). binSpec is a list of bin boundaries, thus it must have a length one greater than the number of bins. pdf is a list of PDF values for each bin. Thus Total[Differences[binSpec] pdf] must be 1.

  • dimension is the dimension of the data. For 1D data, which we use here, it is 1.

  • numberOfPoints is the number of data points that the histogram was built from.

Starting from the example data bins, we can construct the distribution as

{binSpec, counts} = bins

dd = DataDistribution[
       "Histogram", (* type *)
       {counts/Differences[binSpec]/Total[counts], binSpec},  (* data *)
       1,         (* dimension *)
       Infinity   (* no. of data points *)

The usual functions such as Mean, Median, Variance, etc. can be used on dd.

Note: I am not sure if using Infinity for the number of data points can cause any problems.

  • $\begingroup$ Typo in the last code block? $\endgroup$
    – Verbeia
    Commented May 12, 2014 at 20:42
  • $\begingroup$ @Verbeia Not one, but three ... thanks, fixed! $\endgroup$
    – Szabolcs
    Commented May 12, 2014 at 21:07

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.