-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add theil, mpr and tau to metrics (#138)
- Loading branch information
1 parent
0b7e276
commit abbd977
Showing
8 changed files
with
151 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def compute_max_power_ratio(blocks_per_entity): | ||
""" | ||
Calculates the maximum power ratio of a distribution of balances | ||
:param blocks_per_entity: a dictionary with entities and the blocks they have produced | ||
:returns: float that represents the maximum power ratio among all block producers (0 if there weren't any) | ||
""" | ||
if len(blocks_per_entity) == 0: | ||
return 0 | ||
max_nblocks = max(blocks_per_entity.values()) | ||
total_blocks = sum(blocks_per_entity.values()) | ||
return max_nblocks / total_blocks if total_blocks > 0 else 0 |
19 changes: 5 additions & 14 deletions
19
consensus_decentralization/metrics/nakamoto_coefficient.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,10 @@ | ||
from consensus_decentralization.metrics.tau_index import compute_tau_index | ||
|
||
|
||
def compute_nakamoto_coefficient(blocks_per_entity): | ||
""" | ||
Calculates the Nakamoto coefficient of a distribution of blocks to entities | ||
:param blocks_per_entity: a dictionary with entities and the blocks they have produced | ||
:returns: int that represents the Nakamoto coefficient of the given distribution or None if the data is empty | ||
:returns: int that represents the Nakamoto coefficient of the given distribution, or None if the data is empty | ||
""" | ||
total_blocks = sum(blocks_per_entity.values()) | ||
if total_blocks == 0: | ||
return None | ||
nc, power_percentage, top_entities = 0, 0, set() | ||
while power_percentage < 50: | ||
current_max_name = None | ||
for (name, blocks) in blocks_per_entity.items(): | ||
if current_max_name is None or (blocks >= blocks_per_entity[current_max_name] and name not in top_entities): | ||
current_max_name = name | ||
nc += 1 | ||
power_percentage += 100 * blocks_per_entity[current_max_name] / total_blocks | ||
top_entities.add(current_max_name) | ||
return nc | ||
return compute_tau_index(blocks_per_entity=blocks_per_entity, threshold=0.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def compute_tau_index(blocks_per_entity, threshold): | ||
""" | ||
Calculates the tau-decentralization index of a distribution of blocks | ||
:param blocks_per_entity: a dictionary with entities and the blocks they have produced | ||
:param threshold: float, the parameter of the tau-decentralization index, i.e. the threshold for the power | ||
ratio that is captured by the index (e.g. 0.66 for 66%) | ||
:returns: int that corresponds to the tau index of the given distribution, or None if there were no blocks | ||
""" | ||
total_blocks = sum(blocks_per_entity.values()) | ||
if total_blocks == 0: | ||
return None | ||
tau_index, power_ratio_covered = 0, 0 | ||
blocks_per_entity_copy = blocks_per_entity.copy() | ||
while power_ratio_covered < threshold: | ||
current_max_entity = max(blocks_per_entity_copy, key=blocks_per_entity_copy.get) | ||
tau_index += 1 | ||
power_ratio_covered += blocks_per_entity_copy[current_max_entity] / total_blocks | ||
del blocks_per_entity_copy[current_max_entity] | ||
return tau_index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from math import log | ||
|
||
|
||
def compute_theil_index(blocks_per_entity): | ||
""" | ||
Calculates the Thiel index of a distribution of blocks to entities | ||
:param blocks_per_entity: a dictionary with entities and the blocks they have produced | ||
:returns: float that represents the Thiel index of the given distribution | ||
""" | ||
n = len(blocks_per_entity) | ||
if n == 0: | ||
return 0 | ||
total_blocks = sum(blocks_per_entity.values()) | ||
mu = total_blocks / n | ||
theil = 0 | ||
for nblocks in blocks_per_entity.values(): | ||
x = nblocks / mu | ||
if x > 0: | ||
theil += x * log(x) | ||
theil /= n | ||
return theil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters