Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/statistics/histogram.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2007 Gang Liang
5
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <https://www.quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
/*! \file histogram.hpp
21
    \brief statistics tool for generating histogram of given data
22
*/
23
24
#ifndef quantlib_histogram_hpp
25
#define quantlib_histogram_hpp
26
27
#include <ql/utilities/null.hpp>
28
#include <vector>
29
30
namespace QuantLib {
31
32
    //! Histogram class
33
    /*! This class computes the histogram of a given data set.  The
34
        caller can specify the number of bins, the breaks, or the
35
        algorithm for determining these quantities in computing the
36
        histogram.
37
    */
38
    class Histogram {
39
      public:
40
        enum Algorithm { None, Sturges, FD, Scott };
41
42
        //! \name constructors
43
        //@{
44
0
        Histogram() : algorithm_(Algorithm(-1)) {}
45
46
        template <class T>
47
        Histogram(T data_begin, T data_end, Size breaks)
48
        : data_(data_begin, data_end), bins_(breaks + 1) {
49
            calculate();
50
        }
51
52
        template <class T>
53
        Histogram(T data_begin, T data_end, Algorithm algorithm)
54
        : data_(data_begin,data_end), bins_(Null<Size>()),
55
          algorithm_(algorithm) {
56
            calculate();
57
        }
58
59
        template <class T, class U>
60
        Histogram(T data_begin, T data_end, U breaks_begin, U breaks_end)
61
        : data_(data_begin, data_end), bins_(Null<Size>()), breaks_(breaks_begin, breaks_end) {
62
            bins_ = breaks_.size()+1;
63
            calculate();
64
        }
65
        //@}
66
67
        //! \name inspectors
68
        //@{
69
        Size bins() const;
70
        const std::vector<Real>& breaks() const;
71
        Algorithm algorithm() const;
72
        bool empty() const;
73
        //@}
74
75
        //! \name results
76
        //@{
77
        Size counts(Size i) const;
78
        Real frequency(Size i) const;
79
        //@}
80
      private:
81
        std::vector<Real> data_;
82
        Size bins_ = 0;
83
        Algorithm algorithm_ = None;
84
        std::vector<Real> breaks_;
85
        std::vector<Size> counts_;
86
        std::vector<Real> frequency_;
87
        // update counts and frequencies
88
        void calculate();
89
    };
90
91
}
92
93
#endif