Repository Public
1"""Reference values for Statistics/Multivariate (PCA, k-means, hierarchical clustering) and2Optimization/LevenbergMarquardt. References: scikit-learn 1.6 (PCA, KMeans, silhouette),3scipy.cluster.hierarchy, scipy.optimize (curve_fit, least_squares), NIST StRD certified values."""4import numpy as np5from scipy.cluster.hierarchy import fcluster, linkage6from scipy.optimize import curve_fit, least_squares7from sklearn.cluster import KMeans8from sklearn.decomposition import PCA9from sklearn.metrics import silhouette_samples, silhouette_score1011from gen_common import dump1213rng = np.random.default_rng(20261121)14out = {}1516# --- PCA --------------------------------------------------------------------------------------17latent = rng.normal(size=(60, 2))18X = np.c_[latent[:, 0] * 3 + rng.normal(scale=0.5, size=60), latent[:, 0] * -1.5 + latent[:, 1] + rng.normal(scale=0.3, size=60),19 latent[:, 1] * 40 + rng.normal(scale=4, size=60), rng.normal(scale=2, size=60) + 10, latent.sum(axis=1) * 0.2 + rng.normal(scale=0.1, size=60)]202122def pca_block(data, k=None):23 m = PCA(n_components=k).fit(data)24 return {"components": m.components_, "variance": m.explained_variance_, "ratio": m.explained_variance_ratio_,25 "singular": m.singular_values_, "scores": m.transform(data), "mean": m.mean_}262728Z = (X - X.mean(axis=0)) / X.std(axis=0, ddof=1)29new = rng.normal(size=(4, 5)) * [3, 2, 40, 2, 0.5] + [0, 0, 0, 10, 0]30out["pca"] = {31 "X": X, "cov": pca_block(X), "cov_k2": pca_block(X, 2), "corr": pca_block(Z), "sd": X.std(axis=0, ddof=1),32 "corr_matrix_eig": np.sort(np.linalg.eigvalsh(np.corrcoef(X, rowvar=False)))[::-1],33 "cov_matrix_eig": np.sort(np.linalg.eigvalsh(np.cov(X, rowvar=False)))[::-1],34 "new": new, "new_scores_cov": PCA().fit(X).transform(new),35 "wide": None,36}37W = rng.normal(size=(4, 7))38out["pca"]["wide"] = {"X": W, **pca_block(W)}3940# --- k-means -----------------------------------------------------------------------------------41centres = np.array([[0, 0, 0], [6, 1, -2], [-3, 7, 3], [2, -6, 5]], dtype=float)42D = np.vstack([c + rng.normal(scale=1.3, size=(40, 3)) for c in centres])43D = D[rng.permutation(len(D))]44km = {"X": D, "cases": []}45for k, idx in [(2, [0, 1]), (3, [5, 17, 80]), (4, [3, 50, 99, 140]), (5, [1, 2, 3, 4, 5]), (4, [0, 1, 2, 3])]:46 init = D[idx]47 m = KMeans(n_clusters=k, init=init, n_init=1, algorithm="lloyd", tol=0, max_iter=1000).fit(D)48 km["cases"].append({"k": k, "init": init, "centers": m.cluster_centers_, "labels": m.labels_, "inertia": m.inertia_,49 "silhouette": silhouette_score(D, m.labels_), "samples": silhouette_samples(D, m.labels_)})50best = KMeans(n_clusters=4, n_init=50, random_state=0, tol=0, max_iter=1000).fit(D)51km["best4_inertia"] = best.inertia_52km["best4_silhouette"] = silhouette_score(D, best.labels_)53best3 = KMeans(n_clusters=3, n_init=50, random_state=0, tol=0, max_iter=1000).fit(D)54km["best3_inertia"] = best3.inertia_55out["kmeans"] = km5657# --- hierarchical ------------------------------------------------------------------------------58H = D[:45]59hc = {"X": H}60for method in ["single", "complete", "average", "ward"]:61 Zl = linkage(H, method=method)62 hc[method] = {"Z": Zl, "labels3": fcluster(Zl, 3, "maxclust"), "labels5": fcluster(Zl, 5, "maxclust")}63out["hier"] = hc6465# --- Levenberg-Marquardt -------------------------------------------------------------------------66ls = {}67# NIST StRD Misra1a: y = b1 (1 - exp(-b2 x))68mx = np.array([77.6, 114.9, 141.1, 190.8, 239.9, 289.0, 332.8, 378.4, 434.8, 477.3, 536.8, 593.1, 689.1, 760.0])69my = np.array([10.07, 14.73, 17.94, 23.93, 29.61, 35.18, 40.02, 44.82, 50.76, 55.05, 61.01, 66.40, 75.47, 81.78])70f_misra = lambda x, b1, b2: b1 * (1 - np.exp(-b2 * x))71j_misra = lambda x, b1, b2: np.c_[1 - np.exp(-b2 * x), b1 * x * np.exp(-b2 * x)]72popt, pcov = curve_fit(f_misra, mx, my, p0=[250, 5e-4], jac=j_misra, xtol=1e-15, ftol=1e-15, gtol=1e-15)73# certified parameter values and residual sum of squares (NIST StRD, Misra1a); standard errors come from74# curve_fit with the analytic Jacobian75cert = {"b": [2.3894212918e02, 5.5015643181e-04], "rss": 1.2455138894e-01}76assert np.allclose(popt, cert["b"], rtol=1e-8), popt77assert np.isclose(np.sum((f_misra(mx, *popt) - my) ** 2), cert["rss"], rtol=1e-9)78ls["misra1a"] = {"x": mx, "y": my, "start1": [500, 1e-4], "start2": [250, 5e-4], "popt": popt, "pcov": pcov, "certified": cert}79# NIST StRD DanWood: y = b1 x^b280dx = np.array([1.309, 1.471, 1.490, 1.565, 1.611, 1.680])81dy = np.array([2.138, 3.421, 3.597, 4.340, 4.882, 5.660])82f_dan = lambda x, b1, b2: b1 * x ** b283popt, pcov = curve_fit(f_dan, dx, dy, p0=[1, 5], xtol=1e-15, ftol=1e-15, gtol=1e-15)84cert = {"b": [7.6886226176e-01, 3.8604055871e00], "se": [1.8281973860e-02, 5.1726610913e-02], "rss": 4.3173084083e-03}85assert np.allclose(popt, cert["b"], rtol=1e-6), popt86assert np.allclose(np.sqrt(np.diag(pcov)), cert["se"], rtol=1e-3) # curve_fit uses a 2-point Jacobian87ls["danwood"] = {"x": dx, "y": dy, "start1": [1, 5], "start2": [0.7, 4], "popt": popt, "pcov": pcov, "certified": cert}88# exponential decay with offset, noisy89ex = np.linspace(0, 8, 40)90ey = 4.2 * np.exp(-0.9 * ex) + 1.5 + rng.normal(scale=0.05, size=40)91f_exp = lambda x, a, k, c: a * np.exp(-k * x) + c92j_exp = lambda x, a, k, c: np.c_[np.exp(-k * x), -a * x * np.exp(-k * x), np.ones_like(x)]93popt, pcov = curve_fit(f_exp, ex, ey, p0=[1, 1, 0], jac=j_exp, xtol=1e-15, ftol=1e-15, gtol=1e-15)94ls["decay"] = {"x": ex, "y": ey, "start": [1, 1, 0], "popt": popt, "pcov": pcov, "rss": float(np.sum((f_exp(ex, *popt) - ey) ** 2))}95# weighted Gaussian peak96gx = np.linspace(-3, 5, 50)97sig = 0.02 + 0.03 * rng.uniform(size=50)98gy = 2.5 * np.exp(-0.5 * ((gx - 1.2) / 0.8) ** 2) + rng.normal(size=50) * sig99f_g = lambda x, a, m, s: a * np.exp(-0.5 * ((x - m) / s) ** 2)100j_g = lambda x, a, m, s: np.c_[f_g(x, 1, m, s), f_g(x, a, m, s) * (x - m) / s ** 2, f_g(x, a, m, s) * (x - m) ** 2 / s ** 3]101popt, pcov = curve_fit(f_g, gx, gy, p0=[1, 0.5, 1], sigma=sig, absolute_sigma=False, jac=j_g, xtol=1e-15, ftol=1e-15, gtol=1e-15)102ls["gauss"] = {"x": gx, "y": gy, "sigma": sig, "start": [1, 0.5, 1], "popt": popt, "pcov": pcov}103# Rosenbrock as a least-squares problem, and Powell's singular function104r = least_squares(lambda p: [10 * (p[1] - p[0] ** 2), 1 - p[0]], [-1.2, 1.0], method="lm", xtol=1e-15, ftol=1e-15, gtol=1e-15)105ls["rosenbrock"] = {"x": r.x, "cost": r.cost}106out["leastsq"] = ls107108dump("multivariate", out)109