Next: lattices, Previous: histograms, Up: Directory Index [Contents][Index]
Adds the given input data to the histogram. If the histogram is too small, more bins are added.
histogram object
input histogram data
assert(histBins(addDataToHist(Hist(1, {"lin", "dbin", 1}), -2), 1, "finite", "bins")', -2:0)
assert(histBins(addDataToHist(Hist(1, {"lin", "dbin", 1}), -1), 1, "finite", "bins")', -1:0)
assert(histBins(addDataToHist(Hist(1, {"lin", "dbin", 1}), 0), 1, "finite", "bins")', 0:1)
assert(histBins(addDataToHist(Hist(1, {"lin", "dbin", 1}), 1), 1, "finite", "bins")', 0:2)
assert(histBins(addDataToHist(Hist(1, {"lin", "dbin", 1}), 2), 1, "finite", "bins")', 0:3)
assert(histProbs(addDataToHist(Hist(1, {"lin", "dbin", 1}), -2), "finite")', [1 0])
assert(histProbs(addDataToHist(Hist(1, {"lin", "dbin", 1}), -1), "finite")', [1])
assert(histProbs(addDataToHist(Hist(1, {"lin", "dbin", 1}), 0), "finite")', [1])
assert(histProbs(addDataToHist(Hist(1, {"lin", "dbin", 1}), 1), "finite")', [0 1])
assert(histProbs(addDataToHist(Hist(1, {"lin", "dbin", 1}), 2), "finite")', [0 0 1])
assert(histBins(addDataToHist(Hist(1, {"log", "binsper10", 1}), 0.9), 1, "finite", "bins")', -1:1, 1e-6)
assert(histBins(addDataToHist(Hist(1, {"log", "binsper10", 10}), 0.9), 1, "finite", "bins")', -1:0.1:1, 1e-6)
assert(histBins(addDataToHist(Hist(1, {"log", "minrange", 1, "binsper10", 10}), 0.9), 1, "finite", "bins")', -1:0.1:1, 1e-6)
assert(histBins(addDataToHist(Hist(1, {"log", "minrange", 1, "binsper10", 10}), 1.1), 1, "finite", "bins")', [-1:0.1:1, 2:10], 1e-6)
assert(histBins(addDataToHist(Hist(1, {"log", "minrange", 1, "binsper10", 10}), -10.1), 1, "finite", "bins")', [-100:10:-10, -9:-2, -1:0.1:1], 1e-6)
Add multiple histograms together.
operation used to add histograms:
count
sum histogram counts
prob
sum histogram probabilities
histograms to add; []
arguments are ignored
total histogram
hgrm1 = addDataToHist(Hist(1, 0:12), 0.5 + (0:11)'); hgrm2 = addDataToHist(Hist(1, 0:3:12), 0.5 + (0:11)'); hgrmt = addHists("count", hgrm1, hgrm2); p = histProbs(hgrmt); assert(length(p) == 14 && p(2:end-1) == 1/12);
hgrms = arrayfun(@(x) addDataToHist(Hist(1, 0:12), 0.5 + x), 0:11, "UniformOutput", false); hgrmt = addHists("count", hgrms{:}); p = histProbs(hgrmt); assert(length(p) == 14 && p(2:end-1) == 1/12);
Contract a histogram by summing counts over dimensions.
histogram object
dimensions that will remain after contraction; counts in other dimensions will be summed.
hgrm = Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}); hgrm = addDataToHist(hgrm, [octforge_normrnd(0, 1, 1e6, 1), rand(1e6, 1)]); hgrm1 = contractHist(hgrm, 1); hgrm2 = contractHist(hgrm, 2);
p2 = histProbs(hgrm2); c2 = histBinGrids(hgrm2, 1, "centre"); assert(mean(abs(p2(isfinite(c2)) - 1.0)) < 1e-2);
Display a histogram object.
histogram object
display(Hist()) display(Hist(1, {"lin", "dbin", 0.1})) display(Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})) display(Hist(3, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})) display(Hist(4, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})) display(Hist(5, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}))
Generates random values drawn from the probability distribution given by the histogram.
histogram object
number of random values to generate
generated random values
hgrmA = Hist(1, {"lin", "dbin", 0.1}); hgrmB = Hist(1, {"log", "minrange", 0.1, "binsper10", 8}); hgrmC = Hist(2, {"lin", "dbin", 0.1}, {"log", "minrange", 1.0, "binsper10", 8}); N = 1e6; do x = randn(N, 2); oldhgrmA = hgrmA; hgrmA = addDataToHist(hgrmA, x(:,1)); hgrmB = addDataToHist(hgrmB, x(:,1)); hgrmC = addDataToHist(hgrmC, x(:,1:2)); histerr = histDistance(oldhgrmA, hgrmA); until histerr < 1e-2
thgrmA = Hist(1, {"lin", "dbin", 0.1}); x = drawFromHist(hgrmA, N); thgrmA = addDataToHist(thgrmA, x); assert(histDistance(hgrmA, thgrmA) < 0.01);
thgrmC = Hist(2, {"lin", "dbin", 0.1}, {"log", "minrange", 1.0, "binsper10", 8}); x = drawFromHist(hgrmC, 20*N); thgrmC = addDataToHist(thgrmC, x); assert(histDistance(hgrmC, thgrmC) < 0.01);
Create a new object representing a multi-dimensional histogram.
histogram object
dimensionality of the histogram
bin types, one per dimension; possible types:
lin
, dbin
, ‘bin width’, …,
bin0
, ‘starting bin (default 0)’}
log
, minrange
, ‘starting minimal bin range (default: auto)’, …,
binsper10
, ‘number of bins to add per decade’}
Fixed array of bins, cannot be extended
See the tutorial on Hist
.
type
, … )Return quantities relating to the histogram bin boundaries, in gridded arrays of the same size as the probability array.
histogram object
dimension along which to return bin quantities
see histBins()
bin quantities
hgrm = restrictHist(addDataToHist(Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}), octforge_unifrnd(0, 1, 1e6, 2))); sert(histBinGrids(hgrm, 1, "lower"), [-inf, 0:0.1:0.9, inf]' * ones(1, 12), 1e-3) sert(histBinGrids(hgrm, 1, "centre"), [-inf, 0.05:0.1:0.95, inf]' * ones(1, 12), 1e-3) sert(histBinGrids(hgrm, 1, "upper"), [-inf, 0.1:0.1:1.0, inf]' * ones(1, 12), 1e-3)
type
, … )finite
, type
, … )Return quantities relating to the histogram bin boundaries.
histogram object
dimension along which to return bin quantities
one of:
bins
bin boundaries
lower
lower bin boundary
upper
upper bin boundary
centre
bin centre
width
bin width
bin quantities
finite
bin quantities
hgrm = restrictHist(addDataToHist(Hist(1, {"lin", "dbin", 0.1}), octforge_unifrnd(0, 1, 1e6, 1))); sert(histBins(hgrm, 1, "lower"), [-inf, 0:0.1:0.9, inf]', 1e-3) sert(histBins(hgrm, 1, "centre"), [-inf, 0.05:0.1:0.95, inf]', 1e-3) sert(histBins(hgrm, 1, "upper"), [-inf, 0.1:0.1:1.0, inf]', 1e-3) sert(histBins(hgrm, 1, "finite", "lower"), [0:0.1:0.9]', 1e-3) sert(histBins(hgrm, 1, "finite", "centre"), [0.05:0.1:0.95]', 1e-3) sert(histBins(hgrm, 1, "finite", "upper"), [0.1:0.1:1.0]', 1e-3)
Return the unions of bins of the given histograms in each dimension.
cell array of union of bins in each dimension
histograms
hgrm1 = restrictHist(addDataToHist(Hist(1, {"lin", "dbin", 0.1}), octforge_unifrnd(0, 1, 1e6, 1))); hgrm2 = restrictHist(addDataToHist(Hist(1, {"lin", "dbin", 0.1}), octforge_unifrnd(1, 2, 1e6, 1))); hgrm3 = restrictHist(addDataToHist(Hist(1, {"lin", "dbin", 0.1}), octforge_unifrnd(2, 3, 1e6, 1))); sert(histBinUnions(hgrm1, hgrm2), {[0:0.1:2]}, 1e-3) sert(histBinUnions(hgrm1, hgrm3), {[0:0.1:1, 2:0.1:3]}, 1e-3) sert(histBinUnions(hgrm2, hgrm3), {[1:0.1:3]}, 1e-3)
Returns the dimensionality of a histogram object.
histogram object
dimensionality of the histogram
assert(histDim(Hist(1, {"lin", "dbin", 0.1})), 1)
assert(histDim(Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})), 2)
assert(histDim(Hist(3, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})), 3)
assert(histDim(Hist(4, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})), 4)
assert(histDim(Hist(5, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1})), 5)
Computes different distance measures between histograms
histogram objects
defined to be the sum of the absolute difference in probability density in each bin (for a common bin set), multiplied by the bin area.
is the (non-symmetric) KullbackâLeibler divergence D_KL(hgrm1 || hgrm2) >= 0 https://en.wikipedia.org/wiki/Kullback-Leibler_divergence
is the (symmetric) JensenâShannon divergence 0 <= JDS <= 1 https://en.wikipedia.org/wiki/Jensen-Shannon_divergence
hgrm1 = createGaussianHist(1.2, 3.4, "binsize", 0.1); hgrm2 = createGaussianHist(7.3, 3.4, "binsize", 0.1); sert(histDistance(hgrm1, hgrm1), 0.00, 1e-3) sert(histDistance(hgrm1, hgrm2), 1.26, 1e-3)
finite
)Return the probabily densities of each histogram bin
histogram object
probability densities of all bins
probability densities of finite
bins
hgrm1 = Hist(1, {"lin", "dbin", 0.01}); hgrm1 = addDataToHist(hgrm1, octforge_normrnd(0, 1, 1e6, 1)); hgrm2 = Hist(1, {"log", "minrange", 0.5, "binsper10", 50}); hgrm2 = addDataToHist(hgrm2, octforge_normrnd(0, 1, 1e6, 1));
p2 = histProbs(hgrm2); c2 = histBinGrids(hgrm2, 1, "centre"); assert(mean(abs(p2 - octforge_normpdf(c2, 0, 1))) < 0.005);
hgrm3 = Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}); hgrm3 = addDataToHist(hgrm3, rand(1e6, 2));
finite
)Returns the ranges of a histogram object, which are finite if the histogram is non-empty and NaNs otherwise, and the number of bins in each range.
histogram object
dimensions for which to return ranges; defaults to all
ranges of the histogram
number of bins in each range
finite
ranges of the histogram
number of finite
bins in each range
hgrm0 = Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}); rand("seed", 1); # avoid random surprises exceeding tolerance sert(histRange(addDataToHist(hgrm0, octforge_unifrnd(1.2, 9.7, 1000, 2))), [1.2, 9.7; 1.2, 9.7], 0.25) sert(histRange(addDataToHist(hgrm0, [octforge_unifrnd(2.3, 11.3, 1000, 1), octforge_unifrnd(67.9, 103.5, 1000, 1)]), 1), [2.3, 11.3], 0.25) sert(histRange(addDataToHist(hgrm0, [octforge_unifrnd(2.3, 11.3, 1000, 1), octforge_unifrnd(67.9, 103.5, 1000, 1)]), 2), [67.9, 103.5], 0.25)
Returns the total number of counts in a histogram object.
histogram object
total number of counts
hgrm = Hist(2, {"lin", "dbin", 0.01}, {"lin", "dbin", 0.1}); hgrm = addDataToHist(hgrm, [octforge_normrnd(1.7, 4.3, 13579, 1), rand(13579, 1)]); assert(histTotalCount(hgrm), 13579);
Initialise a histogram directly from a given function.
histogram object
function used to initialise histogram
range in dimension ’k’ to evaluate F over
hgrm = Hist(1, {"lin", "dbin", 0.1}); hgrm = initHistFromFunc(hgrm, @(x) octforge_normpdf(x, 1.3, 2.7), [-20, 20]); assert(abs(meanOfHist(hgrm) - 1.3) < 1e-3); assert(abs(stdvOfHist(hgrm) - 2.7) < 1e-3);
hgrm = Hist(2, {"lin", "dbin", 0.1}, {"lin", "dbin", 0.1}); hgrm = initHistFromFunc(hgrm, @(x, y) octforge_normpdf(x, y, 1.0), [-20, 20], [0, 10]); assert(abs(meanOfHist(hgrm, 1) - linspace(0.05, 9.95, 100)) < 1e-4);
Resamples a histogram to a new set of bins
histogram object
dimension along which to resample
new bins in dimension k (dim = number of dimensions)
hgrm = Hist(2, {"lin", "dbin", 0.01}, {"lin", "dbin", 0.1}); hgrm = addDataToHist(hgrm, [octforge_normrnd(1.7, 4.3, 1e6, 1), rand(1e6, 1)]); assert(meanOfHist(hgrm, 1), meanOfHist(resampleHist(hgrm, 1, -30:0.2:30), 1), 1e-3);
Rescale the bins of a histogram.
histogram object
strictly positive scale to apply to bin in each dimension
hgrm = createGaussianHist(1.2, 3.4, "binsize", 0.1); sert(meanOfHist(rescaleHistBins(hgrm, 64.2)), 1.2 * 64.2, 1e-3)
Extract histogram restricted to subrange of bins, as determined by
the ranges [xl_k, xh_k]. Samples outside of these ranges are moved
to the histogram boundary bins [-\inf,xl_k] and [xh_k,\inf], unless
the string "discard" is given as the last argument. If no ranges are
given, histRange()
is used to find the minimum ranges.
restricted histogram object
original histogram object
dimension along which to restrict histogram range
range in dimension k to restrict range to
hgrm = Hist(2, {"lin", 0.01}, {"lin", 0.01}); hgrm = addDataToHist(hgrm, rand(50000,2)); hgrmx = restrictHist(hgrm, 1, [0, 0.5]); hgrmy = restrictHist(hgrm, 2, [0, 0.3]); assert(abs(meanOfHist(contractHist(hgrm, 1)) - 0.5) < 1e-2); assert(abs(meanOfHist(contractHist(hgrm, 2)) - 0.5) < 1e-2); assert(abs(meanOfHist(contractHist(hgrmx, 1)) - 0.25) < 1e-2); assert(abs(meanOfHist(contractHist(hgrmx, 2)) - 0.5) < 1e-2); assert(abs(meanOfHist(contractHist(hgrmy, 1)) - 0.5) < 1e-2); assert(abs(meanOfHist(contractHist(hgrmy, 2)) - 0.15) < 1e-2);
hgrm = Hist(2, -1.0:0.1:2.0, -1.0:0.1:2.0); hgrm = addDataToHist(hgrm, rand(50000,2)); count = histTotalCount(hgrm); hgrmx = restrictHist(hgrm, 1, [0.0, 1.0]); hgrmy = restrictHist(hgrm, 2, [-0.5, 1.5]); hgrmxy = restrictHist(hgrm, [0, 0.5], [0, 0.5]); assert(histTotalCount(hgrmx) == count); assert(histTotalCount(hgrmy) == count); assert(histTotalCount(hgrmxy) == count);
Set a threshold on the histogram probability of each bin; bins with probability below the threshold have their count set to zero.
thresholded histogram object
original histogram object
probability threshold
hgrm = createGaussianHist(1.2, 3.4, "binsize", 0.1); assert(meanOfHist(thresholdHist(hgrm, 1e-10)), 1.2, 1e-3); assert(meanOfHist(thresholdHist(hgrm, 0.1)), 1.2, 1e-3);
Transform the contents of a histogram.
histogram object
function to apply to histogram samples:
convergence requirement on histogram (default = 1e-2)
hgrmA = hgrmA1 = hgrmA2 = hgrmA3 = Hist(1, {"lin", "dbin", 0.1}); N = 1e6; do x = randn(N, 1); oldhgrmA = hgrmA; hgrmA = addDataToHist(hgrmA, x); hgrmA1 = addDataToHist(hgrmA1, abs(x)); hgrmA2 = addDataToHist(hgrmA2, x.^2 - 3); hgrmA3 = addDataToHist(hgrmA3, sin(x)); histerr = histDistance(oldhgrmA, hgrmA); until histerr < 1e-2
t_hgrmA = transformHist(hgrmA, @(x) x); assert(histDistance(hgrmA, t_hgrmA) < 0.01);
t_hgrmA2 = transformHist(hgrmA, @(x) x.^2 - 3); assert(histDistance(hgrmA2, t_hgrmA2) < 0.05);
hgrmB = hgrmB1 = hgrmB2 = hgrmB3 = Hist(2, {"lin", "dbin", 0.1}, {"log", "minrange", 1.0, "binsper10", 8}); N = 1e7; do x = randn(N, 1); y = sign(x) .* 10.^(-1 + 3*rand(N, 1)); oldhgrmB = hgrmB; hgrmB = addDataToHist(hgrmB, [x, y]); hgrmB1 = addDataToHist(hgrmB1, [abs(x), 10.*y]); hgrmB2 = addDataToHist(hgrmB2, [x.^2 - 3, y.^3]); hgrmB3 = addDataToHist(hgrmB3, [sin(x), cos(y)]); histerr = histDistance(oldhgrmB, hgrmB); until histerr < 1e-2
t_hgrmB = transformHist(hgrmB, @(x,y) [x, y]); assert(histDistance(hgrmB, t_hgrmB) < 0.05)
t_hgrmB2 = transformHist(hgrmB, @(x,y) [x.^2 - 3, y.^3]); assert(histDistance(hgrmB2, t_hgrmB2) < 0.1);
Next: lattices, Previous: histograms, Up: Directory Index [Contents][Index]