ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Trends.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
9 class Trends
10 {
11  private static function filterTrendValues(array &$array1, array &$array2): void
12  {
13  foreach ($array1 as $key => $value) {
14  if ((is_bool($value)) || (is_string($value)) || ($value === null)) {
15  unset($array1[$key], $array2[$key]);
16  }
17  }
18  }
19 
20  private static function checkTrendArrays(&$array1, &$array2): void
21  {
22  if (!is_array($array1)) {
23  $array1 = [$array1];
24  }
25  if (!is_array($array2)) {
26  $array2 = [$array2];
27  }
28 
29  $array1 = Functions::flattenArray($array1);
30  $array2 = Functions::flattenArray($array2);
31 
32  self::filterTrendValues($array1, $array2);
33  self::filterTrendValues($array2, $array1);
34 
35  // Reset the array indexes
36  $array1 = array_merge($array1);
37  $array2 = array_merge($array2);
38  }
39 
40  protected static function validateTrendArrays(array $yValues, array $xValues): void
41  {
42  $yValueCount = count($yValues);
43  $xValueCount = count($xValues);
44 
45  if (($yValueCount === 0) || ($yValueCount !== $xValueCount)) {
46  throw new Exception(Functions::NA());
47  } elseif ($yValueCount === 1) {
48  throw new Exception(Functions::DIV0());
49  }
50  }
51 
62  public static function CORREL($yValues, $xValues = null)
63  {
64  if (($xValues === null) || (!is_array($yValues)) || (!is_array($xValues))) {
65  return Functions::VALUE();
66  }
67 
68  try {
69  self::checkTrendArrays($yValues, $xValues);
70  self::validateTrendArrays($yValues, $xValues);
71  } catch (Exception $e) {
72  return $e->getMessage();
73  }
74 
75  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
76 
77  return $bestFitLinear->getCorrelation();
78  }
79 
90  public static function COVAR($yValues, $xValues)
91  {
92  try {
93  self::checkTrendArrays($yValues, $xValues);
94  self::validateTrendArrays($yValues, $xValues);
95  } catch (Exception $e) {
96  return $e->getMessage();
97  }
98 
99  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
100 
101  return $bestFitLinear->getCovariance();
102  }
103 
116  public static function FORECAST($xValue, $yValues, $xValues)
117  {
118  $xValue = Functions::flattenSingleValue($xValue);
119 
120  try {
121  $xValue = StatisticalValidations::validateFloat($xValue);
122  self::checkTrendArrays($yValues, $xValues);
123  self::validateTrendArrays($yValues, $xValues);
124  } catch (Exception $e) {
125  return $e->getMessage();
126  }
127 
128  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
129 
130  return $bestFitLinear->getValueOfYForX($xValue);
131  }
132 
145  public static function GROWTH($yValues, $xValues = [], $newValues = [], $const = true)
146  {
147  $yValues = Functions::flattenArray($yValues);
148  $xValues = Functions::flattenArray($xValues);
149  $newValues = Functions::flattenArray($newValues);
150  $const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
151 
152  $bestFitExponential = Trend::calculate(Trend::TREND_EXPONENTIAL, $yValues, $xValues, $const);
153  if (empty($newValues)) {
154  $newValues = $bestFitExponential->getXValues();
155  }
156 
157  $returnArray = [];
158  foreach ($newValues as $xValue) {
159  $returnArray[0][] = [$bestFitExponential->getValueOfYForX($xValue)];
160  }
161 
162  return $returnArray;
163  }
164 
175  public static function INTERCEPT($yValues, $xValues)
176  {
177  try {
178  self::checkTrendArrays($yValues, $xValues);
179  self::validateTrendArrays($yValues, $xValues);
180  } catch (Exception $e) {
181  return $e->getMessage();
182  }
183 
184  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
185 
186  return $bestFitLinear->getIntersect();
187  }
188 
202  public static function LINEST($yValues, $xValues = null, $const = true, $stats = false)
203  {
204  $const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
205  $stats = ($stats === null) ? false : (bool) Functions::flattenSingleValue($stats);
206  if ($xValues === null) {
207  $xValues = $yValues;
208  }
209 
210  try {
211  self::checkTrendArrays($yValues, $xValues);
212  self::validateTrendArrays($yValues, $xValues);
213  } catch (Exception $e) {
214  return $e->getMessage();
215  }
216 
217  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues, $const);
218 
219  if ($stats === true) {
220  return [
221  [
222  $bestFitLinear->getSlope(),
223  $bestFitLinear->getIntersect(),
224  ],
225  [
226  $bestFitLinear->getSlopeSE(),
227  ($const === false) ? Functions::NA() : $bestFitLinear->getIntersectSE(),
228  ],
229  [
230  $bestFitLinear->getGoodnessOfFit(),
231  $bestFitLinear->getStdevOfResiduals(),
232  ],
233  [
234  $bestFitLinear->getF(),
235  $bestFitLinear->getDFResiduals(),
236  ],
237  [
238  $bestFitLinear->getSSRegression(),
239  $bestFitLinear->getSSResiduals(),
240  ],
241  ];
242  }
243 
244  return [
245  $bestFitLinear->getSlope(),
246  $bestFitLinear->getIntersect(),
247  ];
248  }
249 
263  public static function LOGEST($yValues, $xValues = null, $const = true, $stats = false)
264  {
265  $const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
266  $stats = ($stats === null) ? false : (bool) Functions::flattenSingleValue($stats);
267  if ($xValues === null) {
268  $xValues = $yValues;
269  }
270 
271  try {
272  self::checkTrendArrays($yValues, $xValues);
273  self::validateTrendArrays($yValues, $xValues);
274  } catch (Exception $e) {
275  return $e->getMessage();
276  }
277 
278  foreach ($yValues as $value) {
279  if ($value < 0.0) {
280  return Functions::NAN();
281  }
282  }
283 
284  $bestFitExponential = Trend::calculate(Trend::TREND_EXPONENTIAL, $yValues, $xValues, $const);
285 
286  if ($stats === true) {
287  return [
288  [
289  $bestFitExponential->getSlope(),
290  $bestFitExponential->getIntersect(),
291  ],
292  [
293  $bestFitExponential->getSlopeSE(),
294  ($const === false) ? Functions::NA() : $bestFitExponential->getIntersectSE(),
295  ],
296  [
297  $bestFitExponential->getGoodnessOfFit(),
298  $bestFitExponential->getStdevOfResiduals(),
299  ],
300  [
301  $bestFitExponential->getF(),
302  $bestFitExponential->getDFResiduals(),
303  ],
304  [
305  $bestFitExponential->getSSRegression(),
306  $bestFitExponential->getSSResiduals(),
307  ],
308  ];
309  }
310 
311  return [
312  $bestFitExponential->getSlope(),
313  $bestFitExponential->getIntersect(),
314  ];
315  }
316 
328  public static function RSQ($yValues, $xValues)
329  {
330  try {
331  self::checkTrendArrays($yValues, $xValues);
332  self::validateTrendArrays($yValues, $xValues);
333  } catch (Exception $e) {
334  return $e->getMessage();
335  }
336 
337  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
338 
339  return $bestFitLinear->getGoodnessOfFit();
340  }
341 
352  public static function SLOPE($yValues, $xValues)
353  {
354  try {
355  self::checkTrendArrays($yValues, $xValues);
356  self::validateTrendArrays($yValues, $xValues);
357  } catch (Exception $e) {
358  return $e->getMessage();
359  }
360 
361  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
362 
363  return $bestFitLinear->getSlope();
364  }
365 
376  public static function STEYX($yValues, $xValues)
377  {
378  try {
379  self::checkTrendArrays($yValues, $xValues);
380  self::validateTrendArrays($yValues, $xValues);
381  } catch (Exception $e) {
382  return $e->getMessage();
383  }
384 
385  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues);
386 
387  return $bestFitLinear->getStdevOfResiduals();
388  }
389 
402  public static function TREND($yValues, $xValues = [], $newValues = [], $const = true)
403  {
404  $yValues = Functions::flattenArray($yValues);
405  $xValues = Functions::flattenArray($xValues);
406  $newValues = Functions::flattenArray($newValues);
407  $const = ($const === null) ? true : (bool) Functions::flattenSingleValue($const);
408 
409  $bestFitLinear = Trend::calculate(Trend::TREND_LINEAR, $yValues, $xValues, $const);
410  if (empty($newValues)) {
411  $newValues = $bestFitLinear->getXValues();
412  }
413 
414  $returnArray = [];
415  foreach ($newValues as $xValue) {
416  $returnArray[0][] = [$bestFitLinear->getValueOfYForX($xValue)];
417  }
418 
419  return $returnArray;
420  }
421 }
static FORECAST($xValue, $yValues, $xValues)
FORECAST.
Definition: Trends.php:116
static STEYX($yValues, $xValues)
STEYX.
Definition: Trends.php:376
static validateTrendArrays(array $yValues, array $xValues)
Definition: Trends.php:40
static TREND($yValues, $xValues=[], $newValues=[], $const =true)
TREND.
Definition: Trends.php:402
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static calculate($trendType=self::TREND_BEST_FIT, $yValues=[], $xValues=[], $const =true)
Definition: Trend.php:51
static INTERCEPT($yValues, $xValues)
INTERCEPT.
Definition: Trends.php:175
static LINEST($yValues, $xValues=null, $const =true, $stats=false)
LINEST.
Definition: Trends.php:202
static CORREL($yValues, $xValues=null)
CORREL.
Definition: Trends.php:62
static GROWTH($yValues, $xValues=[], $newValues=[], $const =true)
GROWTH.
Definition: Trends.php:145
static SLOPE($yValues, $xValues)
SLOPE.
Definition: Trends.php:352
static COVAR($yValues, $xValues)
COVAR.
Definition: Trends.php:90
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static LOGEST($yValues, $xValues=null, $const =true, $stats=false)
LOGEST.
Definition: Trends.php:263
static filterTrendValues(array &$array1, array &$array2)
Definition: Trends.php:11
$key
Definition: croninfo.php:18