use [df_15]




gen winsHB=(invlogit(u_map)-.5)*162
gen winsHB_se=(invlogit(u_sd)-.5)*162
gen pHB_se=winsHB/162
rename as_of_year yearID

save alltheta_mwar_hb.dta,replace

	
	
clear


	use   [DF_1], clear
	


	gen byte w = result=="W"
	gen byte l = result=="L"

	rename team_runs     rs
	rename opponent_runs ra

	collapse (sum) w l rs ra, by(retroID name_last name_first yearID teamID)
	drop if missing(retroID) | retroID==""

	gen games = w + l


	gen mgrID = retroID
	
	
	
	merge m:1 mgrID yearID using mgr_patch_2025.dta,nogen

	* collapse duplicate manager-seasons (sum stints)
	collapse (sum) w l rs ra, by(mgrID yearID name_first name_last teamID)
	gen games = w + l

	* keep essentials
	keep mgrID name_first name_last yearID teamID w l games 




merge m:1 yearID mgrID using alltheta_mwar_hb.dta,nogen


drop if w==.


* 0. have: mgrID yearID games winsHB winsHB_lo winsHB_hi
sort mgrID yearID

* 1. make sure games is 0, not missing, before cumulating
by mgrID: replace games = 0 if missing(games)

* 2. cumulative games per manager
by mgrID: gen cum_games = sum(games)





* ---------- ECDF: games to cross for 3 thresholds in one plot ----------
preserve

tempfile cross_ecdf
clear
save `cross_ecdf', emptyok

foreach t in 1 1.5 2 {

    restore, preserve   // go back to full data for each threshold

    local career_score = 2
    local learn_pt     = `t'
    local min_games    = 162
    local confirm      = 0

    keep mgrID yearID cum_games winsHB
    drop if missing(mgrID, yearID, cum_games, winsHB)
    bysort mgrID yearID (cum_games): keep if _n==_N

    bysort mgrID (yearID): gen double final_winsHB = winsHB[_N]
    keep if abs(final_winsHB) >= abs(`career_score')

    bysort mgrID: gen byte mgr_positive = (final_winsHB[_N] > 0)

    gen byte crossed = .
    replace crossed = (winsHB >  abs(`learn_pt') & cum_games >= `min_games') if mgr_positive==1
    replace crossed = (winsHB < -abs(`learn_pt') & cum_games >= `min_games') if mgr_positive==0

    if `confirm' == 0 {
        bysort mgrID (cum_games): gen byte first_cross = crossed==1 & (_n==1 | crossed[_n-1]==0)
    }
    else {
        bysort mgrID (cum_games): gen byte first_cross = 0
        by mgrID: gen int n=_N
        bysort mgrID (cum_games): gen int idx=_n
        forvalues k=0/`confirm' {
            bysort mgrID (cum_games): gen byte c`k' = crossed[_n+`k']
        }
        bysort mgrID (cum_games): replace first_cross = c0
        forvalues k=1/`confirm' {
            bysort mgrID (cum_games): replace first_cross = first_cross & c`k'
        }
        bysort mgrID (cum_games): replace first_cross = first_cross==1 & (_n==1 | first_cross[_n-1]==0)
        drop c*
    }

    bysort mgrID (cum_games): egen double games_to_learn_pt = ///
        min(cond(first_cross, cum_games, .))

    bysort mgrID: keep if _n==1
    drop if missing(games_to_learn_pt)

    sort games_to_learn_pt
    gen double ecdf = _n/_N
    gen double thresh = `t'

    keep games_to_learn_pt ecdf thresh
    append using `cross_ecdf'
    save `cross_ecdf', replace
}

use `cross_ecdf', clear
drop if games_to_learn>2250
twoway ///
(connected ecdf games_to_learn_pt if thresh==1,   lcolor(blue)  lpattern(solid) msymbol(o) mcolor(blue)  mlwidth(0) msize(small)) ///
(connected ecdf games_to_learn_pt if thresh==1.5, lcolor(red)   lpattern(solid) msymbol(o) mcolor(red)   mlwidth(0) msize(small)) ///
(connected ecdf games_to_learn_pt if thresh==2,   lcolor(green) lpattern(solid) msymbol(o) mcolor(green) mlwidth(0) msize(small)) ///
    , ///
    xtitle("Games") ///
    ytitle("Cumulative fraction of managers (≤ x)") ///
    yscale(range(0 1)) ylabel(0(.25)1, angle(0) labsize(8pt) nogrid) ///
    xlabel(150(150)2100, labsize(8pt) angle(45) nogrid) ///
    legend(off) yline(0.5, lwidth(thin) lpattern(dash) lcolor(black)) ///
    graphregion(color(white)) ytitle("") xtitle("")  xsize(3) ysize(2.4)
	
	
	sum games_to_learn_pt if thresh==1,d
	sum games_to_learn_pt if thresh==1.5,d
	sum games_to_learn_pt if thresh==2,d

