{"spec_id":"calibration-curve","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ncalibration-curve: Calibration Curve\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-10\n\"\"\"\n\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, Label\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"\nNEUTRAL = \"#1A1A1A\" if THEME == \"light\" else \"#E8E8E0\"\n\n# Data - Simulate binary classification predictions\nnp.random.seed(42)\nn_samples = 5000\n\ny_prob = np.random.uniform(0, 1, n_samples)\n\ncalibration_factor = 1.3\nadjusted_prob = 1 / (\n    1 + np.exp(-calibration_factor * (np.log(y_prob / (1 - y_prob + 1e-10))))\n)\nadjusted_prob = np.clip(adjusted_prob, 0.01, 0.99)\ny_true = (np.random.uniform(0, 1, n_samples) < adjusted_prob).astype(int)\n\n# Calculate calibration curve (binned)\nn_bins = 10\nbin_edges = np.linspace(0, 1, n_bins + 1)\n\nfraction_of_positives = []\nmean_predicted_value = []\nbin_counts = []\n\nfor i in range(n_bins):\n    mask = (y_prob >= bin_edges[i]) & (y_prob < bin_edges[i + 1])\n    if i == n_bins - 1:\n        mask = (y_prob >= bin_edges[i]) & (y_prob <= bin_edges[i + 1])\n\n    if mask.sum() > 0:\n        fraction_of_positives.append(y_true[mask].mean())\n        mean_predicted_value.append(y_prob[mask].mean())\n        bin_counts.append(mask.sum())\n    else:\n        fraction_of_positives.append(np.nan)\n        mean_predicted_value.append(\n            bin_edges[i] + (bin_edges[i + 1] - bin_edges[i]) / 2\n        )\n        bin_counts.append(0)\n\nvalid_mask = np.array(bin_counts) > 0\nmean_pred_valid = np.array(mean_predicted_value)[valid_mask]\nfrac_pos_valid = np.array(fraction_of_positives)[valid_mask]\ncounts_valid = np.array(bin_counts)[valid_mask]\n\n# Calculate Brier score and ECE\nbrier_score = np.mean((y_prob - y_true) ** 2)\n\nece = 0\ntotal_samples = sum(bin_counts)\nfor i in range(len(bin_counts)):\n    if bin_counts[i] > 0:\n        ece += (bin_counts[i] / total_samples) * abs(\n            fraction_of_positives[i] - mean_predicted_value[i]\n        )\n\n# Create calibration plot\np = figure(\n    width=4800,\n    height=2700,\n    title=\"calibration-curve · bokeh · anyplot.ai\",\n    x_axis_label=\"Mean Predicted Probability\",\n    y_axis_label=\"Fraction of Positives\",\n    x_range=(-0.02, 1.02),\n    y_range=(-0.02, 1.02),\n)\n\n# Add diagonal reference line (perfect calibration)\np.line(\n    [0, 1],\n    [0, 1],\n    line_color=INK_SOFT,\n    line_dash=\"dashed\",\n    line_width=4,\n    legend_label=\"Perfect Calibration\",\n)\n\n# Create source for calibration curve\nsource = ColumnDataSource(\n    data={\"x\": mean_pred_valid, \"y\": frac_pos_valid, \"count\": counts_valid}\n)\n\n# Add HoverTool for interactivity\nhover = HoverTool(\n    tooltips=[\n        (\"Predicted\", \"@x{0.00}\"),\n        (\"Observed\", \"@y{0.00}\"),\n        (\"Count\", \"@count\"),\n    ]\n)\np.add_tools(hover)\n\n# Plot calibration curve with markers\np.line(\"x\", \"y\", source=source, line_color=BRAND, line_width=5, legend_label=\"Classifier\")\np.scatter(\n    \"x\",\n    \"y\",\n    source=source,\n    size=25,\n    color=BRAND,\n    fill_alpha=0.9,\n    line_color=BRAND,\n    line_width=2,\n)\n\n# Add metrics annotation\nmetrics_text = f\"Brier Score: {brier_score:.3f}\\nECE: {ece:.3f}\"\nmetrics_label = Label(\n    x=0.05,\n    y=0.95,\n    x_units=\"data\",\n    y_units=\"data\",\n    text=metrics_text,\n    text_font_size=\"22pt\",\n    text_color=INK,\n    background_fill_color=ELEVATED_BG,\n    background_fill_alpha=0.95,\n    border_line_color=INK_SOFT,\n    border_line_width=1,\n)\np.add_layout(metrics_label)\n\n# Style the plot\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Grid styling\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\n# Legend styling\nif p.legend:\n    p.legend.label_text_font_size = \"20pt\"\n    p.legend.label_text_color = INK_SOFT\n    p.legend.location = \"bottom_right\"\n    p.legend.background_fill_color = ELEVATED_BG\n    p.legend.background_fill_alpha = 0.95\n    p.legend.border_line_color = INK_SOFT\n    p.legend.border_line_width = 1\n    p.legend.padding = 15\n    p.legend.spacing = 10\n\n# Axis styling\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.axis_line_width = 1\np.yaxis.axis_line_width = 1\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\np.xaxis.major_tick_line_width = 1\np.yaxis.major_tick_line_width = 1\n\n# Background\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\np.outline_line_width = 1\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}