{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "name": "softclustering.ipynb"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# soft clustering \u2014 Python demo\n\nNumerical companion to the entry [soft clustering](https://dictionaryofml.org/terms/softclustering.html) of the [Dictionary of Applied Machine Learning](https://dictionaryofml.org/): it recomputes what the entry states and prints one line per check.\n\nNumerical companion to the glossary entry 'softclustering'. The 366 days of 2024 at Krems are data points with the feature vector (morning minimum temperature, maximum daytime temperature). A hard clustering (k-means) assigns every day to one of two clusters, the cold and the warm days. A soft clustering assigns every day a degree of belonging to each cluster: a vector with nonnegative entries that sum to one, a point of the probability simplex. A hard clustering is the special case where that vector is a vertex of the simplex.\n\nRequires NumPy and Matplotlib only, and uses fixed seeds, so the printed numbers reproduce exactly. Generated from [`pythondemos/softclustering.py`](https://dictionaryofml.org/terms/softclustering.py); CC BY 4.0.\n\nBefore running a downloaded copy, scan it: `uvx bandit -q -t B102,B307,B602,B605 softclustering.py` (B102,B307,B602,B605 are the exec, eval and shell checks of [bandit](https://bandit.readthedocs.io/)) reports nothing: the script runs no shell command and evaluates no generated code."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "# Notebook shim: the script resolves output paths relative to __file__,\n# which a notebook kernel does not define; everything lands in the\n# working directory instead.\nimport os\n__file__ = os.path.join(os.getcwd(), \"softclustering.py\")\nos.makedirs(\"pythondemos\", exist_ok=True)"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "\"\"\"Soft clustering of the days of one year at Krems an der Donau: the\ndegree-of-belonging vector of a day as a point of the probability simplex.\n\nPurpose\n-------\nNumerical companion to the glossary entry 'softclustering'.  The 366\ndays of 2024 at Krems are data points with the feature vector (morning\nminimum temperature, maximum daytime temperature).  A hard clustering\n(k-means) assigns every day to one of two clusters, the cold and the\nwarm days.  A soft clustering assigns every day a degree of belonging\nto each cluster: a vector with nonnegative entries that sum to one, a\npoint of the probability simplex.  A hard clustering is the special\ncase where that vector is a vertex of the simplex.\n\nThe demo checks the entry's claims: the posterior probabilities of a\ntwo-component Gaussian mixture model (GMM), learned by EM, are such\ndegree-of-belonging vectors and the negative log-likelihood never\nincreases along the EM iterations; a hard clustering is recovered by\nthe largest degree of belonging; with the covariance matrices fixed at\nsigma^2 I and the cluster probabilities at 1/2, the degrees of\nbelonging turn into 0 or 1 as sigma^2 shrinks and then agree with the\nnearest-centroid assignment of k-means; fuzzy c-means delivers degrees\nof belonging without a probabilistic model, its alternating updates\nnever increase its objective, and it reduces to k-means as the\nfuzzifier tends to 1.\n\nDeterministic: Lloyd's algorithm starts from k-means++ seeds drawn with\na fixed seed; EM and fuzzy c-means start from the k-means centroids.\nSelf-contained: numpy + matplotlib only; the GeoSphere download falls\nback to the committed CSV.\n\nBlocks\n------\n[B-data]   Download the daily minimum and maximum temperature of 2024\n           at Krems (GeoSphere station 3805) or read the committed\n           copy; 366 days, no missing value.\n[B-hard]   k-means with k = 2 (Lloyd's algorithm, best of 20 k-means++\n           restarts): the two centroids, the cold and the warm days;\n           the degree-of-belonging vector of every day is a vertex of\n           the simplex.\n[B-gmm]    EM for a two-component GMM with full covariance matrices,\n           started at the k-means centroids: the degrees of belonging\n           are the posterior probabilities, they sum to one for every\n           day, the negative log-likelihood never increases, and the\n           days whose largest degree of belonging is below 0.8 are\n           counted; the clustering by the largest degree of belonging\n           is compared with k-means.\n[B-limit]  The constrained GMM with cluster probabilities 1/2 and\n           covariance matrices sigma^2 I, means fixed at the k-means\n           centroids: the number of days with a degree of belonging\n           between 0.2 and 0.8 for sigma^2 = 25, 4, 1, and the\n           agreement of the largest degree of belonging with the\n           nearest centroid.\n[B-fcm]    Fuzzy c-means with fuzzifier 2 (and 1.05, 10): the\n           alternating updates never increase the objective, the\n           degrees of belonging sum to one, the ambiguous days are\n           counted, and the fuzzifier 1.05 recovers the k-means\n           centroids and clustering error.\n[B-plot]   Every 18th day for the figure (21 of 366 days) with the\n           cluster of its largest GMM degree of belonging and whether\n           that value reaches 0.8; the k-means centroids and their\n           bisector; the curve of equal GMM degrees of belonging; and\n           the degree of belonging to the cold cluster along the line\n           through the two centroids under k-means, the GMM, the\n           constrained GMM with sigma^2 = 4, and fuzzy c-means.\n\nOutputs\n-------\nsoftclustering_weather.csv     : date, tmin, tmax (committed copy).\nsoftclustering_points_plot.csv : the 21 drawn days, cluster, confident.\nsoftclustering_centroids.csv   : the two k-means centroids.\nsoftclustering_bisector.csv    : two points of the k-means decision\n                                 boundary.\nsoftclustering_contour.csv     : the curve of equal GMM posteriors.\nsoftclustering_segment.csv     : s and the four degrees of belonging\n                                 to the cold cluster along the line.\nsoftclustering.png             : matplotlib preview (checking only).\n\"\"\"\n\n# /// script\n# requires-python = \">=3.10\"\n# dependencies = [\"matplotlib==3.10.7\", \"numpy==2.3.4\"]\n# ///\n\nimport json\nimport urllib.request\nfrom pathlib import Path\n\nimport numpy as np\nimport matplotlib\n\nmatplotlib.use(\"Agg\")\nimport matplotlib.pyplot as plt\n\nOUT_DIR = Path(__file__).parent\n\nreport = []\n\n\ndef check(name, ok):\n    report.append((name, bool(ok)))\n    print(f\"  [{'ok' if ok else 'FAIL'}] {name}\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-data]** Download the daily minimum and maximum temperature of 2024 at Krems (GeoSphere station 3805) or read the committed copy; 366 days, no missing value."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-data] daily minimum and maximum temperature at Krems, 2024\")\nURL = (\"https://dataset.api.hub.geosphere.at/v1/station/historical/\"\n       \"klima-v2-1d?parameters=tlmin,tlmax&station_ids=3805\"\n       \"&start=2024-01-01&end=2024-12-31\")\nCSV = OUT_DIR / \"softclustering_weather.csv\"\ntry:\n    with urllib.request.urlopen(URL, timeout=120) as resp:\n        payload = json.load(resp)\n    params = payload[\"features\"][0][\"properties\"][\"parameters\"]\n    records = [(stamp[:10], lo, hi)\n               for stamp, lo, hi in zip(payload[\"timestamps\"],\n                                        params[\"tlmin\"][\"data\"],\n                                        params[\"tlmax\"][\"data\"])]\n    with open(CSV, \"w\") as fh:\n        fh.write(\"date,tmin,tmax\\n\")\n        for day, lo, hi in records:\n            fh.write(f\"{day},{lo},{hi}\\n\")\n    print(f\"    downloaded {len(records)} days\")\nexcept OSError as err:\n    print(f\"    download failed ({err}); reading the committed CSV\")\n    rows = CSV.read_text().strip().splitlines()[1:]\n    records = [(d, float(a), float(b))\n               for d, a, b in (r.split(\",\") for r in rows)]\n\ncheck(\"[B-data] 366 days for 2024\", len(records) == 366)\ncheck(\"[B-data] no day is missing a temperature\",\n      all(lo is not None and hi is not None for _, lo, hi in records))\nX = np.array([[lo, hi] for _, lo, hi in records], dtype=float)\nm, d = X.shape\nK = 2\n\n\ndef sq_euclid(x, w):\n    return np.sum((x - w) ** 2, axis=-1)\n\n\ndef sq_dist_matrix(X, W):\n    \"\"\"m x k matrix of squared Euclidean distances to the centroids W.\"\"\"\n    return np.stack([sq_euclid(X, w) for w in W], axis=1)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-hard]** k-means with k = 2 (Lloyd's algorithm, best of 20 k-means++ restarts): the two centroids, the cold and the warm days; the degree-of-belonging vector of every day is a vertex of the simplex."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-hard] k-means with k = 2\")\n\n\ndef lloyd(X, W0, max_iter=500):\n    W = np.array(W0, dtype=float)\n    c = np.argmin(sq_dist_matrix(X, W), axis=1)\n    for _ in range(max_iter):\n        W = np.array([X[c == j].mean(axis=0) if np.any(c == j) else W[j]\n                      for j in range(len(W))])\n        c_new = np.argmin(sq_dist_matrix(X, W), axis=1)\n        if np.array_equal(c_new, c):\n            break\n        c = c_new\n    return W, c, float(np.mean(np.min(sq_dist_matrix(X, W), axis=1)))\n\n\ndef kmeanspp_seeds(X, k, rng):\n    W = [X[rng.integers(m)]]\n    for _ in range(1, k):\n        d2 = np.min(sq_dist_matrix(X, np.array(W)), axis=1)\n        W.append(X[rng.choice(m, p=d2 / d2.sum())])\n    return np.array(W)\n\n\nrng = np.random.default_rng(2024)\nbest = None\nfor _ in range(20):\n    W, c, err = lloyd(X, kmeanspp_seeds(X, K, rng))\n    if best is None or err < best[2]:\n        best = (W, c, err)\nW_km, c_km, err_km = best\ncold, warm = sorted(range(K), key=lambda j: W_km[j, 1])\nif cold != 0:  # cluster 1 is the cold one throughout\n    W_km = W_km[[cold, warm]]\n    c_km = np.where(c_km == cold, 0, 1)\n    cold, warm = 0, 1\nU_km = np.eye(K)[c_km]  # one-hot rows: the vertices of the simplex\ncheck(f\"[B-hard] centroids ({W_km[0, 0]:.1f}, {W_km[0, 1]:.1f}) with \"\n      f\"{int(np.sum(c_km == 0))} cold days and ({W_km[1, 0]:.1f}, \"\n      f\"{W_km[1, 1]:.1f}) with {int(np.sum(c_km == 1))} warm days; \"\n      f\"clustering error {err_km:.1f}\",\n      W_km[0, 1] < 12 < W_km[1, 1])\ncheck(\"[B-hard] the degree-of-belonging vector of every day is a vertex \"\n      \"of the simplex: entries in {0, 1} that sum to one\",\n      np.all(np.isin(U_km, (0.0, 1.0))) and np.allclose(U_km.sum(axis=1), 1))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-gmm]** EM for a two-component GMM with full covariance matrices, started at the k-means centroids: the degrees of belonging are the posterior probabilities, they sum to one for every day, the negative log-likelihood never increases, and the days whose largest degree of belonging is below 0.8 are counted; the clustering by the largest degree of belonging is compared with k-means."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-gmm] EM for a two-component GMM with full covariance matrices\")\n\n\ndef mvn_pdf(X, mu, C):\n    Ci = np.linalg.inv(C)\n    diff = X - mu\n    q = np.einsum(\"ij,jk,ik->i\", diff, Ci, diff)\n    return np.exp(-0.5 * q) / np.sqrt((2 * np.pi) ** d * np.linalg.det(C))\n\n\ndef gmm_posterior(X, p, mu, C):\n    dens = np.stack([p[j] * mvn_pdf(X, mu[j], C[j]) for j in range(K)],\n                    axis=1)\n    return dens / dens.sum(axis=1, keepdims=True), -np.mean(\n        np.log(dens.sum(axis=1)))\n\n\ndef em_gmm(X, mu0, max_iter=1000, tol=1e-12):\n    p = np.full(K, 1.0 / K)\n    mu = np.array(mu0, dtype=float)\n    C = np.array([np.cov(X.T)] * K)\n    nll = []\n    for _ in range(max_iter):\n        U, value = gmm_posterior(X, p, mu, C)  # E-step\n        nll.append(value)\n        Nk = U.sum(axis=0)  # M-step\n        p = Nk / m\n        mu = (U.T @ X) / Nk[:, None]\n        for j in range(K):\n            diff = X - mu[j]\n            C[j] = (U[:, j, None] * diff).T @ diff / Nk[j]\n        if len(nll) > 1 and nll[-2] - nll[-1] < tol:\n            break\n    U, value = gmm_posterior(X, p, mu, C)\n    nll.append(value)\n    return p, mu, C, U, nll\n\n\np_g, mu_g, C_g, U_g, nll = em_gmm(X, W_km)\ncheck(f\"[B-gmm] EM stopped after {len(nll) - 1} iterations; cluster \"\n      f\"probabilities ({p_g[0]:.2f}, {p_g[1]:.2f}), means \"\n      f\"({mu_g[0, 0]:.1f}, {mu_g[0, 1]:.1f}) and ({mu_g[1, 0]:.1f}, \"\n      f\"{mu_g[1, 1]:.1f})\", mu_g[0, 1] < mu_g[1, 1])\ncheck(\"[B-gmm] the negative log-likelihood never increases along the \"\n      f\"EM iterations ({nll[0]:.3f} -> {nll[-1]:.3f})\",\n      all(b <= a + 1e-9 for a, b in zip(nll, nll[1:])))\ncheck(\"[B-gmm] every day's degrees of belonging are nonnegative and sum \"\n      \"to one (a point of the simplex)\",\n      np.all(U_g >= 0) and np.allclose(U_g.sum(axis=1), 1))\nn_amb_g = int(np.sum(U_g.max(axis=1) < 0.8))\ncheck(f\"[B-gmm] {n_amb_g} of the {m} days have no degree of belonging \"\n      f\"of 0.8 or more\", 0 < n_amb_g < m / 4)\nc_g = np.argmax(U_g, axis=1)\nagree_g = float(np.mean(c_g == c_km))\ncheck(f\"[B-gmm] the cluster of the largest degree of belonging agrees \"\n      f\"with k-means on {100 * agree_g:.1f}% of the days \"\n      f\"({int(np.sum(c_g == c_km))} of {m})\", agree_g > 0.85)\nmonths = np.array([int(day[5:7]) for day, _, _ in records])\namb_months = np.bincount(months[U_g.max(axis=1) < 0.8], minlength=13)\ncheck(\"[B-gmm] no day of January, February, November or December is \"\n      f\"ambiguous; ambiguous days per month: {amb_months[1:].tolist()}\",\n      amb_months[[1, 2, 11, 12]].sum() == 0)"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-limit]** The constrained GMM with cluster probabilities 1/2 and covariance matrices sigma^2 I, means fixed at the k-means centroids: the number of days with a degree of belonging between 0.2 and 0.8 for sigma^2 = 25, 4, 1, and the agreement of the largest degree of belonging with the nearest centroid."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-limit] the constrained GMM at the k-means centroids as sigma^2 \"\n      \"shrinks\")\n\n\ndef constrained_dob(X, W, sigma2):\n    \"\"\"Posterior under equal cluster probabilities and covariance\n    sigma^2 I with means W; computed in the log domain.\"\"\"\n    L = -sq_dist_matrix(X, W) / (2 * sigma2)\n    L -= L.max(axis=1, keepdims=True)\n    U = np.exp(L)\n    return U / U.sum(axis=1, keepdims=True)\n\n\nbetween = {}\nfor sigma2 in (25.0, 4.0, 1.0):\n    U_s = constrained_dob(X, W_km, sigma2)\n    between[sigma2] = int(np.sum((U_s[:, 0] > 0.2) & (U_s[:, 0] < 0.8)))\n    check(f\"[B-limit] sigma^2 = {sigma2:g}: {between[sigma2]} days with a \"\n          f\"degree of belonging between 0.2 and 0.8; the largest degree \"\n          f\"of belonging names the nearest centroid for every day\",\n          np.array_equal(np.argmax(U_s, axis=1), c_km))\ncheck(\"[B-limit] the count of days between 0.2 and 0.8 falls with \"\n      \"sigma^2 and reaches zero\",\n      between[25.0] > between[4.0] > between[1.0] == 0)\nU_tiny = constrained_dob(X, W_km, 1e-4)\ncheck(\"[B-limit] sigma^2 = 0.0001: every degree of belonging is 0 or 1 \"\n      \"to within 1e-6 and equals the k-means vertex\",\n      np.allclose(U_tiny, U_km, atol=1e-6))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-fcm]** Fuzzy c-means with fuzzifier 2 (and 1.05, 10): the alternating updates never increase the objective, the degrees of belonging sum to one, the ambiguous days are counted, and the fuzzifier 1.05 recovers the k-means centroids and clustering error."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-fcm] fuzzy c-means\")\n\n\ndef fcm_dob(X, W, phi):\n    D2 = np.maximum(sq_dist_matrix(X, W), 1e-12)\n    U = D2 ** (-1.0 / (phi - 1.0))\n    return U / U.sum(axis=1, keepdims=True)\n\n\ndef fcm_objective(X, W, U, phi):\n    return float(np.mean(np.sum(U ** phi * sq_dist_matrix(X, W), axis=1)))\n\n\ndef fcm(X, W0, phi, max_iter=1000, tol=1e-10):\n    \"\"\"Alternating updates: the degrees of belonging for fixed centroids,\n    then the centroids for fixed degrees of belonging.  Returns the\n    centroids, the degrees of belonging, and the objective after every\n    half step.\"\"\"\n    W = np.array(W0, dtype=float)\n    values = []\n    for _ in range(max_iter):\n        U = fcm_dob(X, W, phi)\n        values.append(fcm_objective(X, W, U, phi))\n        W_new = (U ** phi).T @ X / (U ** phi).sum(axis=0)[:, None]\n        values.append(fcm_objective(X, W_new, U, phi))\n        if np.allclose(W_new, W, atol=tol):\n            W = W_new\n            break\n        W = W_new\n    return W, fcm_dob(X, W, phi), values\n\n\nfcm_runs = {}\nfor phi in (2.0, 1.05, 10.0):\n    fcm_runs[phi] = fcm(X, W_km + 0.5, phi)\nW_f, U_f, vals_f = fcm_runs[2.0]\ncheck(f\"[B-fcm] fuzzifier 2: {len(vals_f) // 2} iterations, the objective \"\n      f\"never increases ({vals_f[0]:.2f} -> {vals_f[-1]:.2f}); centroids \"\n      f\"({W_f[0, 0]:.1f}, {W_f[0, 1]:.1f}) and ({W_f[1, 0]:.1f}, \"\n      f\"{W_f[1, 1]:.1f})\",\n      all(b <= a + 1e-9 for a, b in zip(vals_f, vals_f[1:])))\ncheck(\"[B-fcm] fuzzifier 2: every day's degrees of belonging are \"\n      \"nonnegative and sum to one\",\n      np.all(U_f >= 0) and np.allclose(U_f.sum(axis=1), 1))\nn_amb_f = int(np.sum(U_f.max(axis=1) < 0.8))\ncheck(f\"[B-fcm] fuzzifier 2: {n_amb_f} days have no degree of belonging \"\n      f\"of 0.8 or more (GMM: {n_amb_g})\", 0 < n_amb_f < m / 4)\ncheck(\"[B-fcm] fuzzifier 2: the largest degree of belonging names the \"\n      \"nearest fuzzy c-means centroid for every day\",\n      np.array_equal(np.argmax(U_f, axis=1),\n                     np.argmin(sq_dist_matrix(X, W_f), axis=1)))\nW_1, U_1, vals_1 = fcm_runs[1.05]\nn_sharp = int(np.sum(U_1.max(axis=1) > 0.99))\ncheck(f\"[B-fcm] fuzzifier 1.05: centroids within 0.05 of the k-means \"\n      f\"centroids, objective {vals_1[-1]:.1f} within 0.1 of the k-means \"\n      f\"clustering error {err_km:.1f}, and {n_sharp} of the {m} days have \"\n      f\"a largest degree of belonging above 0.99\",\n      np.allclose(W_1, W_km, atol=0.05) and abs(vals_1[-1] - err_km) < 0.1\n      and n_sharp >= m - 10)\nW_10, U_10, _ = fcm_runs[10.0]\ncheck(f\"[B-fcm] fuzzifier 10: every degree of belonging lies between 0.25 \"\n      f\"and 0.75 (smallest {U_10.min():.2f}, largest {U_10.max():.2f})\",\n      np.all((U_10 > 0.25) & (U_10 < 0.75)))"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "**[B-plot]** Every 18th day for the figure (21 of 366 days) with the cluster of its largest GMM degree of belonging and whether that value reaches 0.8; the k-means centroids and their bisector; the curve of equal GMM degrees of belonging; and the degree of belonging to the cold cluster along the line through the two centroids under k-means, the GMM, the constrained GMM with sigma^2 = 4, and fuzzy c-means."
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "print(\"[B-plot] thinned scatter, boundary curves, the line through the \"\n      \"centroids\")\nidx = np.arange(0, m, 18)\nwith open(OUT_DIR / \"softclustering_points_plot.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax,cluster,confident,dobcold\\n\")\n    for i in idx:\n        fh.write(f\"{X[i, 0]:.1f},{X[i, 1]:.1f},{int(c_g[i])},\"\n                 f\"{int(U_g[i].max() >= 0.8)},{U_g[i, 0]:.4f}\\n\")\ncheck(f\"[B-plot] every 18th day: {len(idx)} of the {m} days are drawn, \"\n      f\"{int(np.sum(U_g[idx].max(axis=1) < 0.8))} of them ambiguous\",\n      len(idx) == 21)\nwith open(OUT_DIR / \"softclustering_centroids.csv\", \"w\") as fh:\n    fh.write(\"cluster,tmin,tmax\\n\")\n    for j in range(K):\n        fh.write(f\"{j},{W_km[j, 0]:.4f},{W_km[j, 1]:.4f}\\n\")\n\n# the k-means decision boundary: the bisector of the two centroids\nmid = W_km.mean(axis=0)\naxis_vec = W_km[1] - W_km[0]\nperp = np.array([-axis_vec[1], axis_vec[0]]) / np.linalg.norm(axis_vec)\nwith open(OUT_DIR / \"softclustering_bisector.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax\\n\")\n    for t in (-30.0, 30.0):\n        q = mid + t * perp\n        fh.write(f\"{q[0]:.4f},{q[1]:.4f}\\n\")\n\n# the curve of equal GMM degrees of belonging: the zero level set of\n# the log posterior ratio, one root per column of a grid over the window\nXMIN, XMAX, YMIN, YMAX = -8.0, 23.0, 0.0, 40.0\ntmin_grid = np.linspace(XMIN, XMAX, 321)\ntmax_grid = np.linspace(YMIN, YMAX, 841)\ncontour = []\nroots_per_column = []\nfor t in tmin_grid:\n    col = np.column_stack([np.full_like(tmax_grid, t), tmax_grid])\n    U_col, _ = gmm_posterior(col, p_g, mu_g, C_g)\n    ratio = np.log(U_col[:, 0]) - np.log(U_col[:, 1])\n    sign_change = np.nonzero(np.sign(ratio[:-1]) != np.sign(ratio[1:]))[0]\n    roots_per_column.append(len(sign_change))\n    for i in sign_change:\n        frac = ratio[i] / (ratio[i] - ratio[i + 1])\n        contour.append((t, tmax_grid[i] + frac * (tmax_grid[i + 1] - tmax_grid[i])))\ncontour = np.array(contour)\ncheck(\"[B-plot] within the window of panel (a) the curve of equal degrees of \"\n      \"belonging has at most one point above each morning minimum\",\n      max(roots_per_column) <= 1)\nwith open(OUT_DIR / \"softclustering_contour.csv\", \"w\") as fh:\n    fh.write(\"tmin,tmax\\n\")\n    for q in contour:\n        fh.write(f\"{q[0]:.4f},{q[1]:.4f}\\n\")\nU_on, _ = gmm_posterior(contour, p_g, mu_g, C_g)\ncheck(f\"[B-plot] the curve of equal degrees of belonging has {len(contour)} \"\n      f\"vertices, each with both degrees of belonging within 0.02 of 0.5\",\n      np.all(np.abs(U_on[:, 0] - 0.5) < 0.02))\n\n# the line through the two k-means centroids: s = 0 cold, s = 1 warm\ns = np.linspace(-0.5, 1.5, 401)\nline = W_km[0] + s[:, None] * axis_vec\nu_gmm = gmm_posterior(line, p_g, mu_g, C_g)[0][:, 0]\nu_sig = constrained_dob(line, W_km, 4.0)[:, 0]\nu_fcm = fcm_dob(line, W_f, 2.0)[:, 0]\nu_km = (s < 0.5).astype(float)\nwith open(OUT_DIR / \"softclustering_segment.csv\", \"w\") as fh:\n    fh.write(\"s,kmeans,gmm,sigma4,fcm\\n\")\n    for row in zip(s, u_km, u_gmm, u_sig, u_fcm):\n        fh.write(\",\".join(f\"{v:.4f}\" for v in row) + \"\\n\")\ncheck(\"[B-plot] along the line, the GMM posterior and the constrained GMM \"\n      \"give the cold cluster a degree of belonging of 1 at s = -0.5 and 0 \"\n      \"at s = 1.5 to within 0.02\",\n      all(abs(u[0] - 1) < 0.02 and abs(u[-1]) < 0.02\n          for u in (u_gmm, u_sig)))\ncheck(f\"[B-plot] fuzzy c-means with fuzzifier 2 does not: its degree of \"\n      f\"belonging is {u_fcm[0]:.2f} at s = -0.5 and {u_fcm[-1]:.2f} at \"\n      f\"s = 1.5, since it decays like one over the squared distance\",\n      0.8 < u_fcm[0] < 0.95 and 0.05 < u_fcm[-1] < 0.2)\ncheck(\"[B-plot] the constrained GMM with sigma^2 = 4 crosses 0.5 at the \"\n      \"midpoint s = 0.5, where k-means jumps\",\n      abs(s[np.argmin(np.abs(u_sig - 0.5))] - 0.5) < 0.01)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9.6, 4.0))\nfor j, mk, lab in ((0, \"o\", \"cold days\"), (1, \"s\", \"warm days\")):\n    for conf, face in ((1, \"black\"), (0, \"none\")):\n        sel = idx[(c_g[idx] == j)\n                  & ((U_g[idx].max(axis=1) >= 0.8) == bool(conf))]\n        if len(sel) == 0:\n            continue\n        ax1.scatter(X[sel, 0], X[sel, 1], marker=mk, facecolors=face,\n                    edgecolors=\"black\", s=30,\n                    label=f\"{lab}, {'largest DOB >= 0.8' if conf else 'largest DOB < 0.8'}\")\nax1.plot(contour[:, 0], contour[:, 1], \"k-\", lw=1.2,\n         label=\"equal DOBs under the GMM\")\nbis = np.array([mid + t * perp for t in (-30, 30)])\nax1.plot(bis[:, 0], bis[:, 1], \"k--\", lw=1.0, label=\"k-means decision boundary\")\nax1.plot(line[:, 0], line[:, 1], \"k:\", lw=0.8, label=\"line of panel (b)\")\nax1.scatter(W_km[:, 0], W_km[:, 1], marker=\"x\", color=\"black\", s=60,\n            label=\"k-means centroids\")\nax1.set_xlim(XMIN, XMAX)\nax1.set_ylim(YMIN, YMAX)\nax1.set_xlabel(\"morning minimum temperature (\u00b0C)\")\nax1.set_ylabel(\"maximum daytime temperature (\u00b0C)\")\nax1.set_title(\"(a) every 18th day of 2024 and the two boundaries\")\nax1.legend(frameon=False, fontsize=7, loc=\"upper left\")\nax2.plot(s, u_km, \"k--\", lw=1.0, label=\"k-means (hard)\")\nax2.plot(s, u_gmm, \"k-\", lw=1.4, label=\"GMM posterior\")\nax2.plot(s, u_sig, \"k-.\", lw=1.0, label=\"GMM with sigma^2 = 4\")\nax2.plot(s, u_fcm, \"k:\", lw=1.4, label=\"fuzzy c-means, fuzzifier 2\")\nax2.set_xlabel(\"position s on the line through the centroids\")\nax2.set_ylabel(\"degree of belonging to the cold cluster\")\nax2.set_title(\"(b) soft and hard degrees of belonging\")\nax2.legend(frameon=False, fontsize=8)\nfig.tight_layout()\nfig.savefig(OUT_DIR / \"softclustering.png\", dpi=110)\n\nn_ok = sum(ok for _, ok in report)\nprint(f\"\\n{n_ok}/{len(report)} checks pass\")\nif n_ok != len(report):\n    raise SystemExit(1)"
  }
 ]
}