{"spec_id":"lift-curve","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nlift-curve: Model Lift Chart\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 93/100 | Updated: 2026-05-10\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\n\n# Okabe-Ito palette\nBRAND = \"#009E73\"  # First series, always\nACCENT = \"#C475FD\"  # Second series\n\n# Data: Simulate customer response model predictions\nnp.random.seed(42)\nn_samples = 1000\n\n# Create realistic model predictions with varying quality\nbase_score = np.random.beta(2, 5, n_samples)\ntrue_signal = np.random.rand(n_samples)\n\n# Model scores with predictive power\ny_score = 0.6 * base_score + 0.4 * true_signal\ny_score = np.clip(y_score, 0, 1)\n\n# Generate true labels based on scores\nresponse_prob = 0.3 * y_score + 0.1\ny_true = (np.random.rand(n_samples) < response_prob).astype(int)\n\n# Calculate lift curve\nsorted_indices = np.argsort(y_score)[::-1]\ny_true_sorted = y_true[sorted_indices]\n\ncumsum_responses = np.cumsum(y_true_sorted)\ntotal_responses = y_true_sorted.sum()\nbaseline_rate = total_responses / n_samples\n\npercentile = np.arange(1, n_samples + 1) / n_samples * 100\nexpected_random = np.arange(1, n_samples + 1) * baseline_rate\nlift = cumsum_responses / expected_random\n\n# Sample at key percentiles for visualization\nsample_points = [0] + list(range(9, n_samples, 10)) + [n_samples - 1]\npercentile_sampled = percentile[sample_points]\nlift_sampled = lift[sample_points]\n\n# Create figure\nfig = go.Figure()\n\n# Lift curve (first series = brand color)\nfig.add_trace(\n    go.Scatter(\n        x=percentile_sampled,\n        y=lift_sampled,\n        mode=\"lines+markers\",\n        name=\"Model Lift\",\n        line=dict(color=BRAND, width=4),\n        marker=dict(size=10, color=BRAND),\n        hovertemplate=\"Top %{x:.0f}%<br>Lift: %{y:.2f}x<extra></extra>\",\n    )\n)\n\n# Random selection baseline (second series = accent color)\nfig.add_trace(\n    go.Scatter(\n        x=[0, 100],\n        y=[1, 1],\n        mode=\"lines\",\n        name=\"Random Selection\",\n        line=dict(color=ACCENT, width=3, dash=\"dash\"),\n        hovertemplate=\"Random baseline<br>Lift: 1.0x<extra></extra>\",\n    )\n)\n\n# Add annotation for key insight\ntop_10_lift = lift[int(n_samples * 0.1) - 1]\nfig.add_annotation(\n    x=10,\n    y=top_10_lift,\n    text=f\"Top 10%: {top_10_lift:.1f}x lift\",\n    showarrow=True,\n    arrowhead=2,\n    arrowsize=1.5,\n    arrowwidth=2,\n    arrowcolor=BRAND,\n    ax=60,\n    ay=-40,\n    font=dict(size=18, color=INK),\n    bgcolor=ELEVATED_BG,\n    bordercolor=INK_SOFT,\n    borderwidth=1,\n    borderpad=6,\n)\n\n# Layout with theme-adaptive colors\nfig.update_layout(\n    title=dict(text=\"lift-curve · plotly · anyplot.ai\", font=dict(size=28, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Percentage of Population Targeted (%)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        range=[0, 100],\n        showgrid=True,\n        gridcolor=GRID,\n        gridwidth=1,\n        dtick=10,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Cumulative Lift Ratio\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        range=[0, max(lift_sampled) * 1.1],\n        showgrid=True,\n        gridcolor=GRID,\n        gridwidth=1,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    legend=dict(\n        font=dict(size=16, color=INK_SOFT),\n        x=0.98,\n        y=0.98,\n        xanchor=\"right\",\n        yanchor=\"top\",\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    margin=dict(l=100, r=80, t=100, b=100),\n)\n\n# Save with theme suffix\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}