restore

* Vector exports (pick what you need)
graph export "counts1.pdf", replace       
graph export "counts1.eps", replace      
graph export "counts1.svg", replace       
graph export "counts1.emf", replace       




* ---------- ECDF: games to learn (p ≥ .7) for 3 thresholds ----------
preserve

tempfile learn_ecdf
clear
save `learn_ecdf', emptyok

foreach t in 1 1.5 2 {

    restore, preserve

    local career_score = 2
    local learn_pt     = `t'
    local conf         = 0.70

    keep mgrID yearID cum_games winsHB winsHB_se
    drop if missing(mgrID, yearID, cum_games, winsHB)

    bysort mgrID (cum_games): gen double final_winsHB = winsHB[_N]
    keep if abs(final_winsHB) >= abs(`career_score')

    bysort mgrID: gen byte mgr_positive = (final_winsHB[_N] > 0)

    gen double pr_target = .
    replace pr_target = 1 - normal((abs(`learn_pt') - winsHB)/winsHB_se) ///
        if mgr_positive==1 & winsHB_se>0 & winsHB_se<.
    replace pr_target = normal((-abs(`learn_pt') - winsHB)/winsHB_se) ///
        if mgr_positive==0 & winsHB_se>0 & winsHB_se<.

    bysort mgrID (cum_games): gen byte crossedP = pr_target >= `conf'
    bysort mgrID (cum_games): gen byte first_crossP = ///
        crossedP==1 & (_n==1 | crossedP[_n-1]==0)

    bysort mgrID (cum_games): egen double games_to_learn_pr = ///
        min(cond(first_crossP, cum_games, .))

    bysort mgrID: keep if _n==1
    drop if missing(games_to_learn_pr)

    sort games_to_learn_pr
    gen double F = (_n - 0.5) / _N
    gen double thresh = `t'

    keep games_to_learn_pr F thresh
    append using `learn_ecdf'
    save `learn_ecdf', replace
}

use `learn_ecdf', clear

twoway ///
    (connected F games_to_learn_pr if thresh==1,   lcolor(blue)  lpattern(solid) msymbol(o) mcolor(blue)  mlwidth(0) msize(small)) ///
    (connected F games_to_learn_pr if thresh==1.5,lcolor(red)   lpattern(solid) msymbol(o) mcolor(red)   mlwidth(0) msize(small)) ///
    (connected F games_to_learn_pr if thresh==2,  lcolor(green) lpattern(solid) msymbol(o) mcolor(green) mlwidth(0) msize(small)) ///
	, ///
    xtitle(" ") ///
    ytitle("") ///
    yscale(range(0 1)) ylabel(0(.25)1, angle(0) labsize(8pt) nogrid) ytitle("") ///
    xlabel(300(300)4200, angle(45) labsize(8pt) nogrid) yline(0.5, lw(thin) lpattern(dash) lcolor(black)) ///
    legend(off) ///
    graphregion(color(white))   xsize(3) ysize(2.4)
	
	sum games_to_learn_pr if thresh==1,d
	sum games_to_learn_pr if thresh==1.5,d
	sum games_to_learn_pr if thresh==2,d

restore

* Vector exports (pick what you need)
graph export "counts2.pdf", replace       
graph export "counts2.eps", replace       
graph export "counts2.svg", replace      
graph export "counts2.emf", replace       
restore



gen pHB_se=invlogit(u_sd)-.5
gen pHB=invlogit(u_mean)-.5


keep mgrID cum_games winsHB pHB pHB_se

export delimited using C:\Users\dmk38\Documents\x5\career_evo_mwar_hb_12.13.csv,nolabel replace

