{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Statistical Tools" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipy.stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Helper function to calculate the wheel of fortune" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def wheel_of_fortune(rank_i,n):\n", " return rank_i / (0.5 * n * (n + 1))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def wheel_of_fortune_weights(items:list, item_scores:list):\n", " rank = scipy.stats.rankdata(item_scores)\n", "\n", " n = len(items)\n", "\n", " return wheel_of_fortune(rank, n)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def wheel_of_fortune_selection(items: list, item_scores:list, num_choices=1):\n", " \n", " wheel_weights = wheel_of_fortune_weights(items, item_scores)\n", " \n", " n = min(len(items), num_choices)\n", " \n", " choice = np.random.choice(items, size=n, replace=False, p=wheel_weights)\n", " \n", " if num_choices == 1:\n", " return choice[0]\n", "\n", " return choice\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def combined_wheel_of_fortune_selection(items_list:list, item_scores_list:list, num_choices=1):\n", " \n", " scores = {}\n", " \n", " for i in range(len(items_list)):\n", " items = items_list[i]\n", " item_scores = item_scores_list[i]\n", " \n", " w = wheel_of_fortune_weights(items, item_scores)\n", " #print(items, item_scores)\n", " #print(w)\n", " \n", " for j, item in enumerate(items):\n", " if item in scores:\n", " scores[item] += w[j]\n", " else:\n", " scores[item] = w[j]\n", " \n", " combined_items = []\n", " combined_scores = []\n", " \n", " for i,s in scores.items():\n", " combined_items.append(i)\n", " combined_scores.append(s)\n", " \n", " combined_scores = np.array(combined_scores)\n", " \n", " #print(combined_scores)\n", " #print(np.sum(combined_scores))\n", " \n", " combined_scores /= len(items_list)\n", " \n", " #print(combined_scores)\n", " \n", " #print(np.sum(combined_scores))\n", " \n", " n = min(len(combined_items), num_choices)\n", " \n", " return np.random.choice(combined_items, size=n, replace=False, p=combined_scores)\n", " \n", " " ] } ], "metadata": { "file_extension": ".py", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 }, "nbformat": 4, "nbformat_minor": 4 }