Mike Miller
Mike Miller

Reputation: 85

Split testing with weighting

I am looking to figure out how to use a database of sales completions to influence split testing.

For example, say I have four different page layouts and for each I have the following stats:

Then it would make sense to have version 1 shown most often, and version 4 being next, while version 2 should hardly be shown at all.

Any ideas how I can achieve this?

Upvotes: 0

Views: 180

Answers (2)

Vyktor
Vyktor

Reputation: 21007

Let's say you have display the layouts with following weights:

Version 1: 6
Version 4: 4
Version 3: 3
Version 2: 1

Sum of weight is 14 and weight 6 means that you want to show page approximately 6 times in 14 requests.

If you were using database (which I assume you do it would be)

SELECT SUM(weights) FROM version;

Easiest way to implement random selection with different probabilities of hitting item is to sum weights, sort items by their weights and than just iterate trough all items until you hit zero:

$i = rand( 1, $sum);
foreach( $items as $item){
     $i -= $item->weight;
     if( $i <= 0){
        break;
     }
}
// $item is now your desired $item

$items should be sorted list of class Item{ public $weight; ... }, because it's most probable that the first element will be used (second the second and so on) and least iterations will be required

What's happening inside:

$i = 12;
// first iteration, $weight = 6
$i = 6; // condition didn't match
// second iteration, $weight = 4
$i = 2; // condition didn't match
// third iteration, $weight = 3
$i = -1; // condition matched
// using version 3

Upvotes: 0

Joe
Joe

Reputation: 15812

Very simple solution, mainly depends on how your data looks currently as to what solution is easiest though.

$sales = array
(
    1 => 6,
    2 => 1,
    3 => 3,
    4 => 4
);

$weight = array();

foreach ($sales AS $layout => $num_sales)
{
    for ($i = 0; $i < $num_sales; $i++)
    {
        $weight[] = $layout;
    }
}

/*
$weight = array
(
    1, 1, 1, 1, 1, 1,
    2,
    3, 3, 3,
    4, 4, 4, 4
);
*/

// Pick a random one to use
$layout_to_use = $weight[rand(0, count($weight))];

Upvotes: 1

Related Questions