Class: EntropyTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
MoreMath::Functions
Defined in:
tests/entropy_test.rb

Instance Method Summary collapse

Methods included from MoreMath::Functions

#beta, beta, #beta_regularized, beta_regularized, #cantor_pairing, cantor_pairing, #cantor_pairing_inv, cantor_pairing_inv, #erf, #erfc, #gamma, gamma, #gammaP_regularized, gammaP_regularized, #gammaQ_regularized, gammaQ_regularized, #log_beta, log_beta, #log_ceil, log_ceil, #log_floor, log_floor, #log_gamma, #logb, logb, #numberify_string, numberify_string, #stringify_number, stringify_number

Methods included from MoreMath::Entropy

#collision_entropy_per_symbol, #collision_entropy_total, #entropy_ideal, #entropy_maximum, #entropy_per_symbol, #entropy_probabilities, #entropy_ratio, #entropy_total, #minimum_entropy_per_symbol, #minimum_entropy_total

Methods included from MoreMath::Lambert

#lambert_w

Instance Method Details

#setupObject

The setup method initializes instance variables with various string values.

This method prepares the object with predefined string constants for testing and demonstration purposes. It sets up empty strings, strings of specific lengths, and strings containing various character sets including ASCII, Unicode, and Japanese characters.



15
16
17
18
19
20
21
22
# File 'tests/entropy_test.rb', line 15

def setup
  @empty  = ''
  @low    = ?A * 42
  @string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
  @high   = 'The quick brown fox jumps over the lazy dog'
  @random = "\xAC-\x8A\xF5\xA8\xF7\\\e\xB5\x8CI\x06\xA7"
  @hi     = "こんにちは世界"
end

#test_collision_entropy_per_symbolObject



93
94
95
96
97
98
99
100
101
102
# File 'tests/entropy_test.rb', line 93

def test_collision_entropy_per_symbol
  # For a uniform distribution, collision entropy = log2(size)
  assert_in_delta 2.0, collision_entropy_per_symbol('ABCD'), 1e-12

  # All symbols the same → 0
  assert_in_delta 0.0, collision_entropy_per_symbol('AAAA'), 1e-12

  # Empty string → 0
  assert_in_delta 0.0, collision_entropy_per_symbol(''), 1e-12
end

#test_collision_entropy_totalObject



120
121
122
123
124
125
126
# File 'tests/entropy_test.rb', line 120

def test_collision_entropy_total
  text = 'ABCD'
  per = collision_entropy_per_symbol(text)
  assert_in_delta per * text.size, collision_entropy_total(text), 1e-12

  assert_in_delta 0.0, collision_entropy_total(''), 1e-12
end

#test_entropyObject



24
25
26
27
28
29
30
31
# File 'tests/entropy_test.rb', line 24

def test_entropy
  assert_in_delta 0.0, entropy(@empty), 1e-12
  assert_in_delta 0.0, entropy(@low),    1e-12
  assert_in_delta 3.951, entropy(@string), 1e-3
  assert_in_delta 4.431, entropy(@high),   1e-3
  assert_in_delta 3.700, entropy(@random), 1e-3
  assert_in_delta 2.807, entropy(@hi),     1e-3
end

#test_entropy_idealObject



33
34
35
36
37
38
39
40
41
42
43
# File 'tests/entropy_test.rb', line 33

def test_entropy_ideal
  assert_in_delta 0.0, entropy_ideal(-1), 1e-12
  assert_in_delta 0.0, entropy_ideal(0),  1e-12
  assert_in_delta 0.0, entropy_ideal(0.5), 1e-12
  assert_in_delta 0.0, entropy_ideal(1),  1e-12
  assert_in_delta 1.0,   entropy_ideal(2), 1e-3
  assert_in_delta 1.584, entropy_ideal(3), 1e-3
  assert_in_delta 3.0,   entropy_ideal(8), 1e-3
  assert_in_delta 3.321, entropy_ideal(10), 1e-3
  assert_in_delta 4.0,   entropy_ideal(16), 1e-3
end

#test_entropy_maximumObject



45
46
47
48
49
50
51
52
53
# File 'tests/entropy_test.rb', line 45

def test_entropy_maximum
  text = 'A' * 64
  assert_equal 0, entropy_maximum(text, size: -1)
  assert_equal 0, entropy_maximum(text, size: 0)
  assert_equal 0, entropy_maximum(text, size: 1)
  assert_equal 64, entropy_maximum(text, size: 2)
  assert_equal 256, entropy_maximum(text, size: 16)
  assert_equal 128, entropy_maximum(text[0, 32], size: 16)
end

#test_entropy_probabilitiesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'tests/entropy_test.rb', line 65

def test_entropy_probabilities
  probs = entropy_probabilities('ABAB')
  assert_equal 0.5, probs['A']
  assert_equal 0.5, probs['B']

  probs = entropy_probabilities('AAAA')
  assert_equal 1.0, probs['A']

  probs = entropy_probabilities([])
  assert_equal({}, probs)

  # Ensure the method accepts an Array of symbols
  probs = entropy_probabilities(['x', 'y', 'x'])
  assert_equal 2.0 / 3.0, probs['x']
  assert_equal 1.0 / 3.0, probs['y']
end

#test_entropy_ratioObject



55
56
57
58
59
60
61
62
63
# File 'tests/entropy_test.rb', line 55

def test_entropy_ratio
  assert_in_delta 0.0, entropy_ratio(@empty, size: 128), 1e-12
  assert_in_delta 0.0, entropy_ratio(@low, size: 128), 1e-12
  assert_in_delta 0.564, entropy_ratio(@string, size: 128), 1e-3
  assert_in_delta 0.633, entropy_ratio(@high, size: 128), 1e-3
  assert_in_delta 1.0,   entropy_ratio(@random, size: @random.size), 1e-3
  assert_in_delta 0.462, entropy_ratio(@random, size: 256), 1e-3
  assert_in_delta 0.253, entropy_ratio(@hi, size: 2_136), 1e-3
end

#test_entropy_totalObject



104
105
106
107
108
109
110
# File 'tests/entropy_test.rb', line 104

def test_entropy_total
  text = 'ABCD'
  per = entropy_per_symbol(text)
  assert_in_delta per * text.size, entropy_total(text), 1e-12

  assert_in_delta 0.0, entropy_total(''), 1e-12
end

#test_minimum_entropy_per_symbolObject



82
83
84
85
86
87
88
89
90
91
# File 'tests/entropy_test.rb', line 82

def test_minimum_entropy_per_symbol
  # Uniform distribution → entropy equals log2(size)
  assert_in_delta 2.0, minimum_entropy_per_symbol('ABCD'), 1e-12

  # Single symbol → 0
  assert_in_delta 0.0, minimum_entropy_per_symbol('AAAA'), 1e-12

  # Empty string → 0
  assert_in_delta 0.0, minimum_entropy_per_symbol(''), 1e-12
end

#test_minimum_entropy_totalObject



112
113
114
115
116
117
118
# File 'tests/entropy_test.rb', line 112

def test_minimum_entropy_total
  text = 'ABCD'
  per = minimum_entropy_per_symbol(text)
  assert_in_delta per * text.size, minimum_entropy_total(text), 1e-12

  assert_in_delta 0.0, minimum_entropy_total(''), 1e-12
end