Skip to content

Commit 7a62f46

Browse files
LadyChristinadimkarakostas
authored andcommitted
Add total entities metric
1 parent d03816a commit 7a62f46

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ metrics:
1212
tau_index:
1313
- 0.33
1414
- 0.66
15+
total_entities:
1516

1617
# The ledgers for which an analysis should be performed.
1718
ledgers:

consensus_decentralization/analyze.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from consensus_decentralization.metrics.theil_index import compute_theil_index # noqa: F401
99
from consensus_decentralization.metrics.max_power_ratio import compute_max_power_ratio # noqa: F401
1010
from consensus_decentralization.metrics.tau_index import compute_tau_index # noqa: F401
11+
from consensus_decentralization.metrics.total_entities import compute_total_entities # noqa: F401
1112

1213

1314
def analyze(projects, aggregated_data_filename, output_dir):

consensus_decentralization/metrics/entropy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from math import log
2+
from consensus_decentralization.metrics.total_entities import compute_total_entities
23

34

45
def compute_entropy(block_distribution, alpha):
@@ -41,6 +42,8 @@ def compute_entropy_percentage(block_distribution, alpha):
4142
if sum(block_distribution) == 0:
4243
return None
4344
try:
44-
return compute_entropy(block_distribution, alpha) / compute_max_entropy(len(block_distribution), alpha)
45+
total_entities = compute_total_entities(block_distribution)
46+
return compute_entropy(block_distribution, alpha) / compute_max_entropy(total_entities, alpha)
4547
except ZeroDivisionError:
4648
return 0
49+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def compute_total_entities(block_distribution):
2+
"""
3+
Computes the number of entities that have produced blocks in the given timeframe.
4+
:param block_distribution: list of integers, each being the blocks that an entity has produced
5+
:returns: an integer that represents the number of entities that have produced blocks
6+
"""
7+
return len([v for v in block_distribution if v > 0])

tests/test_metrics.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from consensus_decentralization.metrics import (entropy, gini, nakamoto_coefficient, herfindahl_hirschman_index,
2-
theil_index, max_power_ratio, tau_index)
2+
theil_index, max_power_ratio, tau_index, total_entities)
33
import numpy as np
44

55

@@ -177,3 +177,14 @@ def test_tau_66():
177177

178178
tau_idx = tau_index.compute_tau_index([1], threshold=0.66)
179179
assert tau_idx == 1
180+
181+
182+
def test_total_entities():
183+
entity_count = total_entities.compute_total_entities(block_distribution=[1, 2, 3])
184+
assert entity_count == 3
185+
186+
entity_count = total_entities.compute_total_entities(block_distribution=[0])
187+
assert entity_count == 0
188+
189+
entity_count = total_entities.compute_total_entities(block_distribution=[5, 0, 0])
190+
assert entity_count == 1

0 commit comments

Comments
 (0)