/** * Given a dataset, the method calculates the absolute, relative and * cumulative frequency of each generated class. * * @param array $data */ public static function frequencyContinuous($data = []) { // Make sure that the elements are numeric array_map("self::isNumeric", $data); // Count the elements $items_count = self::countItems($data); // Get the number of classes $classes_num = self::getNumberOfClasses($items_count); // Get the bottom and top ranges $range_margins = Basic::range($data); $range = $range_margins->max - $range_margins->min; // Calculate the group size $group_size = self::calcGroupSize($range, $classes_num); // Create the classes $classes = self::createClasses($range_margins->min, $group_size, $classes_num); // Calculate the absolute, relative and cumulative frequency of each class $classes = self::calcFrequencyContinuous($data, $classes); // Return the classes return $classes; }
/** * Create a function to calculate the exponential regression of a dataset. * * @param array $arrayY set of values. * @return array a function to accept X values and * return corresponding regression Y values and a coefficient of determination */ public static function exponentialRegression($arrayY) { $arrayLength = count($arrayY); $arrayX = Basic::range(1, $arrayLength); $xSum = Basic::sum($arrayX); $yLog = array_map('log', $arrayY); $yLogSum = Basic::sum($yLog); $xSquaredSum = Basic::sum(array_map('NumbersPHP\\Basic::square', $arrayX)); $xyLogSum = Basic::sum(array_map(create_function('$x, $yLog', 'return $x * $yLog;'), $arrayX, $yLog)); $a = ($yLogSum * $xSquaredSum - $xSum * $xyLogSum) / ($arrayLength * $xSquaredSum - $xSum * $xSum); $b = ($arrayLength * $xyLogSum - $xSum * $yLogSum) / ($arrayLength * $xSquaredSum - $xSum * $xSum); $function = create_function('$x', 'if(is_array($x)) {' . 'foreach($x as &$value)' . '$value = exp(' . $a . ') * exp(' . $b . ' * $value);' . 'return $x;' . '}' . 'else ' . 'return exp(' . $a . ') * exp(' . $b . ' * $x);'); return array($function, self::rSquared($arrayY, array_map($function, $arrayX))); }