a
    ”Jej¬  ã                   @   st   d Z ddlZddlZej dd¡ ddlZdZg d¢ZdZ	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ Zedkrpeƒ  dS )aY  
Does xlow's ACTUAL mechanism work? (read-only, trades nothing)

THE CLAIM xlow rests on -- which nothing tested before today:
  xlow does NOT require Pinnacle to be sharper than the exchanges. It requires
  that when ONE exchange is out of line with BOTH Pinnacle and the OTHER
  exchange, that exchange is the one that is wrong. Pinnacle + the agreeing
  exchange form a 2-source consensus; the outlier is the bet.

This matters because book_accuracy_compare.py found Pinnacle has no measurable
AVERAGE edge over Kalshi/Poly (paired log loss, all null). That result was read
as fatal to xlow. It is not: two forecasters can be identical on average and
still be reliably wrong in specific, identifiable configurations. Average
sharpness and outlier-reversion are different claims, and only the second one is
xlow's.

THE TEST
  For every fixture-outcome where the two exchanges disagree and the CHEAPER one
  sits below Pinnacle while the dearer one does NOT (i.e. Pinnacle sides with the
  dearer book -- exactly xlow's shape), ask: does the outcome happen at the
  CONSENSUS rate or at the OUTLIER's rate?
    - lands near consensus -> the outlier was mispriced -> the mechanism works
    - lands near the outlier -> the outlier knew something -> mechanism is dead

  Selection uses PRICES ONLY, never the result. Every book is read at the LAST
  snapshot where all three quote, so no source gets a timing advantage.

  Split at FREEZE_DATE. The [5,12) band was chosen on pre-freeze data, so
  in-sample rows are not evidence -- that mistake was made once today already.

Usage:  python research\outlier_reversion_test.py
é    NzE:\soccer\researchz
2026-07-05))g{®Gáz”?çš™™™™™©?©r   ç¸…ëQ¸¾?)r   ç      Ð?)r   ç      ð?r   c                 C   sH   t | d ƒD ]6}d|v rd|v rd|v r|d |d |d f  S qdS )z;Last snapshot where pinnacle AND kalshi AND poly all quote.ZsnapsZpinnacleZkalshiZpolyN)Úreversed)ÚfxÚd© r
   ú,E:\soccer\research\outlier_reversion_test.pyÚtriples,   s    r   c                 C   s  g }| D �]}||d pdƒs qt |ƒ}|s.q|\}}}tdƒD ]Ö}|| dkrRq@|| || krr|| || fn|| || f\}	}
|	|
kr�q@|| |	 ||  }|| |
 ||  }|dkrÂq@|td krÐq@|| |
 d }| t||	||| |
tj|d  |k�rdndd�¡ q@q|S )	zCOne record per qualifying fixture-outcome. Outcome-blind selection.ÚgdÚ é   r   g       @Zftré   )ÚrelÚoutlierÚ	consensusÚpinÚdearÚhit)r   ÚrangeÚ	XLOW_BANDÚappendÚdictÚBZIDX)ÚfixturesZkeepÚoutr   Útr   ÚkÚpÚiZcheapr   Z	rel_cheapZrel_dearr   r
   r
   r   Úcollect4   s4    

4þr"   c           
         s¼  t d|› �ƒ | st dƒ d S t ddd›ddd›dd	d
›ddd›ddd
›ddd›ddd›d�ƒ tD �]P\‰‰ ‡ ‡fdd„| D ƒ}t|ƒ}|dk r¶t dˆd›dˆ d›d|d›d�ƒ qdtdd„ |D ƒƒ‰dd„ |D ƒ}dd„ |D ƒ}‡fdd„}ˆ| }t|t|ƒ|  ƒt|t|ƒ|  ƒk �r&d nd	}ˆˆ ftk�r<d!nd"}	t dˆd›dˆ d›d|d›dt|ƒ| d# d$›d%t|ƒ| d# d&›d%|d# d$›d%||ƒd'›d||ƒd(›d|› |	› �ƒ qdd S ))NÚ
z  (no rows)ú  Z	deviationZ12sú Únz>5sr   z>8sr   z>10sZACTUALZz_vs_outz>9sZ	z_vs_consz  who was rightc                    s,   g | ]$}ˆ|d    kr ˆ k rn q|‘qS )r   r
   ©Ú.0Úr)ÚhiÚlor
   r   Ú
