{"spec_id":"curve-bias-variance-tradeoff","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ncurve-bias-variance-tradeoff: Bias-Variance Tradeoff Curve\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\n# Remove the script's own directory from sys.path so 'bokeh' resolves to the\n# installed package, not this file (bokeh.py in the same directory).\nsys.path = [p for p in sys.path if os.path.abspath(p) != os.path.abspath(os.path.dirname(__file__) or \".\")]\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import BoxAnnotation, ColumnDataSource, Label, Span\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme tokens\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\"\n\n# Imprint palette — semantic assignments for this spec\nCOLOR_TOTAL = \"#009E73\"  # brand green — first/most prominent (U-shaped key curve)\nCOLOR_BIAS = \"#4467A3\"  # blue — semantic fit with underfitting zone\nCOLOR_VARIANCE = \"#C475FD\"  # purple — second categorical\n\n# Data — theoretical bias-variance tradeoff curves\nnp.random.seed(42)\ncomplexity = np.linspace(0.1, 10, 100)\n\nbias_squared = 4.0 / (1 + complexity) ** 1.2\nvariance = 0.15 * complexity**1.3\nirreducible_error = np.full_like(complexity, 0.5)\ntotal_error = bias_squared + variance + irreducible_error\n\noptimal_idx = np.argmin(total_error)\noptimal_complexity = complexity[optimal_idx]\noptimal_error = total_error[optimal_idx]\n\nsource = ColumnDataSource(\n    data={\n        \"complexity\": complexity,\n        \"bias_squared\": bias_squared,\n        \"variance\": variance,\n        \"irreducible_error\": irreducible_error,\n        \"total_error\": total_error,\n    }\n)\n\n# Plot\ntitle_str = \"curve-bias-variance-tradeoff · python · bokeh · anyplot.ai\"\n\np = figure(\n    width=3200,\n    height=1800,\n    title=title_str,\n    x_axis_label=\"Model Complexity\",\n    y_axis_label=\"Prediction Error\",\n    x_range=(0, 10.5),\n    y_range=(0, 5.5),\n    toolbar_location=None,\n    min_border_bottom=160,\n    min_border_left=180,\n    min_border_top=110,\n    min_border_right=50,\n)\n\n# Shaded underfitting / overfitting zones\nunderfitting_zone = BoxAnnotation(left=0, right=optimal_complexity, fill_alpha=0.07, fill_color=COLOR_BIAS)\noverfitting_zone = BoxAnnotation(left=optimal_complexity, right=11, fill_alpha=0.07, fill_color=COLOR_VARIANCE)\np.add_layout(underfitting_zone)\np.add_layout(overfitting_zone)\n\n# Curves — drawn in order: background lines first, Total Error on top\np.line(\n    \"complexity\",\n    \"irreducible_error\",\n    source=source,\n    line_width=4,\n    line_color=INK_SOFT,\n    line_dash=\"dashed\",\n    legend_label=\"Irreducible Error\",\n)\np.line(\"complexity\", \"bias_squared\", source=source, line_width=5, line_color=COLOR_BIAS, legend_label=\"Bias²\")\np.line(\"complexity\", \"variance\", source=source, line_width=5, line_color=COLOR_VARIANCE, legend_label=\"Variance\")\np.line(\"complexity\", \"total_error\", source=source, line_width=7, line_color=COLOR_TOTAL, legend_label=\"Total Error\")\n\n# Optimal complexity — vertical dotted guide + marker\noptimal_line = Span(location=optimal_complexity, dimension=\"height\", line_color=INK, line_width=2, line_dash=\"dotted\")\np.add_layout(optimal_line)\np.scatter([optimal_complexity], [optimal_error], size=20, color=COLOR_TOTAL, line_color=PAGE_BG, line_width=3)\n\n# Direct curve labels\np.add_layout(Label(x=0.7, y=2.85, text=\"Bias²\", text_font_size=\"28pt\", text_color=COLOR_BIAS, text_font_style=\"bold\"))\np.add_layout(\n    Label(x=8.0, y=2.6, text=\"Variance\", text_font_size=\"28pt\", text_color=COLOR_VARIANCE, text_font_style=\"bold\")\n)\np.add_layout(\n    Label(x=6.3, y=0.68, text=\"Irreducible Error\", text_font_size=\"22pt\", text_color=INK_SOFT, text_font_style=\"italic\")\n)\np.add_layout(\n    Label(x=5.8, y=3.85, text=\"Total Error\", text_font_size=\"28pt\", text_color=COLOR_TOTAL, text_font_style=\"bold\")\n)\np.add_layout(\n    Label(x=optimal_complexity + 0.25, y=optimal_error + 0.28, text=\"Optimal\", text_font_size=\"26pt\", text_color=INK)\n)\n\n# Formula annotation\np.add_layout(\n    Label(\n        x=0.3,\n        y=4.88,\n        text=\"Total Error = Bias² + Variance + Irreducible Error\",\n        text_font_size=\"24pt\",\n        text_color=INK_SOFT,\n        text_font_style=\"italic\",\n    )\n)\n\n# Zone labels (top corners of each region)\np.add_layout(Label(x=0.4, y=4.55, text=\"Underfitting\", text_font_size=\"24pt\", text_color=COLOR_BIAS))\np.add_layout(Label(x=0.4, y=4.17, text=\"(High Bias)\", text_font_size=\"20pt\", text_color=COLOR_BIAS))\np.add_layout(Label(x=7.6, y=4.55, text=\"Overfitting\", text_font_size=\"24pt\", text_color=COLOR_VARIANCE))\np.add_layout(Label(x=7.6, y=4.17, text=\"(High Variance)\", text_font_size=\"20pt\", text_color=COLOR_VARIANCE))\n\n# Text sizing\np.title.text_font_size = \"50pt\"\np.title.text_font_style = \"bold\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Grid — Y-axis grid only for line charts\np.xgrid.grid_line_color = None\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.12\n\n# Background and chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None\n\n# Legend\np.legend.location = \"top_right\"\np.legend.label_text_font_size = \"34pt\"\np.legend.label_text_color = INK_SOFT\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.spacing = 12\np.legend.padding = 20\n\n# Save HTML (interactive artifact) + PNG via headless Chrome\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Window height is set ~140px taller than the figure so the viewport\n# content area (figure height = 1800) isn't clipped by browser chrome.\nW, H = 3200, 1940\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"}