/src/quantlib/ql/math/statistics/discrepancystatistics.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003 Ferdinando Ametrano |
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 | | <http://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 discrepancystatistics.hpp |
21 | | \brief Statistic tool for sequences with discrepancy calculation |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_dicrepancy_statistics_hpp |
25 | | #define quantlib_dicrepancy_statistics_hpp |
26 | | |
27 | | #include <ql/math/statistics/sequencestatistics.hpp> |
28 | | |
29 | | namespace QuantLib { |
30 | | |
31 | | //! Statistic tool for sequences with discrepancy calculation |
32 | | /*! It inherit from SequenceStatistics<Statistics> and adds |
33 | | \f$ L^2 \f$ discrepancy calculation |
34 | | */ |
35 | | class DiscrepancyStatistics : public SequenceStatistics { |
36 | | public: |
37 | | typedef SequenceStatistics::value_type value_type; |
38 | | // constructor |
39 | | DiscrepancyStatistics(Size dimension); |
40 | | //! \name 1-dimensional inspectors |
41 | | //@{ |
42 | | Real discrepancy() const; |
43 | | //@} |
44 | | template <class Sequence> |
45 | | void add(const Sequence& sample, |
46 | | Real weight = 1.0) { |
47 | | add(sample.begin(),sample.end(),weight); |
48 | | } |
49 | | template <class Iterator> |
50 | | void add(Iterator begin, |
51 | | Iterator end, |
52 | | Real weight = 1.0) { |
53 | | SequenceStatistics::add(begin,end,weight); |
54 | | |
55 | | Size k, m, N = samples(); |
56 | | |
57 | | Real r_ik, r_jk, temp = 1.0; |
58 | | Iterator it; |
59 | | for (k=0, it=begin; k<dimension_; ++it, ++k) { |
60 | | r_ik = *it; //i=N |
61 | | temp *= (1.0 - r_ik*r_ik); |
62 | | } |
63 | | cdiscr_ += temp; |
64 | | |
65 | | for (m=0; m<N-1; m++) { |
66 | | temp = 1.0; |
67 | | for (k=0, it=begin; k<dimension_; ++it, ++k) { |
68 | | // running i=1..(N-1) |
69 | | r_ik = stats_[k].data()[m].first; |
70 | | // fixed j=N |
71 | | r_jk = *it; |
72 | | temp *= (1.0 - std::max(r_ik, r_jk)); |
73 | | } |
74 | | adiscr_ += temp; |
75 | | |
76 | | temp = 1.0; |
77 | | for (k=0, it=begin; k<dimension_; ++it, ++k) { |
78 | | // fixed i=N |
79 | | r_ik = *it; |
80 | | // running j=1..(N-1) |
81 | | r_jk = stats_[k].data()[m].first; |
82 | | temp *= (1.0 - std::max(r_ik, r_jk)); |
83 | | } |
84 | | adiscr_ += temp; |
85 | | } |
86 | | temp = 1.0; |
87 | | for (k=0, it=begin; k<dimension_; ++it, ++k) { |
88 | | // fixed i=N, j=N |
89 | | r_ik = r_jk = *it; |
90 | | temp *= (1.0 - std::max(r_ik, r_jk)); |
91 | | } |
92 | | adiscr_ += temp; |
93 | | } |
94 | | void reset(Size dimension = 0); |
95 | | private: |
96 | | mutable Real adiscr_, cdiscr_; |
97 | | Real bdiscr_, ddiscr_; |
98 | | }; |
99 | | |
100 | | |
101 | | // inline definitions |
102 | | |
103 | | inline DiscrepancyStatistics::DiscrepancyStatistics(Size dimension) |
104 | | : SequenceStatistics(dimension) { |
105 | | reset(dimension); |
106 | | } |
107 | | |
108 | 0 | inline void DiscrepancyStatistics::reset(Size dimension) { |
109 | 0 | if (dimension == 0) // if no size given, |
110 | 0 | dimension = dimension_; // keep the current one |
111 | 0 | QL_REQUIRE(dimension != 1, |
112 | 0 | "dimension==1 not allowed"); |
113 | 0 |
|
114 | 0 | SequenceStatistics::reset(dimension); |
115 | 0 |
|
116 | 0 | adiscr_ = 0.0; |
117 | 0 | bdiscr_ = 1.0/std::pow(2.0, Integer(dimension-1)); |
118 | 0 | cdiscr_ = 0.0; |
119 | 0 | ddiscr_ = 1.0/std::pow(3.0, Integer(dimension)); |
120 | 0 | } |
121 | | |
122 | | } |
123 | | |
124 | | |
125 | | #endif |