fixed_bin_histogram.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include "histogram.h"
  3. #include <library/cpp/histogram/adaptive/protos/histo.pb.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/vector.h>
  6. #include <utility>
  7. namespace NKiwiAggr {
  8. class TFixedBinHistogram: private TNonCopyable, public IHistogram {
  9. private:
  10. static const size_t DEFAULT_TRAINING_SET_SIZE = 10000;
  11. static const size_t DEFAULT_INTERVALS = 100;
  12. typedef std::pair<double, double> TWeightedValue; // value, weight
  13. THolder<TVector<TWeightedValue>> TrainingSet;
  14. size_t TrainingSetSize;
  15. bool IsInitialized;
  16. bool IsEmpty;
  17. ui64 Id;
  18. double MinValue;
  19. double MaxValue;
  20. double Sum;
  21. TVector<double> Freqs;
  22. TVector<double> ReserveFreqs;
  23. double ReferencePoint;
  24. double BinRange;
  25. size_t Intervals;
  26. i32 FirstUsedBin;
  27. i32 LastUsedBin;
  28. i32 BaseIndex;
  29. public:
  30. TFixedBinHistogram(size_t intervals, ui64 id = 0, size_t trainingSetSize = DEFAULT_TRAINING_SET_SIZE);
  31. TFixedBinHistogram(const THistogram& histo, size_t defaultIntervals = DEFAULT_INTERVALS, ui64 defaultId = 0, size_t trainingSetSize = DEFAULT_TRAINING_SET_SIZE);
  32. TFixedBinHistogram(IHistogram* histo, size_t defaultIntervals = DEFAULT_INTERVALS, ui64 defaultId = 0, size_t trainingSetSize = DEFAULT_TRAINING_SET_SIZE);
  33. virtual ~TFixedBinHistogram() {
  34. }
  35. virtual void Clear();
  36. virtual void Add(double value, double weight);
  37. virtual void Add(const THistoRec& histoRec);
  38. virtual void Merge(const THistogram& histo, double multiplier);
  39. virtual void Merge(const TVector<THistogram>& histogramsToMerge);
  40. virtual void Merge(TVector<IHistogramPtr> histogramsToMerge);
  41. virtual void Multiply(double factor);
  42. virtual void FromProto(const THistogram& histo);
  43. virtual void ToProto(THistogram& histo);
  44. virtual void SetId(ui64 id);
  45. virtual ui64 GetId();
  46. virtual bool Empty();
  47. virtual double GetMinValue();
  48. virtual double GetMaxValue();
  49. virtual double GetSum();
  50. virtual double GetSumInRange(double leftBound, double rightBound);
  51. virtual double GetSumAboveBound(double bound);
  52. virtual double GetSumBelowBound(double bound);
  53. virtual double CalcUpperBound(double sum);
  54. virtual double CalcLowerBound(double sum);
  55. virtual double CalcUpperBoundSafe(double sum);
  56. virtual double CalcLowerBoundSafe(double sum);
  57. double CalcDensity(double value);
  58. private:
  59. double CalcBinRange(double referencePoint, double maxValue);
  60. void SetFrame(double minValue, double maxValue, bool clear);
  61. void FromIHistogram(IHistogram* histo);
  62. void Initialize();
  63. i32 CalcBin(double value);
  64. double BinStart(i32 i);
  65. double BinEnd(i32 i);
  66. void Shrink(double newMinValue, double newMaxValue);
  67. static bool CompareWeightedValue(const TWeightedValue& left, const TWeightedValue& right) {
  68. return left.first < right.first;
  69. }
  70. };
  71. }