{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rQbXaUeIB8CB"
   },
   "source": [
    "## Prerequisites\n",
    "\n",
    "Run this cell to prepare the environment. This step is obligatory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 40255,
     "status": "ok",
     "timestamp": 1683128857241,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "kNJ3aTwQx7JZ",
    "outputId": "fb8e3fe8-51eb-40f6-9657-e4b7edd6aff5",
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "!pip install retentioneering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "_d7jIjDbxeF5"
   },
   "outputs": [],
   "source": [
    "import retentioneering\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "RUTq2sSXB9m5"
   },
   "source": [
    "## Stattests\n",
    "\n",
    "The full text of [Stattests](https://doc.retentioneering.com/stable/doc/user_guides/stattests.html) user guide is available on the retentioneering website.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "sQfcdtIJDOjg"
   },
   "source": [
    "## Loading data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "fq_XfrkqBuOj"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from retentioneering import datasets\n",
    "\n",
    "stream = datasets.load_simple_shop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "h9mfnbyZDqRZ"
   },
   "source": [
    "## General stattests usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 474,
     "status": "ok",
     "timestamp": 1682603220664,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "oR66cpQtj7q6",
    "outputId": "61f31103-2713-44fa-d33c-b8a1329ad2af"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[219483890 964964743 629881394 ... 901422808 523047643 724268790]\n",
      "[315196393 443659932 865093748 ... 965024600 831491833 962761227]\n"
     ]
    }
   ],
   "source": [
    "data = stream.to_dataframe()\n",
    "users = data['user_id'].unique()\n",
    "index_separator = int(users.shape[0]/2)\n",
    "user_groups = users[:index_separator], users[index_separator:]\n",
    "\n",
    "print(user_groups[0])\n",
    "print(user_groups[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "-25v7HXzkW3o"
   },
   "outputs": [],
   "source": [
    "def cart_share(df):\n",
    "    return len(df[df['event'] == 'cart']) / len(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 326,
     "status": "ok",
     "timestamp": 1682603329228,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "124jBlCHkyyY",
    "outputId": "4df1b49e-c91b-4e76-a174-b13451913566"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15384615384615385"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "some_user = user_groups[0][378]\n",
    "cart_share(data[data['user_id'] == some_user])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 3150,
     "status": "ok",
     "timestamp": 1682604123965,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "Yy5EahD3lSzV",
    "outputId": "2b9df3f2-d43f-4dc8-8362-7cdebe0dbac0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random_group_1 (mean ± SD): 0.075 ± 0.095, n = 1875\n",
      "random_group_2 (mean ± SD): 0.078 ± 0.102, n = 1876\n",
      "'random_group_1' is greater than 'random_group_2' with p-value: 0.21369\n",
      "power of the test: 8.85%\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<retentioneering.tooling.stattests.stattests.StatTests at 0x7f5a977465e0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stream.stattests(\n",
    "    groups=user_groups,\n",
    "    func=cart_share,\n",
    "    group_names=['random_group_1', 'random_group_2'],\n",
    "    test='ttest'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1PbbSM1Flgeo"
   },
   "source": [
    "### Test power"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 3643,
     "status": "ok",
     "timestamp": 1682604154402,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "TlUufhN4lYzA",
    "outputId": "6b0491e0-0674-4d65-f1ab-8816129a9082"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "random_group_1 (mean ± SD): 0.075 ± 0.095, n = 1875\n",
      "random_group_2 (mean ± SD): 0.078 ± 0.102, n = 1876\n",
      "'random_group_1' is greater than 'random_group_2' with p-value: 0.21369\n",
      "power of the test: 2.11%\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<retentioneering.tooling.stattests.stattests.StatTests at 0x7f5a97746fd0>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stream.stattests(\n",
    "    groups=user_groups,\n",
    "    func=cart_share,\n",
    "    group_names=['random_group_1', 'random_group_2'],\n",
    "    test='ttest',\n",
    "    alpha=0.01\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Ke8Wy3khmvXW"
   },
   "source": [
    "### Categorical variables\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "MdFdd0qboAhU"
   },
   "outputs": [],
   "source": [
    "user_group_1 = set(data[data['event'] == 'product1']['user_id'])\n",
    "user_group_2 = set(data[data['event'] == 'product2']['user_id'])\n",
    "\n",
    "user_group_1 -= user_group_1 & user_group_2\n",
    "user_group_2 -= user_group_1 & user_group_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "executionInfo": {
     "elapsed": 9,
     "status": "ok",
     "timestamp": 1682604362598,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "0O0usnq7oBrt",
    "outputId": "97eadb09-4023-4f57-bbd3-823d2425ab2e"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "string"
      },
      "text/plain": [
       "'2'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cart_count(df):\n",
    "    cart_count = len(df[df['event'] == 'cart'])\n",
    "    if cart_count <= 2:\n",
    "        return str(cart_count)\n",
    "    return '>2'\n",
    "\n",
    "some_user = user_groups[0][378]\n",
    "cart_count(data[data['user_id'] == some_user])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "executionInfo": {
     "elapsed": 334,
     "status": "ok",
     "timestamp": 1682604377152,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "x1mUaTxOoHBK",
    "outputId": "f77e0088-28c3-497f-bcbf-dd60e76d5ba1"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "string"
      },
      "text/plain": [
       "'0'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "some_user = user_groups[0][379]\n",
    "cart_count(data[data['user_id'] == some_user])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Kl4LIOJRn823"
   },
   "source": [
    "### Chi2 contingency test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 2251,
     "status": "ok",
     "timestamp": 1682604400755,
     "user": {
      "displayName": "Julia Ostanina",
      "userId": "13687663492290466770"
     },
     "user_tz": -120
    },
    "id": "Yw_Z699soSYu",
    "outputId": "67151b8c-2e98-401a-fbcb-87cb75103d63"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "product_1_group (size): n = 580\n",
      "product_2_group (size): n = 1430\n",
      "Group difference test with p-value: 0.00000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<retentioneering.tooling.stattests.stattests.StatTests at 0x7f5a983deca0>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stream.stattests(\n",
    "    groups=(user_group_1, user_group_2),\n",
    "    func=cart_count,\n",
    "    group_names=('product_1_group', 'product_2_group'),\n",
    "    test='chi2_contingency'\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "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.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
