#pragma once #include #include #include #include #include #include #include #include namespace DB { /** Calculates Shannon Entropy, using HashMap and computing empirical distribution function. * Entropy is measured in bits (base-2 logarithm is used). */ template struct EntropyData { using Weight = UInt64; using HashingMap = HashMap< Value, Weight, HashCRC32, HashTableGrower<4>, HashTableAllocatorWithStackMemory) * (1 << 3)>>; /// For the case of pre-hashed values. using TrivialMap = HashMap< Value, Weight, UInt128TrivialHash, HashTableGrower<4>, HashTableAllocatorWithStackMemory) * (1 << 3)>>; using Map = std::conditional_t, TrivialMap, HashingMap>; Map map; void add(const Value & x) { if (!isNaN(x)) ++map[x]; } void add(const Value & x, const Weight & weight) { if (!isNaN(x)) map[x] += weight; } void merge(const EntropyData & rhs) { for (const auto & pair : rhs.map) map[pair.getKey()] += pair.getMapped(); } void serialize(WriteBuffer & buf) const { map.write(buf); } void deserialize(ReadBuffer & buf) { typename Map::Reader reader(buf); while (reader.next()) { const auto & pair = reader.get(); map[pair.first] = pair.second; } } Float64 get() const { UInt64 total_value = 0; for (const auto & pair : map) total_value += pair.getMapped(); Float64 shannon_entropy = 0; for (const auto & pair : map) { Float64 frequency = Float64(pair.getMapped()) / total_value; shannon_entropy -= frequency * log2(frequency); } return shannon_entropy; } }; template class AggregateFunctionEntropy final : public IAggregateFunctionDataHelper, AggregateFunctionEntropy> { private: size_t num_args; public: AggregateFunctionEntropy(const DataTypes & argument_types_) : IAggregateFunctionDataHelper, AggregateFunctionEntropy>(argument_types_, {}) , num_args(argument_types_.size()) { } String getName() const override { return "entropy"; } DataTypePtr getReturnType() const override { return std::make_shared>(); } void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override { if constexpr (!std::is_same_v) { /// Here we manage only with numerical types const auto & column = assert_cast &>(*columns[0]); this->data(place).add(column.getData()[row_num]); } else { this->data(place).add(UniqVariadicHash::apply(num_args, columns, row_num)); } } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override { this->data(place).merge(this->data(rhs)); } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { this->data(const_cast(place)).serialize(buf); } void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override { this->data(place).deserialize(buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override { auto & column = assert_cast &>(to); column.getData().push_back(this->data(place).get()); } }; }