{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Allocation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The allocation module provides some utils to be used before running A/B test experiments. Groups allocation is the \n",
"process that assigns (allocates) a list of users either to a group A (e.g. control) or to a group B (e.g. treatment). \n",
"This module provides functionalities to randomly allocate users in two or more groups (A/B/C/...).\n",
"\n",
"Let's import first the tools needed."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"pycharm": {
"is_executing": false,
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from abexp.core.allocation import Allocator\n",
"from abexp.core.analysis_frequentist import FrequentistAnalyzer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complete randomization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we want to randomly assign users in *n* groups (where *n*=2) in order to run an A/B test experiment with 2 \n",
"variants, so called control and treatment groups. Complete randomization does not require any data on the user, and in \n",
"practice, it yields balanced design for large-sample sizes."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# Generate random data\n",
"user_id = np.arange(100)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# Run allocation\n",
"df, stats = Allocator.complete_randomization(user_id=user_id, \n",
" ngroups=2,\n",
" prop=[0.4, 0.6],\n",
" seed=42)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" group | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 1 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id group\n",
"0 0 1\n",
"1 1 1\n",
"2 2 1\n",
"3 3 1\n",
"4 4 1"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Users list with group assigned\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" group | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" #users | \n",
" 40 | \n",
" 60 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"group 0 1\n",
"#users 40 60"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Statistics of the randomization: #users per group\n",
"stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: Post-allocation checks can be made to ensure the groups homogeneity and in case of imbalance, a new randomization \n",
"can be performed (see the [Homogeneity check](#homogeneity_check) section below for details)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Blocks randomization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In some case, one would like to consider one or more confounding factor(s) i.e. features which could unbalance the \n",
"groups and bias the results if not taken into account during the randomization process. In this example we want to \n",
"randomly assign users in n groups (where n=3, one control and two treatment groups) considering a confounding factor \n",
"('level'). Users with similar characteristics (level) define a block, and randomization is conducted within a block. \n",
"This enables balanced and homogeneous groups of similar sizes according to the confounding feature."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# Generate random data\n",
"np.random.seed(42)\n",
"df = pd.DataFrame(data={'user_id': np.arange(1000),\n",
" 'level': np.random.randint(1, 6, size=1000)})"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# Run allocation\n",
"df, stats = Allocator.blocks_randomization(df=df, \n",
" id_col='user_id', \n",
" stratum_cols='level',\n",
" ngroups=3, \n",
" seed=42)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" level | \n",
" group | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 4 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 5 | \n",
" 2 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 3 | \n",
" 2 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 5 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id level group\n",
"0 0 4 1\n",
"1 1 5 2\n",
"2 2 3 2\n",
"3 3 5 1\n",
"4 4 5 0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Users data with group assigned\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" group | \n",
" 0 | \n",
" 1 | \n",
" 2 | \n",
"
\n",
" \n",
" level | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 70 | \n",
" 70 | \n",
" 70 | \n",
"
\n",
" \n",
" 2 | \n",
" 64 | \n",
" 63 | \n",
" 63 | \n",
"
\n",
" \n",
" 3 | \n",
" 62 | \n",
" 64 | \n",
" 64 | \n",
"
\n",
" \n",
" 4 | \n",
" 69 | \n",
" 69 | \n",
" 68 | \n",
"
\n",
" \n",
" 5 | \n",
" 68 | \n",
" 68 | \n",
" 68 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"group 0 1 2\n",
"level \n",
"1 70 70 70\n",
"2 64 63 63\n",
"3 62 64 64\n",
"4 69 69 68\n",
"5 68 68 68"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Statistics of the randomization: #users per group in each level\n",
"stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Multi-level block randomization__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can stratify randomization on two or more features. In the example below we want to randomly allocate users in *n* \n",
"groups (where *n*=5) in order to run an A/B test experiment with 5 variants, one control and four treatment groups. The\n",
"stratification will be based on the user level and paying status in order to create homogeneous groups."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# Generate random data\n",
"np.random.seed(42)\n",
"df = pd.DataFrame(data={'user_id': np.arange(1000),\n",
" 'is_paying': np.random.randint(0, 2, size=1000),\n",
" 'level': np.random.randint(1, 7, size=1000)})\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [],
"source": [
"# Run allocation\n",
"df, stats = Allocator.blocks_randomization(df=df, \n",
" id_col='user_id', \n",
" stratum_cols=['level', 'is_paying'], \n",
" ngroups=5,\n",
" seed=42)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" is_paying | \n",
" level | \n",
" group | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 6 | \n",
" 2 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 0 | \n",
" 1 | \n",
" 3 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 0 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id is_paying level group\n",
"0 0 0 6 2\n",
"1 1 1 1 1\n",
"2 2 0 1 0\n",
"3 3 0 1 3\n",
"4 4 0 5 1"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Users data with group assigned\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"pycharm": {
"is_executing": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" group | \n",
" 0 | \n",
" 1 | \n",
" 2 | \n",
" 3 | \n",
" 4 | \n",
"
\n",
" \n",
" level | \n",
" is_paying | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 0 | \n",
" 19 | \n",
" 17 | \n",
" 19 | \n",
" 18 | \n",
" 19 | \n",
"
\n",
" \n",
" 1 | \n",
" 15 | \n",
" 17 | \n",
" 18 | \n",
" 18 | \n",
" 18 | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" 17 | \n",
" 17 | \n",
" 14 | \n",
" 17 | \n",
" 17 | \n",
"
\n",
" \n",
" 1 | \n",
" 18 | \n",
" 17 | \n",
" 16 | \n",
" 18 | \n",
" 17 | \n",
"
\n",
" \n",
" 3 | \n",
" 0 | \n",
" 16 | \n",
" 16 | \n",
" 16 | \n",
" 15 | \n",
" 16 | \n",
"
\n",
" \n",
" 1 | \n",
" 19 | \n",
" 19 | \n",
" 19 | \n",
" 19 | \n",
" 19 | \n",
"
\n",
" \n",
" 4 | \n",
" 0 | \n",
" 12 | \n",
" 12 | \n",
" 12 | \n",
" 12 | \n",
" 11 | \n",
"
\n",
" \n",
" 1 | \n",
" 15 | \n",
" 15 | \n",
" 15 | \n",
" 14 | \n",
" 15 | \n",
"
\n",
" \n",
" 5 | \n",
" 0 | \n",
" 18 | \n",
" 18 | \n",
" 17 | \n",
" 16 | \n",
" 17 | \n",
"
\n",
" \n",
" 1 | \n",
" 17 | \n",
" 18 | \n",
" 19 | \n",
" 18 | \n",
" 19 | \n",
"
\n",
" \n",
" 6 | \n",
" 0 | \n",
" 18 | \n",
" 19 | \n",
" 19 | \n",
" 18 | \n",
" 18 | \n",
"
\n",
" \n",
" 1 | \n",
" 16 | \n",
" 15 | \n",
" 16 | \n",
" 16 | \n",
" 15 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"group 0 1 2 3 4\n",
"level is_paying \n",
"1 0 19 17 19 18 19\n",
" 1 15 17 18 18 18\n",
"2 0 17 17 14 17 17\n",
" 1 18 17 16 18 17\n",
"3 0 16 16 16 15 16\n",
" 1 19 19 19 19 19\n",
"4 0 12 12 12 12 11\n",
" 1 15 15 15 14 15\n",
"5 0 18 18 17 16 17\n",
" 1 17 18 19 18 19\n",
"6 0 18 19 19 18 18\n",
" 1 16 15 16 16 15"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Statistics of the randomization: #users per group in each level and paying status\n",
"stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Homogeneity check\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Complete randomization** does not guarantee homogeneous groups, but it yields balanced design for large-sample sizes. \n",
"**Blocks randomization** guarantees homogeneous groups based on categorical variables (but not on continuous variable).\n",
"\n",
"Thus, we can perform post-allocation checks to ensure the groups homogeneity both for continuous or categorical \n",
"variables. In case of imbalance, a new randomization can be performed."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" points | \n",
" collected_bonus | \n",
" is_paying | \n",
" level | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 202 | \n",
" 6580 | \n",
" 1 | \n",
" 4 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 448 | \n",
" 4075 | \n",
" 0 | \n",
" 5 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 370 | \n",
" 2713 | \n",
" 1 | \n",
" 6 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 206 | \n",
" 3062 | \n",
" 0 | \n",
" 3 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 171 | \n",
" 3976 | \n",
" 0 | \n",
" 5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id points collected_bonus is_paying level\n",
"0 0 202 6580 1 4\n",
"1 1 448 4075 0 5\n",
"2 2 370 2713 1 6\n",
"3 3 206 3062 0 3\n",
"4 4 171 3976 0 5"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Generate random data\n",
"np.random.seed(42)\n",
"df = pd.DataFrame(data={'user_id': np.arange(1000),\n",
" 'points': np.random.randint(100, 500, size=1000),\n",
" 'collected_bonus': np.random.randint(2000, 7000, size=1000),\n",
" 'is_paying': np.random.randint(0, 2, size=1000),\n",
" 'level': np.random.randint(1, 7, size=1000)})\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Single iteration__\n",
"\n",
"In the cell below it is shown a single iteration of check homogeneity analysis."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Run allocation\n",
"df, stats = Allocator.blocks_randomization(df=df, \n",
" id_col='user_id', \n",
" stratum_cols=['level', 'is_paying'], \n",
" ngroups=2,\n",
" seed=42)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" coef | \n",
" std err | \n",
" z | \n",
" P>|z| | \n",
" [0.025 | \n",
" 0.975] | \n",
"
\n",
" \n",
" \n",
" \n",
" user_id | \n",
" -3.000000e-04 | \n",
" 0.000000 | \n",
" -1.505000e+00 | \n",
" 0.132 | \n",
" -0.001000 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" points | \n",
" 2.000000e-04 | \n",
" 0.001000 | \n",
" 3.660000e-01 | \n",
" 0.714 | \n",
" -0.001000 | \n",
" 0.0010 | \n",
"
\n",
" \n",
" collected_bonus | \n",
" 6.935000e-05 | \n",
" 0.000044 | \n",
" 1.559000e+00 | \n",
" 0.119 | \n",
" -0.000018 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" C(is_paying, Treatment('1'))[T.0] | \n",
" 8.000000e-03 | \n",
" 0.127000 | \n",
" 6.300000e-02 | \n",
" 0.950 | \n",
" -0.240000 | \n",
" 0.2560 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.1] | \n",
" -1.180000e-02 | \n",
" 0.215000 | \n",
" -5.500000e-02 | \n",
" 0.956 | \n",
" -0.433000 | \n",
" 0.4090 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.2] | \n",
" 1.440000e-02 | \n",
" 0.226000 | \n",
" 6.400000e-02 | \n",
" 0.949 | \n",
" -0.429000 | \n",
" 0.4580 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.4] | \n",
" -1.646000e-16 | \n",
" 0.213000 | \n",
" -7.740000e-16 | \n",
" 1.000 | \n",
" -0.417000 | \n",
" 0.4170 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.5] | \n",
" -1.628000e-16 | \n",
" 0.215000 | \n",
" -7.570000e-16 | \n",
" 1.000 | \n",
" -0.422000 | \n",
" 0.4220 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.6] | \n",
" -1.628000e-16 | \n",
" 0.214000 | \n",
" -7.590000e-16 | \n",
" 1.000 | \n",
" -0.420000 | \n",
" 0.4200 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" coef std err z \\\n",
"user_id -3.000000e-04 0.000000 -1.505000e+00 \n",
"points 2.000000e-04 0.001000 3.660000e-01 \n",
"collected_bonus 6.935000e-05 0.000044 1.559000e+00 \n",
"C(is_paying, Treatment('1'))[T.0] 8.000000e-03 0.127000 6.300000e-02 \n",
"C(level, Treatment('3'))[T.1] -1.180000e-02 0.215000 -5.500000e-02 \n",
"C(level, Treatment('3'))[T.2] 1.440000e-02 0.226000 6.400000e-02 \n",
"C(level, Treatment('3'))[T.4] -1.646000e-16 0.213000 -7.740000e-16 \n",
"C(level, Treatment('3'))[T.5] -1.628000e-16 0.215000 -7.570000e-16 \n",
"C(level, Treatment('3'))[T.6] -1.628000e-16 0.214000 -7.590000e-16 \n",
"\n",
" P>|z| [0.025 0.975] \n",
"user_id 0.132 -0.001000 0.0001 \n",
"points 0.714 -0.001000 0.0010 \n",
"collected_bonus 0.119 -0.000018 0.0000 \n",
"C(is_paying, Treatment('1'))[T.0] 0.950 -0.240000 0.2560 \n",
"C(level, Treatment('3'))[T.1] 0.956 -0.433000 0.4090 \n",
"C(level, Treatment('3'))[T.2] 0.949 -0.429000 0.4580 \n",
"C(level, Treatment('3'))[T.4] 1.000 -0.417000 0.4170 \n",
"C(level, Treatment('3'))[T.5] 1.000 -0.422000 0.4220 \n",
"C(level, Treatment('3'))[T.6] 1.000 -0.420000 0.4200 "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Run homogeneity check analysis\n",
"X = df.drop(columns=['group'])\n",
"y = df['group']\n",
"\n",
"analyzer = FrequentistAnalyzer()\n",
"analysis = analyzer.check_homogeneity(X, y, cat_cols=['is_paying','level'])\n",
"\n",
"analysis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``check_homogeneity`` function performs univariate logistic regression per each feature of the input dataset. If the \n",
"p-value (column ``P>|z|`` in the table above) of any variables is below a certain threshold (e.g. ``threshold = 0.2``), \n",
"the random allocation is considered to be non homogeneous and it must be repeated. For instance, in the table above the \n",
"variable ``collected_bonus`` is not homogeneously split across groups ``p-value = 0.119``."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Multiple iterations__"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" user_id | \n",
" points | \n",
" collected_bonus | \n",
" is_paying | \n",
" level | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 202 | \n",
" 6580 | \n",
" 1 | \n",
" 4 | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" 448 | \n",
" 4075 | \n",
" 0 | \n",
" 5 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" 370 | \n",
" 2713 | \n",
" 1 | \n",
" 6 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 206 | \n",
" 3062 | \n",
" 0 | \n",
" 3 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 171 | \n",
" 3976 | \n",
" 0 | \n",
" 5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" user_id points collected_bonus is_paying level\n",
"0 0 202 6580 1 4\n",
"1 1 448 4075 0 5\n",
"2 2 370 2713 1 6\n",
"3 3 206 3062 0 3\n",
"4 4 171 3976 0 5"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Generate random data\n",
"np.random.seed(42)\n",
"df = pd.DataFrame(data={'user_id': np.arange(1000),\n",
" 'points': np.random.randint(100, 500, size=1000),\n",
" 'collected_bonus': np.random.randint(2000, 7000, size=1000),\n",
" 'is_paying': np.random.randint(0, 2, size=1000),\n",
" 'level': np.random.randint(1, 7, size=1000)})\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the cell below we repeatedly perform random allocation until it creates homogeneous groups (up to a maximum number \n",
"of iterations). The groups are considered to be homogeneous when the p-value (column ``P>|z|``) of any variables is \n",
"below a certain threshold (e.g. ``p-values < 0.2``). "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" coef | \n",
" std err | \n",
" z | \n",
" P>|z| | \n",
" [0.025 | \n",
" 0.975] | \n",
"
\n",
" \n",
" \n",
" \n",
" user_id | \n",
" -1.000000e-04 | \n",
" 0.000000 | \n",
" -5.640000e-01 | \n",
" 0.573 | \n",
" -0.001000 | \n",
" 0.000 | \n",
"
\n",
" \n",
" points | \n",
" 2.000000e-04 | \n",
" 0.001000 | \n",
" 3.200000e-01 | \n",
" 0.749 | \n",
" -0.001000 | \n",
" 0.001 | \n",
"
\n",
" \n",
" collected_bonus | \n",
" 2.449000e-05 | \n",
" 0.000044 | \n",
" 5.520000e-01 | \n",
" 0.581 | \n",
" -0.000063 | \n",
" 0.000 | \n",
"
\n",
" \n",
" C(is_paying, Treatment('1'))[T.0] | \n",
" 1.570000e-02 | \n",
" 0.127000 | \n",
" 1.240000e-01 | \n",
" 0.901 | \n",
" -0.232000 | \n",
" 0.264 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.1] | \n",
" -1.180000e-02 | \n",
" 0.215000 | \n",
" -5.500000e-02 | \n",
" 0.956 | \n",
" -0.433000 | \n",
" 0.409 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.2] | \n",
" -1.440000e-02 | \n",
" 0.226000 | \n",
" -6.400000e-02 | \n",
" 0.949 | \n",
" -0.458000 | \n",
" 0.429 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.4] | \n",
" -9.064000e-17 | \n",
" 0.213000 | \n",
" -4.260000e-16 | \n",
" 1.000 | \n",
" -0.417000 | \n",
" 0.417 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.5] | \n",
" -9.236000e-17 | \n",
" 0.215000 | \n",
" -4.290000e-16 | \n",
" 1.000 | \n",
" -0.422000 | \n",
" 0.422 | \n",
"
\n",
" \n",
" C(level, Treatment('3'))[T.6] | \n",
" -9.237000e-17 | \n",
" 0.214000 | \n",
" -4.310000e-16 | \n",
" 1.000 | \n",
" -0.420000 | \n",
" 0.420 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" coef std err z \\\n",
"user_id -1.000000e-04 0.000000 -5.640000e-01 \n",
"points 2.000000e-04 0.001000 3.200000e-01 \n",
"collected_bonus 2.449000e-05 0.000044 5.520000e-01 \n",
"C(is_paying, Treatment('1'))[T.0] 1.570000e-02 0.127000 1.240000e-01 \n",
"C(level, Treatment('3'))[T.1] -1.180000e-02 0.215000 -5.500000e-02 \n",
"C(level, Treatment('3'))[T.2] -1.440000e-02 0.226000 -6.400000e-02 \n",
"C(level, Treatment('3'))[T.4] -9.064000e-17 0.213000 -4.260000e-16 \n",
"C(level, Treatment('3'))[T.5] -9.236000e-17 0.215000 -4.290000e-16 \n",
"C(level, Treatment('3'))[T.6] -9.237000e-17 0.214000 -4.310000e-16 \n",
"\n",
" P>|z| [0.025 0.975] \n",
"user_id 0.573 -0.001000 0.000 \n",
"points 0.749 -0.001000 0.001 \n",
"collected_bonus 0.581 -0.000063 0.000 \n",
"C(is_paying, Treatment('1'))[T.0] 0.901 -0.232000 0.264 \n",
"C(level, Treatment('3'))[T.1] 0.956 -0.433000 0.409 \n",
"C(level, Treatment('3'))[T.2] 0.949 -0.458000 0.429 \n",
"C(level, Treatment('3'))[T.4] 1.000 -0.417000 0.417 \n",
"C(level, Treatment('3'))[T.5] 1.000 -0.422000 0.422 \n",
"C(level, Treatment('3'))[T.6] 1.000 -0.420000 0.420 "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define parameters\n",
"rep = 100\n",
"threshold = 0.2\n",
"\n",
"analyzer = FrequentistAnalyzer()\n",
"\n",
"for i in np.arange(rep):\n",
" \n",
" # Run allocation\n",
" df, stats = Allocator.blocks_randomization(df=df, \n",
" id_col='user_id', \n",
" stratum_cols=['level', 'is_paying'], \n",
" ngroups=2,\n",
" seed=i + 45)\n",
" # Run homogeneity check analysis \n",
" X = df.drop(columns=['group'])\n",
" y = df['group']\n",
"\n",
" analysis = analyzer.check_homogeneity(X, y, cat_cols=['is_paying','level'])\n",
" \n",
" # Check p-values\n",
" if all(analysis['P>|z|'] > threshold): \n",
" break\n",
" \n",
" df = df.drop(columns=['group'])\n",
"\n",
"analysis"
]
}
],
"metadata": {
"hide_input": false,
"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.6"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}