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].
Answer
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]
typeis a string describing the type of the data distribution. For histogram distributions it is"Histogram"datamust have the format{pdf, binSpec}for histogram distributions (it is different for other distributions).binSpecis a list of bin boundaries, thus it must have a length one greater than the number of bins.pdfis a list ofPDFvalues for each bin. ThusTotal[Differences[binSpec] pdf]must be1.dimensionis the dimension of the data. For 1D data, which we use here, it is 1.numberOfPointsis 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.
Comments
Post a Comment