"""Case 004: conditional slopes and recentering, NumPy + SciPy.
Download case-004-shared-data.csv beside this script.
Uses the R-generated observations so coefficient comparisons are direct.
"""
import csv
from pathlib import Path
import numpy as np
import scipy
from scipy.stats import t

with Path(__file__).with_name('case-004-shared-data.csv').open() as f:
    rows=list(csv.DictReader(f))
x,z,y=(np.array([float(r[k]) for r in rows]) for k in ['x','z','y'])
X=np.column_stack([np.ones(len(y)),x,z,x*z])
beta=np.linalg.lstsq(X,y,rcond=None)[0]
df=len(y)-X.shape[1]
variance=np.sum((y-X@beta)**2)/df
cov=variance*np.linalg.inv(X.T@X)
se=np.sqrt(np.diag(cov))
p=2*t.sf(np.abs(beta/se),df)
print('NumPy',np.__version__,'SciPy',scipy.__version__)
for name,b,s,pv in zip(['intercept','x','z','x:z'],beta,se,p):
    print(name,'estimate:',b,'SE:',s,'p:',pv)
z_sd=z.std(ddof=1)
slopes=[]
for label,m in [('low',-z_sd),('mean',0.),('high',z_sd)]:
    contrast=np.array([0,1,0,m])
    b=contrast@beta
    s=np.sqrt(contrast@cov@contrast)
    slopes.append(b)
    print(label,'slope:',b,'SE:',s,'p:',2*t.sf(abs(b/s),df))
z_high=z-z_sd
X_high=np.column_stack([np.ones(len(y)),x,z_high,x*z_high])
beta_high=np.linalg.lstsq(X_high,y,rcond=None)[0]
difference=np.max(np.abs(X@beta-X_high@beta_high))
print('Recentered x:',beta_high[1],'interaction:',beta_high[3])
print('Maximum fitted-value difference:',difference)
assert p[1]>.1 and p[2]>.1 and p[3]<1e-10
assert slopes[0]<0<slopes[2]
assert abs(beta_high[1]-slopes[2])<1e-10
assert abs(beta_high[3]-beta[3])<1e-10 and difference<1e-10
assert abs(beta[3]-1.300907)<1e-6
print('All checks passed.')
