Skip to content

Commit

Permalink
[Entropy] + separate files in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghitamutu committed Sep 1, 2024
1 parent 57c5739 commit 71bf2d8
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 449 deletions.
2 changes: 1 addition & 1 deletion GViewCore/include/GView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ namespace Regex
namespace Entropy
{
CORE_EXPORT double ShannonEntropy(const BufferView& buffer);
CORE_EXPORT double RenyiEntropy(const BufferView& buffer);
CORE_EXPORT double RenyiEntropy(const BufferView& buffer, double alpha);
} // namespace Entropy

/*
Expand Down
27 changes: 24 additions & 3 deletions GViewCore/src/Entropy/Entropy.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Internal.hpp"

#include <math.h>
#include <array>

constexpr uint32 MAX_NUMBER_OF_BYTES = 256;

Expand All @@ -15,6 +16,17 @@ void SetFrequencies(const BufferView& buffer, std::array<char, MAX_NUMBER_OF_BYT
}
}

/*
In physics, the word entropy has important physical implications as the amount of "disorder" of a system.
In mathematics, a more abstract definition is used.
The (Shannon) entropy of a variable X is defined as
H(X) congruent - sum_x P(x) log_2[P(x)]
bits, where P(x) is the probability that X is in the state x, and P log_2 P is defined as 0 if P = 0.
The joint entropy of variables X_1, ..., X_n is then defined by
H(X_1, ..., X_n) congruent - sum_(x_1) ... sum_(x_n) P(x_1, ..., x_n) log_2[P(x_1, ..., x_n)].
*/
double ShannonEntropy_private(const BufferView& buffer, std::array<char, MAX_NUMBER_OF_BYTES>& frequency)
{
double entropy = 0.0;
Expand All @@ -26,7 +38,7 @@ double ShannonEntropy_private(const BufferView& buffer, std::array<char, MAX_NUM
entropy -= probability * log2(probability);
}

return entropy; // max log2(n) = 8
return entropy; // max log2(n) = 8 (the entire sum)
}

double ShannonEntropy(const BufferView& buffer)
Expand All @@ -36,6 +48,14 @@ double ShannonEntropy(const BufferView& buffer)
return ShannonEntropy_private(buffer, frequency);
}

/*
Rényi entropy is defined as:
H_α(p_1, p_2, ..., p_n) = 1/(1 - α) ln( sum_(i = 1)^n p_i^α), where α>0, α!=1.
As α->1, H_α(p_1, p_2, ..., p_n) converges to H(p_1, p_2, ..., p_n), which is Shannon's measure of entropy.
Rényi's measure satisfies
H_α(p_1, p_2, ..., p_n)<=H_α'(p_1, p_2, ..., p_n)
for α<=α'.
*/
double RenyiEntropy(const BufferView& buffer, double alpha)
{
std::array<char, MAX_NUMBER_OF_BYTES> frequency{};
Expand All @@ -47,13 +67,14 @@ double RenyiEntropy(const BufferView& buffer, double alpha)

double sum = 0.0;
for (auto f : frequency) {
double probability = static_cast<double>(f) / buffer.GetLength();
if (probability > 0) {
if (f > 0) {
const double probability = static_cast<double>(f) / buffer.GetLength();
sum += pow(probability, alpha);
}
}

// Convert to bits if using log base e
// return std::max(((1.0 / (1.0 - alpha)) * log(sum)) / log(2), 0.0);
return ((1.0 / (1.0 - alpha)) * log(sum)) / log(2);
}
} // namespace GView::Entropy
2 changes: 1 addition & 1 deletion GenericPlugins/EntropyVisualizer/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
target_sources(EntropyVisualizer PRIVATE EntropyVisualizer.cpp)
target_sources(EntropyVisualizer PRIVATE Plugin.cpp EntropyVisualizer.cpp)
Loading

0 comments on commit 71bf2d8

Please sign in to comment.