forked from sportstensor/sn41
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoring.py
More file actions
1447 lines (1240 loc) · 67.2 KB
/
scoring.py
File metadata and controls
1447 lines (1240 loc) · 67.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
scoring.py — Performance-Based Incentive Mechanism for Prediction Market Traders
This module implements a two-phase optimization system that rewards traders (miners) based on
their historical trading performance. The mechanism distributes a fixed budget among eligible
participants, prioritizing those who demonstrate consistent profitability and trading volume.
HOW IT WORKS:
-------------
The system tracks trading activity over a rolling 30-day window, organizing trades into daily
epochs. For each epoch, it:
1. Calculates Performance Metrics:
- ROI (Return on Investment): Profit divided by trading volume
- Qualified Volume: Volume from winning trades (after fees)
- Trailing Performance: Historical performance across all epochs
2. Applies Eligibility Gates:
- Minimum ROI threshold (prevents rewarding unprofitable traders)
- Minimum volume requirement (ensures meaningful participation)
- Build-up period: Traders must demonstrate consistent activity over multiple epochs
3. Runs Two-Phase Optimization:
Phase 1: Maximizes the total qualified volume that can be funded within budget constraints
Phase 2: Redistributes payouts to favor higher-ROI traders while maintaining volume targets
4. Allocates Tokens:
- Converts optimized scores into token weights
- Distributes rewards proportionally based on funded volume and signal strength (ROI)
- Enforces diversity caps to prevent any single trader from dominating
KEY FEATURES:
-------------
- Dual Pool System: Separate scoring for registered miners vs. general pool traders
- Volume Decay: Recent activity weighted more heavily than older trades
- Smooth Transitions: Ramp constraints prevent sudden allocation changes
- Budget Management: Ensures total payouts never exceed available budget
- Performance Gating: Only profitable, active traders receive rewards
The system is designed to incentivize high-quality trading signals while maintaining fairness
and preventing gaming through volume requirements and historical performance tracking.
"""
import numpy as np
import cvxpy as cp
from scipy.stats import spearmanr, pearsonr
from tabulate import tabulate
import bittensor as bt
from collections import defaultdict
from typing import Dict, Any, List
from datetime import datetime, timedelta, timezone
from constants import (
ROLLING_HISTORY_IN_DAYS,
ROI_MIN,
VOLUME_MIN,
VOLUME_FEE,
VOLUME_DECAY,
RAMP,
RHO_CAP,
KAPPA_NEXT,
KAPPA_SCALING_FACTOR,
ENABLE_STATIC_WEIGHTING,
GENERAL_POOL_WEIGHT_PERCENTAGE,
MINER_WEIGHT_PERCENTAGE,
MIN_EPOCHS_FOR_ELIGIBILITY,
MIN_PREDICTIONS_FOR_ELIGIBILITY,
BURN_UID,
EXCESS_MINER_WEIGHT_UID,
EXCESS_MINER_MIN_WEIGHT,
EXCESS_MINER_TAKE_PERCENTAGE,
MAX_EPOCH_BUDGET_PERCENTAGE_FOR_BOOST,
MINER_POOL_BUDGET_BOOST_PERCENTAGE,
MINER_POOL_WEIGHT_BOOST_PERCENTAGE,
ENABLE_ES_MINER_INCENTIVES,
ESM_MIN_MULTIPLIER,
ENABLE_ES_MINER_LOSS_COMPENSATION,
ESM_LOSS_COMPENSATION_PERCENTAGE,
ENABLE_ES_GP_INCENTIVES,
ESGP_MIN_MULTIPLIER,
ENABLE_ES_GP_LOSS_COMPENSATION,
ESGP_LOSS_COMPENSATION_PERCENTAGE
)
def score_miners(
all_uids: List[int],
all_hotkeys: List[str],
trading_history: List[Dict[str, Any]],
current_epoch_budget: float,
verbose: bool = False,
target_epoch_idx: int = None
):
"""
Score the miners based on the trading history.
Creates epoch-based numpy matrices (similar to simulate_epochs.py) where:
- Rows = epochs (days), indexed 0 to 29 for last 30 days
- Columns = entities (miner_ids for miners, profile_ids for general pool)
- Separates miners from general pool users
"""
if current_epoch_budget is None:
raise ValueError("current_epoch_budget is required")
if trading_history is None:
raise ValueError("trading_history is required")
if all_uids is None:
raise ValueError("all_uids is required")
if all_hotkeys is None:
raise ValueError("all_hotkeys is required")
# Normalize trading_history to always be a list
# Handle case where API might return dict with "data" field (defensive programming)
if isinstance(trading_history, dict):
if "data" in trading_history:
trading_history = trading_history["data"]
else:
raise ValueError(f"trading_history is a dict but missing 'data' field. Keys: {list(trading_history.keys())}")
if not isinstance(trading_history, list):
raise ValueError(f"trading_history must be a list or dict with 'data' field, got {type(trading_history)}")
# Convert the trading history to match the format expected by the scoring function
"""
{
"position_id": 69,
"account_id": 3,
"profile_id": "0xc70",
"miner_id": 17,
"miner_hotkey": "5EqZoEKc6c8TaG4xRRHTT1uZiQF5jkjQCeUV5t77L6YbeaJ8",
"is_general_pool": false,
"market_id": "684074",
"token_id": "17172534480191114731684649039231483628133799672384902614021128663005351577066",
"date_created": "2025-11-21T23:18:22.076Z",
"volume": 4.99998838889,
"expected_fees": 0.0499998838889,
"actual_fees": 0.05,
"pnl": 6.11112161111,
"is_correct": true,
"is_completed": true,
"completed_at": "2025-11-22T11:31:15.127Z",
"is_reward_eligible": true
}
"""
# Build epoch-based data structures similar to simulate_epochs.py
miner_history = build_epoch_history(
trading_history=trading_history,
all_uids=all_uids,
all_hotkeys=all_hotkeys,
is_miner_pool=True,
target_epoch_idx=target_epoch_idx
)
general_pool_history = build_epoch_history(
trading_history=trading_history,
all_uids=all_uids,
all_hotkeys=all_hotkeys,
is_miner_pool=False,
target_epoch_idx=target_epoch_idx
)
# Calculate fees collected for the target epoch
# If target_epoch_idx is None, use the last epoch (current behavior)
epoch_idx = target_epoch_idx if target_epoch_idx is not None else -1
miner_fees = np.sum(miner_history["fees_prev"][epoch_idx]) if miner_history["n_entities"] > 0 else 0.0
gp_fees = np.sum(general_pool_history["fees_prev"][epoch_idx]) if general_pool_history["n_entities"] > 0 else 0.0
current_epoch_fees_collected = miner_fees + gp_fees
if verbose:
print(f"Current epoch fees collected: {current_epoch_fees_collected:,.2f}")
print(f"-> Miner pool fees: {miner_fees:,.2f}")
print(f"-> General pool fees: {gp_fees:,.2f}")
if ENABLE_STATIC_WEIGHTING:
# Calculate the budget for each pool based on our constants that reallocate the total fees collected to the miners and general pool.
miner_pool_epoch_fees = current_epoch_fees_collected * MINER_WEIGHT_PERCENTAGE
general_pool_epoch_fees = current_epoch_fees_collected * GENERAL_POOL_WEIGHT_PERCENTAGE
# Calculate the max budget for each pool based on our constants. This is the max budget for the pool for the epoch.
max_current_epoch_budget = current_epoch_budget * MAX_EPOCH_BUDGET_PERCENTAGE_FOR_BOOST
miner_pool_epoch_max_budget = max_current_epoch_budget * MINER_WEIGHT_PERCENTAGE
# If max epoch budgets are greater than our fees, set the fees to the max budget. This gives more weights (and in turn, more incentives) to the miners when we aren't using the full budget.
miner_pool_max_budget_weighted = miner_pool_epoch_fees
if miner_pool_epoch_max_budget > miner_pool_epoch_fees:
miner_pool_max_budget_weighted = miner_pool_epoch_max_budget
#bt.logging.info(f"Miner pool max budget weighted is greater than fees ({miner_pool_epoch_fees:,.2f}). Setting to {miner_pool_epoch_max_budget:,.2f} ({MAX_EPOCH_BUDGET_PERCENTAGE_FOR_BOOST * 100:.2f}% boost)")
print(f"Miner pool max budget is greater than fees ({miner_pool_epoch_fees:,.2f}). Setting to {miner_pool_epoch_max_budget:,.2f} ({MAX_EPOCH_BUDGET_PERCENTAGE_FOR_BOOST * 100:.2f}% boost)")
# Calculate the subnet budget for each pool based on our constants. This is the total budget for the subnet for the epoch.
miner_pool_epoch_budget = current_epoch_budget * MINER_WEIGHT_PERCENTAGE
general_pool_epoch_budget = current_epoch_budget * GENERAL_POOL_WEIGHT_PERCENTAGE
else:
# Use the fees collected for each pool directly, which acts as dynamic weighting.
miner_pool_epoch_budget = miner_fees
general_pool_epoch_budget = gp_fees
# Calculate the max budget for the miner pool based on our constants. This is the max budget for the pool for the epoch.
miner_pool_max_budget_weighted = miner_pool_epoch_budget
if MINER_POOL_BUDGET_BOOST_PERCENTAGE > 0:
miner_pool_max_budget_weighted = miner_pool_epoch_budget * (1 + MINER_POOL_BUDGET_BOOST_PERCENTAGE)
if miner_pool_max_budget_weighted + general_pool_epoch_budget > current_epoch_budget:
miner_pool_max_budget_weighted = miner_pool_epoch_budget
#bt.logging.info(f"Miner pool budget boost would exceed total budget. Using regular budget instead.\n")
print(f"Miner pool budget boost would exceed total budget. Using regular budget instead.\n")
else:
#bt.logging.info(f"Miner pool budget boost would not exceed total budget. Using boosted budget of {miner_pool_max_budget_weighted:,.2f} (+{MINER_POOL_BUDGET_BOOST_PERCENTAGE * 100:.2f}%)\n")
print(f"Miner pool budget boost would not exceed total budget. Using boosted budget of {miner_pool_max_budget_weighted:,.2f} (+{MINER_POOL_BUDGET_BOOST_PERCENTAGE * 100:.2f}%)\n")
# Calculate the miner pool scores using epoch-based history
miners_scores = score_with_epochs(
epoch_history=miner_history,
budget=miner_pool_epoch_budget,
max_budget_weighted=miner_pool_max_budget_weighted,
roi_min=ROI_MIN,
volume_min=VOLUME_MIN,
require_epoch_preds=False,
verbose=verbose
)
# Check all positive miner scores and apply early stage miner incentives
if ENABLE_ES_MINER_INCENTIVES and len(miners_scores["scores"]) > 0:
miners_scores = apply_early_stage_miner_incentives(miner_history, miners_scores, current_epoch_budget)
# Calculate the general pool scores using epoch-based history
general_pool_scores = score_with_epochs(
epoch_history=general_pool_history,
budget=general_pool_epoch_budget,
max_budget_weighted=None, # General pool is not eligible for the max budget boost.
roi_min=ROI_MIN,
volume_min=VOLUME_MIN,
require_epoch_preds=True,
verbose=verbose
)
# Check all positive general pool trader scores and apply early stage incentives
if ENABLE_ES_GP_INCENTIVES and len(general_pool_scores["scores"]) > 0:
general_pool_scores = apply_early_stage_gp_incentives(general_pool_history, general_pool_scores, general_pool_epoch_budget)
return miner_history, general_pool_history, miners_scores, general_pool_scores, miner_pool_epoch_budget, general_pool_epoch_budget
def build_epoch_history(
trading_history: List[Dict[str, Any]],
all_uids: List[int],
all_hotkeys: List[str],
is_miner_pool: bool,
target_epoch_idx: int = None
):
"""
Build epoch-based numpy matrices similar to simulate_epochs.py.
Returns a dictionary with:
- v_prev: (n_epochs, n_entities) - qualified volume per epoch
- profit_prev: (n_epochs, n_entities) - profit per epoch
- fees_prev: (n_epochs, n_entities) - fees per epoch
- unqualified_prev: (n_epochs, n_entities) - losing volume per epoch
- entity_ids: list of miner_ids or profile_ids
- entity_map: dict mapping entity_id -> column index
- epoch_dates: list of date strings for each epoch (index 0 = oldest)
- miner_profiles: dict mapping of miner_id to polymarket_id
- account_map: dict mapping of entity_id to account_id
"""
# Determine date range and number of epochs
today = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
start_date = today - timedelta(days=ROLLING_HISTORY_IN_DAYS)
# Determine number of epochs based on target_epoch_idx
if target_epoch_idx is not None:
# For historical simulation: create epochs 0 through target_epoch_idx
n_epochs = target_epoch_idx + 1
else:
# For current epoch: use full rolling window (default behavior)
n_epochs = ROLLING_HISTORY_IN_DAYS
# Create list of epoch dates (0 = oldest, n_epochs-1 = most recent)
epoch_dates = [(start_date + timedelta(days=i)).date() for i in range(n_epochs)]
# First pass: collect all entity IDs and organize trades by epoch
entity_set = set()
epoch_trades = defaultdict(list) # epoch_idx -> list of trades
miner_profiles = {}
account_map = {}
for trade in trading_history:
# Skip if the trade does not have an account_id (should not happen)
if trade["account_id"] is None:
continue
# Skip if the trade is not completed
if not trade["is_completed"]:
continue
# Parse date
date_completed = trade["completed_at"]
if isinstance(date_completed, str):
date_completed = datetime.fromisoformat(date_completed.replace('Z', '+00:00'))
trade_date = date_completed.date()
# Find epoch index
if trade_date < epoch_dates[0] or trade_date >= today.date():
continue # outside our window
epoch_idx = (trade_date - epoch_dates[0]).days
# Get account ID and map to entity_id
account_id = trade["account_id"]
# Filter by pool type
if is_miner_pool:
# Miner pool: validate miner
if trade["is_general_pool"]:
continue
miner_id = trade.get("miner_id")
miner_hotkey = trade.get("miner_hotkey")
if miner_id is None or miner_hotkey is None:
continue
# Validate: miner_id exists, has a registered hotkey, and hotkey matches
if miner_id not in all_uids or miner_id >= len(all_hotkeys) or all_hotkeys[miner_id] != miner_hotkey:
continue
entity_id = miner_id
is_reward_eligible = trade["is_reward_eligible"]
# If the trade is reward eligible, add the profile id to the miner profiles
if is_reward_eligible:
if miner_id not in miner_profiles:
miner_profiles[miner_id] = trade["profile_id"].lower()
elif miner_profiles[miner_id] != trade["profile_id"]:
# append additional profile ids to be validated later in the validator
miner_profiles[miner_id] += f",{trade['profile_id'].lower()}"
# Map the entity id to the account id only if the trade is reward eligible
if entity_id not in account_map:
account_map[entity_id] = account_id
else:
# General pool
if not trade["is_general_pool"]:
continue
entity_id = trade["profile_id"]
# Map the profile id to the account id
if entity_id not in account_map:
account_map[entity_id] = account_id
entity_set.add(entity_id)
epoch_trades[epoch_idx].append((entity_id, trade))
# Create entity mapping
entity_ids = sorted(list(entity_set))
entity_map = {eid: idx for idx, eid in enumerate(entity_ids)}
n_entities = len(entity_ids)
# n_epochs was already calculated above based on target_epoch_idx
# Initialize matrices (like simulate_epochs.py)
volume_prev = np.zeros((n_epochs, n_entities)) # volume
qualified_prev = np.zeros((n_epochs, n_entities)) # qualified volume
unqualified_prev = np.zeros((n_epochs, n_entities)) # losing volume
profit_prev = np.zeros((n_epochs, n_entities)) # profit
fees_prev = np.zeros((n_epochs, n_entities)) # fees collected
trade_counts = np.zeros((n_epochs, n_entities)) # number of trades
correct_trade_counts = np.zeros((n_epochs, n_entities)) # number of correct trades
# Second pass: populate matrices
for epoch_idx in range(n_epochs):
if epoch_idx not in epoch_trades:
continue
for entity_id, trade in epoch_trades[epoch_idx]:
col_idx = entity_map[entity_id]
expected_volume = trade["expected_volume"] if trade["expected_volume"] is not None else 0.0
volume = trade["volume"] if trade["volume"] is not None else 0.0
expected_fees = trade["expected_fees"] if trade["expected_fees"] is not None else 0.0
actual_fees = trade["actual_fees"] if trade["actual_fees"] is not None else 0.0
pnl = trade["pnl"] if trade["pnl"] is not None else 0.0
is_correct = trade["is_correct"]
is_reward_eligible = trade["is_reward_eligible"]
# Check expected and actual volume. If actual volume is more, the trader added to their position outside of Almanac. Mark the trade as unqualified.
# Give a $5 buffer for potential rounding issues.
if volume > expected_volume and abs(volume - expected_volume) > 5 and is_reward_eligible:
is_reward_eligible = False
print(f"Trader {entity_id} has more volume than expected ({volume} > {expected_volume}). Marking trade as not reward eligible.")
# Check expected and actual fees. If actual fees are less, the trader paid less fees than expected. Mark the trade as unqualified.
# Give a 10% buffer so we're not completely strict on the fees.
if actual_fees < expected_fees and (actual_fees / expected_fees) < 0.9 and is_reward_eligible:
is_reward_eligible = False
if actual_fees > 0:
print(f"Trader {entity_id} has less fees than expected ({actual_fees} < {expected_fees}). Marking trade as not reward eligible.")
# Always collect fees for all trades, even if the trade is not reward eligible
fees_prev[epoch_idx, col_idx] += actual_fees
if is_reward_eligible and actual_fees > 0:
volume_prev[epoch_idx, col_idx] += volume
if is_correct:
# Winning trade: qualified volume (after fee deduction)
qualified = volume - actual_fees
qualified_prev[epoch_idx, col_idx] += qualified
correct_trade_counts[epoch_idx, col_idx] += 1
else:
# Losing trade: unqualified volume
unqualified_prev[epoch_idx, col_idx] += volume
profit_prev[epoch_idx, col_idx] += pnl
trade_counts[epoch_idx, col_idx] += 1 # Count each trade
print()
return {
"volume_prev": volume_prev,
"qualified_prev": qualified_prev,
"unqualified_prev": unqualified_prev,
"profit_prev": profit_prev,
"fees_prev": fees_prev,
"trade_counts": trade_counts,
"correct_trade_counts": correct_trade_counts,
"entity_ids": entity_ids,
"entity_map": entity_map,
"epoch_dates": [str(d) for d in epoch_dates],
"n_epochs": n_epochs,
"n_entities": n_entities,
"miner_profiles": miner_profiles,
"account_map": account_map
}
def check_build_up_eligibility(epoch_history: Dict[str, Any]) -> np.ndarray:
"""
Check if entities meet the build-up period requirements for eligibility.
Returns a boolean array where True means the entity meets build-up requirements.
"""
n_entities = epoch_history["n_entities"]
n_epochs = epoch_history["n_epochs"]
trade_counts = epoch_history["trade_counts"] # (n_epochs, n_entities)
volume_prev = epoch_history["volume_prev"] # (n_epochs, n_entities)
# Initialize eligibility array
eligible = np.ones(n_entities, dtype=bool)
for entity_idx in range(n_entities):
# Check minimum epochs requirement
entity_epochs_with_trades = np.sum(trade_counts[:, entity_idx] > 0)
if entity_epochs_with_trades < MIN_EPOCHS_FOR_ELIGIBILITY:
eligible[entity_idx] = False
continue
# Check minimum trades requirement
total_trades = np.sum(trade_counts[:, entity_idx])
if total_trades < MIN_PREDICTIONS_FOR_ELIGIBILITY:
eligible[entity_idx] = False
continue
return eligible
def score_with_epochs(
epoch_history: Dict[str, Any],
budget: float,
roi_min: float,
volume_min: float,
max_budget_weighted: float,
require_epoch_preds: bool = False,
verbose: bool = True
):
"""
Score entities using epoch-based history (similar to simulate_epochs.py).
Takes the epoch history matrices and calculates:
1. Trailing aggregates (total volume, total profit, ROI)
2. Latest epoch data (for v_prev, roi_prev)
3. Runs Phase 1 and Phase 2 optimization
4. Returns scores/payouts for each entity
"""
volume_prev_matrix = epoch_history["volume_prev"] # (n_epochs, n_entities)
qualified_prev_matrix = epoch_history["qualified_prev"] # (n_epochs, n_entities)
unqualified_prev_matrix = epoch_history["unqualified_prev"] # (n_epochs, n_entities)
profit_prev_matrix = epoch_history["profit_prev"] # (n_epochs, n_entities)
entity_ids = epoch_history["entity_ids"]
n_entities = epoch_history["n_entities"]
# If no entities, return empty
if n_entities == 0:
return {
"entity_ids": [],
"scores": np.array([]),
"x_opt": np.array([]),
"sol1": None,
"sol2": None
}
"""
We need to collect this epoch's current data.
The qualified volume is then the latest array entry and we
mask the volume by lookin at the trailing ROI.
This concept is designed to gate out traders who don't actually trade
from freeloading in this epoch but also ensure there is no flicker from
traders with no long term signal
"""
# Calculate trailing aggregates (sum across all epochs, axis=0)
total_volume = np.sum(volume_prev_matrix, axis=0)
total_profit = np.sum(profit_prev_matrix, axis=0)
# Calculate trailing ROI for each entity
roi_trailing = np.divide(
total_profit,
np.maximum(total_volume, 1e-12),
out=np.zeros_like(total_profit),
where=total_volume > 0
)
# Check build-up eligibility requirements
build_up_eligible = check_build_up_eligibility(epoch_history)
# Debug: Print build-up eligibility stats
if verbose:
n_build_up_eligible = np.sum(build_up_eligible)
print(f"Build-up eligibility: {n_build_up_eligible}/{n_entities} entities meet build-up requirements")
print(f" - MIN_EPOCHS_FOR_ELIGIBILITY: {MIN_EPOCHS_FOR_ELIGIBILITY}")
print(f" - MIN_TRADES_FOR_ELIGIBILITY: {MIN_PREDICTIONS_FOR_ELIGIBILITY}")
# Combine all eligibility requirements
qual_mask = (
(roi_trailing >= roi_min) &
(qualified_prev_matrix >= volume_min) &
build_up_eligible
)
qual_volume = np.where(qual_mask, qualified_prev_matrix, 0.0)
# Get latest epoch data (last row = most recent epoch, index -1)
current_epoch_idx = epoch_history["n_epochs"] - 1
current_epoch_volume = volume_prev_matrix[current_epoch_idx]
current_epoch_profit = profit_prev_matrix[current_epoch_idx]
current_epoch_roi = np.divide(
current_epoch_profit,
np.maximum(current_epoch_volume, 1e-12),
out=np.zeros_like(current_epoch_profit),
where=current_epoch_volume > 0,
)
#current_epoch_qual_mask = (current_epoch_roi >= roi_min) & (current_epoch_volume >= volume_min)
#current_epoch_qual_volume = np.where(current_epoch_qual_mask, current_epoch_volume, 0.0)
# --- Calculate v_memory from historical data ---
# v_memory is a decaying memory of historical volume
# We need to simulate the decay process from the historical data
v_memory = np.zeros(n_entities)
# Simulate the decay process through all historical epochs
for epoch_idx in range(epoch_history["n_epochs"]):
epoch_volume = volume_prev_matrix[epoch_idx]
v_memory = VOLUME_DECAY * v_memory + epoch_volume
# --- Effective volume: balance fresh vs memory ---
alpha = 1.0 / (2.0 - VOLUME_DECAY) # e.g., decay=0.98 -> alpha≈0.980392
v_eff = alpha * current_epoch_volume + (1.0 - alpha) * v_memory
"""
Kappa computation is a custom function as its a bit complex.
It is broken down in the compute joint kappa function.
"""
kappa_bar = compute_joint_kappa_from_history(epoch_history)
x_prev = np.zeros(n_entities)
last_duals = None
last_alloc = np.zeros(n_entities)
last_tokens = np.zeros(n_entities)
last_scores = np.zeros(n_entities)
"""
A dictionary that contains the critial data for the solvers
"""
p_dict = {
"budget": budget, # alotted budget
"max_budget_weighted": max_budget_weighted, # max budget weighted
"total_volume": np.sum(total_volume), # all trailing volume
"total_q_volume": np.sum(qual_volume), # all trailing volume
"v_prev": qualified_prev_matrix, # qualified volume array
"v_block": current_epoch_volume, # total qualified vol this epoch
"v_memory": v_memory, # decaying volume
"v_eff": v_eff, # History respecting volume
"v_qual": qual_volume, # qualified volume
"block_v_qual": qual_volume, # block qualified volume
"roi_trailing": roi_trailing, # trailing roi
"roi_block": current_epoch_roi, # block roi array
"roi_qual": qual_volume, # qualified volume
"block_roi_qual": qual_volume, # block qualified volume
"epoch_history": epoch_history, # epoch history for build-up checks
# --- CONSTRAINT SETTINGS ---
"roi_min": roi_min, # minimum roi constraint
"v_min": volume_min, # minimum volume constraint
"x_prev": x_prev, # allocations this epoch.
"kappa_bar": kappa_bar, # payout rate
"ramp": RAMP, # allocation delta rate
"rho_cap": RHO_CAP, # max allocation per miner
"require_epoch_preds": require_epoch_preds # require current epoch predictions toggle
}
"""
Solver Phase 1
Objective: maximize routed qualified volume
Method: Choose gates x* that allows a bettor to route volume through
and get paid by the budget for their signal
Return: optimal 0 <= x* <= 1 for all i miners and total qualified volume T*
which is the maximum we can afford to fund with out budget B. Also returns
total payout, number of funded bettors, number of possible eligible bettors
as well as a status print out (see validator_min.py)
"""
sol1 = solve_phase1(p_dict, verbose=verbose)
if sol1["x_star"] is not None:
x_prev = np.clip(sol1["x_star"], 0.0, 1.0)
"""
Solver Phase 2
Objective: Redistribute payout from weak skill to strong skill and adjust gates x* to x**
Method: Choose gates x** by passing in x* to the 2nd problem and minimize the cost
of funding the volume.
Return: optimal 0 <= x** <= 1 for all i miners which minimizes total cost of budget
paid out. Returns new budget and cost vector c* as well.
"""
# --- Phase 2: maximize signal given T* and x* ---
sol2 = None
if sol1["status"] in ("optimal", "optimal_inaccurate") and sol1["T_star"] > 0:
sol2 = solve_phase2(p_dict, x1=sol1["x_star"], T1=sol1["T_star"], verbose=verbose)
if sol2["x_star"] is not None:
x_prev = np.clip(sol2["x_star"], 0.0, 1.0)
# --- store final state for next epoch ---
last_duals = sol2.get("duals", None) if sol2 else None
last_alloc = x_prev.copy()
"""
Miner scoring and allocation
The notion here is use the final optimization values to sum up the scores
of those miners that actually added signal while scoring the
"""
if sol2 is not None:
x_star = np.array(sol2.get("x_star", np.zeros_like(x_prev)))
c_star = np.array(sol2.get("c_star", np.zeros_like(x_prev)))
else:
x_star = np.zeros_like(x_prev)
c_star = np.zeros_like(x_prev)
# emission weight = funded volume × signal strength (ROI)
raw_scores = x_star * c_star
# normalize so total payout == budget
total_score = np.sum(raw_scores)
if total_score > 0:
normalized_scores = raw_scores / total_score
else:
normalized_scores = np.zeros_like(raw_scores)
# convert to token allocations using the actual payout from Phase 2 or Phase 1
if sol2 and sol2.get("payout") is not None:
epoch_payout = sol2["payout"]
elif sol1 and sol1.get("payout") is not None:
epoch_payout = sol1["payout"]
else:
epoch_payout = 0.0
token_allocations = normalized_scores * epoch_payout
if verbose:
# Set numpy print options for cleaner output
np.set_printoptions(precision=8, suppress=True, floatmode='fixed')
print("normalized_scores", normalized_scores)
print("token_allocations", token_allocations)
# Reset to default
np.set_printoptions()
"""
Diagnostics: For every epoch it is important to print out a lot of data
so we can observe the optimizer and ensure it is working
well.
"""
# Spearman
roi = np.maximum(roi_trailing, 0.0)
# Block-level accounting / diagnostics
block_unqualified = unqualified_prev_matrix[current_epoch_idx].sum()
block_qualified = qualified_prev_matrix[current_epoch_idx].sum()
block_fees = VOLUME_FEE * (block_unqualified + block_qualified)
# Historical (all epochs so far)
total_unqualified = np.sum(unqualified_prev_matrix)
total_qualified = np.sum(qualified_prev_matrix)
if verbose:
print(
f"###########################################\n"
f"## [Validator]: epoch {current_epoch_idx}:\n"
f"## Stage 1: Optimization\n"
f"###########################################\n"
f"total_unqualified = {total_unqualified:.2f}, \n"
f"total_qualified = {total_qualified:.2f},\n"
f"block_unqualified = {block_unqualified:.2f}, \n"
f"block_qualified = {block_qualified:.2f},\n"
f"total = {block_unqualified + block_qualified:.2f},\n"
f"fees = {block_fees:.2f},\n"
f"budget = {budget:.2f},\n"
f"status = {sol1['status']},\n"
f"T* = {sol1['T_star']:.2f},\n"
f"net = {block_fees - sol1['payout']:.2f}\n"
f"payout = {sol1['payout']:.2f}\n"
f"numfunded = {sol1['num_funded']:.2f}\n"
f"eligible = {sol1['num_eligible']:.2f}\n"
f"###########################################\n"
f"\n"
f"###########################################\n"
f"[Validator]: epoch {current_epoch_idx}:\n"
f"Stage 2: Optimization\n"
f"###########################################\n"
f"status = {sol2['status'] if sol2 else 'n/a'},\n"
f"Payout* = {sol2['payout'] if sol2 else 0.0:.2f},\n"
f"Phase 1 ρ(ROI, x*) = {spearmanr(roi, sol1['x_star'])[0]},\n"
f"Phase 2 ρ(ROI, x**) = {spearmanr(roi, sol2['x_star'])[0] if sol2 else 'n/a'},\n"
)
return {
"entity_ids": entity_ids,
"scores": normalized_scores,
"tokens": token_allocations,
"sol1": sol1,
"sol2": sol2,
"total_volume": total_volume,
"total_profit": total_profit,
"roi_trailing": roi_trailing,
"kappa_bar": kappa_bar
}
def solve_phase1(p, verbose=False):
"""
Phase 1: Maximize routed qualified volume given budget & payout constraints.
"""
#########################################
## Historical data and size
#########################################
v_prev, v_eff ,v_block, roi_trailing, x_prev = p["v_prev"], p["v_eff"], p["v_block"], p["roi_trailing"], p["x_prev"]
n = v_block.size
#########################################
## Numerical scaling
## First we have to make a scale so the
## optimizer can properly find an interior
## point
#########################################
B = p["budget"] # budget size
v_memory = p.get("v_memory", v_prev) # decaying volume
v_block = np.maximum(v_block, 1e-12) # block volume
c = v_eff*roi_trailing # costs
## settings
kappa = p["kappa_bar"] # dimensionless
rho_cap = p.get("rho_cap", RHO_CAP) # diversity
ramp = p.get("ramp", RAMP) # ramp
#########################################
## Eligibility Gates
## We then have to generate a cost per units
## of signal and then using trailing volume
## make a boolean which flags eligibility
#########################################
# Get build-up eligibility from the epoch history
epoch_history = p.get("epoch_history")
if epoch_history is not None:
build_up_eligible = check_build_up_eligibility(epoch_history)
else:
# Fallback: assume all are eligible if no epoch history provided
build_up_eligible = np.ones(len(roi_trailing), dtype=bool)
# require_epoch_preds: If True, require current epoch predictions (v_block) for eligibility
require_epoch_preds = p.get("require_epoch_preds", False)
if require_epoch_preds:
# Require current epoch predictions - use v_block (total volume this epoch)
eligible = (
(roi_trailing >= p["roi_min"]) &
(v_block >= p["v_min"]) &
build_up_eligible
).astype(float)
else:
# Use decaying volume memory - only use v_memory (historical volume with decay)
eligible = (
(roi_trailing >= p["roi_min"]) &
(v_memory >= p["v_min"]) &
build_up_eligible
).astype(float)
if verbose:
print({"kappa": kappa})
print({"roi_trailing": roi_trailing*eligible})
# 3) Decision variable
x = cp.Variable(n)
# 4) Constraints
eps = 1e-9
cons = []
cons += [x >= 0, x <= 1] # gates
cons += [kappa * (v_eff @ x) <= B] # budget
cons += [x <= eligible] # eligibility
cons += [kappa * v_eff[i] * x[i] <= rho_cap*B for i in range(n)] # diversity
#TODO: these might no longer be needed at all.
#cons += [c @ x <= kappa * (v_eff @ x + eps)] # payout/volume cap
#cons += [x - x_prev <= ramp, x_prev - x <= ramp] # ramp
#cons += [x >= 1e-2*eligible] # dust constraint
#cons += [c[i] * x[i] <= rho_cap * B for i in range(n)] # diversity cap
# 5) Objective
prob = cp.Problem(cp.Maximize(v_block@x), cons)
prob.solve(solver=cp.ECOS, verbose=False)
# 6) === Dual diagnostics table ===
if prob.status in ("optimal", "optimal_inaccurate"):
if verbose:
print("\n====================[ Phase 1 Dual Summary ]====================")
labels = [
("x >= 0", cons[0]),
("x <= 1", cons[1]),
("budget cap", cons[2]),
("x <= eligible", cons[3]),
]
# Diversity group (vector of n)
diversity_cons = cons[4:]
rows = []
for name, cstr in labels:
if hasattr(cstr, "dual_value") and cstr.dual_value is not None:
duals = np.array(cstr.dual_value, dtype=float)
mag = np.max(np.abs(duals))
rows.append((name, mag))
else:
rows.append((name, 0.0))
# Handle diversity separately
mags = []
for c in diversity_cons:
if hasattr(c, "dual_value") and c.dual_value is not None:
mags.append(np.max(np.abs(np.array(c.dual_value, dtype=float))))
rows.append(("diversity (all)", float(np.max(mags)) if mags else 0.0))
# Pretty print summary
print(f"{'Constraint':35s} | {'Dual Magnitude':>15s}")
print("-" * 55)
for name, mag in rows:
marker = "⛔" if mag > 1e-6 else " "
print(f"{name:35s} | {mag:15.6f} {marker}")
print("=" * 55 + "\n")
# 6) Return
x_star = None if x.value is None else x.value.copy()
T_star = float(v_block @ x_star) if x_star is not None else 0.0
payout = float(np.dot(v_block * np.maximum(roi_trailing, 0.0), x_star)) if x_star is not None else 0.0
num_funded = int(np.sum((v_block * np.maximum(roi_trailing, 0.0) * x_star) > 0)) if x_star is not None else 0
num_eligible = int(np.sum(eligible))
return {
"status": prob.status,
"T_star": T_star, # in the same units as your printed "qualified"
"x_star": x_star,
"payout": payout,
"num_funded": num_funded,
"num_eligible": num_eligible,
}
def solve_phase2(p, x1, T1, verbose=False):
"""
Phase 2: Redistribute fixed payout to favor higher ROI while staying
close to x1 (Phase 1 gates).
"""
v = p["v_block"]
n = v.size
v_eff = p["v_eff"]
roi = p["roi_trailing"]
B = p["budget"]
max_budget = p.get("max_budget_weighted", B) if p.get("max_budget_weighted") is not None else B # default max_budget to budget if not provided
kappa = p["kappa_bar"]
# Token cost per miner if fully opened
c = kappa * v_eff
##eligibility
# Get build-up eligibility from the epoch history
epoch_history = p.get("epoch_history")
if epoch_history is not None:
build_up_eligible = check_build_up_eligibility(epoch_history)
else:
# Fallback: assume all are eligible if no epoch history provided
build_up_eligible = np.ones(len(roi), dtype=bool)
# require_epoch_preds: If True, require current epoch predictions (v_block) for eligibility
require_epoch_preds = p.get("require_epoch_preds", False)
if require_epoch_preds:
# Require current epoch predictions - use v_block (total volume this epoch)
eligible = (
(roi >= p["roi_min"]) &
(v >= p["v_min"]) &
build_up_eligible
).astype(float)
else:
# Use decaying volume memory - only use v_eff (historical volume with decay)
eligible = (
(roi >= p["roi_min"]) &
(v_eff >= p["v_min"]) &
build_up_eligible
).astype(float)
# Total payout from Phase 1 (fixed)
P1 = float(np.dot(c, x1))
# signed ROI deviation from kappa
delta = roi - kappa
# weight positive deltas (above kappa) more, negative ones less
w = delta
# Decision variable
x = cp.Variable(n)
# Constraints
cons = []
cons += [x >= 0, x <= 1]
cons += [x <= eligible] # eligibility
cons += [c @ x <= min(P1, B, max_budget)] # fixed total payout (ensured not greater than budget or max budget weighted)
# Objective: favor ROI while staying close to prior gates
# dynamic smoothness penalty λ
roi_spread = np.std(p["roi_trailing"])
vol_mean = np.mean(p["v_eff"])
# dynamic smoothness penalty λ. higher value means more smoothness, less volatility.
lam = 5
if verbose:
print({"watx": w @ x, "scale": lam * cp.sum_squares(x - x1)})
obj = cp.Maximize(w @ x - lam * cp.sum_squares(x - x1))
prob = cp.Problem(obj, cons)
prob.solve(solver=cp.ECOS, verbose=False)
if x.value is not None:
dx = x.value - x1
s1 = spearmanr(roi, x1)[0]
s2 = spearmanr(roi, x.value)[0]
if verbose:
print("Δx mean:", float(np.mean(np.abs(dx))))
print("Spearman Δ:", float(s2 - s1))
print("ρ(ROI, x**):", float(s2))
return {
"status": prob.status,
"x_star": None if x.value is None else x.value.copy(),
"c_star": c,
"payout": min(P1, B, max_budget), # ensure payout is not greater than budget or max budget weighted
}
"""
Function: compute_joint_kappa
Purpose: Computes the value of kappa as an endogenous variable that depends
on previous volume and roi. The goal is to restrict the payout rate
of the budget by setting the “exchange rate” between qualified flow and token budget.
"""
def compute_joint_kappa_from_history(epoch_history: Dict[str, Any], lookback=ROLLING_HISTORY_IN_DAYS, smooth=0.3, fee_rate=VOLUME_FEE, kappa_next=KAPPA_NEXT, joint_kappa=None) -> float:
"""
Compute kappa_bar from epoch history matrices.
κ_next from *aggregates* over the last `lookback` epochs:
κ = (sum fees over active epochs) / (sum positive profit over active epochs)
Active epoch = total volume>0 and total profit>0.
No per-epoch division => no 1/0 explosions. Uses prior κ if not enough signal.
"""
v_prev = epoch_history["qualified_prev"]
profit_prev = epoch_history["profit_prev"]
prev_kappa = kappa_next
if v_prev.shape[0] < 5:
return prev_kappa
V_hist = v_prev[-lookback:]
P_hist = profit_prev[-lookback:]
vols = np.sum(V_hist, axis=1) # per-epoch total volume
prof = np.sum(P_hist, axis=1) # per-epoch total profit
# --- active epochs: positive profit and nonzero volume ---
active = (vols > 0) & (prof > 0)
if np.count_nonzero(active) < max(3, lookback // 10):
return prev_kappa
fees_sum = fee_rate * float(np.sum(vols[active]))
posprofit_sum= float(np.sum(prof[active])) # strictly >0 by mask
# aggregate ratio; guarded but should be safe due to mask