1 #ifndef FAIF_TS_DISCRETIZERS 2 #define FAIF_TS_DISCRETIZERS 4 #if defined(_MSC_VER) && (_MSC_VER >= 1700) 6 #pragma warning(disable:4100) 7 #pragma warning(disable:4512) 18 #include <boost/concept_check.hpp> 19 #include <boost/scoped_array.hpp> 20 #include <boost/bind.hpp> 21 #include <boost/algorithm/minmax_element.hpp> 22 #include <boost/lambda/bind.hpp> 23 #include <boost/lambda/construct.hpp> 24 #include <boost/lambda/core.hpp> 25 #include <boost/lambda/lambda.hpp> 27 #include <boost/serialization/serialization.hpp> 28 #include <boost/serialization/access.hpp> 29 #include <boost/serialization/base_object.hpp> 30 #include <boost/serialization/nvp.hpp> 31 #include <boost/serialization/utility.hpp> 32 #include <boost/serialization/vector.hpp> 35 namespace timeseries {
38 template<
typename V>
class Section :
public std::pair<V, V> {
40 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<V>));
44 Section(V min_val, V max_val) : std::pair<V, V>(min_val, max_val) { }
45 V getMin()
const {
return this->first; }
46 V getMax()
const {
return this->second; }
47 bool operator==(
const Section& s)
const {
return this->first == s.first && this->second == s.second; }
50 template<
class Archive>
52 ar & boost::serialization::make_nvp(
"Base",
53 boost::serialization::base_object<std::pair<V,V> >(*
this)
60 inline std::ostream& operator<<(std::ostream& os, const Section<V>& s) {
61 os <<
"(Min: " << s.getMin() <<
", Max:" << s.getMax() <<
")";
66 template<
typename V>
class Discretizer :
public std::vector<Section<V> > {
72 std::lower_bound( this->begin(), this->end(), value,
74 if( it == this->end() )
75 return static_cast<int>(this->size()) - 1;
77 return static_cast<int>(it - this->begin());
82 template<
class Archive>
84 ar & boost::serialization::make_nvp(
"Base",
85 boost::serialization::base_object<std::vector<
Section<V> > >(*
this)
91 inline std::ostream& operator<<(std::ostream& os, const Discretizer<V>& d) {
92 std::copy(d.begin(), d.end(), std::ostream_iterator< Section<V> >(os,
",") );
100 BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<It>));
102 typedef typename It::value_type
Value;
104 if( begin == end || num_sections == 0)
106 std::pair<It, It> mm = boost::minmax_element(begin, end );
107 Value length = (*(mm.second) - *(mm.first)) / static_cast<Value>(num_sections);
108 Value curr_min = *(mm.first) - length;
109 Value curr_max = *(mm.first);
110 typename boost::lambda::var_type<Value>::type vmin(boost::lambda::var(curr_min));
111 typename boost::lambda::var_type<Value>::type vmax(boost::lambda::var(curr_max));
112 std::generate_n(back_inserter(ret), num_sections,
113 boost::lambda::bind(boost::lambda::constructor<
Section<Value> >(),
114 vmin += boost::lambda::constant(length),
115 vmax += boost::lambda::constant(length)));
121 template<
typename It>
124 BOOST_CONCEPT_ASSERT((boost::ForwardIterator<It>));
126 typedef typename It::value_type
Value;
127 typedef typename std::vector<Value> ValVect;
129 if( begin == end || num_sections == 0)
131 ValVect values(begin, end);
132 std::sort(values.begin(), values.end() );
133 const Value& minValue = values.front();
134 const Value& maxValue = values.back();
136 Discretizer<Value> current = createEqualWidthSections(values.begin(), values.end(), num_sections );
138 const int MAX_NUMBER_OF_ITERATIONS = 100;
139 const Value MEANS_NOT_CHANGE_EPSILON = 0.001*(maxValue - minValue);
141 for(
int i = 0; i < MAX_NUMBER_OF_ITERATIONS; ++i ) {
146 boost::scoped_array< ValVect > result(
new ValVect[num_sections] );
148 for(
typename ValVect::const_iterator it = values.begin(); it != values.end(); ++it) {
149 result[current.
discretize(*it)].push_back(*it);
152 boost::scoped_array<Value> means(
new Value[num_sections] );
153 Value distance = 0.0;
154 const ValVect* disRes = result.get();
155 Value* mu = means.get();
157 Value secMean = ( sec->getMax() + sec->getMin() ) / 2.0;
158 if(disRes->empty() ) {
162 Value acc = std::accumulate(disRes->begin(), disRes->end(), 0.0);
163 Value m = acc /
static_cast<Value
>(disRes->size());
164 distance += std::fabs( secMean - m );
170 if(distance < MEANS_NOT_CHANGE_EPSILON)
174 Value secStart = minValue;
175 for(
unsigned int j = 0; j < num_sections - 1; ++j ) {
176 Value secStop = (means[j]+means[j+1])/2.0;
189 #endif //FAIF_TS_DISCRETIZERS
Definition: Discretizer.hpp:66
Definition: Discretizer.hpp:38
double Value
value - real number
Definition: TimeSeries.hpp:49
int discretize(V value) const
Definition: Discretizer.hpp:69
void serialize(Archive &ar, const unsigned int)
serialization using boost::serialization
Definition: Discretizer.hpp:83
Section()
required by serialization
Definition: Discretizer.hpp:43
void serialize(Archive &ar, const unsigned int)
serialization using boost::serialization
Definition: Discretizer.hpp:51
Discretizer< typename It::value_type > createKMeansSections(It begin, It end, const unsigned int num_sections)
Definition: Discretizer.hpp:122