{"spec_id":"lift-curve","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nlift-curve: Model Lift Chart\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-10\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, 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\nBRAND = \"#009E73\"  # Okabe-Ito position 1\nSECONDARY = \"#C475FD\"  # Okabe-Ito position 2 (for baseline)\n\n# Data - Simulated customer response data for marketing campaign\nnp.random.seed(42)\nn_samples = 1000\n\n# Create realistic model predictions with reasonable discrimination\ny_true = np.random.binomial(1, 0.15, n_samples)  # 15% baseline response rate\n\n# Generate scores that correlate with true outcomes but imperfectly\nnoise = np.random.normal(0, 0.3, n_samples)\ny_score = 0.3 + 0.5 * y_true + noise\ny_score = np.clip(y_score, 0, 1)  # Keep scores in valid range\n\n# Calculate lift curve data\nsorted_indices = np.argsort(y_score)[::-1]\ny_true_sorted = y_true[sorted_indices]\n\nn_positives = y_true.sum()\nbaseline_rate = n_positives / n_samples\n\npercentiles = np.arange(1, 101)\ncumulative_lift = []\nn_selected_list = []\n\nfor pct in percentiles:\n    n_selected = int(n_samples * pct / 100)\n    n_positives_selected = y_true_sorted[:n_selected].sum()\n    response_rate = n_positives_selected / n_selected if n_selected > 0 else 0\n    lift = response_rate / baseline_rate if baseline_rate > 0 else 0\n    cumulative_lift.append(lift)\n    n_selected_list.append(n_selected)\n\n# Create data source with hover tooltips\nsource = ColumnDataSource(data={\"percentile\": percentiles, \"lift\": cumulative_lift, \"n_selected\": n_selected_list})\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"lift-curve · bokeh · anyplot.ai\",\n    x_axis_label=\"Population Targeted (%)\",\n    y_axis_label=\"Cumulative Lift Ratio (Model / Random)\",\n    x_range=(0, 105),\n    y_range=(0, max(cumulative_lift) * 1.15),\n)\n\n# Add hover tool for interactivity\nhover = HoverTool(\n    tooltips=[(\"Population %\", \"@percentile%\"), (\"Lift Ratio\", \"@lift{0.00}\"), (\"Customers\", \"@n_selected{,}\")]\n)\np.add_tools(hover)\n\n# Add horizontal reference line at y=1 (random selection baseline)\nbaseline = Span(location=1, dimension=\"width\", line_color=INK_SOFT, line_width=3, line_dash=\"dashed\")\np.add_layout(baseline)\n\n# Plot the lift curve\np.line(x=\"percentile\", y=\"lift\", source=source, line_width=5, line_color=BRAND, legend_label=\"Model Lift\")\n\n# Add scatter points at deciles for emphasis\ndecile_indices = [9, 19, 29, 39, 49, 59, 69, 79, 89, 99]  # 10%, 20%, ... 100%\ndecile_source = ColumnDataSource(\n    data={\"percentile\": [percentiles[i] for i in decile_indices], \"lift\": [cumulative_lift[i] for i in decile_indices]}\n)\n\np.scatter(\n    x=\"percentile\",\n    y=\"lift\",\n    source=decile_source,\n    size=20,\n    fill_color=SECONDARY,\n    line_color=BRAND,\n    line_width=3,\n    legend_label=\"Decile Markers\",\n)\n\n# Styling for large canvas\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# Spine and axis colors\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 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\np.legend.location = \"top_right\"\np.legend.label_text_font_size = \"18pt\"\np.legend.label_text_color = INK_SOFT\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_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\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with Selenium for PNG\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"}