<listcomp>Z   ó    zreport.<locals>.<listcomp>é   z.2fú-z    Z5dz  (too few)c                 s   s   | ]}|d  V  qdS )r   Nr
   r'   r
   r
   r   Ú	<genexpr>_   r-   zreport.<locals>.<genexpr>c                 S   s   g | ]}|d  ‘qS )r   r
   r'   r
   r
   r   r,   `   r-   c                 S   s   g | ]}|d  ‘qS )r   r
   r'   r
   r
   r   r,   a   r-   c                    s8   t dd„ | D ƒƒ}|dkr0ˆ t | ƒ t |¡ S tdƒS )Nc                 s   s   | ]}|d |  V  qdS ©r   Nr
   )r(   Úxr
   r
   r   r0   d   r-   z$report.<locals>.z.<locals>.<genexpr>r   Únan)ÚsumÚmathZsqrtÚfloat)ZpsÚvar)Úhitsr
   r   Úzc   s    zreport.<locals>.zZ	CONSENSUSz  <-- xlow bandr   éd   z7.2fz% z9.2fz+9.2fz+10.2f)ÚprintÚBANDSÚlenr4   Úabsr   )
ÚrowsÚlabelÚsr&   ÚoÚcr9   ZactZnearZstarr
   )r*   r8   r+   r   ÚreportR   sN    $ÿÿÿ
".ÿ0ÿÿÿÿþþrD   c                 C   sz   dd„ | D ƒ}t |ƒ}|dk r:td|d›d|› d�ƒ dS td	d
„ |D ƒƒ| }td|d›d|d›d|d d›d�ƒ dS )z7ROI of buying the outlier at its own price, mid-to-mid.c                 S   s4   g | ],}t d  |d   kr(t d k rn q|‘qS )r   r   r   )r   r'   r
   r
   r   r,   r   r-   zeconomics.<locals>.<listcomp>r.   r$   Z28sz n=z too fewNc                 s   s*   | ]"}|d  rd|d  d ndV  qdS )r   r   r   g      ð¿Nr
   r'   r
   r
   r   r0   w   r-   zeconomics.<locals>.<genexpr>Z4dz  ROI@mid = r:   z+7.1fú%)r=   r;   r4   )r?   r@   rA   r&   Zroir
   r
   r   Ú	economicsp   s    rF   c                  C   s¼   t  ¡ } tdd„ | D ƒƒ}td|› �ƒ t| dd„ ƒ}t| dd„ ƒ}|| }t|dƒ t|dt› d	�ƒ t|d
t› d�ƒ tdƒ t|dƒ t|dƒ t|dƒ tdƒ tdƒ tdƒ d S )Nc                 s   s   | ]}t |ƒrd V  qdS r1   )r   )r(   Úfr
   r
   r   r0   }   r-   zmain.<locals>.<genexpr>z=fixtures with Pinnacle + Kalshi + Poly at a shared snapshot: c                 S   s   | t kS ©N©ÚFREEZE©Úgr
   r
   r   Ú<lambda>€   r-   zmain.<locals>.<lambda>c                 S   s   | t kS rH   rI   rK   r
   r
   r   rM   �   r-   z;ALL DATA (in-sample + out-of-sample pooled -- context only)zIN-SAMPLE (<= z+) -- the band was chosen here, NOT evidencezOUT-OF-SAMPLE (> z) -- this is the real testz9
ECONOMICS in the xlow band (buy the outlier at its mid):z	in-samplezout-of-sampleZpooledzF
READ: actual landing near CONSENSUS = the outlier was mispriced = thezEmechanism works. Landing near the OUTLIER = the cheap book was right.z)Only the out-of-sample block is evidence.)r   Úloadr4   r;   r"   rD   rJ   rF   )r   ZbothZinsZoosZallrr
   r
   r   Úmain{   s     



rO   Ú__main__)Ú__doc__Úsysr5   ÚpathÚinsertZbook_accuracy_comparer   rJ   r<   r   r   r"   rD   rF   rO   Ú__name__r
   r
   r
   r   Ú<module>   s    