{"spec_id":"gain-curve","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ngain-curve: Cumulative Gains Chart\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-11\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool\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# Okabe-Ito palette\nBRAND = \"#009E73\"  # First series\nSECONDARY = \"#4467A3\"  # imprint blue — perfect-model reference (red reserved for semantic bad)\nNEUTRAL = \"#888888\"  # Adaptive gray for reference\n\n# Data - Simulated customer response model\nnp.random.seed(42)\nn_samples = 1000\n\n# Generate realistic probability scores from a classification model\npositive_ratio = 0.15\nn_positive = int(n_samples * positive_ratio)\nn_negative = n_samples - n_positive\n\n# Scores for positive cases (skewed towards higher probabilities)\npositive_scores = np.random.beta(5, 2, n_positive)\n# Scores for negative cases (skewed towards lower probabilities)\nnegative_scores = np.random.beta(2, 5, n_negative)\n\ny_true = np.concatenate([np.ones(n_positive), np.zeros(n_negative)])\ny_score = np.concatenate([positive_scores, negative_scores])\n\n# Shuffle to mix positives and negatives\nshuffle_idx = np.random.permutation(n_samples)\ny_true = y_true[shuffle_idx]\ny_score = y_score[shuffle_idx]\n\n# Calculate cumulative gains curve\n# Sort by predicted probability (descending)\nsorted_idx = np.argsort(y_score)[::-1]\ny_true_sorted = y_true[sorted_idx]\n\n# Cumulative gains\ncumulative_positives = np.cumsum(y_true_sorted)\ntotal_positives = np.sum(y_true)\n\n# Percentages for axes\npct_population = np.arange(1, n_samples + 1) / n_samples * 100\npct_captured = cumulative_positives / total_positives * 100\n\n# Add origin point for complete curve\npct_population = np.insert(pct_population, 0, 0)\npct_captured = np.insert(pct_captured, 0, 0)\n\n# Random baseline (diagonal line)\nbaseline = np.array([0, 100])\n\n# Perfect model curve\npositive_pct = positive_ratio * 100\nperfect_x = np.array([0, positive_pct, 100])\nperfect_y = np.array([0, 100, 100])\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"gain-curve · bokeh · anyplot.ai\",\n    x_axis_label=\"Percentage of Population Targeted (%)\",\n    y_axis_label=\"Percentage of Positive Cases Captured (%)\",\n    x_range=(0, 100),\n    y_range=(0, 105),\n)\n\n# Create data sources\nsource_model = ColumnDataSource(data={\"x\": pct_population, \"y\": pct_captured})\nsource_baseline = ColumnDataSource(data={\"x\": baseline, \"y\": baseline})\nsource_perfect = ColumnDataSource(data={\"x\": perfect_x, \"y\": perfect_y})\n\n# Plot the curves\n# Model gain curve (brand green - primary series)\nmodel_line = p.line(\n    x=\"x\", y=\"y\", source=source_model, line_color=BRAND, line_width=4, line_alpha=0.9, legend_label=\"Model Gain Curve\"\n)\n\n# Random baseline (neutral gray)\nbaseline_line = p.line(\n    x=\"x\",\n    y=\"y\",\n    source=source_baseline,\n    line_color=INK_SOFT,\n    line_width=3,\n    line_dash=\"dashed\",\n    line_alpha=0.6,\n    legend_label=\"Random Baseline\",\n)\n\n# Perfect model (secondary color)\nperfect_line = p.line(\n    x=\"x\",\n    y=\"y\",\n    source=source_perfect,\n    line_color=SECONDARY,\n    line_width=3,\n    line_dash=\"dotted\",\n    line_alpha=0.8,\n    legend_label=\"Perfect Model\",\n)\n\n# Add hover tool for interactivity\nhover_model = HoverTool(renderers=[model_line], tooltips=[(\"Population %\", \"@x{0.0}\"), (\"Captured %\", \"@y{0.0}\")])\nhover_baseline = HoverTool(renderers=[baseline_line], tooltips=[(\"Population %\", \"@x{0.0}\"), (\"Baseline %\", \"@y{0.0}\")])\nhover_perfect = HoverTool(renderers=[perfect_line], tooltips=[(\"Population %\", \"@x{0.0}\"), (\"Perfect %\", \"@y{0.0}\")])\np.add_tools(hover_model, hover_baseline, hover_perfect)\n\n# Style legend\nif p.legend:\n    p.legend.background_fill_color = ELEVATED_BG\n    p.legend.border_line_color = INK_SOFT\n    p.legend.border_line_width = 1\n    p.legend.label_text_color = INK_SOFT\n    p.legend.label_text_font_size = \"18pt\"\n    p.legend.glyph_height = 30\n    p.legend.glyph_width = 50\n    p.legend.spacing = 15\n    p.legend.padding = 20\n    p.legend.location = \"center\"\n\n# Styling\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_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\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# Background\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Save HTML and screenshot with Selenium\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"}