~ubuntu-branches/ubuntu/trusty/abyss/trusty-proposed

« back to all changes in this revision

Viewing changes to Common/PDF.h

  • Committer: Package Import Robot
  • Author(s): Shaun Jackman
  • Date: 2011-12-13 17:04:24 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20111213170424-5lenznlcdridsj81
Tags: 1.3.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef STATS_H
2
 
#define STATS_H 1
3
 
 
4
 
#include "Histogram.h"
5
 
#include <cassert>
6
 
#include <cmath>
7
 
#include <vector>
8
 
 
9
 
class Histogram;
10
 
 
11
 
/** Probability density function */
12
 
class PDF
13
 
{
14
 
  public:
15
 
        /** Construct a PDF from a histogram. */
16
 
        PDF(const Histogram& h) : m_dist(h.maximum() + 1), m_stdDev(h.sd())
17
 
        {
18
 
                unsigned count = h.size();
19
 
                m_minp = (double)1 / count;
20
 
                for (size_t i = 0; i < m_dist.size(); i++) {
21
 
                        unsigned n = h.count(i);
22
 
                        m_dist[i] = n > 0 ? (double)n / count : m_minp;
23
 
                }
24
 
        }
25
 
 
26
 
        /** Return the probability of x. */
27
 
        double getP(size_t x) const
28
 
        {
29
 
                return x < m_dist.size() ? m_dist[x] : m_minp;
30
 
        }
31
 
 
32
 
        double getMinP() const { return m_minp; }
33
 
 
34
 
        size_t getMaxIdx() const {
35
 
                assert(!m_dist.empty());
36
 
                return m_dist.size() - 1;
37
 
        }
38
 
 
39
 
        double getSampleStdDev(unsigned n) const
40
 
        {
41
 
                return m_stdDev / sqrt(n);
42
 
        }
43
 
 
44
 
  private:
45
 
        std::vector<double> m_dist;
46
 
        double m_stdDev;
47
 
        double m_minp;
48
 
};
49
 
 
50
 
namespace std {
51
 
        template<>
52
 
        inline void swap(PDF&, PDF&) { assert(false); }
53
 
}
54
 
 
55
 
#endif