Debian Codegraph @

src/dr/util/FrequencyDistribution.java

raw symbols SCIP
1 /*
2 * FrequencyDistribution.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 /**
29 * @author Alexei Drummond
30 * @version $Id: FrequencyDistribution.java,v 1.3 2005/05/24 20:26:01 rambaut Exp $
31 */
32 public class FrequencyDistribution {
33
34 /** The size of the bin */
35 private double binSize;
36
37 /** the lower boundary of first bin */
38 private double start = 0.0;
39
40 /** the number of values lower than first bin */
41 private double smaller;
42
43 /** the number of values greater than last bin */
44 private double larger;
45
46 /** the frequncy counts of each bin */
47 private int[] bins;
48
49 public FrequencyDistribution(double start, int numBins, double binSize) {
50 init(start, numBins, binSize);
51 }
52
53 public FrequencyDistribution(int numBins, double binSize) {
54 init(0.0, numBins, binSize);
55 }
56
57 public FrequencyDistribution(double[] stats, int numBins, double binSize) {
58 init(0.0, numBins, binSize);
59 for (double stat : stats) {
60 addValue(stat);
61 }
62 }
63
64 /**
65 * Returns the number of bins.
66 */
67 public int getBinCount() {
68 return bins.length;
69 }
70
71 public double getBinSize() {
72 return binSize;
73 }
74
75 /** Returns lower bound of first bin. */
76 public double getLowerBound() {
77 return start;
78 }
79
80 /**
81 * @return the number of values falling in this bin.
82 */
83 public int getFrequency(int bin) {
84 return bins[bin];
85 }
86
87 public double getProbability(int bin) {
88 int total = 0;
89 for (int b : bins) {
90 total = total + b;
91 }
92 if (total == 0) {
93 return 0.0;
94 } else {
95 return (double) bins[bin] / (double) total;
96 }
97 }
98
99 public void addValue(double value) {
100
101 double diff = value - start;
102
103 int index = (int)(diff / binSize);
104 if (index < 0) {
105 smaller += 1;
106 } else if (index >= bins.length) {
107 larger += 1;
108 } else bins[index] += 1;
109 }
110
111 public String toString() {
112 StringBuffer buffer = new StringBuffer();
113 buffer.append("< ").append(start).append("\t").append(smaller).append("\n");
114 double mean;
115 for (int i = 0; i < bins.length; i++) {
116 mean = start + (binSize * ((double)i + 0.5));
117 buffer.append(mean).append("\t").append(bins[i]).append("\n");
118 }
119 double end = start + (binSize * bins.length);
120 buffer.append(">= ").append(end).append("\t").append(larger).append("\n");
121 return new String(buffer);
122 }
123
124 private void init(double start, int numBins, double binSize) {
125 bins = new int[numBins];
126 this.binSize = binSize;
127 smaller = 0;
128 larger = 0;
129 this.start = start;
130 }
131 }