You are here

public function AcquiaLiftLearnReport::getAggregatedData in Acquia Lift Connector 7.2

Returns the per-variation confidence report data aggregated over dates.

Return value

array An array of report data keyed by option id.

2 calls to AcquiaLiftLearnReport::getAggregatedData()
AcquiaLiftLearnReport::getOverallConfidence in includes/AcquiaLiftLearnReport.inc
Gets the overall confidence of the report, based on aggregated data.
AcquiaLiftLearnReport::getOverallWinner in includes/AcquiaLiftLearnReport.inc
Gets the overall winner of the campaign, based on aggregated data.

File

includes/AcquiaLiftLearnReport.inc, line 235

Class

AcquiaLiftLearnReport
Class for Acquia Lift Learn Reports.

Code

public function getAggregatedData() {
  if (empty($this->interpretedResults)) {
    $this->interpretedResults = array(
      'confidence' => FALSE,
      'winner' => NULL,
      'data' => array(),
    );
    $report_data = $this
      ->getAggregateResults();
    if (empty($report_data['totals_by_var'])) {
      $this->hasData = FALSE;
      return array();
    }
    $data = array();
    $i = $total_means = $mean_control = $var_control = $num_control = 0;
    $num_variations = count($report_data['totals_by_var']);

    // We'll be getting a confidence interval for the conversion rate of each
    // variation based on the configured confidence measure. We need to convert
    // this into a p-value to look up the correct normal quantile.
    $desired_p_value = (1 - $this->confidence_measure / 100) / 2;
    if (in_array($desired_p_value, self::$probabilities)) {
      $pos = array_search($desired_p_value, self::$probabilities);
    }
    else {
      $pos = 0;
      while ($desired_p_value < self::$probabilities[$pos]) {
        $pos++;
      }
    }
    $quantile = self::$normal_quantiles[$pos];
    $min_decisions_reached = TRUE;
    foreach ($report_data['totals_by_var'] as $choice_id => $results) {
      $count = $results['total_plays_explore'];
      if ($count < self::MIN_DECISION_THRESHOLD) {
        $min_decisions_reached = FALSE;
      }
      $goals = $results['total_goals_explore'];
      $val = $results['total_goals_value_explore'];
      $rate = $count > 0 ? $goals / $count : 0;
      $mean = $count > 0 ? $val / $count : 0;

      // @todo We are making an assumption here that all goals received had the
      //   same value. In the rare cases where there are multiple goals of
      //   different values, this will result in an inaccurate calculation of
      //   the variance. We will be fixing this in our next gen reporting.
      $goal_value = $goals ? floor($val / $goals) : 0;

      // Calculate confidence bounds for conversion rate.
      $sd = $count ? sqrt($rate * (1 - $rate) / $count) : 0;
      $upper = $goal_value * ($rate + $quantile * $sd);
      $lower = $goal_value * ($rate - $quantile * $sd);
      $variance = $count > 1 ? ($goals * pow($goal_value - $mean, 2) + ($count - $goals) * pow($mean, 2)) / ($count - 1) : 0;
      $option_label = $this
        ->getOptionLabelForChoice($choice_id);
      $data[$choice_id] = array(
        'counter' => $i,
        'option_id' => $choice_id,
        'option_label' => $option_label,
        'goals' => $goals,
        'count' => $count,
        'mean' => $mean,
        'variance' => $variance,
        'conversion' => _format_report_percentage($rate),
        'estimated_value' => _format_report_number($mean, TRUE, 4),
        'estimated_higher' => _format_report_number($upper, TRUE, 4),
        'estimated_lower' => _format_report_number($lower, TRUE, 4),
        'margin_error' => _format_report_number(($upper - $lower) / 2, TRUE, 4),
        'control' => $i === 0,
        'confidence' => self::NA_STRING,
        'lift_default' => self::NA_STRING,
        'lift_random' => self::NA_STRING,
      );
      $total_means += $mean;
      $i++;
    }
    $control_variation = reset($data);

    // Now we can do some comparisons of the data.
    $rand_mean = $total_means / $num_variations;
    foreach ($data as $choice_id => &$values) {

      // Calculate lift over random.
      if ($rand_mean) {
        $lift_random = ($values['estimated_value'] - $rand_mean) / $rand_mean * 100;
      }
      else {
        $lift_random = 0;
      }
      $values['lift_random'] = $values['count'] > 0 ? $lift_random : self::NA_STRING;

      // It only makes sense to calculate lift over default if there has been
      // at least one decision for each variation and we have a non-zero value
      // for the mean of the default.
      if (!$values['control'] && $control_variation['count'] && $control_variation['mean'] && $values['count']) {
        $lift_default = ($values['mean'] - $control_variation['mean']) / $control_variation['mean'];
        $values['lift_default'] = _format_report_percentage($lift_default, TRUE, TRUE, 2);
      }

      // Only calculate confidence if our minimum decision threshold was reached
      // for each variation.
      if ($min_decisions_reached) {
        $other_variations = $data;
        unset($other_variations[$choice_id]);
        $this
          ->doComparisons($values, $other_variations);
      }
    }
    $this->interpretedResults['data'] = $data;
  }
  return $this->interpretedResults['data'];
}