001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.math.statistics.distribution;
031
032import org.openimaj.feature.DoubleFV;
033import org.openimaj.util.array.ArrayUtils;
034
035/**
036 * Simple Histogram based on a DoubleFV.
037 * 
038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
039 * 
040 */
041public class Histogram extends DoubleFV {
042        private static final long serialVersionUID = 1L;
043
044        /**
045         * Construct a histogram with the given number of bins.
046         * 
047         * @param nbins
048         *            number of bins
049         */
050        public Histogram(int nbins) {
051                super(nbins);
052        }
053
054        /**
055         * Construct a histogram by concatenating the given histograms
056         * 
057         * @param hs
058         *            histograms to concatenate
059         */
060        public Histogram(DoubleFV... hs) {
061                final double[][] hists = new double[hs.length][];
062                for (int i = 0; i < hs.length; i++) {
063                        hists[i] = hs[i].values;
064                }
065
066                this.values = ArrayUtils.concatenate(hists);
067        }
068
069        /**
070         * Construct from values array and dimensions
071         * 
072         * @param data
073         *            the flat array of values
074         */
075        public Histogram(double[] data) {
076                super(data);
077        }
078
079        /**
080         * Normalise to unit area
081         */
082        public void normalise() {
083                double sum = 0;
084
085                for (int i = 0; i < values.length; i++)
086                        sum += values[i];
087
088                for (int i = 0; i < values.length; i++)
089                        values[i] /= sum;
090        }
091
092        /**
093         * l1 norm
094         */
095        public void normaliseL1() {
096                double sum = 0;
097
098                for (int i = 0; i < values.length; i++)
099                        sum += Math.abs(values[i]);
100
101                if (sum != 0)
102                        for (int i = 0; i < values.length; i++)
103                                values[i] /= sum;
104        }
105
106        /**
107         * l2 norm
108         */
109        public void normaliseL2() {
110                double sumsq = 0;
111
112                for (int i = 0; i < values.length; i++)
113                        sumsq += values[i] * values[i];
114
115                if (sumsq != 0)
116                        for (int i = 0; i < values.length; i++)
117                                values[i] /= Math.sqrt(sumsq);
118        }
119
120        /**
121         * Compute the maximum value in the histogram
122         * 
123         * @return the maximum value
124         */
125        public double max()
126        {
127                double max = Double.MIN_VALUE;
128                for (int i = 0; i < values.length; i++)
129                        max = Math.max(values[i], max);
130                return max;
131        }
132
133        @Override
134        public Histogram clone() {
135                return (Histogram) super.clone();
136        }
137
138        /**
139         * Create a new histogram by concatenating this one with the given ones.
140         * 
141         * @param hs
142         *            histograms to concatenate
143         * @return new histogram that is the concatenation of the argument
144         *         histograms
145         */
146        public Histogram combine(Histogram... hs) {
147                final int hsLength = hs == null ? 0 : hs.length;
148                final double[][] hists = new double[1 + hsLength][];
149                hists[0] = this.values;
150
151                for (int i = 0; i < hsLength; i++) {
152                        hists[i + 1] = hs[i].values;
153                }
154
155                return new Histogram(ArrayUtils.concatenate(hists));
156        }
157}