Skip to content

Commit 43e8645

Browse files
committed
Generalise mpr to concentration ratios
1 parent f6dbfbb commit 43e8645

File tree

6 files changed

+46
-24
lines changed

6 files changed

+46
-24
lines changed

config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ metrics:
88
hhi:
99
nakamoto_coefficient:
1010
theil_index:
11-
max_power_ratio:
11+
concentration_ratio:
12+
- 1
13+
- 3
1214
tau_index:
1315
- 0.33
1416
- 0.66

consensus_decentralization/analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from consensus_decentralization.metrics.entropy import compute_entropy, compute_entropy_percentage # noqa: F401
77
from consensus_decentralization.metrics.herfindahl_hirschman_index import compute_hhi # noqa: F401
88
from consensus_decentralization.metrics.theil_index import compute_theil_index # noqa: F401
9-
from consensus_decentralization.metrics.max_power_ratio import compute_max_power_ratio # noqa: F401
9+
from consensus_decentralization.metrics.concentration_ratio import compute_concentration_ratio # noqa: F401
1010
from consensus_decentralization.metrics.tau_index import compute_tau_index # noqa: F401
1111
from consensus_decentralization.metrics.total_entities import compute_total_entities # noqa: F401
1212

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def compute_concentration_ratio(block_distribution, topn):
2+
"""
3+
Calculates the n-concentration ratio of a distribution of balances
4+
:param block_distribution: a list of integers, each being the blocks that an entity has produced, sorted in descending order
5+
:param topn: the number of top block producers to consider
6+
:returns: float that represents the ratio of blocks produced by the top n block producers (0 if there weren't any)
7+
"""
8+
total_blocks = sum(block_distribution)
9+
return sum(block_distribution[:topn]) / total_blocks if total_blocks else 0

consensus_decentralization/metrics/max_power_ratio.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/metrics.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ The metrics that have been implemented so far are the following:
2929
or the redundancy, in a population. In practice, it is calculated as the maximum possible entropy minus the observed
3030
entropy. The output is a real number. Values close to 0 indicate equality and values towards infinity indicate
3131
inequality. Therefore, a high Theil Index suggests a population that is highly centralized.
32-
6. **Max power ratio**: The max power ratio represents the share of blocks that are produced by the most "powerful"
33-
entity, i.e. the entity that produces the most blocks. The output of the metric is a decimal number in [0,1].
32+
6. **Concentration ratio**: The n-concentration ratio represents the share of blocks that are produced by the n most
33+
"powerful" entities, i.e. the entities that produce the most blocks. The output of the metric is a decimal
34+
number in [0,1]. Values typically used are the 1-concentration ratio and the 3-concentration ratio.
3435
7. **Tau-decentralization index**: The tau-decentralization index is a generalization of the Nakamoto coefficient.
3536
It is defined as the minimum number of entities that collectively produce more than a given threshold of the total
3637
blocks within a given timeframe. The threshold parameter is a decimal in [0, 1] (0.66 by default) and the output of

tests/test_metrics.py

Lines changed: 30 additions & 12 deletions
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, total_entities)
2+
theil_index, concentration_ratio, tau_index, total_entities)
33
import numpy as np
44

55

@@ -72,6 +72,9 @@ def test_gini():
7272
g6 = gini.compute_gini([0, 0])
7373
assert g6 is None
7474

75+
g7 = gini.compute_gini([1])
76+
assert g7 == 0
77+
7578

7679
def test_nc():
7780
nc1 = nakamoto_coefficient.compute_nakamoto_coefficient([3, 2, 1])
@@ -137,21 +140,36 @@ def test_compute_theil_index():
137140
assert theil_t == 0
138141

139142

140-
def test_compute_max_power_ratio():
141-
max_mpr = max_power_ratio.compute_max_power_ratio([3, 2, 1])
142-
assert max_mpr == 0.5
143+
def test_compute_concentration_ratio():
144+
cr1 = concentration_ratio.compute_concentration_ratio([3, 2, 1], 1)
145+
assert cr1 == 0.5
146+
147+
cr3 = concentration_ratio.compute_concentration_ratio([3, 2, 1], 3)
148+
assert cr3 == 1
149+
150+
cr1 = concentration_ratio.compute_concentration_ratio([3, 2, 1, 1, 1, 1], 1)
151+
assert cr1 == 1 / 3
152+
153+
cr3 = concentration_ratio.compute_concentration_ratio([3, 2, 1, 1, 1, 1], 3)
154+
assert cr3 == 6/9
155+
156+
cr1 = concentration_ratio.compute_concentration_ratio([1], 1)
157+
assert cr1 == 1
158+
159+
cr3 = concentration_ratio.compute_concentration_ratio([1], 3)
160+
assert cr3 == 1
143161

144-
max_mpr = max_power_ratio.compute_max_power_ratio([3, 2, 1, 1, 1, 1])
145-
assert max_mpr == 1 / 3
162+
cr1 = concentration_ratio.compute_concentration_ratio([1, 1, 1], 1)
163+
assert cr1 == 1 / 3
146164

147-
max_mpr = max_power_ratio.compute_max_power_ratio([1])
148-
assert max_mpr == 1
165+
cr3 = concentration_ratio.compute_concentration_ratio([1, 1, 1], 3)
166+
assert cr3 == 1
149167

150-
max_mpr = max_power_ratio.compute_max_power_ratio([1, 1, 1])
151-
assert max_mpr == 1 / 3
168+
cr1 = concentration_ratio.compute_concentration_ratio([], 1)
169+
assert cr1 == 0
152170

153-
max_mpr = max_power_ratio.compute_max_power_ratio([])
154-
assert max_mpr == 0
171+
cr3 = concentration_ratio.compute_concentration_ratio([], 3)
172+
assert cr3 == 0
155173

156174

157175
def test_tau_33():

0 commit comments

Comments
 (0)