IMP  2.0.1
The Integrative Modeling Platform
HistogramD.h
Go to the documentation of this file.
1 /**
2  * \file IMP/statistics/HistogramD.h \brief Holds a histogram
3  *
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPSTATISTICS_HISTOGRAM_D_H
9 #define IMPSTATISTICS_HISTOGRAM_D_H
10 #include <IMP/statistics/statistics_config.h>
11 #include "internal/histogram.h"
13 #include <IMP/algebra/VectorD.h>
15 #include <IMP/algebra/GridD.h>
17 #include <IMP/base_types.h>
18 #include <limits>
19 #include <vector>
20 IMPSTATISTICS_BEGIN_NAMESPACE
21 /** Dynamically build a histogram embedded in D-dimensional space. */
22 template <int D, class Grid=algebra::GridD<D,
23  typename algebra::DenseGridStorageD<D,
24  double>,
25  double,
26  typename algebra::DefaultEmbeddingD<D> > >
28  public:
29 
30  typedef Grid CountGrid;
31  HistogramD(): count_(std::numeric_limits<double>::max()){}
32  HistogramD(double voxel_size,
33  const algebra::BoundingBoxD<D> &bb
34  ): grid_(voxel_size, bb, 0),
35  count_(0){}
36  /** Increase the count for the bin that holds a
37  value that is in range for this histogram.*/
38  void add(const algebra::VectorInputD<D>& x, double weight=1) {
39  IMP_USAGE_CHECK(count_ != std::numeric_limits<double>::max(),
40  "Using uninitialized histogram");
41  typename CountGrid::ExtendedIndex ei=grid_.get_nearest_extended_index(x);
42  if (grid_.get_has_index(ei)) {
43  grid_[grid_.get_index(ei)]+=weight;
44  }
45  count_+=weight;
46  }
47  //! Get the sum of all counts in the histogram.
48  double get_total_count() const {
49  return count_;
50  }
51  HistogramD<D> get_frequencies() const {
52  CountGrid grid(grid_.get_unit_cell()[0],
53  algebra::get_bounding_box(grid_), 0);
54  grid_.apply(internal::Frequency<D, Grid>(grid, 1.0/count_));
55  return HistogramD<D>(grid);
56  }
57  /** Get a histogram that has the pdf value as the value for the bin.*/
59  CountGrid grid(grid_.get_unit_cell()[0],
60  algebra::get_bounding_box(grid_), 0);
61  double volume
62  =algebra::get_volume(grid_.get_bounding_box(*grid_.all_indexes_begin()));
63  grid_.apply(internal::Frequency<D, Grid>(grid, 1.0/(count_*volume)));
64  return HistogramD<D>(grid);
65  }
66  const CountGrid& get_counts() const {
67  return grid_;
68  }
70  algebra::VectorD<D> zeros(grid_.get_bounding_box().get_corner(0));
71  std::fill(zeros.coordinates_begin(), zeros.coordinates_end(), 0.0);
72  return grid_.apply(internal::Mean<D>(zeros)).mn/count_;
73  }
74  unsigned int get_dimension() const {
75  return grid_.get_dimension();
76  }
77  algebra::VectorD<D>
78  get_standard_deviation(const algebra::VectorInputD<D> &mean) const {
79  algebra::VectorD<D> zeros(grid_.get_bounding_box().get_corner(0));
80  std::fill(zeros.coordinates_begin(), zeros.coordinates_end(), 0.0);
81  algebra::VectorD<D> s2
82  = grid_.apply(internal::Sigma2<D>(mean, zeros)).sigma2;
83  s2/=count_;
84  for (unsigned int i=0; i< get_dimension(); ++i) {
85  s2[i] = std::sqrt(s2[i]);
86  }
87  return s2;
88  }
89  algebra::BoundingBoxD<D> get_bounding_box() const {
90  return IMP::algebra::get_bounding_box(grid_);
91  }
92  FloatPair get_minimum_and_maximum() const {
93  return grid_.apply(internal::MinMax<D>()).minmax;
94  }
95 
96  IMP_SHOWABLE_INLINE(HistogramD,out << "count: " << count_);
97 private:
98  HistogramD(const CountGrid &g): grid_(g), count_(1){}
99  CountGrid grid_;
100  double count_;
101 };
102 
103 #ifndef IMP_DOXYGEN
104 typedef HistogramD<1> Histogram1D;
105 IMP_VALUES(Histogram1D, Histogram1Ds);
106 typedef HistogramD<2> Histogram2D;
107 IMP_VALUES(Histogram2D, Histogram2Ds);
108 typedef HistogramD<3> Histogram3D;
109 IMP_VALUES(Histogram3D, Histogram3Ds);
110 typedef HistogramD<4> Histogram4D;
111 IMP_VALUES(Histogram4D, Histogram4Ds);
112 typedef HistogramD<5> Histogram5D;
113 IMP_VALUES(Histogram5D, Histogram5Ds);
114 typedef HistogramD<6> Histogram6D;
115 IMP_VALUES(Histogram6D, Histogram6Ds);
116 typedef HistogramD<-1> HistogramKD;
117 IMP_VALUES(HistogramKD, HistogramKDs);
118 #endif
119 
120 /** Return the midpoint of the bin that best approximates the
121  specified quantile (passed as a fraction). That is,
122 passing .5 returns the median. And passing .9*/
123 IMPSTATISTICSEXPORT double get_quantile(const Histogram1D &h,
124  double fraction);
125 
126 IMPSTATISTICS_END_NAMESPACE
127 #endif /* IMPSTATISTICS_HISTOGRAM_D_H */