Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a way to adapt <a href="https://github.com/ianbarber/PHPIR/blob/master/holtwinters.php" rel="nofollow">Ian Barber's</a> function to do what I needed.</p> <pre><code>&lt;?php error_reporting(E_ALL); ini_set('display_errors','On'); $anYear1 = array(); $anYear2 = array(); $nStop = 10; for($i = 1; $i &lt;= 12; $i++) { $anYear1[$i] = rand(100,400); if ($i &lt;= $nStop) { $anYear2[$i+12] = rand(200,600); } } print_r($anYear1); print_r($anYear2); $anData = array_merge($anYear1,$anYear2); print_r(forecastHoltWinters($anData)); function forecastHoltWinters($anData, $nForecast = 2, $nSeasonLength = 4, $nAlpha = 0.2, $nBeta = 0.01, $nGamma = 0.01, $nDevGamma = 0.1) { // Calculate an initial trend level $nTrend1 = 0; for($i = 0; $i &lt; $nSeasonLength; $i++) { $nTrend1 += $anData[$i]; } $nTrend1 /= $nSeasonLength; $nTrend2 = 0; for($i = $nSeasonLength; $i &lt; 2*$nSeasonLength; $i++) { $nTrend2 += $anData[$i]; } $nTrend2 /= $nSeasonLength; $nInitialTrend = ($nTrend2 - $nTrend1) / $nSeasonLength; // Take the first value as the initial level $nInitialLevel = $anData[0]; // Build index $anIndex = array(); foreach($anData as $nKey =&gt; $nVal) { $anIndex[$nKey] = $nVal / ($nInitialLevel + ($nKey + 1) * $nInitialTrend); } // Build season buffer $anSeason = array_fill(0, count($anData), 0); for($i = 0; $i &lt; $nSeasonLength; $i++) { $anSeason[$i] = ($anIndex[$i] + $anIndex[$i+$nSeasonLength]) / 2; } // Normalise season $nSeasonFactor = $nSeasonLength / array_sum($anSeason); foreach($anSeason as $nKey =&gt; $nVal) { $anSeason[$nKey] *= $nSeasonFactor; } $anHoltWinters = array(); $anDeviations = array(); $nAlphaLevel = $nInitialLevel; $nBetaTrend = $nInitialTrend; foreach($anData as $nKey =&gt; $nVal) { $nTempLevel = $nAlphaLevel; $nTempTrend = $nBetaTrend; $nAlphaLevel = $nAlpha * $nVal / $anSeason[$nKey] + (1.0 - $nAlpha) * ($nTempLevel + $nTempTrend); $nBetaTrend = $nBeta * ($nAlphaLevel - $nTempLevel) + ( 1.0 - $nBeta ) * $nTempTrend; $anSeason[$nKey + $nSeasonLength] = $nGamma * $nVal / $nAlphaLevel + (1.0 - $nGamma) * $anSeason[$nKey]; $anHoltWinters[$nKey] = ($nAlphaLevel + $nBetaTrend * ($nKey + 1)) * $anSeason[$nKey]; $anDeviations[$nKey] = $nDevGamma * abs($nVal - $anHoltWinters[$nKey]) + (1-$nDevGamma) * (isset($anDeviations[$nKey - $nSeasonLength]) ? $anDeviations[$nKey - $nSeasonLength] : 0); } $anForecast = array(); $nLast = end($anData); for($i = 1; $i &lt;= $nForecast; $i++) { $nComputed = round($nAlphaLevel + $nBetaTrend * $anSeason[$nKey + $i]); if ($nComputed &lt; 0) { // wildly off due to outliers $nComputed = $nLast; } $anForecast[] = $nComputed; } return $anForecast; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload