
    j                       d Z ddlmZ ddlZddlZddlZddlmZ ddlZ	 ee
      j                         j                  j                  Zej                  j                  d ee             ddlmZmZmZmZ ddlmZmZmZmZmZ dZdgd	g ee      dd	ged
Zdddd	 	 	 	 	 	 	 	 	 ddZddZ e!dk(  r e" e              y)zOResearch-only matched-control and simple classifier study for extreme BTC bars.    )annotationsN)Path)TailRule#moving_block_bootstrap_matched_lifttail_classifier_metrics#volatility_matched_downside_profile)DISCOVERY_END	OOS_STARTVALIDATION_START_windowcausal_event_frame)bb_pct_b_norm	sopr_normosc_norm	mlp_score	rvol_norm)r   rvoldownside_triocombined)g?皙?g333333?         @)tail_fractionsbootstrap_repetitions	bar_hoursc               F   t        j                  |       }|j                  j                  j	                         j                  j                         |_        t        t        j                  ddgt                    }t        |||      }t        |d t              }t        |t        t        t        j                  d      z
        }t        |t        d       }	t        D 
ci c]   }
|
t!        ||
   j#                  d            " }}
t        D 
cg c]  }
t%        |
d||
          }}
|D ci c]]  }t        |      |D ci c]A  }|j&                  |||	dj)                         D ci c]  \  }}|t+        |||	       c}}C c}}}_ }}}}}|D ci c]B  }|j&                  |||	dj)                         D ci c]  \  }}|t-        ||d|
       c}}D }}}}i }t/        d   j0                  j2                  j5                         j7                               D ]}  }t        |t        j8                  | d      t        j8                  | d            }t;        |      dk  rK|D ci c]  }|j&                  t+        ||d	       c}|t        |      <    t<        j)                         D ci c]%  \  }}|t?        |||d      t?        ||	|d      d' }}}t        |       d|dddt        |      d| dddt;        |      t;        |      t;        |	      d|D cg c]&  }|j&                  |j@                  |jB                  d( c}||||dS c c}
w c c}
w c c}}w c c}}}w c c}}}}w c c}}w c c}}}w c c}w c c}}w c c}w )Nr   r   )r      )nanosecondsr   low)	discovery
validationoos)tail_fraction)r#   repetitionstimez-01-01z-12-31 23:59:59d   )featuresr#   )r!   r"   z(values at t; outcome is contiguous next gzh close-to-close returnu;   calendar-year × realized-volatility decile standardizationzUdiscovery-trained L2 logistic baseline; relative 10% tails in later reporting windowszV deterministic moving-block resamples (28 six-hour bars) for matched 10% downside liftzXcalendar-year 10% relative tails using discovery-fixed rules; descriptive stability only)
predictorsmatched_controlr   
classifier	bootstrapchronological_slices)featuresidecut)	data_path
definitionwindowsdownside_rulesvolatility_matched_downside bootstrap_matched_downside_10pct$calendar_year_matched_downside_10pctr+   )"pdread_csvcolumnsstrlowerstriplistdictfromkeysRULE_FEATURESr   r   r	   r   r
   	Timedeltafloatquantiler   r.   itemsr   r   sorteddtyeardropnaunique	TimestamplenMODEL_FEATURE_SETSr   r/   r0   )r1   r   r   r   datar'   framer    r!   r"   r.   cutsrulestailrulewindowprofilesvaluesr,   chronologicalrH   yearlynameclassifierss                           )tools/run_mlp_extreme_tail_model_study.pyrunr\       s    ;;y!D<<##))+//557DLDMM;"Lm"LMNHtXCEt]3I 0)bllWX>Y2YZJ
%D
)CMZ[]'GU9W-66t<==]D[DQRMXgud7m4MER #	 #D 	D	
 

 	 LL3<J_b%c%i%i%k%kMFE ;E4W[\\%k  
 	
 #  	&  D 	 1:\_"`"f"f"h	
 #i 7D>S  #i	
 	
    35MuV}'',,335<<>?vV_ =r||tfTcLd?efv; $
 LL=fdZ^__$
c$i 	 @ 1668
 9ND(	 	1)ZRZjno*9cH\`a
 	
 9   ^DYqMQhi\">2q12  3I  J$~
 "%Ys:WZ[^W_`ejkej]at||TYYtxxXejk'/,50=! Q \R
	
$

$ ls`   !%M'M,0M>
'M7,M1 M7M>
'NNN7 N?*N3+N
1M77M>
Nc                 V   t        j                  t              } | j                  dt        d       | j                  dt               | j                  dt
        d	       | j                  d
t        dd       | j                         }t        |j                  |j                  |j                        }t        j                  |d      }t        |       |j                  rE|j                  j                   j#                  dd       |j                  j%                  |dz          y)N)descriptionz--dataT)typerequiredz--output)r_   z--bootstrap-repetitionsr   )r_   defaultz--bar-hoursr   zBExpected chart-bar duration; labels crossing >1.5 bars are dropped)r_   ra   help)r   r      )indent)parentsexist_ok
r   )argparseArgumentParser__doc__add_argumentr   intrC   
parse_argsr\   rN   r   r   jsondumpsprintoutputparentmkdir
write_text)parserargsresulttexts       r[   mainry   i   s    $$9F
td;

.
1SI
E3  FJ  KD$2L2LX\XfXfgF::fQ'D	$K{{   =td{+    __main__)
r1   r   r   ztuple[float, ...]r   rl   r   rC   returnzdict[str, object])r|   rl   )#rj   
__future__r   rh   rn   syspathlibr   pandasr8   __file__resolverr   REPOpathinsertr;   "strategies.mlp_tail_event_researchr   r   r   r   %tools.run_mlp_extreme_bar_event_studyr	   r
   r   r   r   rA   r>   rM   r\   ry   __name__
SystemExit rz   r[   <module>r      s    U "   
  H~&&-- 3t9   z y ;M-(k:M:	  );!$FF &F 	F
 F FR  z
TV
 rz   