GeezerVison is showing github.com — clean up a bit · WinVector/Examples@d50ec82
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: rebalance/select_example.ipynb -295Lines changed: 0 additions & 295 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 534 | 534 | ")" | 535 | 535 | ] | 536 | 536 | }, | 537 | | - { | 538 | | - "cell_type": "code", | 539 | | - "execution_count": 11, | 540 | | - "metadata": {}, | 541 | | - "outputs": [], | 542 | | - "source": [ | 543 | | - "n_alternatives = 5" | 544 | | - ] | 545 | | - }, | 546 | | - { | 547 | | - "cell_type": "code", | 548 | | - "execution_count": 12, | 549 | | - "metadata": {}, | 550 | | - "outputs": [ | 551 | | - { | 552 | | - "name": "stdout", | 553 | | - "output_type": "stream", | 554 | | - "text": [ | 555 | | - "\n", | 556 | | - "data {\n", | 557 | | - " int<lower=1> n_vars; // number of variables per alternative\n", | 558 | | - " int<lower=1> m_examples; // number of examples\n", | 559 | | - " int<lower=5, upper=5> n_alternatives; // number of alternatives\n", | 560 | | - " array[m_examples] int<lower=0, upper=1> a_position_was_picked;\n", | 561 | | - " matrix[m_examples, n_vars] x_1; // features examples\n", | 562 | | - " matrix[m_examples, n_vars] x_2; // features examples\n", | 563 | | - " matrix[m_examples, n_vars] x_3; // features examples\n", | 564 | | - " matrix[m_examples, n_vars] x_4; // features examples\n", | 565 | | - " matrix[m_examples, n_vars] x_5; // features examples\n", | 566 | | - "}\n", | 567 | | - "parameters {\n", | 568 | | - " vector[n_vars] beta; // model parameters\n", | 569 | | - "}\n", | 570 | | - "transformed parameters {\n", | 571 | | - " array[n_alternatives] vector[m_examples] p_like; // modeled raw pick score of item\n", | 572 | | - " vector[m_examples] prob_if_no_select;\n", | 573 | | - " vector[m_examples] prob_if_select_1;\n", | 574 | | - " // modeled predictions\n", | 575 | | - " p_like[1] = logit(x_1 * beta);\n", | 576 | | - " p_like[2] = logit(x_2 * beta);\n", | 577 | | - " p_like[3] = logit(x_3 * beta);\n", | 578 | | - " p_like[4] = logit(x_4 * beta);\n", | 579 | | - " p_like[5] = logit(x_5 * beta);\n", | 580 | | - " prob_if_no_select = (1.0 - p_like[1]) * (1.0 - p_like[2]) * (1.0 - p_like[3]) * (1.0 - p_like[4]) * (1.0 - p_like[5]);\n", | 581 | | - " prob_if_select_1 = (\n", | 582 | | - " (1.0/1.0) * p_like[1] * (1.0 - p_like[2]) * (1.0 - p_like[3]) * (1.0 - p_like[4]) * (1.0 - p_like[5])\n", | 583 | | - " + (1.0/2.0) * p_like[1] * p_like[2] * (1.0 - p_like[3]) * (1.0 - p_like[4]) * (1.0 - p_like[5])\n", | 584 | | - " + (1.0/2.0) * p_like[1] * (1.0 - p_like[2]) * p_like[3] * (1.0 - p_like[4]) * (1.0 - p_like[5])\n", | 585 | | - " + (1.0/2.0) * p_like[1] * (1.0 - p_like[2]) * (1.0 - p_like[3]) * p_like[4] * (1.0 - p_like[5])\n", | 586 | | - " + (1.0/2.0) * p_like[1] * (1.0 - p_like[2]) * (1.0 - p_like[3]) * (1.0 - p_like[4]) * p_like[5]\n", | 587 | | - " + (1.0/3.0) * p_like[1] * p_like[2] * p_like[3] * (1.0 - p_like[4]) * (1.0 - p_like[5])\n", | 588 | | - " + (1.0/3.0) * p_like[1] * p_like[2] * (1.0 - p_like[3]) * p_like[4] * (1.0 - p_like[5])\n", | 589 | | - " + (1.0/3.0) * p_like[1] * p_like[2] * (1.0 - p_like[3]) * (1.0 - p_like[4]) * p_like[5]\n", | 590 | | - " + (1.0/3.0) * p_like[1] * (1.0 - p_like[2]) * p_like[3] * p_like[4] * (1.0 - p_like[5])\n", | 591 | | - " + (1.0/3.0) * p_like[1] * (1.0 - p_like[2]) * p_like[3] * (1.0 - p_like[4]) * p_like[5]\n", | 592 | | - " + (1.0/3.0) * p_like[1] * (1.0 - p_like[2]) * (1.0 - p_like[3]) * p_like[4] * p_like[5]\n", | 593 | | - " + (1.0/4.0) * p_like[1] * p_like[2] * p_like[3] * p_like[4] * (1.0 - p_like[5])\n", | 594 | | - " + (1.0/4.0) * p_like[1] * p_like[2] * p_like[3] * (1.0 - p_like[4]) * p_like[5]\n", | 595 | | - " + (1.0/4.0) * p_like[1] * p_like[2] * (1.0 - p_like[3]) * p_like[4] * p_like[5]\n", | 596 | | - " + (1.0/4.0) * p_like[1] * (1.0 - p_like[2]) * p_like[3] * p_like[4] * p_like[5]\n", | 597 | | - " + (1.0/5.0) * p_like[1] * p_like[2] * p_like[3] * p_like[4] * p_like[5]\n", | 598 | | - " );\n", | 599 | | - "}\n", | 600 | | - "model {\n", | 601 | | - " // basic priors\n", | 602 | | - " beta ~ normal(0, 10);\n", | 603 | | - " // log probability of observed selection as a function of parameters\n", | 604 | | - " for (ex_i in 1:m_examples) {\n", | 605 | | - " if (a_position_was_picked > 0) {\n", | 606 | | - " target += log(prob_if_select_1[ex_i]);\n", | 607 | | - " } else {\n", | 608 | | - " target += log(prob_if_no_select[ex_i]);\n", | 609 | | - " }\n", | 610 | | - " }\n", | 611 | | - "}\n", | 612 | | - "\n" | 613 | | - ] | 614 | | - } | 615 | | - ], | 616 | | - "source": [ | 617 | | - "lb = \"\"\"\n", | 618 | | - "\"\"\"\n", | 619 | | - "\n", | 620 | | - "terms = []\n", | 621 | | - "for s in powerset(range(2, n_alternatives+1)):\n", | 622 | | - " s = set([1] + list(s))\n", | 623 | | - " term = (\n", | 624 | | - " f\"(1.0/{len(s)}.0) * \" \n", | 625 | | - " + \" * \".join([(f\"p_like[{i}]\" if i in s else f\"(1.0 - p_like[{i}])\") for i in range(1, n_alternatives+1)])\n", | 626 | | - " )\n", | 627 | | - " terms.append(term)\n", | 628 | | - "expr = (\n", | 629 | | - " \"(\" + lb + \" \"\n", | 630 | | - " + (lb + \" + \").join(terms)\n", | 631 | | - " + lb +\" )\"\n", | 632 | | - ")\n", | 633 | | - "expr_no_select = (\n", | 634 | | - " \" * \".join([f\"(1.0 - p_like[{i}])\" for i in range(1, n_alternatives+1)])\n", | 635 | | - " )\n", | 636 | | - "\n", | 637 | | - "model_src = (f\"\"\"\n", | 638 | | - "data {{\n", | 639 | | - " int<lower=1> n_vars; // number of variables per alternative\n", | 640 | | - " int<lower=1> m_examples; // number of examples\n", | 641 | | - " int<lower={n_alternatives}, upper={n_alternatives}> n_alternatives; // number of alternatives\n", | 642 | | - " array[m_examples] int<lower=0, upper=1> a_position_was_picked;\n", | 643 | | - "\"\"\"\n", | 644 | | - " + lb.join([f\" matrix[m_examples, n_vars] x_{sel_j}; // features examples\" for sel_j in range(1, n_alternatives+1)])\n", | 645 | | - " + f\"\"\"\n", | 646 | | - "}}\n", | 647 | | - "parameters {{\n", | 648 | | - " vector[n_vars] beta; // model parameters\n", | 649 | | - "}}\n", | 650 | | - "transformed parameters {{\n", | 651 | | - " array[n_alternatives] vector[m_examples] p_like; // modeled raw pick score of item\n", | 652 | | - " vector[m_examples] prob_if_no_select;\n", | 653 | | - " vector[m_examples] prob_if_select_1;\n", | 654 | | - " // modeled predictions\n", | 655 | | - "\"\"\"\n", | 656 | | - " + lb.join([f\" p_like[{sel_j}] = logit(x_{sel_j} * beta);\" for sel_j in range(1, n_alternatives+1)])\n", | 657 | | - " + f\"\"\"\n", | 658 | | - " prob_if_no_select = {expr_no_select};\n", | 659 | | - " prob_if_select_1 = {expr};\n", | 660 | | - "}}\n", | 661 | | - "model {{\n", | 662 | | - " // basic priors\n", | 663 | | - " beta ~ normal(0, 10);\n", | 664 | | - " // log probability of observed selection as a function of parameters\n", | 665 | | - " for (ex_i in 1:m_examples) {{\n", | 666 | | - " if (a_position_was_picked > 0) {{\n", | 667 | | - " target += log(prob_if_select_1[ex_i]);\n", | 668 | | - " }} else {{\n", | 669 | | - " target += log(prob_if_no_select[ex_i]);\n", | 670 | | - " }}\n", | 671 | | - " }}\n", | 672 | | - "}}\n", | 673 | | - "\"\"\")\n", | 674 | | - "\n", | 675 | | - "print(model_src)" | 676 | | - ] | 677 | | - }, | 678 | | - { | 679 | | - "cell_type": "code", | 680 | | - "execution_count": 13, | 681 | | - "metadata": {}, | 682 | | - "outputs": [ | 683 | | - { | 684 | | - "data": { | 685 | | - "text/html": [ | 686 | | - "<div>\n", | 687 | | - "<style scoped>\n", | 688 | | - " .dataframe tbody tr th:only-of-type {\n", | 689 | | - " vertical-align: middle;\n", | 690 | | - " }\n", | 691 | | - "\n", | 692 | | - " .dataframe tbody tr th {\n", | 693 | | - " vertical-align: top;\n", | 694 | | - " }\n", | 695 | | - "\n", | 696 | | - " .dataframe thead th {\n", | 697 | | - " text-align: right;\n", | 698 | | - " }\n", | 699 | | - "</style>\n", | 700 | | - "<table border=\"1\" class=\"dataframe\">\n", | 701 | | - " <thead>\n", | 702 | | - " <tr style=\"text-align: right;\">\n", | 703 | | - " <th></th>\n", | 704 | | - " <th>presentation_group</th>\n", | 705 | | - " <th>sampled_positive</th>\n", | 706 | | - " </tr>\n", | 707 | | - " </thead>\n", | 708 | | - " <tbody>\n", | 709 | | - " <tr>\n", | 710 | | - " <th>0</th>\n", | 711 | | - " <td>1</td>\n", | 712 | | - " <td>True</td>\n", | 713 | | - " </tr>\n", | 714 | | - " <tr>\n", | 715 | | - " <th>1</th>\n", | 716 | | - " <td>2</td>\n", | 717 | | - " <td>True</td>\n", | 718 | | - " </tr>\n", | 719 | | - " <tr>\n", | 720 | | - " <th>2</th>\n", | 721 | | - " <td>3</td>\n", | 722 | | - " <td>True</td>\n", | 723 | | - " </tr>\n", | 724 | | - " <tr>\n", | 725 | | - " <th>3</th>\n", | 726 | | - " <td>4</td>\n", | 727 | | - " <td>True</td>\n", | 728 | | - " </tr>\n", | 729 | | - " <tr>\n", | 730 | | - " <th>4</th>\n", | 731 | | - " <td>5</td>\n", | 732 | | - " <td>True</td>\n", | 733 | | - " </tr>\n", | 734 | | - " <tr>\n", | 735 | | - " <th>...</th>\n", | 736 | | - " <td>...</td>\n", | 737 | | - " <td>...</td>\n", | 738 | | - " </tr>\n", | 739 | | - " <tr>\n", | 740 | | - " <th>199995</th>\n", | 741 | | - " <td>199996</td>\n", | 742 | | - " <td>True</td>\n", | 743 | | - " </tr>\n", | 744 | | - " <tr>\n", | 745 | | - " <th>199996</th>\n", | 746 | | - " <td>199997</td>\n", | 747 | | - " <td>True</td>\n", | 748 | | - " </tr>\n", | 749 | | - " <tr>\n", | 750 | | - " <th>199997</th>\n", | 751 | | - " <td>199998</td>\n", | 752 | | - " <td>True</td>\n", | 753 | | - " </tr>\n", | 754 | | - " <tr>\n", | 755 | | - " <th>199998</th>\n", | 756 | | - " <td>199999</td>\n", | 757 | | - " <td>True</td>\n", | 758 | | - " </tr>\n", | 759 | | - " <tr>\n", | 760 | | - " <th>199999</th>\n", | 761 | | - " <td>200000</td>\n", | 762 | | - " <td>True</td>\n", | 763 | | - " </tr>\n", | 764 | | - " </tbody>\n", | 765 | | - "</table>\n", | 766 | | - "<p>200000 rows × 2 columns</p>\n", | 767 | | - "</div>" | 768 | | - ], | 769 | | - "text/plain": [ | 770 | | - " presentation_group sampled_positive\n", | 771 | | - "0 1 True\n", | 772 | | - "1 2 True\n", | 773 | | - "2 3 True\n", | 774 | | - "3 4 True\n", | 775 | | - "4 5 True\n", | 776 | | - "... ... ...\n", | 777 | | - "199995 199996 True\n", | 778 | | - "199996 199997 True\n", | 779 | | - "199997 199998 True\n", | 780 | | - "199998 199999 True\n", | 781 | | - "199999 200000 True\n", | 782 | | - "\n", | 783 | | - "[200000 rows x 2 columns]" | 784 | | - ] | 785 | | - }, | 786 | | - "execution_count": 13, | 787 | | - "metadata": {}, | 788 | | - "output_type": "execute_result" | 789 | | - } | 790 | | - ], | 791 | | - "source": [ | 792 | | - "d_sampled_sorted = (\n", | 793 | | - " d_sampled\n", | 794 | | - " .sort_values(\n", | 795 | | - " ['presentation_group', 'sampled_positive'], \n", | 796 | | - " ascending=[True, False], \n", | 797 | | - " inplace=False, \n", | 798 | | - " ignore_index=True)\n", | 799 | | - ")\n", | 800 | | - "\n", | 801 | | - "had_pick = (\n", | 802 | | - " d_sampled_sorted\n", | 803 | | - " .groupby(['presentation_group'])['sampled_positive']\n", | 804 | | - " .max()\n", | 805 | | - " .reset_index(drop=False, inplace=False)\n", | 806 | | - " .sort_values(['presentation_group'], inplace=False, ignore_index=True)\n", | 807 | | - ")\n", | 808 | | - "\n", | 809 | | - "had_pick" | 810 | | - ] | 811 | | - }, | 812 | | - { | 813 | | - "cell_type": "code", | 814 | | - "execution_count": 14, | 815 | | - "metadata": {}, | 816 | | - "outputs": [ | 817 | | - { | 818 | | - "data": { | 819 | | - "text/plain": [ | 820 | | - "(1000000, 7)" | 821 | | - ] | 822 | | - }, | 823 | | - "execution_count": 14, | 824 | | - "metadata": {}, | 825 | | - "output_type": "execute_result" | 826 | | - } | 827 | | - ], | 828 | | - "source": [ | 829 | | - "d_sampled_sorted.shape" | 830 | | - ] | 831 | | - }, | 832 | 537 | { | 833 | 538 | "cell_type": "code", | 834 | 539 | "execution_count": null, | |
|