{"spec_id":"curve-bias-variance-tradeoff","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ncurve-bias-variance-tradeoff: Bias-Variance Tradeoff Curve\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette assignments\nBIAS_COLOR = \"#009E73\"  # position 1 — first series (brand green)\nVARIANCE_COLOR = \"#C475FD\"  # position 2 — lavender\nIRRED_COLOR = INK_MUTED  # semantic muted — baseline noise floor\nTOTAL_COLOR = \"#AE3030\"  # semantic red — total error (bad/loss)\n\n# Data — theoretical bias-variance decomposition\ncomplexity = np.linspace(0.5, 10, 100)\nbias_squared = 0.8 / (1 + 0.5 * complexity) ** 2\nvariance = 0.02 * complexity**1.5\nirreducible_error = np.full_like(complexity, 0.1)\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\n# Figure\nfig = go.Figure()\n\n# Shaded underfitting / overfitting zones\nfig.add_vrect(x0=0.5, x1=optimal_complexity, fillcolor=\"rgba(0,158,115,0.08)\", layer=\"below\", line_width=0)\nfig.add_vrect(x0=optimal_complexity, x1=10, fillcolor=\"rgba(196,117,253,0.08)\", layer=\"below\", line_width=0)\n\n# Curves\nfig.add_trace(\n    go.Scatter(\n        x=complexity, y=bias_squared, mode=\"lines\", name=\"Bias²\", line=dict(color=BIAS_COLOR, width=4, dash=\"dash\")\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=complexity, y=variance, mode=\"lines\", name=\"Variance\", line=dict(color=VARIANCE_COLOR, width=4, dash=\"dash\")\n    )\n)\nfig.add_trace(\n    go.Scatter(\n        x=complexity,\n        y=irreducible_error,\n        mode=\"lines\",\n        name=\"Irreducible Error\",\n        line=dict(color=IRRED_COLOR, width=3, dash=\"dot\"),\n    )\n)\nfig.add_trace(\n    go.Scatter(x=complexity, y=total_error, mode=\"lines\", name=\"Total Error\", line=dict(color=TOTAL_COLOR, width=5))\n)\n\n# Optimal complexity marker\nfig.add_trace(\n    go.Scatter(\n        x=[optimal_complexity],\n        y=[optimal_error],\n        mode=\"markers\",\n        name=\"Optimal Complexity\",\n        marker=dict(color=TOTAL_COLOR, size=16, symbol=\"star\", line=dict(color=PAGE_BG, width=2)),\n    )\n)\n\n# Vertical line at optimal point\nfig.add_vline(\n    x=optimal_complexity,\n    line=dict(color=TOTAL_COLOR, width=2, dash=\"dash\"),\n    annotation_text=\"Optimal<br>Complexity\",\n    annotation_position=\"bottom left\",\n    annotation_font=dict(size=12, color=TOTAL_COLOR),\n)\n\n# Zone labels — separated horizontally from the top-left legend\nfig.add_annotation(\n    x=2.0, y=0.83, text=\"<b>Underfitting</b><br>(High Bias)\", showarrow=False, font=dict(size=12, color=BIAS_COLOR)\n)\nfig.add_annotation(\n    x=8.2,\n    y=0.83,\n    text=\"<b>Overfitting</b><br>(High Variance)\",\n    showarrow=False,\n    font=dict(size=12, color=VARIANCE_COLOR),\n)\n\n# Formula annotation — centered below zone labels in a clear area\nfig.add_annotation(\n    x=5.5,\n    y=0.64,\n    text=\"<b>Total Error = Bias² + Variance + ε</b>\",\n    showarrow=False,\n    font=dict(size=13, color=INK),\n    bgcolor=ELEVATED_BG,\n    bordercolor=INK_SOFT,\n    borderwidth=1,\n    borderpad=6,\n)\n\n# Direct curve labels at the right edge of each curve\n_y_bias = float(bias_squared[-1])\n_y_var = float(variance[-1])\n_y_irred = float(irreducible_error[-1])\n_y_total = float(total_error[-1])\n\nfig.add_annotation(\n    x=1.02,\n    xref=\"paper\",\n    y=_y_total,\n    yref=\"y\",\n    text=\"Total Error\",\n    showarrow=False,\n    xanchor=\"left\",\n    font=dict(size=11, color=TOTAL_COLOR),\n)\nfig.add_annotation(\n    x=1.02,\n    xref=\"paper\",\n    y=_y_var,\n    yref=\"y\",\n    text=\"Variance\",\n    showarrow=False,\n    xanchor=\"left\",\n    font=dict(size=11, color=VARIANCE_COLOR),\n)\nfig.add_annotation(\n    x=1.02,\n    xref=\"paper\",\n    y=_y_irred,\n    yref=\"y\",\n    text=\"Irred. Error\",\n    showarrow=False,\n    xanchor=\"left\",\n    font=dict(size=11, color=IRRED_COLOR),\n)\nfig.add_annotation(\n    x=1.02,\n    xref=\"paper\",\n    y=_y_bias,\n    yref=\"y\",\n    text=\"Bias²\",\n    showarrow=False,\n    xanchor=\"left\",\n    font=dict(size=11, color=BIAS_COLOR),\n)\n\ntitle = \"curve-bias-variance-tradeoff · python · plotly · anyplot.ai\"\n\nfig.update_layout(\n    autosize=False,\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n    title=dict(text=title, font=dict(size=16, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Model Complexity\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        tickvals=[1, 3, 5, 7, 9],\n        ticktext=[\"Low\", \"\", \"Medium\", \"\", \"High\"],\n        range=[0, 10.5],\n        showgrid=True,\n        gridcolor=GRID,\n        gridwidth=1,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n        showline=False,\n        mirror=False,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Prediction Error\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        range=[0, 0.9],\n        showgrid=True,\n        gridcolor=GRID,\n        gridwidth=1,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n        showline=False,\n        mirror=False,\n    ),\n    legend=dict(\n        x=0.02,\n        y=0.98,\n        xanchor=\"left\",\n        yanchor=\"top\",\n        font=dict(size=10, color=INK_SOFT),\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    margin=dict(l=80, r=120, t=80, b=80),\n)\n\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}