60 lines
3.9 KiB
Python
60 lines
3.9 KiB
Python
"""Proposed percentile -> cost/power/toughness derivation, evaluated on card-sample.json.
|
|
Usage: python3 -I stats.py <card-sample.json>"""
|
|
import json, sys, collections, statistics, itertools
|
|
d = json.load(open(sys.argv[1])); cards = d['cards']; P = d['percentiles']
|
|
def pct(s, v): # DESIGN 1.1 counting: cut points strictly below + half of equal
|
|
if v is None: return None
|
|
cut = P[s]; return int(sum(c < v for c in cut) + sum(c == v for c in cut) / 2)
|
|
TH = [26, 39, 51, 63, 78] # v1.1 costThresholds (fame -> cost 1..6)
|
|
BUDGET = [None, 3, 4, 6, 8, 10, 12] # P+T per cost, MTG vanilla curve (2/1, 2/2, 3/3, 4/4, 5/5, 6/6)
|
|
def derive(c):
|
|
a, dp, r = (pct(k, c['stats'][k]) for k in ('attention', 'depth', 'reach'))
|
|
vals = [x for x in (a, dp, r) if x is not None]
|
|
fame = int(sum(vals) / len(vals) + 0.5)
|
|
cost = 1 + sum(t <= fame for t in TH)
|
|
b = BUDGET[cost]
|
|
kw = 'Multilingual' if r >= 75 else ('Local' if c['stats']['reach'] == 0 else None)
|
|
if kw == 'Multilingual': b -= 1 # evasion-like keyword is paid for out of the body
|
|
if kw == 'Local': b += 0 # Local = gains +1/+1 next to a same-country Location (conditional, no tax)
|
|
share = (a if a is not None else 50) / max(1, (a if a is not None else 50) + dp) # attention vs depth
|
|
pw = min(b - 1, max(1, round(b * share))); t = b - pw
|
|
return dict(title=c['title'], a=a, d=dp, r=r, fame=fame, cost=cost, P=pw, T=t, kw=kw,
|
|
trend=c['pageviews']['trendPercent'], daily=c['pageviews']['dailyAverage'])
|
|
rows = [derive(c) for c in cards]; n = len(rows)
|
|
def naive(c): # straw man: power = attention tier, toughness = depth tier, cost from their sum
|
|
ti = c['tiers']; pw = (ti['attention'] or 1 + 1) // 2 + 0; t = (ti['depth'] + 1) // 2
|
|
return dict(cost=max(1, min(6, round((pw + t) / 2))), P=pw, T=t)
|
|
nv = [naive(c) for c in cards]
|
|
def dominated_pairs(rs, use_kw=False):
|
|
k = 0
|
|
for x, y in itertools.permutations(rs, 2):
|
|
if x['cost'] <= y['cost'] and x['P'] >= y['P'] and x['T'] >= y['T'] and \
|
|
(x['cost'] < y['cost'] or x['P'] > y['P'] or x['T'] > y['T']) and (not use_kw or x.get('kw') == y.get('kw')):
|
|
k += 1
|
|
return k
|
|
print('cost distribution', sorted(collections.Counter(r['cost'] for r in rows).items()))
|
|
print('power distribution', sorted(collections.Counter(r['P'] for r in rows).items()))
|
|
print('toughness distribution', sorted(collections.Counter(r['T'] for r in rows).items()))
|
|
print('keywords', collections.Counter(r['kw'] for r in rows))
|
|
print('P/T by cost:')
|
|
for cst in range(1, 7):
|
|
g = [r for r in rows if r['cost'] == cst]
|
|
if g: print(' ', cst, len(g), collections.Counter(f"{r['P']}/{r['T']}" for r in g).most_common(6))
|
|
pairs = n * (n - 1)
|
|
print('strict-dominance pairs proposed', dominated_pairs(rows), 'of', pairs, '; same-keyword only', dominated_pairs(rows, True))
|
|
print('strict-dominance pairs naive ', dominated_pairs(nv), 'of', pairs)
|
|
# efficiency by fame quintile
|
|
q = sorted(rows, key=lambda r: r['fame'])
|
|
print('fame quintile: mean cost, mean (P+T)/cost proposed | naive (P+T)/cost')
|
|
idx = sorted(range(n), key=lambda i: rows[i]['fame'])
|
|
for k in range(5):
|
|
sl = idx[k * n // 5:(k + 1) * n // 5]
|
|
print(f" Q{k+1} fame {rows[sl[0]]['fame']}-{rows[sl[-1]]['fame']}: cost {statistics.mean(rows[i]['cost'] for i in sl):.2f} "
|
|
f"eff {statistics.mean((rows[i]['P']+rows[i]['T'])/rows[i]['cost'] for i in sl):.2f} | naive eff "
|
|
f"{statistics.mean((nv[i]['P']+nv[i]['T'])/nv[i]['cost'] for i in sl):.2f}")
|
|
top = sorted(rows, key=lambda r: -r['fame'])[:6] + sorted(rows, key=lambda r: r['fame'])[:3]
|
|
print('examples (top fame, bottom fame):')
|
|
for r in top: print(f" {r['title'][:34]:34s} a{r['a']} d{r['d']} r{r['r']} fame{r['fame']} cost{r['cost']} {r['P']}/{r['T']} {r['kw'] or ''}")
|
|
tr = [r for r in rows if r['trend'] is not None and r['trend'] >= 25 and (r['daily'] or 0) >= 20]
|
|
print('trending (+25% and daily>=20):', len(tr), f'{100*len(tr)/n:.1f}%')
|