File indexing completed on 2025-01-18 09:28:20
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
0009 #define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_CUMUL_DIST_HPP_DE_01_01_2006
0010
0011 #include <vector>
0012 #include <functional>
0013 #include <boost/parameter/keyword.hpp>
0014 #include <boost/range.hpp>
0015 #include <boost/mpl/placeholders.hpp>
0016 #include <boost/accumulators/accumulators_fwd.hpp>
0017 #include <boost/accumulators/framework/accumulator_base.hpp>
0018 #include <boost/accumulators/framework/extractor.hpp>
0019 #include <boost/accumulators/numeric/functional.hpp>
0020 #include <boost/accumulators/framework/parameters/sample.hpp>
0021 #include <boost/accumulators/statistics_fwd.hpp>
0022 #include <boost/accumulators/statistics/count.hpp>
0023 #include <boost/serialization/vector.hpp>
0024 #include <boost/serialization/utility.hpp>
0025
0026 namespace boost { namespace accumulators
0027 {
0028
0029
0030
0031 BOOST_PARAMETER_NESTED_KEYWORD(tag, p_square_cumulative_distribution_num_cells, num_cells)
0032
0033 BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_cumulative_distribution_num_cells)
0034
0035 namespace impl
0036 {
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 template<typename Sample>
0056 struct p_square_cumulative_distribution_impl
0057 : accumulator_base
0058 {
0059 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
0060 typedef std::vector<float_type> array_type;
0061 typedef std::vector<std::pair<float_type, float_type> > histogram_type;
0062
0063 typedef iterator_range<typename histogram_type::iterator> result_type;
0064
0065 template<typename Args>
0066 p_square_cumulative_distribution_impl(Args const &args)
0067 : num_cells(args[p_square_cumulative_distribution_num_cells])
0068 , heights(num_cells + 1)
0069 , actual_positions(num_cells + 1)
0070 , desired_positions(num_cells + 1)
0071 , positions_increments(num_cells + 1)
0072 , histogram(num_cells + 1)
0073 , is_dirty(true)
0074 {
0075 std::size_t b = this->num_cells;
0076
0077 for (std::size_t i = 0; i < b + 1; ++i)
0078 {
0079 this->actual_positions[i] = i + 1.;
0080 this->desired_positions[i] = i + 1.;
0081 this->positions_increments[i] = numeric::fdiv(i, b);
0082 }
0083 }
0084
0085 template<typename Args>
0086 void operator ()(Args const &args)
0087 {
0088 this->is_dirty = true;
0089
0090 std::size_t cnt = count(args);
0091 std::size_t sample_cell = 1;
0092 std::size_t b = this->num_cells;
0093
0094
0095 if (cnt <= b + 1)
0096 {
0097 this->heights[cnt - 1] = args[sample];
0098
0099
0100 if (cnt == b + 1)
0101 {
0102 std::sort(this->heights.begin(), this->heights.end());
0103 }
0104 }
0105 else
0106 {
0107
0108 if (args[sample] < this->heights[0])
0109 {
0110 this->heights[0] = args[sample];
0111 sample_cell = 1;
0112 }
0113 else if (this->heights[b] <= args[sample])
0114 {
0115 this->heights[b] = args[sample];
0116 sample_cell = b;
0117 }
0118 else
0119 {
0120 typename array_type::iterator it;
0121 it = std::upper_bound(
0122 this->heights.begin()
0123 , this->heights.end()
0124 , args[sample]
0125 );
0126
0127 sample_cell = std::distance(this->heights.begin(), it);
0128 }
0129
0130
0131 for (std::size_t i = sample_cell; i < b + 1; ++i)
0132 {
0133 ++this->actual_positions[i];
0134 }
0135
0136
0137
0138 for (std::size_t i = 1; i < b + 1; ++i)
0139 {
0140 this->desired_positions[i] += this->positions_increments[i];
0141 }
0142
0143
0144 for (std::size_t i = 1; i < b; ++i)
0145 {
0146
0147 float_type d = this->desired_positions[i] - this->actual_positions[i];
0148
0149
0150 float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
0151
0152
0153 float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
0154
0155
0156 float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
0157 float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
0158
0159 if ( ( d >= 1. && dp > 1. ) || ( d <= -1. && dm < -1. ) )
0160 {
0161 short sign_d = static_cast<short>(d / std::abs(d));
0162
0163
0164 float_type h = this->heights[i] + sign_d / (dp - dm) * ( (sign_d - dm) * hp + (dp - sign_d) * hm );
0165
0166 if ( this->heights[i - 1] < h && h < this->heights[i + 1] )
0167 {
0168 this->heights[i] = h;
0169 }
0170 else
0171 {
0172
0173 if (d>0)
0174 {
0175 this->heights[i] += hp;
0176 }
0177 if (d<0)
0178 {
0179 this->heights[i] -= hm;
0180 }
0181 }
0182 this->actual_positions[i] += sign_d;
0183 }
0184 }
0185 }
0186 }
0187
0188 template<typename Args>
0189 result_type result(Args const &args) const
0190 {
0191 if (this->is_dirty)
0192 {
0193 this->is_dirty = false;
0194
0195
0196
0197
0198
0199 std::size_t cnt = count(args);
0200
0201 for (std::size_t i = 0; i < this->histogram.size(); ++i)
0202 {
0203 this->histogram[i] = std::make_pair(this->heights[i], numeric::fdiv(this->actual_positions[i], cnt));
0204 }
0205 }
0206
0207 return make_iterator_range(this->histogram);
0208 }
0209
0210
0211
0212 template<class Archive>
0213 void serialize(Archive & ar, const unsigned int file_version)
0214 {
0215 ar & num_cells;
0216 ar & heights;
0217 ar & actual_positions;
0218 ar & desired_positions;
0219 ar & positions_increments;
0220 ar & histogram;
0221 ar & is_dirty;
0222 }
0223
0224 private:
0225 std::size_t num_cells;
0226 array_type heights;
0227 array_type actual_positions;
0228 array_type desired_positions;
0229 array_type positions_increments;
0230 mutable histogram_type histogram;
0231 mutable bool is_dirty;
0232 };
0233
0234 }
0235
0236
0237
0238
0239 namespace tag
0240 {
0241 struct p_square_cumulative_distribution
0242 : depends_on<count>
0243 , p_square_cumulative_distribution_num_cells
0244 {
0245
0246
0247 typedef accumulators::impl::p_square_cumulative_distribution_impl<mpl::_1> impl;
0248 };
0249 }
0250
0251
0252
0253
0254 namespace extract
0255 {
0256 extractor<tag::p_square_cumulative_distribution> const p_square_cumulative_distribution = {};
0257
0258 BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_cumulative_distribution)
0259 }
0260
0261 using extract::p_square_cumulative_distribution;
0262
0263
0264
0265 template<>
0266 struct as_weighted_feature<tag::p_square_cumulative_distribution>
0267 {
0268 typedef tag::weighted_p_square_cumulative_distribution type;
0269 };
0270
0271 template<>
0272 struct feature_of<tag::weighted_p_square_cumulative_distribution>
0273 : feature_of<tag::p_square_cumulative_distribution>
0274 {
0275 };
0276
0277 }}
0278
0279 #endif