"""Constructed teaching fixtures only. Run: python3 final_investigation_lab.py"""
import math
import numpy as np
# Classification, one declared threshold.
tp,fp,fn,tn=160,80,40,720
assert tp+fp+fn+tn==1000
precision=tp/(tp+fp);recall=tp/(tp+fn);accuracy=(tp+tn)/1000
assert abs(precision-2/3)<1e-12 and recall==.8 and accuracy==.88
# Four genuine forecast targets under two already-issued teaching forecasts.
y=np.array([100.,120.,80.,100.]);a=np.array([100.,100.,100.,100.]);b=np.array([110.,110.,90.,90.])
mae=lambda z:float(np.mean(abs(y-z)))
rmse=lambda z:float(math.sqrt(np.mean((y-z)**2)))
assert mae(a)==mae(b)==10
assert abs(rmse(a)-math.sqrt(200))<1e-12 and rmse(b)==10
# One-dimensional clustering objective.
values=np.array([1.,2.,8.,9.]);cluster1=values[:2];cluster2=values[2:]
centers=[float(cluster1.mean()),float(cluster2.mean())]
within=float(((cluster1-centers[0])**2).sum()+((cluster2-centers[1])**2).sum())
assert centers==[1.5,8.5] and within==1
# PCA uses the original scales of two constructed numeric features.
points=np.array([[1.,1.],[2.,1.],[3.,2.],[4.,3.]])
cov=np.cov(points,rowvar=False);eig=np.linalg.eigvalsh(cov)
assert np.allclose(cov,[[5/3,7/6],[7/6,11/12]])
assert abs(eig[-1]/eig.sum()-.9743690699952497)<1e-12
# A simplified decision rule, valid only for the stated costs and calibrated p.
review_cost=2;miss_cost=20;threshold=review_cost/miss_cost
assert threshold==.1
print('CLASSIFICATION precision',round(precision,4),'recall',round(recall,4),'accuracy',round(accuracy,4))
print('FORECAST A MAE',mae(a),'RMSE',round(rmse(a),4))
print('FORECAST B MAE',mae(b),'RMSE',round(rmse(b),4))
print('K-MEANS teaching centers',centers,'within sum of squares',within)
print('PCA covariance',np.round(cov,4).tolist(),'first variance fraction',round(float(eig[-1]/eig.sum()),6))
print('DECISION break-even probability',threshold)
print('FINAL INVESTIGATION ARITHMETIC CHECKS PASS')
