original_string: protected function weighted_variance(array $assessments) {
          $first = reset($assessments);
          if (empty($first)) {
              return null;
          }
          $dimids = array_keys($first->dimgrades);
          $asids  = array_keys($assessments);
          $vars   = array();  // to be returned
          foreach ($dimids as $dimid) {
              $n = 0;
              $s = 0;
              $sumweight = 0;
              foreach ($asids as $asid) {
                  $x = $assessments[$asid]->dimgrades[$dimid];    // value (data point)
                  $weight = $assessments[$asid]->weight;          // the values' weight
                  if ($weight == 0) {
                      continue;
                  }
                  if ($n == 0) {
                      $n = 1;
                      $mean = $x;
                      $s = 0;
                      $sumweight = $weight;
                  } else {
                      $n++;
                      $temp = $weight + $sumweight;
                      $q = $x - $mean;
                      $r = $q * $weight / $temp;
                      $s = $s + $sumweight * $q * $r;
                      $mean = $mean + $r;
                      $sumweight = $temp;
                  }
              }
              if ($sumweight > 0 and $n > 1) {
                  // for the sample: $vars[$dimid] = ($s * $n) / (($n - 1) * $sumweight);
                  // for the population:
                  $vars[$dimid] = $s / $sumweight;
              } else {
                  $vars[$dimid] = null;
              }
          }
          return $vars;
      }
code: protected function weighted_variance(array $assessments) {
          $first = reset($assessments);
          if (empty($first)) {
              return null;
          }
          $dimids = array_keys($first->dimgrades);
          $asids  = array_keys($assessments);
          $vars   = array();  // to be returned
          foreach ($dimids as $dimid) {
              $n = 0;
              $s = 0;
              $sumweight = 0;
              foreach ($asids as $asid) {
                  $x = $assessments[$asid]->dimgrades[$dimid];    // value (data point)
                  $weight = $assessments[$asid]->weight;          // the values' weight
                  if ($weight == 0) {
                      continue;
                  }
                  if ($n == 0) {
                      $n = 1;
                      $mean = $x;
                      $s = 0;
                      $sumweight = $weight;
                  } else {
                      $n++;
                      $temp = $weight + $sumweight;
                      $q = $x - $mean;
                      $r = $q * $weight / $temp;
                      $s = $s + $sumweight * $q * $r;
                      $mean = $mean + $r;
                      $sumweight = $temp;
                  }
              }
              if ($sumweight > 0 and $n > 1) {
                  // for the sample: $vars[$dimid] = ($s * $n) / (($n - 1) * $sumweight);
                  // for the population:
                  $vars[$dimid] = $s / $sumweight;
              } else {
                  $vars[$dimid] = null;
              }
          }
          return $vars;
      }
code_tokens: protected function weighted_variance ( array $ assessments ) { $ first = reset ( $ assessments ) ; if ( empty ( $ first ) ) { return null ; } $ dimids = array_keys ( $ first -> dimgrades ) ; $ asids = array_keys ( $ assessments ) ; $ vars = array ( ) ; // to be returned foreach ( $ dimids as $ dimid ) { $ n = 0 ; $ s = 0 ; $ sumweight = 0 ; foreach ( $ asids as $ asid ) { $ x = $ assessments [ $ asid ] -> dimgrades [ $ dimid ] ; // value (data point) $ weight = $ assessments [ $ asid ] -> weight ; // the values' weight if ( $ weight == 0 ) { continue ; } if ( $ n == 0 ) { $ n = 1 ; $ mean = $ x ; $ s = 0 ; $ sumweight = $ weight ; } else { $ n ++ ; $ temp = $ weight + $ sumweight ; $ q = $ x - $ mean ; $ r = $ q * $ weight / $ temp ; $ s = $ s + $ sumweight * $ q * $ r ; $ mean = $ mean + $ r ; $ sumweight = $ temp ; } } if ( $ sumweight > 0 and $ n > 1 ) { // for the sample: $vars[$dimid] = ($s * $n) / (($n - 1) * $sumweight); // for the population: $ vars [ $ dimid ] = $ s / $ sumweight ; } else { $ vars [ $ dimid ] = null ; } } return $ vars ; }
docstring: Given a set of a submission's assessments, returns standard deviations of all their dimensions
  
  The passed structure must be array of assessments objects with at least ->weight
  and ->dimgrades properties. This implementation uses weighted incremental algorithm as
  suggested in "D. H. D. West (1979). Communications of the ACM, 22, 9, 532-535:
  Updating Mean and Variance Estimates: An Improved Method"
  {@link http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Weighted_incremental_algorithm}
  
  @param array $assessments as prepared by {@link self::prepare_data_from_recordset()}
  @return null|array indexed by dimension id
docstring_tokens: Given a set of a submission s assessments returns standard deviations of all their dimensions