"""Case 003: separation and Firth penalized likelihood, NumPy + SciPy.
Self-contained deterministic 40-observation example. No CSV needed.
Uses the exact two-group Firth likelihood for this saturated binary model.
The +1/2 terms follow the Jeffreys information penalty in this special case;
they are not an arbitrary correction or a general Firth-regression routine.
"""
import numpy as np
import scipy
from scipy.optimize import minimize_scalar, brentq
from scipy.special import expit
from scipy.stats import chi2

y = np.r_[np.zeros(20), np.ones(20)]
x = y.copy()

def ordinary_loglik(intercept, slope):
    eta = intercept + slope*x
    return float(np.sum(y*eta - np.logaddexp(0, eta)))

# Along this separating direction the likelihood increases without a finite MLE.
path = [2., 10., 25., 50.]
values = [ordinary_loglik(-b/2,b) for b in path]
for b,ll in zip(path,values):
    print('Separating slope:', b, 'ordinary log likelihood:', ll)

def penalized(intercept,slope):
    eta = np.array([intercept,intercept+slope])
    # Group 0: 0/20 events; group 1: 20/20 events.
    # Jeffreys penalty adds .5 log(p_g(1-p_g)) per group, plus a constant.
    return float(np.sum(np.array([.5,20.5])*eta - 21*np.logaddexp(0,eta)))

intercept = -np.log(41.)
slope = 2*np.log(41.)
maximum = penalized(intercept,slope)

def profile(b):
    opt = minimize_scalar(lambda a:-penalized(a,b),bounds=(-40,40),
                          method='bounded',options={'xatol':1e-12})
    if not opt.success:
        raise RuntimeError('Profile optimization failed')
    return -opt.fun

cutoff = chi2.ppf(.95,1)
root = lambda b:2*(maximum-profile(b))-cutoff
lower = brentq(root,0,slope)
upper = brentq(root,slope,30)
print('NumPy',np.__version__,'SciPy',scipy.__version__)
print('Firth intercept:',intercept,'slope:',slope)
print('95% profile penalized-likelihood slope interval:',lower,upper)
print('Midpoint probability:',expit(intercept+.5*slope))
print('Finite penalized estimate does not make the ordinary MLE finite.')
assert all(a<b for a,b in zip(values,values[1:]))
assert np.isclose(slope,7.427144,atol=1e-6)
assert np.isclose(lower,4.379495,atol=1e-5)
assert np.isclose(upper,13.177302,atol=1e-5)
assert np.isclose(expit(intercept+.5*slope),.5)
print('All checks passed.')
