This repository has been archived by the owner on Jun 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatisticsAggregators.h
109 lines (79 loc) · 3.14 KB
/
StatisticsAggregators.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
// This file defines some IStatisticsAggregator implementations used by the
// example code in Classification.h, DensityEstimation.h, etc. Note we
// represent IStatisticsAggregator instances using simple structs so that all
// tree data can be stored contiguously in a linear array.
#include <math.h>
#include <limits>
#include <vector>
#include <iostream>
#include "Interfaces.h"
#include "DataPointCollection.h"
// Maximum number of categories allowed.
#define MAX_BINS 5
namespace MicrosoftResearch { namespace Cambridge { namespace Sherwood
{
// Histogram Aggregator is used for classification problems
// Should be able to deal with multi-dimensional data (I think)
struct HistogramAggregator
{
public:
std::vector<unsigned int> bins_;
unsigned int binCount_;
unsigned int sampleCount_;
public:
/// <summary>
/// Returns the Shannon entropy
/// </summary>
double Entropy() const;
/// <summary>
/// Creates a HistogramAggregator instance with number of bins set to 0
/// </summary>
HistogramAggregator();
/// <summary>
/// Creates a HistogramAggregator instance with number of bins set to nClasses
/// </summary>
/// <param name="nClasses"> Number of bins </param>
HistogramAggregator(int nClasses);
float GetProbability(int classIndex) const;
unsigned int BinCount() const { return binCount_; }
unsigned int SampleCount() const { return sampleCount_; }
int FindTallestBinIndex() const;
//////////// IStatisticsAggregator implementation ////////////////
void Clear();
void Aggregate(const IDataPointCollection& data, unsigned int index);
void Aggregate(const HistogramAggregator& aggregator);
HistogramAggregator DeepClone() const;
//////////// END IStatisticsAggregator implementation ////////////////
};
// DiffEntropyAggregator is basically just a 1-d gaussian and stores
// the mean, variance, and sum squared error
// The "entropy" value calculated is log(var_) = differential entropy
// hence the name.
struct DiffEntropyAggregator
{
public:
float mean_;
float sse_;
float var_;
unsigned int sample_count_;
/// <summary>
/// Returns the Shannon entropy
/// </summary>
double DifferentialEntropy() const;
/// <summary>
/// Creates a DiffEntropyAggregator instance with mean, sse, and var = 0
/// </summary>
DiffEntropyAggregator();
unsigned int SampleCount() const { return sample_count_; }
float GetMean() const { return mean_; }
//////////// IStatisticsAggregator implementation ////////////////
void Clear();
void Aggregate(const IDataPointCollection& data, unsigned int index);
void Aggregate(const DiffEntropyAggregator& aggregator);
DiffEntropyAggregator DeepClone() const;
//////////// END IStatisticsAggregator implementation ////////////////
// For debugging
void Aggregate(float datum);
};
} } }